From a3e0117ff0fdd7d832902b9f933569b490e16e62 Mon Sep 17 00:00:00 2001 From: Jakub Senko Date: Tue, 4 Sep 2018 14:23:55 +0200 Subject: [PATCH] [ticket/14754] Only one email notification per topic PHPBB3-14754 --- .../container/services_notification.yml | 6 ++ phpBB/phpbb/notification/method/email.php | 79 ++++++++++++++++++- .../notification/method/method_interface.php | 2 +- phpBB/phpbb/notification/type/post.php | 6 ++ tests/notification/base.php | 5 ++ tests/notification/submit_post_base.php | 5 ++ 6 files changed, 101 insertions(+), 2 deletions(-) diff --git a/phpBB/config/default/container/services_notification.yml b/phpBB/config/default/container/services_notification.yml index 6c3cea3dbc..a9db2bdd38 100644 --- a/phpBB/config/default/container/services_notification.yml +++ b/phpBB/config/default/container/services_notification.yml @@ -206,8 +206,14 @@ services: - '@user_loader' - '@user' - '@config' + - '@dbal.conn' - '%core.root_path%' - '%core.php_ext%' + - '%tables.topics_watch%' + - '%tables.topics_track%' + - '%tables.posts%' + - '%tables.forums_watch%' + - '%tables.forums_track%' tags: - { name: notification.method } diff --git a/phpBB/phpbb/notification/method/email.php b/phpBB/phpbb/notification/method/email.php index 6376d13dc7..66182f2d78 100644 --- a/phpBB/phpbb/notification/method/email.php +++ b/phpBB/phpbb/notification/method/email.php @@ -28,21 +28,51 @@ class email extends \phpbb\notification\method\messenger_base /** @var \phpbb\config\config */ protected $config; + /** @var \phpbb\db\driver\driver_interface */ + protected $db; + + /** @var string */ + protected $topics_watch_table; + + /** @var string */ + protected $topics_track_table; + + /** @var string */ + protected $posts_table; + + /** @var string */ + protected $forums_watch_table; + + /** @var string */ + protected $forums_track_table; + /** * Notification Method email Constructor * * @param \phpbb\user_loader $user_loader * @param \phpbb\user $user * @param \phpbb\config\config $config + * @param \phpbb\db\driver\driver_interface $db * @param string $phpbb_root_path * @param string $php_ext + * @param string $topics_watch_table + * @param string $topics_track_table + * @param string $posts_table + * @param string $forums_watch_table + * @param string $forums_track_table */ - public function __construct(\phpbb\user_loader $user_loader, \phpbb\user $user, \phpbb\config\config $config, $phpbb_root_path, $php_ext) + public function __construct(\phpbb\user_loader $user_loader, \phpbb\user $user, \phpbb\config\config $config, \phpbb\db\driver\driver_interface $db, $phpbb_root_path, $php_ext, $topics_watch_table, $topics_track_table, $posts_table, $forums_watch_table, $forums_track_table) { parent::__construct($user_loader, $phpbb_root_path, $php_ext); $this->user = $user; $this->config = $config; + $this->db = $db; + $this->topics_watch_table = $topics_watch_table; + $this->topics_track_table = $topics_track_table; + $this->posts_table = $posts_table; + $this->forums_watch_table = $forums_watch_table; + $this->forums_track_table = $forums_track_table; } /** @@ -68,6 +98,53 @@ class email extends \phpbb\notification\method\messenger_base return parent::is_available($notification_type) && $this->config['email_enable'] && !empty($this->user->data['user_email']); } + /** + * {@inheritdoc} + */ + public function get_notified_users($notification_type_id, array $options) + { + $notified_users = array(); + + if ($notification_type_id == 'notification.type.post' && !empty($options['item_parent_id'])) + { + // Topics watch + $sql = 'SELECT tw.user_id + FROM ' . $this->topics_watch_table . ' tw + LEFT JOIN ' . $this->topics_track_table . ' tt + ON (tt.user_id = tw.user_id AND tt.topic_id = tw.topic_id) + LEFT JOIN ' . $this->posts_table . ' p + ON (p.topic_id = tw.topic_id) + WHERE tw.topic_id = ' . (int) $options['item_parent_id'] . ' + AND p.post_time > tt.mark_time + HAVING COUNT(p.post_id) > 1'; + $result = $this->db->sql_query($sql); + while ($row = $this->db->sql_fetchrow($result)) + { + $notified_users[$row['user_id']] = $row; + } + $this->db->sql_freeresult($result); + + // Forums watch + $sql = 'SELECT fw.user_id + FROM ' . $this->forums_watch_table . ' fw + LEFT JOIN ' . $this->forums_track_table . ' ft + ON (ft.user_id = fw.user_id AND ft.forum_id = fw.forum_id) + LEFT JOIN ' . $this->posts_table . ' p + ON (p.forum_id = fw.forum_id) + WHERE p.topic_id = ' . (int) $options['item_parent_id'] . ' + AND p.post_time > ft.mark_time + HAVING COUNT(p.post_id) > 1'; + $result = $this->db->sql_query($sql); + while ($row = $this->db->sql_fetchrow($result)) + { + $notified_users[$row['user_id']] = $row; + } + $this->db->sql_freeresult($result); + } + + return $notified_users; + } + /** * Parse the queue and notify the users */ diff --git a/phpBB/phpbb/notification/method/method_interface.php b/phpBB/phpbb/notification/method/method_interface.php index c2e4940485..aa13bfde69 100644 --- a/phpBB/phpbb/notification/method/method_interface.php +++ b/phpBB/phpbb/notification/method/method_interface.php @@ -41,7 +41,7 @@ interface method_interface /** * Return the list of the users already notified * - * @param int $notification_type_id Type of the notification + * @param int $notification_type_id ID of the notification type * @param array $options * @return array User */ diff --git a/phpBB/phpbb/notification/type/post.php b/phpBB/phpbb/notification/type/post.php index f0e938d3ce..64ed91a5f5 100644 --- a/phpBB/phpbb/notification/type/post.php +++ b/phpBB/phpbb/notification/type/post.php @@ -457,6 +457,12 @@ class post extends \phpbb\notification\type\base } $data_array = array_merge(array( + 'poster_id' => $post['poster_id'], + 'topic_title' => $post['topic_title'], + 'post_subject' => $post['post_subject'], + 'post_username' => $post['post_username'], + 'forum_id' => $post['forum_id'], + 'forum_name' => $post['forum_name'], 'post_time' => $post['post_time'], 'post_id' => $post['post_id'], 'topic_id' => $post['topic_id'] diff --git a/tests/notification/base.php b/tests/notification/base.php index f7faf50d68..0e254a66fd 100644 --- a/tests/notification/base.php +++ b/tests/notification/base.php @@ -103,6 +103,11 @@ abstract class phpbb_tests_notification_base extends phpbb_database_test_case $phpbb_container->setParameter('tables.notifications', 'phpbb_notifications'); $phpbb_container->setParameter('tables.user_notifications', 'phpbb_user_notifications'); $phpbb_container->setParameter('tables.notification_types', 'phpbb_notification_types'); + $phpbb_container->setParameter('tables.topics_watch', 'phpbb_topics_watch'); + $phpbb_container->setParameter('tables.topics_track', 'phpbb_topics_track'); + $phpbb_container->setParameter('tables.posts', 'phpbb_posts'); + $phpbb_container->setParameter('tables.forums_watch', 'phpbb_forums_watch'); + $phpbb_container->setParameter('tables.forums_track', 'phpbb_forums_track'); $this->notifications = new phpbb_notification_manager_helper( array(), diff --git a/tests/notification/submit_post_base.php b/tests/notification/submit_post_base.php index 466d3ec07f..6f8e10f357 100644 --- a/tests/notification/submit_post_base.php +++ b/tests/notification/submit_post_base.php @@ -130,6 +130,11 @@ abstract class phpbb_notification_submit_post_base extends phpbb_database_test_c $phpbb_container->setParameter('tables.notifications', 'phpbb_notifications'); $phpbb_container->setParameter('tables.user_notifications', 'phpbb_user_notifications'); $phpbb_container->setParameter('tables.notification_types', 'phpbb_notification_types'); + $phpbb_container->setParameter('tables.topics_watch', 'phpbb_topics_watch'); + $phpbb_container->setParameter('tables.topics_track', 'phpbb_topics_track'); + $phpbb_container->setParameter('tables.posts', 'phpbb_posts'); + $phpbb_container->setParameter('tables.forums_watch', 'phpbb_forums_watch'); + $phpbb_container->setParameter('tables.forums_track', 'phpbb_forums_track'); $phpbb_container->set('content.visibility', new \phpbb\content_visibility($auth, $config, $phpbb_dispatcher, $db, $user, $phpbb_root_path, $phpEx, FORUMS_TABLE, POSTS_TABLE, TOPICS_TABLE, USERS_TABLE)); $phpbb_container->compile();