| Total Complexity | 141 |
| Total Lines | 760 |
| Duplicated Lines | 0 % |
| Changes | 4 | ||
| Bugs | 0 | 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 |
||
| 21 | class CreatePost_Notify_Background extends SMF_BackgroundTask |
||
| 22 | { |
||
| 23 | /** |
||
| 24 | * Constants for reply types. |
||
| 25 | */ |
||
| 26 | const NOTIFY_TYPE_REPLIES_AND_MODERATION = 1; |
||
| 27 | const NOTIFY_TYPE_REPLIES_AND_OWN_TOPIC_MODERATION = 2; |
||
| 28 | const NOTIFY_TYPE_ONLY_REPLIES = 3; |
||
| 29 | const NOTIFY_TYPE_NOTHING = 4; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Constants for frequencies. |
||
| 33 | */ |
||
| 34 | const FREQUENCY_NOTHING = 0; |
||
| 35 | const FREQUENCY_EVERYTHING = 1; |
||
| 36 | const FREQUENCY_FIRST_UNREAD_MSG = 2; |
||
| 37 | const FREQUENCY_DAILY_DIGEST = 3; |
||
| 38 | const FREQUENCY_WEEKLY_DIGEST = 4; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Minutes to wait before sending notifications about about mentions |
||
| 42 | * and quotes in unwatched and/or edited posts. |
||
| 43 | */ |
||
| 44 | const MENTION_DELAY = 5; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var array Info about members to be notified. |
||
| 48 | */ |
||
| 49 | private $members = array( |
||
| 50 | // These three contain nested arrays of member info. |
||
| 51 | 'mentioned' => array(), |
||
| 52 | 'quoted' => array(), |
||
| 53 | 'watching' => array(), |
||
| 54 | |||
| 55 | // These ones just contain member IDs. |
||
| 56 | 'all' => array(), |
||
| 57 | 'emailed' => array(), |
||
| 58 | 'alerted' => array(), |
||
| 59 | 'done' => array(), |
||
| 60 | ); |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var array Alerts to be inserted into the alerts table. |
||
| 64 | */ |
||
| 65 | private $alert_rows = array(); |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var array Members' notification and alert preferences. |
||
| 69 | */ |
||
| 70 | private $prefs = array(); |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @var int Timestamp after which email notifications should be sent about |
||
| 74 | * mentions and quotes in unwatched and/or edited posts. |
||
| 75 | */ |
||
| 76 | private $mention_mail_time = 0; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * This executes the task: loads up the info, puts the email in the queue |
||
| 80 | * and inserts any alerts as needed. |
||
| 81 | * |
||
| 82 | * @return bool Always returns true |
||
| 83 | * @throws Exception |
||
| 84 | */ |
||
| 85 | public function execute() |
||
| 86 | { |
||
| 87 | global $smcFunc, $sourcedir, $scripturl, $language, $modSettings, $user_info, $txt; |
||
| 88 | |||
| 89 | require_once($sourcedir . '/Subs-Post.php'); |
||
| 90 | require_once($sourcedir . '/Mentions.php'); |
||
| 91 | require_once($sourcedir . '/Subs-Notify.php'); |
||
| 92 | require_once($sourcedir . '/Subs.php'); |
||
| 93 | require_once($sourcedir . '/ScheduledTasks.php'); |
||
| 94 | loadEssentialThemeData(); |
||
| 95 | |||
| 96 | $msgOptions = &$this->_details['msgOptions']; |
||
| 97 | $topicOptions = &$this->_details['topicOptions']; |
||
| 98 | $posterOptions = &$this->_details['posterOptions']; |
||
| 99 | $type = &$this->_details['type']; |
||
| 100 | |||
| 101 | // Board id is required; if missing, log an error and return |
||
| 102 | if (!isset($topicOptions['board'])) |
||
| 103 | { |
||
| 104 | require_once($sourcedir . '/Errors.php'); |
||
| 105 | loadLanguage('Errors'); |
||
| 106 | log_error($txt['missing_board_id'], 'general', __FILE__, __LINE__); |
||
| 107 | return true; |
||
| 108 | } |
||
| 109 | |||
| 110 | // poster_time not always supplied, but used throughout |
||
| 111 | if (empty($msgOptions['poster_time'])) |
||
| 112 | $msgOptions['poster_time'] = 0; |
||
| 113 | |||
| 114 | $this->mention_mail_time = $msgOptions['poster_time'] + self::MENTION_DELAY * 60; |
||
| 115 | |||
| 116 | // We need some more info about the quoted and mentioned members. |
||
| 117 | if (!empty($msgOptions['quoted_members'])) |
||
| 118 | $this->members['quoted'] = Mentions::getMentionsByContent('quote', $msgOptions['id'], array_keys($msgOptions['quoted_members'])); |
||
| 119 | if (!empty($msgOptions['mentioned_members'])) |
||
| 120 | $this->members['mentioned'] = Mentions::getMentionsByContent('msg', $msgOptions['id'], array_keys($msgOptions['mentioned_members'])); |
||
| 121 | |||
| 122 | $group_permissions = array( |
||
| 123 | 'allowed' => array(), |
||
| 124 | 'denied' => array(), |
||
| 125 | ); |
||
| 126 | $request = $smcFunc['db_query']('', ' |
||
| 127 | SELECT id_group, deny |
||
| 128 | FROM {db_prefix}board_permissions_view |
||
| 129 | WHERE id_board = {int:current_board}', |
||
| 130 | array( |
||
| 131 | 'current_board' => $topicOptions['board'], |
||
| 132 | ) |
||
| 133 | ); |
||
| 134 | while (list ($id_group, $deny) = $smcFunc['db_fetch_row']($request)) |
||
| 135 | $group_permissions[$deny === '0' ? 'allowed' : 'denied'][] = $id_group; |
||
| 136 | $smcFunc['db_free_result']($request); |
||
| 137 | |||
| 138 | // Find the people interested in receiving notifications for this topic |
||
| 139 | $request = $smcFunc['db_query']('', ' |
||
| 140 | SELECT |
||
| 141 | ln.id_member, ln.id_board, ln.id_topic, ln.sent, |
||
| 142 | mem.email_address, mem.lngfile, mem.pm_ignore_list, |
||
| 143 | mem.id_group, mem.id_post_group, mem.additional_groups, |
||
| 144 | mem.time_format, mem.time_offset, mem.timezone, |
||
| 145 | t.id_member_started, t.id_member_updated |
||
| 146 | FROM {db_prefix}log_notify AS ln |
||
| 147 | INNER JOIN {db_prefix}members AS mem ON (ln.id_member = mem.id_member) |
||
| 148 | LEFT JOIN {db_prefix}topics AS t ON (t.id_topic = ln.id_topic) |
||
| 149 | WHERE ' . ($type == 'topic' ? 'ln.id_board = {int:board}' : 'ln.id_topic = {int:topic}') . ' |
||
| 150 | AND ln.id_member != {int:member}', |
||
| 151 | array( |
||
| 152 | 'member' => $posterOptions['id'], |
||
| 153 | 'topic' => $topicOptions['id'], |
||
| 154 | 'board' => $topicOptions['board'], |
||
| 155 | ) |
||
| 156 | ); |
||
| 157 | while ($row = $smcFunc['db_fetch_assoc']($request)) |
||
| 158 | { |
||
| 159 | // Skip members who aren't allowed to see this board |
||
| 160 | $groups = array_merge(array($row['id_group'], $row['id_post_group']), (empty($row['additional_groups']) ? array() : explode(',', $row['additional_groups']))); |
||
| 161 | |||
| 162 | $is_denied = array_intersect($group_permissions['denied'], $groups) != array(); |
||
| 163 | if (!in_array(1, $groups) && ($is_denied || array_intersect($groups, $group_permissions['allowed']) == array())) |
||
| 164 | continue; |
||
| 165 | else |
||
| 166 | { |
||
| 167 | $row['groups'] = $groups; |
||
| 168 | unset($row['id_group'], $row['id_post_group'], $row['additional_groups']); |
||
| 169 | } |
||
| 170 | |||
| 171 | // If this user subscribes both to the topic and the board there will be two records returned. |
||
| 172 | // Copy board/topic data to the new record or it will be lost. |
||
| 173 | if (!empty($this->members['watching'][$row['id_member']])) { |
||
| 174 | if ($this->members['watching'][$row['id_member']]['id_board'] > 0) { |
||
| 175 | $row['id_board'] = $this->members['watching'][$row['id_member']]['id_board']; |
||
| 176 | } |
||
| 177 | if ($this->members['watching'][$row['id_member']]['id_topic'] > 0) { |
||
| 178 | $row['id_topic'] = $this->members['watching'][$row['id_member']]['id_topic']; |
||
| 179 | } |
||
| 180 | } |
||
| 181 | |||
| 182 | $this->members['watching'][$row['id_member']] = $row; |
||
| 183 | } |
||
| 184 | $smcFunc['db_free_result']($request); |
||
| 185 | |||
| 186 | // Filter out mentioned and quoted members who can't see this board. |
||
| 187 | if (!empty($this->members['mentioned']) || !empty($this->members['quoted'])) |
||
| 188 | { |
||
| 189 | foreach (array('mentioned', 'quoted') as $member_type) |
||
| 190 | { |
||
| 191 | foreach ($this->members[$member_type] as $member_id => $member_data) |
||
| 192 | { |
||
| 193 | // The member receiving the alert has ignored the member mentioning them. |
||
| 194 | if (!empty($member_data['mentioned_by']['ignored'])) { |
||
| 195 | unset($this->members[$member_type][$member_id], $msgOptions[$member_type . '_members'][$member_id]); |
||
| 196 | } |
||
| 197 | |||
| 198 | $is_denied = array_intersect($group_permissions['denied'], $member_data['groups']) != array(); |
||
| 199 | if (!in_array(1, $member_data['groups']) && ($is_denied || array_intersect($member_data['groups'], $group_permissions['allowed']) == array())) |
||
| 200 | unset($this->members[$member_type][$member_id], $msgOptions[$member_type . '_members'][$member_id]); |
||
| 201 | } |
||
| 202 | } |
||
| 203 | } |
||
| 204 | |||
| 205 | $unnotified = array_filter($this->members['watching'], function ($member) { |
||
| 206 | return empty($member['sent']); |
||
| 207 | }); |
||
| 208 | |||
| 209 | |||
| 210 | // Modified post, or dealing with delayed mention and quote notifications. |
||
| 211 | if ($type == 'edit' || !empty($this->_details['respawns'])) |
||
| 212 | { |
||
| 213 | // Notifications about modified posts only go to members who were mentioned or quoted |
||
| 214 | $this->members['watching'] = $type == 'edit' ? array(): $unnotified; |
||
| 215 | |||
| 216 | // If this post has no quotes or mentions, just delete any obsolete alerts and bail out. |
||
| 217 | if (empty($this->members['quoted']) && empty($this->members['mentioned'])) |
||
| 218 | { |
||
| 219 | $this->updateAlerts($msgOptions['id']); |
||
| 220 | return true; |
||
| 221 | } |
||
| 222 | |||
| 223 | // Never notify about edits to ancient posts. |
||
| 224 | if (!empty($modSettings['oldTopicDays']) && time() > $msgOptions['poster_time'] + $modSettings['oldTopicDays'] * 86400) |
||
| 225 | return true; |
||
| 226 | |||
| 227 | // If editing is only allowed for a brief time, send after editing becomes disabled. |
||
| 228 | if (!empty($modSettings['edit_disable_time']) && $modSettings['edit_disable_time'] <= self::MENTION_DELAY) |
||
| 229 | { |
||
| 230 | $this->mention_mail_time = $msgOptions['poster_time'] + $modSettings['edit_disable_time'] * 60; |
||
| 231 | } |
||
| 232 | // Otherwise, impose a delay before sending notifications about edited posts. |
||
| 233 | else |
||
| 234 | { |
||
| 235 | if (!empty($this->_details['respawns'])) |
||
| 236 | { |
||
| 237 | $request = $smcFunc['db_query']('', ' |
||
| 238 | SELECT modified_time |
||
| 239 | FROM {db_prefix}messages |
||
| 240 | WHERE id_msg = {int:msg} |
||
| 241 | LIMIT 1', |
||
| 242 | array( |
||
| 243 | 'msg' => $msgOptions['id'], |
||
| 244 | ) |
||
| 245 | ); |
||
| 246 | list($real_modified_time) = $smcFunc['db_fetch_row']($request); |
||
| 247 | $smcFunc['db_free_result']($request); |
||
| 248 | |||
| 249 | // If it was modified again while we weren't looking, bail out. |
||
| 250 | // A future instance of this task will take care of it instead. |
||
| 251 | if ((!empty($msgOptions['modify_time']) ? $msgOptions['modify_time'] : $msgOptions['poster_time']) < $real_modified_time) |
||
| 252 | return true; |
||
| 253 | } |
||
| 254 | |||
| 255 | $this->mention_mail_time = (!empty($msgOptions['modify_time']) ? $msgOptions['modify_time'] : $msgOptions['poster_time']) + self::MENTION_DELAY * 60; |
||
| 256 | } |
||
| 257 | } |
||
| 258 | |||
| 259 | $this->members['all'] = array_unique(array_merge(array_keys($this->members['watching']), array_keys($this->members['quoted']), array_keys($this->members['mentioned']))); |
||
| 260 | |||
| 261 | if (empty($this->members['all'])) |
||
| 262 | return true; |
||
| 263 | |||
| 264 | $this->prefs = getNotifyPrefs($this->members['all'], '', true); |
||
| 265 | |||
| 266 | // May as well disable these, since they'll be stripped out anyway. |
||
| 267 | $disable = array('attach', 'img', 'iurl', 'url', 'youtube'); |
||
| 268 | if (!empty($modSettings['disabledBBC'])) |
||
| 269 | { |
||
| 270 | $disabledBBC = $modSettings['disabledBBC']; |
||
| 271 | $disable = array_unique(array_merge($disable, explode(',', $modSettings['disabledBBC']))); |
||
| 272 | } |
||
| 273 | $modSettings['disabledBBC'] = implode(',', $disable); |
||
| 274 | |||
| 275 | // Notify any members who were mentioned. |
||
| 276 | if (!empty($this->members['mentioned'])) |
||
| 277 | $this->handleMentionedNotifications(); |
||
| 278 | |||
| 279 | // Notify any members who were quoted. |
||
| 280 | if (!empty($this->members['quoted'])) |
||
| 281 | $this->handleQuoteNotifications(); |
||
| 282 | |||
| 283 | // Handle rest of the notifications for watched topics and boards |
||
| 284 | if (!empty($this->members['watching'])) |
||
| 285 | $this->handleWatchedNotifications(); |
||
| 286 | |||
| 287 | // Put this back the way we found it. |
||
| 288 | if (!empty($disabledBBC)) |
||
| 289 | $modSettings['disabledBBC'] = $disabledBBC; |
||
| 290 | |||
| 291 | // Track what we sent. |
||
| 292 | $members_to_log = array_intersect($this->members['emailed'], array_keys($this->members['watching'])); |
||
| 293 | if (!empty($members_to_log)) |
||
| 294 | { |
||
| 295 | $smcFunc['db_query']('', ' |
||
| 296 | UPDATE {db_prefix}log_notify |
||
| 297 | SET sent = {int:is_sent} |
||
| 298 | WHERE ' . ($type == 'topic' ? 'id_board = {int:board}' : 'id_topic = {int:topic}') . ' |
||
| 299 | AND id_member IN ({array_int:members})', |
||
| 300 | array( |
||
| 301 | 'topic' => $topicOptions['id'], |
||
| 302 | 'board' => $topicOptions['board'], |
||
| 303 | // 'members' => $this->members['emailed'], |
||
| 304 | 'members' => $members_to_log, |
||
| 305 | 'is_sent' => 1, |
||
| 306 | ) |
||
| 307 | ); |
||
| 308 | } |
||
| 309 | |||
| 310 | // Insert it into the digest for daily/weekly notifications |
||
| 311 | if ($type != 'edit' && empty($this->_details['respawns'])) |
||
| 312 | { |
||
| 313 | $smcFunc['db_insert']('', |
||
| 314 | '{db_prefix}log_digest', |
||
| 315 | array( |
||
| 316 | 'id_topic' => 'int', 'id_msg' => 'int', 'note_type' => 'string', 'exclude' => 'int', |
||
| 317 | ), |
||
| 318 | array($topicOptions['id'], $msgOptions['id'], $type, $posterOptions['id']), |
||
| 319 | array() |
||
| 320 | ); |
||
| 321 | } |
||
| 322 | |||
| 323 | // Insert the alerts if any |
||
| 324 | $this->updateAlerts($msgOptions['id']); |
||
| 325 | |||
| 326 | // If there is anyone still to notify via email, create a new task later. |
||
| 327 | $unnotified = array_diff_key($unnotified, array_flip($this->members['emailed'])); |
||
| 328 | if (!empty($unnotified) || !empty($msgOptions['mentioned_members']) || !empty($msgOptions['quoted_members'])) |
||
| 329 | { |
||
| 330 | $new_details = $this->_details; |
||
| 331 | |||
| 332 | if (empty($new_details['respawns'])) |
||
| 333 | $new_details['respawns'] = 0; |
||
| 334 | |||
| 335 | if ($new_details['respawns']++ < 10) |
||
| 336 | { |
||
| 337 | $smcFunc['db_insert']('', |
||
| 338 | '{db_prefix}background_tasks', |
||
| 339 | array( |
||
| 340 | 'task_file' => 'string', |
||
| 341 | 'task_class' => 'string', |
||
| 342 | 'task_data' => 'string', |
||
| 343 | 'claimed_time' => 'int', |
||
| 344 | ), |
||
| 345 | array( |
||
| 346 | '$sourcedir/tasks/CreatePost-Notify.php', |
||
| 347 | 'CreatePost_Notify_Background', |
||
| 348 | $smcFunc['json_encode']($new_details), |
||
| 349 | max(0, $this->mention_mail_time - MAX_CLAIM_THRESHOLD), |
||
| 350 | ), |
||
| 351 | array('id_task') |
||
| 352 | ); |
||
| 353 | } |
||
| 354 | } |
||
| 355 | |||
| 356 | return true; |
||
| 357 | } |
||
| 358 | |||
| 359 | private function updateAlerts($msg_id) |
||
| 360 | { |
||
| 361 | global $smcFunc; |
||
| 362 | |||
| 363 | // We send alerts only on the first iteration of this task. |
||
| 364 | if (!empty($this->_details['respawns'])) |
||
| 365 | return; |
||
| 366 | |||
| 367 | // Delete alerts about any mentions and quotes that no longer exist. |
||
| 368 | if ($this->_details['type'] == 'edit') |
||
| 369 | { |
||
| 370 | $old_alerts = array(); |
||
| 371 | |||
| 372 | $request = $smcFunc['db_query']('', ' |
||
| 373 | SELECT content_action, id_member |
||
| 374 | FROM {db_prefix}user_alerts |
||
| 375 | WHERE content_id = {int:msg_id} |
||
| 376 | AND content_type = {literal:msg} |
||
| 377 | AND (content_action = {literal:quote} OR content_action = {literal:mention})', |
||
| 378 | array( |
||
| 379 | 'msg_id' => $msg_id, |
||
| 380 | ) |
||
| 381 | ); |
||
| 382 | if ($smcFunc['db_num_rows']($request) != 0) |
||
| 383 | { |
||
| 384 | while ($row = $smcFunc['db_fetch_assoc']($request)) |
||
| 385 | $old_alerts[$row['content_action']][$row['id_member']] = $row['id_member']; |
||
| 386 | } |
||
| 387 | $smcFunc['db_free_result']($request); |
||
| 388 | |||
| 389 | if (!empty($old_alerts)) |
||
| 390 | { |
||
| 391 | $request = $smcFunc['db_query']('', ' |
||
| 392 | SELECT content_type, id_mentioned |
||
| 393 | FROM {db_prefix}mentions |
||
| 394 | WHERE content_id = {int:msg_id} |
||
| 395 | AND (content_type = {literal:quote} OR content_type = {literal:msg})', |
||
| 396 | array( |
||
| 397 | 'msg_id' => $msg_id, |
||
| 398 | ) |
||
| 399 | ); |
||
| 400 | while ($row = $smcFunc['db_fetch_assoc']($request)) |
||
| 401 | { |
||
| 402 | $content_action = $row['content_type'] == 'quote' ? 'quote' : 'mention'; |
||
| 403 | unset($old_alerts[$content_action][$row['id_mentioned']]); |
||
| 404 | } |
||
| 405 | $smcFunc['db_free_result']($request); |
||
| 406 | |||
| 407 | $conditions = array(); |
||
| 408 | |||
| 409 | if (!empty($old_alerts['quote'])) |
||
| 410 | $conditions[] = '(content_action = {literal:quote} AND id_member IN ({array_int:members_not_quoted}))'; |
||
| 411 | |||
| 412 | if (!empty($old_alerts['mention'])) |
||
| 413 | $conditions[] = '(content_action = {literal:mention} AND id_member IN ({array_int:members_not_mentioned}))'; |
||
| 414 | |||
| 415 | if (!empty($conditions)) |
||
| 416 | { |
||
| 417 | $smcFunc['db_query']('', ' |
||
| 418 | DELETE FROM {db_prefix}user_alerts |
||
| 419 | WHERE content_id = {int:msg_id} |
||
| 420 | AND content_type = {literal:msg} |
||
| 421 | AND (' . implode(' OR ', $conditions) . ')', |
||
| 422 | array( |
||
| 423 | 'msg_id' => $msg_id, |
||
| 424 | 'members_not_quoted' => empty($old_alerts['quote']) ? array(0) : $old_alerts['quote'], |
||
| 425 | 'members_not_mentioned' => empty($old_alerts['mention']) ? array(0) : $old_alerts['mention'], |
||
| 426 | ) |
||
| 427 | ); |
||
| 428 | |||
| 429 | foreach ($old_alerts as $member_ids) |
||
| 430 | updateMemberData($member_ids, array('alerts' => '-')); |
||
| 431 | } |
||
| 432 | } |
||
| 433 | } |
||
| 434 | |||
| 435 | // Insert the new alerts. |
||
| 436 | if (!empty($this->alert_rows)) |
||
| 437 | { |
||
| 438 | $smcFunc['db_insert']('', |
||
| 439 | '{db_prefix}user_alerts', |
||
| 440 | array( |
||
| 441 | 'alert_time' => 'int', |
||
| 442 | 'id_member' => 'int', |
||
| 443 | 'id_member_started' => 'int', |
||
| 444 | 'member_name' => 'string', |
||
| 445 | 'content_type' => 'string', |
||
| 446 | 'content_id' => 'int', |
||
| 447 | 'content_action' => 'string', |
||
| 448 | 'is_read' => 'int', |
||
| 449 | 'extra' => 'string', |
||
| 450 | ), |
||
| 451 | $this->alert_rows, |
||
| 452 | array() |
||
| 453 | ); |
||
| 454 | |||
| 455 | updateMemberData(array_column($this->alert_rows, 'id_member'), array('alerts' => '+')); |
||
| 456 | } |
||
| 457 | } |
||
| 458 | |||
| 459 | /** |
||
| 460 | * Notifies members about new posts in topics they are watching |
||
| 461 | * and new topics in boards they are watching. |
||
| 462 | */ |
||
| 463 | protected function handleWatchedNotifications() |
||
| 629 | } |
||
| 630 | } |
||
| 631 | |||
| 632 | /** |
||
| 633 | * Notifies members when their posts are quoted in other posts. |
||
| 634 | */ |
||
| 635 | protected function handleQuoteNotifications() |
||
| 706 | } |
||
| 707 | } |
||
| 708 | |||
| 709 | /** |
||
| 710 | * Notifies members when they are mentioned in other members' posts. |
||
| 711 | */ |
||
| 712 | protected function handleMentionedNotifications() |
||
| 785 | ?> |