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

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