Total Complexity | 78 |
Total Lines | 404 |
Duplicated Lines | 0 % |
Changes | 3 | ||
Bugs | 1 | Features | 0 |
Complex classes like CreatePost_Notify_Background often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use CreatePost_Notify_Background, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | class CreatePost_Notify_Background extends SMF_BackgroundTask |
||
19 | { |
||
20 | /** |
||
21 | * Constants for receiving email notfications. |
||
22 | */ |
||
23 | const RECEIVE_NOTIFY_EMAIL = 0x02; |
||
24 | const RECEIVE_NOTIFY_ALERT = 0x01; |
||
25 | |||
26 | /** |
||
27 | * Constants for reply types. |
||
28 | */ |
||
29 | const NOTIFY_TYPE_REPLY_AND_MODIFY = 1; |
||
30 | const NOTIFY_TYPE_REPLY_AND_TOPIC_START_FOLLOWING = 2; |
||
31 | const NOTIFY_TYPE_ONLY_REPLIES = 3; |
||
32 | const NOTIFY_TYPE_NOTHING = 4; |
||
33 | |||
34 | /** |
||
35 | * Constants for frequencies. |
||
36 | */ |
||
37 | const FREQUENCY_NOTHING = 0; |
||
38 | const FREQUENCY_EVERYTHING = 1; |
||
39 | const FREQUENCY_FIRST_UNREAD_MSG = 2; |
||
40 | const FREQUENCY_DAILY_DIGEST = 3; |
||
41 | const FREQUENCY_WEEKLY_DIGEST = 4; |
||
42 | |||
43 | /** |
||
44 | * This handles notifications when a new post is created - new topic, reply, quotes and mentions. |
||
45 | * @return bool Always returns true |
||
46 | */ |
||
47 | public function execute() |
||
48 | { |
||
49 | global $smcFunc, $sourcedir, $scripturl, $language, $modSettings; |
||
50 | |||
51 | require_once($sourcedir . '/Subs-Post.php'); |
||
52 | require_once($sourcedir . '/Mentions.php'); |
||
53 | require_once($sourcedir . '/Subs-Notify.php'); |
||
54 | |||
55 | $msgOptions = $this->_details['msgOptions']; |
||
56 | $topicOptions = $this->_details['topicOptions']; |
||
57 | $posterOptions = $this->_details['posterOptions']; |
||
58 | $type = $this->_details['type']; |
||
59 | |||
60 | $members = array(); |
||
61 | $quotedMembers = array(); |
||
62 | $done_members = array(); |
||
63 | $alert_rows = array(); |
||
64 | |||
65 | if ($type == 'reply' || $type == 'topic') |
||
66 | { |
||
67 | $quotedMembers = self::getQuotedMembers($msgOptions, $posterOptions); |
||
68 | $members = array_keys($quotedMembers); |
||
69 | } |
||
70 | |||
71 | // Insert the post mentions |
||
72 | if (!empty($msgOptions['mentioned_members'])) |
||
73 | { |
||
74 | Mentions::insertMentions('msg', $msgOptions['id'], $msgOptions['mentioned_members'], $posterOptions['id']); |
||
75 | $members = array_merge($members, array_keys($msgOptions['mentioned_members'])); |
||
76 | } |
||
77 | |||
78 | // Find the people interested in receiving notifications for this topic |
||
79 | $request = $smcFunc['db_query']('', ' |
||
80 | SELECT mem.id_member, ln.id_topic, ln.id_board, ln.sent, mem.email_address, mem.lngfile, b.member_groups, |
||
81 | mem.id_group, mem.id_post_group, mem.additional_groups, t.id_member_started, mem.pm_ignore_list, |
||
82 | t.id_member_updated |
||
83 | FROM {db_prefix}log_notify AS ln |
||
84 | INNER JOIN {db_prefix}members AS mem ON (ln.id_member = mem.id_member) |
||
85 | LEFT JOIN {db_prefix}topics AS t ON (t.id_topic = ln.id_topic) |
||
86 | LEFT JOIN {db_prefix}boards AS b ON (b.id_board = ln.id_board OR b.id_board = t.id_board) |
||
87 | WHERE ln.id_topic = {int:topic} |
||
88 | OR ln.id_board = {int:board}', |
||
89 | array( |
||
90 | 'topic' => $topicOptions['id'], |
||
91 | 'board' => $topicOptions['board'], |
||
92 | ) |
||
93 | ); |
||
94 | |||
95 | $watched = array(); |
||
96 | while ($row = $smcFunc['db_fetch_assoc']($request)) |
||
97 | { |
||
98 | $groups = array_merge(array($row['id_group'], $row['id_post_group']), (empty($row['additional_groups']) ? array() : explode(',', $row['additional_groups']))); |
||
99 | if (!in_array(1, $groups) && count(array_intersect($groups, explode(',', $row['member_groups']))) == 0) |
||
100 | continue; |
||
101 | |||
102 | $members[] = $row['id_member']; |
||
103 | $watched[$row['id_member']] = $row; |
||
104 | } |
||
105 | |||
106 | $smcFunc['db_free_result']($request); |
||
107 | |||
108 | if (empty($members)) |
||
109 | return true; |
||
110 | |||
111 | $members = array_unique($members); |
||
112 | $prefs = getNotifyPrefs($members, '', true); |
||
113 | |||
114 | // Do we have anyone to notify via mention? Handle them first and cross them off the list |
||
115 | if (!empty($msgOptions['mentioned_members'])) |
||
116 | { |
||
117 | $mentioned_members = Mentions::getMentionsByContent('msg', $msgOptions['id'], array_keys($msgOptions['mentioned_members'])); |
||
118 | self::handleMentionedNotifications($msgOptions, $mentioned_members, $prefs, $done_members, $alert_rows); |
||
119 | } |
||
120 | |||
121 | // Notify members which might've been quoted |
||
122 | self::handleQuoteNotifications($msgOptions, $posterOptions, $quotedMembers, $prefs, $done_members, $alert_rows); |
||
123 | |||
124 | // Save ourselves a bit of work in the big loop below |
||
125 | foreach ($done_members as $done_member) |
||
126 | unset($watched[$done_member]); |
||
127 | |||
128 | // Handle rest of the notifications for watched topics and boards |
||
129 | foreach ($watched as $member => $data) |
||
130 | { |
||
131 | $frequency = isset($prefs[$member]['msg_notify_pref']) ? $prefs[$member]['msg_notify_pref'] : self::FREQUENCY_NOTHING; |
||
132 | $notify_types = !empty($prefs[$member]['msg_notify_type']) ? $prefs[$member]['msg_notify_type'] : self::NOTIFY_TYPE_REPLY_AND_MODIFY; |
||
133 | |||
134 | // Don't send a notification if the watching member ignored the member who made the action. |
||
135 | if (!empty($data['pm_ignore_list']) && in_array($data['id_member_updated'], explode(',', $data['pm_ignore_list']))) |
||
136 | continue; |
||
137 | if (!in_array($type, array('reply', 'topic')) && $notify_types == self::NOTIFY_TYPE_REPLY_AND_TOPIC_START_FOLLOWING && $member != $data['id_member_started']) |
||
138 | continue; |
||
139 | elseif (in_array($type, array('reply', 'topic')) && $member == $posterOptions['id']) |
||
140 | continue; |
||
141 | elseif (!in_array($type, array('reply', 'topic')) && $notify_types == self::NOTIFY_TYPE_ONLY_REPLIES) |
||
142 | continue; |
||
143 | elseif ($notify_types == self::NOTIFY_TYPE_NOTHING) |
||
144 | continue; |
||
145 | |||
146 | // Don't send a notification if they don't want any... |
||
147 | if (in_array($frequency, array(self::FREQUENCY_NOTHING, self::FREQUENCY_DAILY_DIGEST, self::FREQUENCY_WEEKLY_DIGEST))) |
||
148 | continue; |
||
149 | // ... or if we already sent one and they don't want more... |
||
150 | elseif ($frequency === self::FREQUENCY_FIRST_UNREAD_MSG && $data['sent']) |
||
151 | continue; |
||
152 | // ... or if they aren't on the bouncer's list. |
||
153 | elseif (!empty($this->_details['members_only']) && !in_array($member, $this->_details['members_only'])) |
||
154 | continue; |
||
155 | |||
156 | // Watched topic? |
||
157 | if (!empty($data['id_topic']) && $type != 'topic' && !empty($prefs[$member])) |
||
158 | { |
||
159 | $pref = !empty($prefs[$member]['topic_notify_' . $topicOptions['id']]) ? $prefs[$member]['topic_notify_' . $topicOptions['id']] : (!empty($prefs[$member]['topic_notify']) ? $prefs[$member]['topic_notify'] : 0); |
||
160 | $message_type = 'notification_' . $type; |
||
161 | |||
162 | if ($type == 'reply') |
||
163 | { |
||
164 | if (!empty($prefs[$member]['msg_receive_body'])) |
||
165 | $message_type .= '_body'; |
||
166 | if (!empty($frequency)) |
||
167 | $message_type .= '_once'; |
||
168 | } |
||
169 | |||
170 | $content_type = 'topic'; |
||
171 | } |
||
172 | // A new topic in a watched board then? |
||
173 | elseif ($type == 'topic') |
||
174 | { |
||
175 | $pref = !empty($prefs[$member]['board_notify_' . $topicOptions['board']]) ? $prefs[$member]['board_notify_' . $topicOptions['board']] : (!empty($prefs[$member]['board_notify']) ? $prefs[$member]['board_notify'] : 0); |
||
176 | |||
177 | $content_type = 'board'; |
||
178 | |||
179 | $message_type = !empty($frequency) ? 'notify_boards_once' : 'notify_boards'; |
||
180 | if (!empty($prefs[$member]['msg_receive_body'])) |
||
181 | $message_type .= '_body'; |
||
182 | } |
||
183 | // If neither of the above, this might be a redundent row due to the OR clause in our SQL query, skip |
||
184 | else |
||
185 | continue; |
||
186 | |||
187 | // Bitwise check: Receiving a email notification? |
||
188 | if ($pref & self::RECEIVE_NOTIFY_EMAIL) |
||
189 | { |
||
190 | $replacements = array( |
||
191 | 'TOPICSUBJECT' => $msgOptions['subject'], |
||
192 | 'POSTERNAME' => un_htmlspecialchars($posterOptions['name']), |
||
193 | 'TOPICLINK' => $scripturl . '?topic=' . $topicOptions['id'] . '.new#new', |
||
194 | 'MESSAGE' => trim(un_htmlspecialchars(strip_tags(strtr(parse_bbc(un_preparsecode($msgOptions['body']), false), array('<br>' => "\n", '</div>' => "\n", '</li>' => "\n", '[' => '[', ']' => ']', ''' => '\''))))), |
||
195 | 'UNSUBSCRIBELINK' => $scripturl . '?action=notifyboard;board=' . $topicOptions['board'] . '.0', |
||
196 | ); |
||
197 | |||
198 | $emaildata = loadEmailTemplate($message_type, $replacements, empty($data['lngfile']) || empty($modSettings['userLanguage']) ? $language : $data['lngfile']); |
||
199 | $mail_result = sendmail($data['email_address'], $emaildata['subject'], $emaildata['body'], null, 'm' . $topicOptions['id'], $emaildata['is_html']); |
||
200 | |||
201 | // We failed, don't trigger a alert as we don't have a way to attempt to resend just the email currently. |
||
202 | if ($mail_result === false) |
||
203 | continue; |
||
204 | } |
||
205 | |||
206 | // Bitwise check: Receiving a alert? |
||
207 | if ($pref & self::RECEIVE_NOTIFY_ALERT) |
||
208 | { |
||
209 | $alert_rows[] = array( |
||
210 | 'alert_time' => time(), |
||
211 | 'id_member' => $member, |
||
212 | // Only tell sender's information for new topics and replies |
||
213 | 'id_member_started' => in_array($type, array('topic', 'reply')) ? $posterOptions['id'] : 0, |
||
214 | 'member_name' => in_array($type, array('topic', 'reply')) ? $posterOptions['name'] : '', |
||
215 | 'content_type' => $content_type, |
||
216 | 'content_id' => $topicOptions['id'], |
||
217 | 'content_action' => $type, |
||
218 | 'is_read' => 0, |
||
219 | 'extra' => $smcFunc['json_encode'](array( |
||
220 | 'topic' => $topicOptions['id'], |
||
221 | 'board' => $topicOptions['board'], |
||
222 | 'content_subject' => $msgOptions['subject'], |
||
223 | 'content_link' => $scripturl . '?topic=' . $topicOptions['id'] . '.new;topicseen#new', |
||
224 | )), |
||
225 | ); |
||
226 | updateMemberData($member, array('alerts' => '+')); |
||
227 | } |
||
228 | |||
229 | $smcFunc['db_query']('', ' |
||
230 | UPDATE {db_prefix}log_notify |
||
231 | SET sent = {int:is_sent} |
||
232 | WHERE (id_topic = {int:topic} OR id_board = {int:board}) |
||
233 | AND id_member = {int:member}', |
||
234 | array( |
||
235 | 'topic' => $topicOptions['id'], |
||
236 | 'board' => $topicOptions['board'], |
||
237 | 'member' => $member, |
||
238 | 'is_sent' => 1, |
||
239 | ) |
||
240 | ); |
||
241 | } |
||
242 | |||
243 | // Insert it into the digest for daily/weekly notifications |
||
244 | $smcFunc['db_insert']('', |
||
245 | '{db_prefix}log_digest', |
||
246 | array( |
||
247 | 'id_topic' => 'int', 'id_msg' => 'int', 'note_type' => 'string', 'exclude' => 'int', |
||
248 | ), |
||
249 | array($topicOptions['id'], $msgOptions['id'], $type, $posterOptions['id']), |
||
250 | array() |
||
251 | ); |
||
252 | |||
253 | // Insert the alerts if any |
||
254 | if (!empty($alert_rows)) |
||
255 | $smcFunc['db_insert']('', |
||
256 | '{db_prefix}user_alerts', |
||
257 | array('alert_time' => 'int', 'id_member' => 'int', 'id_member_started' => 'int', 'member_name' => 'string', |
||
258 | 'content_type' => 'string', 'content_id' => 'int', 'content_action' => 'string', 'is_read' => 'int', 'extra' => 'string'), |
||
259 | $alert_rows, |
||
260 | array() |
||
261 | ); |
||
262 | |||
263 | return true; |
||
264 | } |
||
265 | |||
266 | protected static function handleQuoteNotifications($msgOptions, $posterOptions, $quotedMembers, $prefs, &$done_members, &$alert_rows) |
||
310 | } |
||
311 | } |
||
312 | } |
||
313 | |||
314 | protected static function getQuotedMembers($msgOptions, $posterOptions) |
||
377 | } |
||
378 | |||
379 | protected static function handleMentionedNotifications($msgOptions, $members, $prefs, &$done_members, &$alert_rows) |
||
427 | ?> |