From 2a5baad61bace05ee4907f7125555777cf7b1401 Mon Sep 17 00:00:00 2001 From: Nathan Guse Date: Thu, 27 Sep 2012 20:05:06 -0500 Subject: [PATCH] [ticket/11103] Only notify a user once for a single item Note: The user may be notified multiple times IF they use different notification options. e.g They are subscribed to topics they have bookmarked by a notification and subscribed to the topic by an email notification. In this case, they would receive two notifications. This occurs because we do not want to omit any more direct types of notifications (if they want an email, they should _always_ get at least one email). PHPBB3-11103 --- phpBB/includes/functions_posting.php | 2 +- phpBB/includes/notifications/service.php | 18 ++++++++++++++---- .../notifications/type/approve_post.php | 11 ++++++++++- .../notifications/type/approve_topic.php | 11 ++++++++++- phpBB/includes/notifications/type/base.php | 11 ++++++++++- phpBB/includes/notifications/type/bookmark.php | 11 ++++++++++- .../includes/notifications/type/interface.php | 2 +- phpBB/includes/notifications/type/pm.php | 11 ++++++++++- phpBB/includes/notifications/type/post.php | 11 ++++++++++- .../notifications/type/post_in_queue.php | 11 ++++++++++- phpBB/includes/notifications/type/quote.php | 11 ++++++++++- phpBB/includes/notifications/type/topic.php | 11 ++++++++++- .../notifications/type/topic_in_queue.php | 11 ++++++++++- 13 files changed, 116 insertions(+), 16 deletions(-) diff --git a/phpBB/includes/functions_posting.php b/phpBB/includes/functions_posting.php index 41bdd9f598..6262cee4ad 100644 --- a/phpBB/includes/functions_posting.php +++ b/phpBB/includes/functions_posting.php @@ -2234,7 +2234,7 @@ function submit_post($mode, $subject, $username, $topic_type, &$poll, &$data, $u switch ($mode) { case 'post' : - $phpbb_notifications->add_notifications(array('topic', 'quote'), $notification_data); + $phpbb_notifications->add_notifications(array('quote', 'topic'), $notification_data); break; case 'reply' : diff --git a/phpBB/includes/notifications/service.php b/phpBB/includes/notifications/service.php index 2c1eb859c7..35d60a7c51 100644 --- a/phpBB/includes/notifications/service.php +++ b/phpBB/includes/notifications/service.php @@ -256,16 +256,24 @@ class phpbb_notifications_service * @param string|array $item_type Type identifier or array of item types (only acceptable if the $data is identical for the specified types) * @param array $data Data specific for this type that will be inserted */ - public function add_notifications($item_type, $data) + public function add_notifications($item_type, $data, $options = array()) { + $options = array_merge(array( + 'ignore_users' => array(), + ), $options); + if (is_array($item_type)) { + $notified_users = array(); + $temp_options = $options; + foreach ($item_type as $type) { - $this->add_notifications($type, $data); + $temp_options['ignore_users'] = $options['ignore_users'] + $notified_users; + $notified_users += $this->add_notifications($type, $data, $temp_options); } - return; + return $notified_users; } $item_type_class_name = $this->get_item_type_class_name($item_type); @@ -273,9 +281,11 @@ class phpbb_notifications_service $item_id = $item_type_class_name::get_item_id($data); // find out which users want to receive this type of notification - $notify_users = $item_type_class_name::find_users_for_notification($this->phpbb_container, $data); + $notify_users = $item_type_class_name::find_users_for_notification($this->phpbb_container, $data, $options); $this->add_notifications_for_users($item_type, $data, $notify_users); + + return $notify_users; } /** diff --git a/phpBB/includes/notifications/type/approve_post.php b/phpBB/includes/notifications/type/approve_post.php index faa88e862c..912b9082a2 100644 --- a/phpBB/includes/notifications/type/approve_post.php +++ b/phpBB/includes/notifications/type/approve_post.php @@ -67,8 +67,12 @@ class phpbb_notifications_type_approve_post extends phpbb_notifications_type_pos * * @return array */ - public static function find_users_for_notification(ContainerBuilder $phpbb_container, $post) + public static function find_users_for_notification(ContainerBuilder $phpbb_container, $post, $options = array()) { + $options = array_merge(array( + 'ignore_users' => array(), + ), $options); + $db = $phpbb_container->get('dbal.conn'); $users = array(); @@ -90,6 +94,11 @@ class phpbb_notifications_type_approve_post extends phpbb_notifications_type_pos $result = $db->sql_query($sql); while ($row = $db->sql_fetchrow($result)) { + if (isset($options['ignore_users'][$row['user_id']]) && in_array($row['method'], $options['ignore_users'][$row['user_id']])) + { + continue; + } + if (!isset($rowset[$row['user_id']])) { $notify_users[$row['user_id']] = array(); diff --git a/phpBB/includes/notifications/type/approve_topic.php b/phpBB/includes/notifications/type/approve_topic.php index fbe44c8a2c..e0e3a38e46 100644 --- a/phpBB/includes/notifications/type/approve_topic.php +++ b/phpBB/includes/notifications/type/approve_topic.php @@ -67,8 +67,12 @@ class phpbb_notifications_type_approve_topic extends phpbb_notifications_type_to * * @return array */ - public static function find_users_for_notification(ContainerBuilder $phpbb_container, $post) + public static function find_users_for_notification(ContainerBuilder $phpbb_container, $post, $options = array()) { + $options = array_merge(array( + 'ignore_users' => array(), + ), $options); + $db = $phpbb_container->get('dbal.conn'); $users = array(); @@ -90,6 +94,11 @@ class phpbb_notifications_type_approve_topic extends phpbb_notifications_type_to $result = $db->sql_query($sql); while ($row = $db->sql_fetchrow($result)) { + if (isset($options['ignore_users'][$row['user_id']]) && in_array($row['method'], $options['ignore_users'][$row['user_id']])) + { + continue; + } + if (!isset($rowset[$row['user_id']])) { $notify_users[$row['user_id']] = array(); diff --git a/phpBB/includes/notifications/type/base.php b/phpBB/includes/notifications/type/base.php index 0da4dc8d93..01720b4554 100644 --- a/phpBB/includes/notifications/type/base.php +++ b/phpBB/includes/notifications/type/base.php @@ -264,8 +264,12 @@ abstract class phpbb_notifications_type_base implements phpbb_notifications_type * * @return array */ - protected static function _find_users_for_notification(ContainerBuilder $phpbb_container, $item_id) + protected static function _find_users_for_notification(ContainerBuilder $phpbb_container, $item_id, $options) { + $options = array_merge(array( + 'ignore_users' => array(), + ), $options); + $db = $phpbb_container->get('dbal.conn'); $rowset = array(); @@ -277,6 +281,11 @@ abstract class phpbb_notifications_type_base implements phpbb_notifications_type $result = $db->sql_query($sql); while ($row = $db->sql_fetchrow($result)) { + if (isset($options['ignore_users'][$row['user_id']]) && in_array($row['method'], $options['ignore_users'][$row['user_id']])) + { + continue; + } + if (!isset($rowset[$row['user_id']])) { $rowset[$row['user_id']] = array(); diff --git a/phpBB/includes/notifications/type/bookmark.php b/phpBB/includes/notifications/type/bookmark.php index 0e5358b105..a452583c77 100644 --- a/phpBB/includes/notifications/type/bookmark.php +++ b/phpBB/includes/notifications/type/bookmark.php @@ -56,8 +56,12 @@ class phpbb_notifications_type_bookmark extends phpbb_notifications_type_post * * @return array */ - public static function find_users_for_notification(ContainerBuilder $phpbb_container, $post) + public static function find_users_for_notification(ContainerBuilder $phpbb_container, $post, $options = array()) { + $options = array_merge(array( + 'ignore_users' => array(), + ), $options); + $db = $phpbb_container->get('dbal.conn'); $users = array(); @@ -94,6 +98,11 @@ class phpbb_notifications_type_bookmark extends phpbb_notifications_type_post $result = $db->sql_query($sql); while ($row = $db->sql_fetchrow($result)) { + if (isset($options['ignore_users'][$row['user_id']]) && in_array($row['method'], $options['ignore_users'][$row['user_id']])) + { + continue; + } + if (!isset($rowset[$row['user_id']])) { $notify_users[$row['user_id']] = array(); diff --git a/phpBB/includes/notifications/type/interface.php b/phpBB/includes/notifications/type/interface.php index c85d7441f6..95c307013e 100644 --- a/phpBB/includes/notifications/type/interface.php +++ b/phpBB/includes/notifications/type/interface.php @@ -27,7 +27,7 @@ interface phpbb_notifications_type_interface public static function is_available(ContainerBuilder $phpbb_container); - public static function find_users_for_notification(ContainerBuilder $phpbb_container, $type_data); + public static function find_users_for_notification(ContainerBuilder $phpbb_container, $type_data, $options); public function get_title(); diff --git a/phpBB/includes/notifications/type/pm.php b/phpBB/includes/notifications/type/pm.php index 75c79e35e6..f0730f285c 100644 --- a/phpBB/includes/notifications/type/pm.php +++ b/phpBB/includes/notifications/type/pm.php @@ -70,8 +70,12 @@ class phpbb_notifications_type_pm extends phpbb_notifications_type_base * * @return array */ - public static function find_users_for_notification(ContainerBuilder $phpbb_container, $pm) + public static function find_users_for_notification(ContainerBuilder $phpbb_container, $pm, $options = array()) { + $options = array_merge(array( + 'ignore_users' => array(), + ), $options); + $service = $phpbb_container->get('notifications'); $db = $phpbb_container->get('dbal.conn'); $user = $phpbb_container->get('user'); @@ -92,6 +96,11 @@ class phpbb_notifications_type_pm extends phpbb_notifications_type_base $result = $db->sql_query($sql); while ($row = $db->sql_fetchrow($result)) { + if (isset($options['ignore_users'][$row['user_id']]) && in_array($row['method'], $options['ignore_users'][$row['user_id']])) + { + continue; + } + if (!isset($rowset[$row['user_id']])) { $notify_users[$row['user_id']] = array(); diff --git a/phpBB/includes/notifications/type/post.php b/phpBB/includes/notifications/type/post.php index e47ddafe1d..d654a2e3a3 100644 --- a/phpBB/includes/notifications/type/post.php +++ b/phpBB/includes/notifications/type/post.php @@ -76,8 +76,12 @@ class phpbb_notifications_type_post extends phpbb_notifications_type_base * * @return array */ - public static function find_users_for_notification(ContainerBuilder $phpbb_container, $post) + public static function find_users_for_notification(ContainerBuilder $phpbb_container, $post, $options = array()) { + $options = array_merge(array( + 'ignore_users' => array(), + ), $options); + // Let's continue to use the phpBB subscriptions system, at least for now. // It may not be the nicest thing, but it is already working and it would be significant work to replace it //$users = parent::_find_users_for_notification($phpbb_container, $post['topic_id']); @@ -119,6 +123,11 @@ class phpbb_notifications_type_post extends phpbb_notifications_type_base $result = $db->sql_query($sql); while ($row = $db->sql_fetchrow($result)) { + if (isset($options['ignore_users'][$row['user_id']]) && in_array($row['method'], $options['ignore_users'][$row['user_id']])) + { + continue; + } + if (!isset($rowset[$row['user_id']])) { $notify_users[$row['user_id']] = array(); diff --git a/phpBB/includes/notifications/type/post_in_queue.php b/phpBB/includes/notifications/type/post_in_queue.php index 5f8f9988c5..64f68c07e2 100644 --- a/phpBB/includes/notifications/type/post_in_queue.php +++ b/phpBB/includes/notifications/type/post_in_queue.php @@ -77,8 +77,12 @@ class phpbb_notifications_type_post_in_queue extends phpbb_notifications_type_po * * @return array */ - public static function find_users_for_notification(ContainerBuilder $phpbb_container, $post) + public static function find_users_for_notification(ContainerBuilder $phpbb_container, $post, $options = array()) { + $options = array_merge(array( + 'ignore_users' => array(), + ), $options); + $db = $phpbb_container->get('dbal.conn'); $auth_approve = $phpbb_container->get('auth')->acl_get_list(false, 'm_approve', $post['forum_id']); @@ -97,6 +101,11 @@ class phpbb_notifications_type_post_in_queue extends phpbb_notifications_type_po $result = $db->sql_query($sql); while ($row = $db->sql_fetchrow($result)) { + if (isset($options['ignore_users'][$row['user_id']]) && in_array($row['method'], $options['ignore_users'][$row['user_id']])) + { + continue; + } + if (!isset($rowset[$row['user_id']])) { $notify_users[$row['user_id']] = array(); diff --git a/phpBB/includes/notifications/type/quote.php b/phpBB/includes/notifications/type/quote.php index be149673c2..f162b37126 100644 --- a/phpBB/includes/notifications/type/quote.php +++ b/phpBB/includes/notifications/type/quote.php @@ -63,8 +63,12 @@ class phpbb_notifications_type_quote extends phpbb_notifications_type_post * * @return array */ - public static function find_users_for_notification(ContainerBuilder $phpbb_container, $post) + public static function find_users_for_notification(ContainerBuilder $phpbb_container, $post, $options = array()) { + $options = array_merge(array( + 'ignore_users' => array(), + ), $options); + $db = $phpbb_container->get('dbal.conn'); $usernames = false; @@ -113,6 +117,11 @@ class phpbb_notifications_type_quote extends phpbb_notifications_type_post $result = $db->sql_query($sql); while ($row = $db->sql_fetchrow($result)) { + if (isset($options['ignore_users'][$row['user_id']]) && in_array($row['method'], $options['ignore_users'][$row['user_id']])) + { + continue; + } + if (!isset($rowset[$row['user_id']])) { $notify_users[$row['user_id']] = array(); diff --git a/phpBB/includes/notifications/type/topic.php b/phpBB/includes/notifications/type/topic.php index 32d30bc142..ddef0147ba 100644 --- a/phpBB/includes/notifications/type/topic.php +++ b/phpBB/includes/notifications/type/topic.php @@ -76,8 +76,12 @@ class phpbb_notifications_type_topic extends phpbb_notifications_type_base * * @return array */ - public static function find_users_for_notification(ContainerBuilder $phpbb_container, $topic) + public static function find_users_for_notification(ContainerBuilder $phpbb_container, $topic, $options = array()) { + $options = array_merge(array( + 'ignore_users' => array(), + ), $options); + // Let's continue to use the phpBB subscriptions system, at least for now. // It may not be the nicest thing, but it is already working and it would be significant work to replace it //$users = parent::_find_users_for_notification($phpbb_container, $topic['forum_id']); @@ -119,6 +123,11 @@ class phpbb_notifications_type_topic extends phpbb_notifications_type_base $result = $db->sql_query($sql); while ($row = $db->sql_fetchrow($result)) { + if (isset($options['ignore_users'][$row['user_id']]) && in_array($row['method'], $options['ignore_users'][$row['user_id']])) + { + continue; + } + if (!isset($rowset[$row['user_id']])) { $notify_users[$row['user_id']] = array(); diff --git a/phpBB/includes/notifications/type/topic_in_queue.php b/phpBB/includes/notifications/type/topic_in_queue.php index 559af3e505..dc7f7aa105 100644 --- a/phpBB/includes/notifications/type/topic_in_queue.php +++ b/phpBB/includes/notifications/type/topic_in_queue.php @@ -77,8 +77,12 @@ class phpbb_notifications_type_topic_in_queue extends phpbb_notifications_type_t * * @return array */ - public static function find_users_for_notification(ContainerBuilder $phpbb_container, $topic) + public static function find_users_for_notification(ContainerBuilder $phpbb_container, $topic, $options = array()) { + $options = array_merge(array( + 'ignore_users' => array(), + ), $options); + $db = $phpbb_container->get('dbal.conn'); $auth_approve = $phpbb_container->get('auth')->acl_get_list(false, 'm_approve', $topic['forum_id']); @@ -97,6 +101,11 @@ class phpbb_notifications_type_topic_in_queue extends phpbb_notifications_type_t $result = $db->sql_query($sql); while ($row = $db->sql_fetchrow($result)) { + if (isset($options['ignore_users'][$row['user_id']]) && in_array($row['method'], $options['ignore_users'][$row['user_id']])) + { + continue; + } + if (!isset($rowset[$row['user_id']])) { $notify_users[$row['user_id']] = array();