PluginProbe
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses / 2.7.5
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses v2.7.5
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 / Services / NotificationPref.php

NotificationPref.php in FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses 2.7.5, at app/Services/NotificationPref.php

189 lines 6.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\Services;
4
5 use FluentCommunity\App\Functions\Utility;
6 use FluentCommunity\App\Models\NotificationSubscription;
7 use FluentCommunity\App\Models\Space;
8 use FluentCommunity\Framework\Support\Arr;
9
10 class NotificationPref
11 {
12 public static function getGlobalPrefs()
13 {
14 $pref = Utility::getEmailNotificationSettings();
15 $valids = ['com_my_post_mail', 'reply_my_com_mail', 'mention_mail', 'digest_email_status', 'messaging_email_status'];
16
17 $pref = Arr::only($pref, $valids);
18
19 $pref = array_map(function ($value) {
20 return $value === 'yes' ? 1 : 0;
21 }, $pref);
22
23 return $pref;
24 }
25
26 public static function getUserPrefs($userId)
27 {
28 return Utility::getFromCache('user_notification_pref_' . $userId, function () use ($userId) {
29 $prefs = NotificationSubscription::where('user_id', $userId)
30 ->select(['notification_type', 'is_read', 'object_id'])
31 ->get();
32
33 if ($prefs->isEmpty()) {
34 return [];
35 }
36
37 $fromattedPrefs = [];
38 foreach ($prefs as $pref) {
39 $key = $pref->notification_type;
40 if ($pref->object_id) {
41 $key = $pref->notification_type . '_' . $pref->object_id;
42 }
43 $fromattedPrefs[$key] = $pref->is_read;
44 }
45 return $fromattedPrefs;
46 }, 86400 * 30);
47 }
48
49 public static function updateUserPrefs($userId, $prefs = [])
50 {
51 $validKeys = [
52 'com_my_post_mail',
53 'reply_my_com_mail',
54 'mention_mail',
55 'digest_mail'
56 ];
57
58 $validPrefs = [];
59 foreach ($prefs as $key => $value) {
60 if (in_array($key, $validKeys)) {
61 $validPrefs[$key] = $value ? 1 : 0;
62 } else if (strpos($key, 'np_by_') === 0) {
63 // This is the notification by object. We are processing per key when updating
64 $validPrefs[$key] = $value ? 1 : 0;
65 } else if ($key == 'message_email_frequency') {
66 $validPrefs[$key] = $value;
67 }
68 }
69
70 $ids = [];
71 foreach ($validPrefs as $key => $value) {
72 $exist = NotificationSubscription::where('user_id', $userId)
73 ->where('notification_type', $key)
74 ->first();
75
76 if ($exist) {
77 $exist->is_read = $value;
78 $exist->save();
79 $ids[] = $exist->id;
80 } else {
81 $newData = [
82 'user_id' => $userId,
83 'object_type' => 'notification_pref',
84 'notification_type' => $key,
85 'is_read' => $value
86 ];
87
88 $isByObject = strpos($key, 'np_by_') === 0;
89
90 // Find the last number in the key _{number}
91 if ($isByObject) {
92 $matches = [];
93 preg_match('/\d+$/', $key, $matches);
94 if (isset($matches[0])) {
95 $objectId = (int)$matches[0];
96 if ($objectId && Space::where('id', $objectId)->exists()) {
97 // Now remove the last _{number} from the key. Make sure you are removing the last one
98 $newData['notification_type'] = substr($key, 0, strrpos($key, '_'));
99 $newData['object_id'] = $objectId;
100 $exist = NotificationSubscription::where('user_id', $userId)
101 ->where('notification_type', $newData['notification_type'])
102 ->where('object_id', $objectId)
103 ->first();
104 if($exist) {
105 $exist->is_read = $value;
106 $exist->save();
107 $ids[] = $exist->id;
108 continue;
109 }
110 }
111 }
112 }
113 $created = NotificationSubscription::create($newData);
114 $ids[] = $created->id;
115 }
116 }
117
118 // Delete the rest
119 NotificationSubscription::where('user_id', $userId)
120 ->whereNotIn('id', $ids)
121 ->delete();
122
123 // delete the cache now
124 Utility::forgetCache('user_notification_pref_' . $userId);
125
126 return self::getUserPrefs($userId);
127 }
128
129 public static function updateUserSinglePref($userId, $prefKey, $prefValue, $objectId = null)
130 {
131 $prefs = self::getUserPrefs($userId);
132 $prefs[$prefKey] = $prefValue ? 1 : 0;
133
134 if ($objectId) {
135 $prefs[$prefKey . '_' . $objectId] = $prefValue;
136 }
137
138 return self::updateUserPrefs($userId, $prefs);
139 }
140
141 public static function isPrefEnabled($userId, $prefKey, $globalStatus = false)
142 {
143 $prefs = self::getUserPrefs($userId);
144
145 if (!isset($prefs[$prefKey])) {
146 return $globalStatus;
147 }
148
149 return (bool)$prefs[$prefKey];
150 }
151
152 public static function willGetCommentEmail($userId, $globalStatus = null)
153 {
154 if ($globalStatus === null) {
155 $globalStatus = Arr::get(self::getGlobalPrefs(), 'com_my_post_mail', false);
156 }
157
158 return self::isPrefEnabled($userId, 'com_my_post_mail', $globalStatus);
159 }
160
161 public static function willGetCommentReplyEmail($userId, $globalStatus = null)
162 {
163 if ($globalStatus === null) {
164 $globalStatus = Arr::get(self::getGlobalPrefs(), 'reply_my_com_mail', false);
165 }
166
167 return self::isPrefEnabled($userId, 'reply_my_com_mail', $globalStatus);
168 }
169
170 public static function willGetMentionEmail($userId, $globalStatus = null)
171 {
172 if ($globalStatus === null) {
173 $globalStatus = Arr::get(self::getGlobalPrefs(), 'mention_mail', false);
174 }
175
176 return self::isPrefEnabled($userId, 'mention_mail', $globalStatus);
177 }
178
179 public static function willGetDigestEmail($userId, $globalStatus = null)
180 {
181 if ($globalStatus === null) {
182 $globalStatus = Arr::get(self::getGlobalPrefs(), 'digest_email_status', false);
183 }
184
185 return self::isPrefEnabled($userId, 'digest_mail', $globalStatus);
186 }
187
188 }
189