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

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