PluginProbe
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses / 1.0.96
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses v1.0.96
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 1.0.96, at app/Services/NotificationPref.php

178 lines 5.6 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'];
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 // now remove the _{number} from the key
64 $validPrefs[$key] = $value ? 1 : 0;
65 }
66 }
67
68 $ids = [];
69 foreach ($validPrefs as $key => $value) {
70 $exist = NotificationSubscription::where('user_id', $userId)
71 ->where('notification_type', $key)
72 ->first();
73 if ($exist) {
74 $exist->is_read = $value;
75 $exist->save();
76 $ids[] = $exist->id;
77
78 } else {
79 $newData = [
80 'user_id' => $userId,
81 'object_type' => 'notification_pref',
82 'notification_type' => $key,
83 'is_read' => $value
84 ];
85
86 $isByObject = strpos($key, 'np_by_') === 0;
87
88 // Find the last number in the key _{number}
89 if ($isByObject) {
90 $matches = [];
91 preg_match('/\d+$/', $key, $matches);
92 if (isset($matches[0])) {
93 $objectId = (int)$matches[0];
94 if ($objectId && Space::where('id', $objectId)->exists()) {
95 // Now remove the last _{number} from the key. Make sure you are removing the last one
96 $newData['notification_type'] = substr($key, 0, strrpos($key, '_'));
97 $newData['object_id'] = $objectId;
98 }
99 }
100 }
101
102 $created = NotificationSubscription::create($newData);
103 $ids[] = $created->id;
104 }
105 }
106
107 // Delete the rest
108 NotificationSubscription::where('user_id', $userId)
109 ->whereNotIn('id', $ids)
110 ->delete();
111
112 // delete the cache now
113 Utility::forgetCache('user_notification_pref_' . $userId);
114
115 return self::getUserPrefs($userId);
116 }
117
118 public static function updateUserSinglePref($userId, $prefKey, $prefValue, $objectId = null)
119 {
120 $prefs = self::getUserPrefs($userId);
121 $prefs[$prefKey] = $prefValue ? 1 : 0;
122
123 if ($objectId) {
124 $prefs[$prefKey . '_' . $objectId] = $prefValue;
125 }
126
127 return self::updateUserPrefs($userId, $prefs);
128 }
129
130 public static function isPrefEnabled($userId, $prefKey, $globalStatus = false)
131 {
132 $prefs = self::getUserPrefs($userId);
133
134 if (!isset($prefs[$prefKey])) {
135 return $globalStatus;
136 }
137
138 return (bool)$prefs[$prefKey];
139 }
140
141 public static function willGetCommentEmail($userId, $globalStatus = null)
142 {
143 if ($globalStatus === null) {
144 $globalStatus = Arr::get(self::getGlobalPrefs(), 'com_my_post_mail', false);
145 }
146
147 return self::isPrefEnabled($userId, 'com_my_post_mail', $globalStatus);
148 }
149
150 public static function willGetCommentReplyEmail($userId, $globalStatus = null)
151 {
152 if ($globalStatus === null) {
153 $globalStatus = Arr::get(self::getGlobalPrefs(), 'reply_my_com_mail', false);
154 }
155
156 return self::isPrefEnabled($userId, 'reply_my_com_mail', $globalStatus);
157 }
158
159 public static function willGetMentionEmail($userId, $globalStatus = null)
160 {
161 if ($globalStatus === null) {
162 $globalStatus = Arr::get(self::getGlobalPrefs(), 'mention_mail', false);
163 }
164
165 return self::isPrefEnabled($userId, 'mention_mail', $globalStatus);
166 }
167
168 public static function willGetDigestEmail($userId, $globalStatus = null)
169 {
170 if ($globalStatus === null) {
171 $globalStatus = Arr::get(self::getGlobalPrefs(), 'digest_mail', false);
172 }
173
174 return self::isPrefEnabled($userId, 'digest_mail', $globalStatus);
175 }
176
177 }
178