when($subObjectId, function ($q) use ($subObjectId) { $q->where('sub_object_id', $subObjectId); }) ->get(); if ($media->isEmpty()) { return; } $this->queueMediaDelete($media); }, 10, 2); add_action('deleted_user', [$this, 'handleUserDeleted'], 10, 2); } public function handleFeedDeleted($feed) { $feed->comments()->delete(); $feed->reactions()->delete(); $feed->activities()->delete(); $this->handleHashTagDeleted($feed->terms); $this->queueMediaDelete($feed->media); if ($feed->comtent_type == 'survey') { Reaction::where('type', 'survey_vote') ->where('object_id', $feed->id) ->delete(); } // Loop is used to delete notification with notified user data from fcom_notification_users table foreach ($feed->notifications as $notification) { $notification->delete(); } } public function handleHashTagDeleted($terms) { // Loop is used to delete terms with related data from fcom_term_feed table foreach ($terms as $term) { $term->delete(); } } public function queueMediaDelete($media) { if (is_null($media)) { return false; } if ($media instanceof \FluentCommunity\Framework\Database\Orm\Collection) { if ($media->isEmpty()) { return false; } if (apply_filters('fluent_community/handle_remove_bulk_media', false, $media)) { return true; } foreach ($media as $medium) { if ($medium->driver == 'local') { $medium->delete(); } else { $medium->is_active = 0; $medium->save(); } } } else { if ($media->driver == 'local') { $media->delete(); } else { $media->is_active = 0; $media->save(); } } } public function handleMediaDeleted($media) { if (!$media) { return false; } if ($media instanceof \FluentCommunity\Framework\Database\Orm\Collection) { if ($media->isEmpty()) { return false; } if (apply_filters('fluent_community/handle_remove_bulk_media', false, $media)) { return true; } foreach ($media as $medium) { $medium->delete(); } } else { $media->delete(); } } /* * Remove old notifications which are more than 1 month old */ public function maybeDeleteOldNotifications() { $notifications = Notification::where('updated_at', '<', gmdate('Y-m-d H:i:s', strtotime('-1 month'))) ->limit(100) ->get(); $ids = []; foreach ($notifications as $notification) { $ids[] = $notification->id; } if (!$ids) { return false; } NotificationSubscriber::whereIn('object_id', $ids)->delete(); $notifications->delete(); if (microtime(true) - FLUENT_COMMUNITY_START_TIME < 30) { $this->maybeDeleteOldNotifications(); } } public function deleteOldDraftMedias() { $oldUnusedMedias = Media::where('is_active', '0') ->where('created_at', '<=', gmdate('Y-m-d H:i:s', current_time('timestamp') - 7200)) // 2 hours old media items ->limit(30) ->get(); if ($oldUnusedMedias->isEmpty()) { return false; } $this->handleMediaDeleted($oldUnusedMedias); if (microtime(true) - FLUENT_COMMUNITY_START_TIME < 45) { $this->deleteOldDraftMedias(); } return true; } public function handleUserDeleted($userId, $reassign) { /* * - Delete user related feeds where user_id = user_id * - Delete user related comments where user_id = user_id * - Delete likes where user_id = user_id * - Delete user related notifications where user_id = user_id * * fcom_chat_messages where user_id = user_id * fcom_chat_thread_users where user_id = user_id * * fcom_meta where object_type = user and object_id = user_id * * fcom_notification_users where user_id = user_id * fcom_post_comments where user_id = user_id * fcom_post_reactions where user_id = user_id * fcom_posts where user_id = user_id & type = text * fcom_space_user where user_id = user_id * fcom_user_activities where user_id = user_id * fcom_xprofile where user_id = user_id * * Delete the related media data as well which are connected with posts and comments * * ## Media * where object_source = user_photo|user_cover_photo|comment|feed|chat_message and user_id = user_id * * */ if (defined('FLUENT_MESSAGING_CHAT_VERSION')) { Utility::getApp('db')->table('fcom_chat_messages')->where('user_id', $userId)->delete(); Utility::getApp('db')->table('fcom_chat_thread_users')->where('user_id', $userId)->delete(); Utility::getApp('db')->table('fcom_chat_thread_users')->where('user_id', $userId)->delete(); } // meta Utility::getApp('db')->table('fcom_meta')->where('object_type', 'user')->where('object_id', $userId)->delete(); // notifications Utility::getApp('db')->table('fcom_notification_users')->where('user_id', $userId)->delete(); // comments Utility::getApp('db')->table('fcom_post_comments')->where('user_id', $userId)->delete(); // reactions Utility::getApp('db')->table('fcom_post_reactions')->where('user_id', $userId)->delete(); // posts Utility::getApp('db')->table('fcom_posts')->where('user_id', $userId)->where('type', 'text')->delete(); // space user Utility::getApp('db')->table('fcom_space_user')->where('user_id', $userId)->delete(); // user activities Utility::getApp('db')->table('fcom_user_activities')->where('user_id', $userId)->delete(); // xprofile Utility::getApp('db')->table('fcom_xprofile')->where('user_id', $userId)->delete(); // Delete the related media data as well which are connected with posts and comments // We are just going to update the is_active column to 0 then it will be deleted via cron jobs Media::whereIn('object_source', [ 'user_photo', 'user_cover_photo', 'comment', 'feed', 'chat_message' ]) ->where('user_id', $userId) ->update([ 'is_active' => 0 ]); } }