PluginProbe
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses / 1.1.0
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses v1.1.0
2.10.0 2.10.01 2.9.1 2.9.0 2.8.1 2.8.0 2.7.7 2.7.5 2.7.0 2.6.01 2.6.0 2.5.0 2.4.01 trunk 1.0.90 1.0.91 1.0.92 1.0.93 1.0.94 1.0.95 1.0.96 1.0.97 1.0.98 1.0.99 1.1.0 All 77 releases
fluent-community / app / Hooks / Handlers / CleanupHandler.php

CleanupHandler.php in FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses 1.1.0, at app/Hooks/Handlers/CleanupHandler.php

255 lines 8.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentCommunity\App\Hooks\Handlers;
4
5 use FluentCommunity\App\Functions\Utility;
6 use FluentCommunity\App\Models\Media;
7 use FluentCommunity\App\Models\Notification;
8 use FluentCommunity\App\Models\NotificationSubscriber;
9 use FluentCommunity\App\Models\Reaction;
10 use FluentCommunity\App\Services\Helper;
11
12 class CleanupHandler
13 {
14 public function register()
15 {
16 add_action('fluent_community/feed/before_deleted', [$this, 'handleFeedDeleted'], 10, 1);
17 add_action('fluent_community/remove_old_notifications', [$this, 'maybeDeleteOldNotifications']);
18
19 add_action('fluent_community/comment/media_deleted', [$this, 'queueMediaDelete'], 10, 1);
20 add_action('fluent_community/feed/media_deleted', [$this, 'queueMediaDelete'], 10, 1);
21 add_action('fluent_community/maybe_delete_draft_medias', [$this, 'deleteOldDraftMedias'], 10);
22 add_action('fluent_community/remove_medias_by_url', function ($mediaUrls, $wheres = []) {
23 if (!$mediaUrls) {
24 return;
25 }
26
27 $subObjectId = isset($wheres['sub_object_id']) ? $wheres['sub_object_id'] : null;
28 $media = Media::whereIn('media_url', $mediaUrls)
29 ->when($subObjectId, function ($q) use ($subObjectId) {
30 $q->where('sub_object_id', $subObjectId);
31 })
32 ->get();
33
34 if ($media->isEmpty()) {
35 return;
36 }
37
38 $this->queueMediaDelete($media);
39 }, 10, 2);
40
41 add_action('deleted_user', [$this, 'handleUserDeleted'], 10, 2);
42
43 }
44
45 public function handleFeedDeleted($feed)
46 {
47 $feed->comments()->delete();
48 $feed->reactions()->delete();
49 $feed->activities()->delete();
50 $this->queueMediaDelete($feed->media);
51
52 if ($feed->comtent_type == 'survey') {
53 Reaction::where('type', 'survey_vote')
54 ->where('object_id', $feed->id)
55 ->delete();
56 }
57
58 // Loop is used to delete notification with notified user data from fcom_notification_users table
59 foreach ($feed->notifications as $notification) {
60 $notification->delete();
61 }
62
63 Utility::getApp('db')->table('fcom_term_feed')->where('post_id', $feed->id)->delete();
64 }
65
66 public function queueMediaDelete($media)
67 {
68 if (is_null($media)) {
69 return false;
70 }
71
72 if ($media instanceof \FluentCommunity\Framework\Database\Orm\Collection) {
73
74 if ($media->isEmpty()) {
75 return false;
76 }
77
78 if (apply_filters('fluent_community/handle_remove_bulk_media', false, $media)) {
79 return true;
80 }
81
82 foreach ($media as $medium) {
83 if ($medium->driver == 'local') {
84 $medium->delete();
85 } else {
86 $medium->is_active = 0;
87 $medium->save();
88 }
89 }
90 } else {
91 if ($media->driver == 'local') {
92 $media->delete();
93 } else {
94 $media->is_active = 0;
95 $media->save();
96 }
97 }
98 }
99
100 public function handleMediaDeleted($media)
101 {
102 if (!$media) {
103 return false;
104 }
105
106 if ($media instanceof \FluentCommunity\Framework\Database\Orm\Collection) {
107 if ($media->isEmpty()) {
108 return false;
109 }
110
111 if (apply_filters('fluent_community/handle_remove_bulk_media', false, $media)) {
112 return true;
113 }
114
115 foreach ($media as $medium) {
116 $medium->delete();
117 }
118 } else {
119 $media->delete();
120 }
121 }
122
123 /*
124 * Remove old notifications which are more than 1 month old
125 */
126 public function maybeDeleteOldNotifications()
127 {
128 $notifications = Notification::where('updated_at', '<', gmdate('Y-m-d H:i:s', strtotime('-1 month')))
129 ->limit(100)
130 ->get();
131
132 $ids = [];
133 foreach ($notifications as $notification) {
134 $ids[] = $notification->id;
135 }
136
137 if (!$ids) {
138
139 // Let's delete meta data
140 Utility::getApp('db')->table('fcom_meta')->whereIn('meta_key', [
141 '_last_mention_email_user_id',
142 '_last_email_user_id'
143 ])
144 ->limit(9000)
145 ->where('created_at', '<', gmdate('Y-m-d H:i:s', strtotime('-1 week')))
146 ->delete();
147
148 return false;
149 }
150
151 NotificationSubscriber::whereIn('object_id', $ids)->delete();
152
153 Notification::whereIn('id', $ids)->delete();
154
155 if (microtime(true) - FLUENT_COMMUNITY_START_TIME < 30) {
156 $this->maybeDeleteOldNotifications();
157 }
158
159
160 }
161
162 public function deleteOldDraftMedias()
163 {
164 $oldUnusedMedias = Media::where('is_active', '0')
165 ->where('created_at', '<=', gmdate('Y-m-d H:i:s', current_time('timestamp') - 7200)) // 2 hours old media items
166 ->limit(30)
167 ->get();
168
169 if ($oldUnusedMedias->isEmpty()) {
170 return false;
171 }
172
173 $this->handleMediaDeleted($oldUnusedMedias);
174
175 if (microtime(true) - FLUENT_COMMUNITY_START_TIME < 45) {
176 $this->deleteOldDraftMedias();
177 }
178
179 return true;
180 }
181
182 public function handleUserDeleted($userId, $reassign)
183 {
184 /*
185 * - Delete user related feeds where user_id = user_id
186 * - Delete user related comments where user_id = user_id
187 * - Delete likes where user_id = user_id
188 * - Delete user related notifications where user_id = user_id
189 *
190 * fcom_chat_messages where user_id = user_id
191 * fcom_chat_thread_users where user_id = user_id
192 *
193 * fcom_meta where object_type = user and object_id = user_id
194 *
195 * fcom_notification_users where user_id = user_id
196 * fcom_post_comments where user_id = user_id
197 * fcom_post_reactions where user_id = user_id
198 * fcom_posts where user_id = user_id & type = text
199 * fcom_space_user where user_id = user_id
200 * fcom_user_activities where user_id = user_id
201 * fcom_xprofile where user_id = user_id
202 *
203 * Delete the related media data as well which are connected with posts and comments
204 *
205 * ## Media
206 * where object_source = user_photo|user_cover_photo|comment|feed|chat_message and user_id = user_id
207 *
208 *
209 */
210 if (defined('FLUENT_MESSAGING_CHAT_VERSION')) {
211 Utility::getApp('db')->table('fcom_chat_messages')->where('user_id', $userId)->delete();
212 Utility::getApp('db')->table('fcom_chat_thread_users')->where('user_id', $userId)->delete();
213 Utility::getApp('db')->table('fcom_chat_thread_users')->where('user_id', $userId)->delete();
214 }
215
216 // meta
217 Utility::getApp('db')->table('fcom_meta')->where('object_type', 'user')->where('object_id', $userId)->delete();
218
219 // notifications
220 Utility::getApp('db')->table('fcom_notification_users')->where('user_id', $userId)->delete();
221
222 // comments
223 Utility::getApp('db')->table('fcom_post_comments')->where('user_id', $userId)->delete();
224
225 // reactions
226 Utility::getApp('db')->table('fcom_post_reactions')->where('user_id', $userId)->delete();
227
228 // posts
229 Utility::getApp('db')->table('fcom_posts')->where('user_id', $userId)->where('type', 'text')->delete();
230
231 // space user
232 Utility::getApp('db')->table('fcom_space_user')->where('user_id', $userId)->delete();
233
234 // user activities
235 Utility::getApp('db')->table('fcom_user_activities')->where('user_id', $userId)->delete();
236
237 // xprofile
238 Utility::getApp('db')->table('fcom_xprofile')->where('user_id', $userId)->delete();
239
240 // Delete the related media data as well which are connected with posts and comments
241 // We are just going to update the is_active column to 0 then it will be deleted via cron jobs
242 Media::whereIn('object_source', [
243 'user_photo',
244 'user_cover_photo',
245 'comment',
246 'feed',
247 'chat_message'
248 ])
249 ->where('user_id', $userId)
250 ->update([
251 'is_active' => 0
252 ]);
253 }
254 }
255