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

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