From aa76f6b9f2b85a1f85da40d71eb533589c106a09 Mon Sep 17 00:00:00 2001 From: rxu Date: Wed, 1 Jul 2020 13:26:35 +0700 Subject: [PATCH] [ticket/16544] Use forum_notify.txt for forum subscription email notifications PHPBB3-16544 --- .../container/services_notification.yml | 10 ++ phpBB/includes/functions_posting.php | 2 + phpBB/install/schemas/schema_data.sql | 2 + phpBB/language/en/ucp.php | 1 + phpBB/phpbb/notification/type/forum.php | 145 ++++++++++++++++++ phpBB/phpbb/notification/type/post.php | 12 -- tests/notification/submit_post_base.php | 2 +- 7 files changed, 161 insertions(+), 13 deletions(-) create mode 100644 phpBB/phpbb/notification/type/forum.php diff --git a/phpBB/config/default/container/services_notification.yml b/phpBB/config/default/container/services_notification.yml index 41f80cdd30..c18e358114 100644 --- a/phpBB/config/default/container/services_notification.yml +++ b/phpBB/config/default/container/services_notification.yml @@ -176,6 +176,16 @@ services: tags: - { name: notification.type } + notification.type.forum: + class: phpbb\notification\type\forum + shared: false + parent: notification.type.post + calls: + - [set_user_loader, ['@user_loader']] + - [set_config, ['@config']] + tags: + - { name: notification.type } + # ----- Notification's methods ----- # Service MUST NOT be shared for all the plugins to work. notification.method_collection: diff --git a/phpBB/includes/functions_posting.php b/phpBB/includes/functions_posting.php index c056d93fb4..9314616e2b 100644 --- a/phpBB/includes/functions_posting.php +++ b/phpBB/includes/functions_posting.php @@ -2399,6 +2399,7 @@ function submit_post($mode, $subject, $username, $topic_type, &$poll_ary, &$data 'notification.type.quote', 'notification.type.bookmark', 'notification.type.post', + 'notification.type.forum', ), $notification_data); break; @@ -2417,6 +2418,7 @@ function submit_post($mode, $subject, $username, $topic_type, &$poll_ary, &$data 'notification.type.bookmark', 'notification.type.topic', 'notification.type.post', + 'notification.type.forum', ), $notification_data); break; } diff --git a/phpBB/install/schemas/schema_data.sql b/phpBB/install/schemas/schema_data.sql index da7cad3915..92a3e9008a 100644 --- a/phpBB/install/schemas/schema_data.sql +++ b/phpBB/install/schemas/schema_data.sql @@ -845,5 +845,7 @@ INSERT INTO phpbb_user_notifications (item_type, item_id, user_id, method) VALUE INSERT INTO phpbb_user_notifications (item_type, item_id, user_id, method) VALUES('notification.type.post', 0, 2, 'notification.method.email'); INSERT INTO phpbb_user_notifications (item_type, item_id, user_id, method) VALUES('notification.type.topic', 0, 2, 'notification.method.board'); INSERT INTO phpbb_user_notifications (item_type, item_id, user_id, method) VALUES('notification.type.topic', 0, 2, 'notification.method.email'); +INSERT INTO phpbb_user_notifications (item_type, item_id, user_id, method) VALUES('notification.type.forum', 0, 2, 'notification.method.board'); +INSERT INTO phpbb_user_notifications (item_type, item_id, user_id, method) VALUES('notification.type.forum', 0, 2, 'notification.method.email'); # POSTGRES COMMIT # diff --git a/phpBB/language/en/ucp.php b/phpBB/language/en/ucp.php index 7e78c84976..7cfbc2d50c 100644 --- a/phpBB/language/en/ucp.php +++ b/phpBB/language/en/ucp.php @@ -336,6 +336,7 @@ $lang = array_merge($lang, array( 'NOTIFICATION_TYPE' => 'Notification type', 'NOTIFICATION_TYPE_BOOKMARK' => 'Someone replies to a topic you have bookmarked', 'NOTIFICATION_TYPE_GROUP_REQUEST' => 'Someone requests to join a group you lead', + 'NOTIFICATION_TYPE_FORUM' => 'Someone replies to a topic in a forum to which you are subscribed', 'NOTIFICATION_TYPE_IN_MODERATION_QUEUE' => 'A post or topic needs approval', 'NOTIFICATION_TYPE_MODERATION_QUEUE' => 'Your topics/posts are approved or disapproved by a moderator', 'NOTIFICATION_TYPE_PM' => 'Someone sends you a private message', diff --git a/phpBB/phpbb/notification/type/forum.php b/phpBB/phpbb/notification/type/forum.php new file mode 100644 index 0000000000..3c1c4579f1 --- /dev/null +++ b/phpBB/phpbb/notification/type/forum.php @@ -0,0 +1,145 @@ + +* @license GNU General Public License, version 2 (GPL-2.0) +* +* For full copyright and license information, please see +* the docs/CREDITS.txt file. +* +*/ + +namespace phpbb\notification\type; + +/** +* Forum notifications class +* This class handles notifications for replies to a topic in a forum user subscribed to +*/ + +class forum extends \phpbb\notification\type\post +{ + /** + * Get notification type name + * + * @return string + */ + public function get_type() + { + return 'notification.type.forum'; + } + + /** + * Notification option data (for outputting to the user) + * + * @var bool|array False if the service should use it's default data + * Array of data (including keys 'id', 'lang', and 'group') + */ + static public $notification_option = [ + 'lang' => 'NOTIFICATION_TYPE_FORUM', + 'group' => 'NOTIFICATION_GROUP_POSTING', + ]; + + /** + * Find the users who want to receive notifications + * + * @param array $post Data from submit_post + * @param array $options Options for finding users for notification + * + * @return array + */ + public function find_users_for_notification($post, $options = []) + { + $options = array_merge([ + 'ignore_users' => [], + ], $options); + + $users = []; + + $sql = 'SELECT user_id + FROM ' . FORUMS_WATCH_TABLE . ' + WHERE forum_id = ' . (int) $post['forum_id'] . ' + AND notify_status = ' . NOTIFY_YES . ' + AND user_id <> ' . (int) $post['poster_id']; + $result = $this->db->sql_query($sql); + while ($row = $this->db->sql_fetchrow($result)) + { + $users[] = (int) $row['user_id']; + } + $this->db->sql_freeresult($result); + + $notify_users = $this->get_authorised_recipients($users, $post['forum_id'], $options, true); + + if (empty($notify_users)) + { + return []; + } + + // Try to find the users who already have been notified about replies and have not read the topic since and just update their notifications + $notified_users = $this->notification_manager->get_notified_users($this->get_type(), [ + 'item_parent_id' => static::get_item_parent_id($post), + 'read' => 0, + ]); + + foreach ($notified_users as $user => $notification_data) + { + unset($notify_users[$user]); + + /** @var post $notification */ + $notification = $this->notification_manager->get_item_type_class($this->get_type(), $notification_data); + $update_responders = $notification->add_responders($post); + if (!empty($update_responders)) + { + $this->notification_manager->update_notification($notification, $update_responders, [ + 'item_parent_id' => self::get_item_parent_id($post), + 'read' => 0, + 'user_id' => $user, + ]); + } + } + + return $notify_users; + } + + /** + * Get email template + * + * @return string|bool + */ + public function get_email_template() + { + return 'forum_notify'; + } + + /** + * Get email template variables + * + * @return array + */ + public function get_email_template_variables() + { + if ($this->get_data('post_username')) + { + $username = $this->get_data('post_username'); + } + else + { + $username = $this->user_loader->get_username($this->get_data('poster_id'), 'username'); + } + + return [ + 'AUTHOR_NAME' => htmlspecialchars_decode($username), + 'FORUM_NAME' => htmlspecialchars_decode(censor_text($this->get_data('forum_name'))), + 'POST_SUBJECT' => htmlspecialchars_decode(censor_text($this->get_data('post_subject'))), + 'TOPIC_TITLE' => htmlspecialchars_decode(censor_text($this->get_data('topic_title'))), + + 'U_VIEW_POST' => generate_board_url() . "/viewtopic.{$this->php_ext}?p={$this->item_id}#p{$this->item_id}", + 'U_NEWEST_POST' => generate_board_url() . "/viewtopic.{$this->php_ext}?f={$this->get_data('forum_id')}&t={$this->item_parent_id}&e=1&view=unread#unread", + 'U_TOPIC' => generate_board_url() . "/viewtopic.{$this->php_ext}?f={$this->get_data('forum_id')}&t={$this->item_parent_id}", + 'U_VIEW_TOPIC' => generate_board_url() . "/viewtopic.{$this->php_ext}?f={$this->get_data('forum_id')}&t={$this->item_parent_id}", + 'U_FORUM' => generate_board_url() . "/viewforum.{$this->php_ext}?f={$this->get_data('forum_id')}", + 'U_STOP_WATCHING_FORUM' => generate_board_url() . "/viewtopic.{$this->php_ext}?uid={$this->user_id}&f={$this->get_data('forum_id')}&t={$this->item_parent_id}&unwatch=forum", + ]; + } +} diff --git a/phpBB/phpbb/notification/type/post.php b/phpBB/phpbb/notification/type/post.php index 64ed91a5f5..a25328ff43 100644 --- a/phpBB/phpbb/notification/type/post.php +++ b/phpBB/phpbb/notification/type/post.php @@ -129,18 +129,6 @@ class post extends \phpbb\notification\type\base } $this->db->sql_freeresult($result); - $sql = 'SELECT user_id - FROM ' . FORUMS_WATCH_TABLE . ' - WHERE forum_id = ' . (int) $post['forum_id'] . ' - AND notify_status = ' . NOTIFY_YES . ' - AND user_id <> ' . (int) $post['poster_id']; - $result = $this->db->sql_query($sql); - while ($row = $this->db->sql_fetchrow($result)) - { - $users[] = (int) $row['user_id']; - } - $this->db->sql_freeresult($result); - $notify_users = $this->get_authorised_recipients($users, $post['forum_id'], $options, true); if (empty($notify_users)) diff --git a/tests/notification/submit_post_base.php b/tests/notification/submit_post_base.php index 82db7753b5..3906c1d74c 100644 --- a/tests/notification/submit_post_base.php +++ b/tests/notification/submit_post_base.php @@ -135,7 +135,7 @@ abstract class phpbb_notification_submit_post_base extends phpbb_database_test_c $phpbb_container->compile(); // Notification Types - $notification_types = array('quote', 'bookmark', 'post', 'post_in_queue', 'topic', 'topic_in_queue', 'approve_topic', 'approve_post'); + $notification_types = array('quote', 'bookmark', 'post', 'post_in_queue', 'topic', 'topic_in_queue', 'approve_topic', 'approve_post', 'forum'); $notification_types_array = array(); foreach ($notification_types as $type) {