PluginProbe
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses / 2.5.0
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses v2.5.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.5.0, at app/Hooks/Handlers/CleanupHandler.php

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