PluginProbe
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses / 2.7.0
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses v2.7.0
2.11.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 All 78 releases
fluent-community / app / Hooks / Handlers / CleanupHandler.php

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

323 lines 10.4 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
11 class CleanupHandler
12 {
13 public function register()
14 {
15 add_action('fluent_community/feed/before_deleted', [$this, 'handleFeedDeleted'], 10, 1);
16 add_action('fluent_community/lesson/before_deleted', [$this, 'handleLessonDeleted'], 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, 'handleMediaDelete'], 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 public function handleMediaDelete($media)
45 {
46 if(!$media) {
47 return;
48 }
49
50 if ($media && $media instanceof Media && $media->object_source == 'lesson_document') {
51 $this->handleLessonMediaDelete($media->feed_id, $media);
52 return;
53 }
54
55 $this->queueMediaDelete($media);
56 }
57
58 public function handleFeedDeleted($feed)
59 {
60 foreach ($feed->comments as $comment) {
61 $comment->delete();
62 }
63
64 $feed->reactions()->delete();
65 $feed->activities()->delete();
66 $this->queueMediaDelete($feed->media);
67
68 if ($feed->comtent_type == 'survey') {
69 Reaction::where('type', 'survey_vote')
70 ->where('object_id', $feed->id)
71 ->delete();
72 }
73
74 // Loop is used to delete notification with notified user data from fcom_notification_users table
75 foreach ($feed->notifications as $notification) {
76 $notification->delete();
77 }
78
79 Utility::getApp('db')->table('fcom_term_feed')->where('post_id', $feed->id)->delete();
80 }
81
82 public function handleLessonMediaDelete($lessonId, $media, $mediaKeys = [], $isCollection = false)
83 {
84 $mediaKeys = $isCollection ? $mediaKeys : [$media->media_key];
85
86 $duplicateMediaKeys = Media::whereIn('media_key', $mediaKeys)
87 ->where('feed_id', '!=', $lessonId)
88 ->groupBy('media_key')
89 ->pluck('media_key')
90 ->all();
91
92 $deletedIds = [];
93 if (!empty($duplicateMediaKeys)) {
94 $deletedIds = Media::where('feed_id', $lessonId)
95 ->whereIn('media_key', $duplicateMediaKeys)
96 ->pluck('id')
97 ->all();
98
99 Media::where('feed_id', $lessonId)
100 ->whereIn('media_key', $duplicateMediaKeys)
101 ->delete();
102 }
103
104 if (!$isCollection) {
105 if (empty($deletedIds)) {
106 $this->queueMediaDelete($media);
107 }
108 return;
109 }
110
111 $lessonMedia = $media ? $media->filter(function ($medium) use ($deletedIds) {
112 return !in_array($medium->id, $deletedIds);
113 }) : null;
114
115 $this->queueMediaDelete($lessonMedia);
116 }
117
118 public function handleLessonDeleted($lesson)
119 {
120 $lesson->comments()->delete();
121 $lesson->reactions()->delete();
122 $lesson->lessonCompleted()->delete();
123
124 $mediaKeys = $lesson->media ? $lesson->media->pluck('media_key')->all() : [];
125
126 $this->handleLessonMediaDelete($lesson->id, $lesson->media, $mediaKeys, true);
127 }
128
129 public function queueMediaDelete($media)
130 {
131 if (is_null($media)) {
132 return false;
133 }
134
135 if ($media instanceof \FluentCommunity\Framework\Database\Orm\Collection) {
136
137 if ($media->isEmpty()) {
138 return false;
139 }
140
141 if (apply_filters('fluent_community/handle_remove_bulk_media', false, $media)) {
142 return true;
143 }
144
145 foreach ($media as $medium) {
146 if ($medium->driver == 'local') {
147 $medium->delete();
148 } else {
149 $medium->is_active = 0;
150 $medium->save();
151 }
152 }
153 } else {
154 if ($media->driver == 'local') {
155 $media->delete();
156 } else {
157 $media->is_active = 0;
158 $media->save();
159 }
160 }
161 }
162
163 public function handleMediaDeleted($media)
164 {
165 if (!$media) {
166 return false;
167 }
168
169 if ($media instanceof \FluentCommunity\Framework\Database\Orm\Collection) {
170 if ($media->isEmpty()) {
171 return false;
172 }
173
174 if (apply_filters('fluent_community/handle_remove_bulk_media', false, $media)) {
175 return true;
176 }
177
178 foreach ($media as $medium) {
179 $medium->delete();
180 }
181 } else {
182 $media->delete();
183 }
184 }
185
186 /*
187 * Remove old notifications which are more than 1 month old
188 */
189 public function maybeDeleteOldNotifications()
190 {
191 $notifications = Notification::where('updated_at', '<', gmdate('Y-m-d H:i:s', strtotime('-1 month')))
192 ->limit(100)
193 ->get();
194
195 $ids = [];
196 foreach ($notifications as $notification) {
197 $ids[] = $notification->id;
198 }
199
200 if (!$ids) {
201
202 // Let's delete meta data
203 Utility::getApp('db')->table('fcom_meta')->whereIn('meta_key', [
204 '_last_mention_email_user_id',
205 '_last_email_user_id'
206 ])
207 ->limit(9000)
208 ->where('created_at', '<', gmdate('Y-m-d H:i:s', strtotime('-1 week')))
209 ->delete();
210
211 return false;
212 }
213
214 NotificationSubscriber::whereIn('object_id', $ids)->delete();
215
216 Notification::whereIn('id', $ids)->delete();
217
218 if (microtime(true) - FLUENT_COMMUNITY_START_TIME < 30) {
219 $this->maybeDeleteOldNotifications();
220 }
221
222
223 }
224
225 public function deleteOldDraftMedias()
226 {
227 $oldUnusedMedias = Media::where('is_active', '0')
228 ->where('created_at', '<=', gmdate('Y-m-d H:i:s', current_time('timestamp') - 7200)) // 2 hours old media items
229 ->limit(30)
230 ->get();
231
232 if ($oldUnusedMedias->isEmpty()) {
233 return false;
234 }
235
236 $this->handleMediaDeleted($oldUnusedMedias);
237
238 if (microtime(true) - FLUENT_COMMUNITY_START_TIME < 45) {
239 $this->deleteOldDraftMedias();
240 }
241
242 return true;
243 }
244
245 public function handleUserDeleted($userId, $reassign)
246 {
247 /*
248 * - Delete user related feeds where user_id = user_id
249 * - Delete user related comments where user_id = user_id
250 * - Delete likes where user_id = user_id
251 * - Delete user related notifications where user_id = user_id
252 *
253 * fcom_chat_messages where user_id = user_id
254 * fcom_chat_thread_users where user_id = user_id
255 *
256 * fcom_meta where object_type = user and object_id = user_id
257 *
258 * fcom_notification_users where user_id = user_id
259 * fcom_post_comments where user_id = user_id
260 * fcom_post_reactions where user_id = user_id
261 * fcom_posts where user_id = user_id & type = text
262 * fcom_space_user where user_id = user_id
263 * fcom_user_activities where user_id = user_id
264 * fcom_xprofile where user_id = user_id
265 *
266 * Delete the related media data as well which are connected with posts and comments
267 *
268 * ## Media
269 * where object_source = user_photo|user_cover_photo|comment|feed|chat_message and user_id = user_id
270 *
271 *
272 */
273 if (defined('FLUENT_MESSAGING_CHAT_VERSION')) {
274 \FluentMessaging\App\Models\Thread::whereHas('thread_users', function ($q) use ($userId) {
275 $q->where('user_id', $userId);
276 })
277 ->whereNull('space_id')
278 ->delete();
279
280 Utility::getApp('db')->table('fcom_chat_messages')->where('user_id', $userId)->delete();
281 Utility::getApp('db')->table('fcom_chat_thread_users')->where('user_id', $userId)->delete();
282 }
283
284 // meta
285 Utility::getApp('db')->table('fcom_meta')->where('object_type', 'user')->where('object_id', $userId)->delete();
286
287 // notifications
288 Utility::getApp('db')->table('fcom_notification_users')->where('user_id', $userId)->delete();
289
290 // comments
291 Utility::getApp('db')->table('fcom_post_comments')->where('user_id', $userId)->delete();
292
293 // reactions
294 Utility::getApp('db')->table('fcom_post_reactions')->where('user_id', $userId)->delete();
295
296 // posts
297 Utility::getApp('db')->table('fcom_posts')->where('user_id', $userId)->where('type', 'text')->delete();
298
299 // space user
300 Utility::getApp('db')->table('fcom_space_user')->where('user_id', $userId)->delete();
301
302 // user activities
303 Utility::getApp('db')->table('fcom_user_activities')->where('user_id', $userId)->delete();
304
305 // xprofile
306 Utility::getApp('db')->table('fcom_xprofile')->where('user_id', $userId)->delete();
307
308 // Delete the related media data as well which are connected with posts and comments
309 // We are just going to update the is_active column to 0 then it will be deleted via cron jobs
310 Media::whereIn('object_source', [
311 'user_photo',
312 'user_cover_photo',
313 'comment',
314 'feed',
315 'chat_message'
316 ])
317 ->where('user_id', $userId)
318 ->update([
319 'is_active' => 0
320 ]);
321 }
322 }
323