1
0
mirror of https://github.com/phpbb/phpbb.git synced 2025-07-30 21:40:43 +02:00

Merge pull request #5909 from marc1706/ticket/14754

[ticket/14754] Only one email notification per topic
This commit is contained in:
Marc Alexander
2020-05-01 16:19:36 +02:00
10 changed files with 601 additions and 3 deletions

View File

@@ -206,8 +206,10 @@ services:
- '@user_loader'
- '@user'
- '@config'
- '@dbal.conn'
- '%core.root_path%'
- '%core.php_ext%'
- '%tables.notification_emails%'
tags:
- { name: notification.method }

View File

@@ -35,6 +35,7 @@ parameters:
tables.migrations: '%core.table_prefix%migrations'
tables.moderator_cache: '%core.table_prefix%moderator_cache'
tables.modules: '%core.table_prefix%modules'
tables.notification_emails: '%core.table_prefix%notification_emails'
tables.notification_types: '%core.table_prefix%notification_types'
tables.notifications: '%core.table_prefix%notifications'
tables.poll_options: '%core.table_prefix%poll_options'

View File

@@ -0,0 +1,53 @@
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @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\db\migration\data\v33x;
class add_notification_emails_table extends \phpbb\db\migration\migration
{
static public function depends_on()
{
return [
'\phpbb\db\migration\data\v330\v330',
];
}
public function effectively_installed()
{
return $this->db_tools->sql_table_exists($this->table_prefix . 'notification_emails');
}
public function update_schema()
{
return [
'add_tables' => [
$this->table_prefix . 'notification_emails' => [
'COLUMNS' => [
'notification_type_id' => ['USINT', 0],
'item_id' => ['ULINT', 0],
'item_parent_id' => ['ULINT', 0],
'user_id' => ['ULINT', 0],
],
'PRIMARY_KEY' => ['notification_type_id', 'item_id', 'item_parent_id', 'user_id'],
],
],
];
}
public function revert_schema()
{
return [
'drop_tables' => [$this->table_prefix . 'notification_emails'],
];
}
}

View File

@@ -28,21 +28,31 @@ class email extends \phpbb\notification\method\messenger_base
/** @var \phpbb\config\config */
protected $config;
/** @var \phpbb\db\driver\driver_interface */
protected $db;
/** @var string Notification emails table */
protected $notification_emails_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 $notification_emails_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, $notification_emails_table)
{
parent::__construct($user_loader, $phpbb_root_path, $php_ext);
$this->user = $user;
$this->config = $config;
$this->db = $db;
$this->notification_emails_table = $notification_emails_table;
}
/**
@@ -68,11 +78,87 @@ 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 = [];
$sql = 'SELECT user_id
FROM ' . $this->notification_emails_table . '
WHERE notification_type_id = ' . (int) $notification_type_id .
(isset($options['item_id']) ? ' AND item_id = ' . (int) $options['item_id'] : '') .
(isset($options['item_parent_id']) ? ' AND item_parent_id = ' . (int) $options['item_parent_id'] : '') .
(isset($options['user_id']) ? ' AND user_id = ' . (int) $options['user_id'] : '');
$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
*/
public function notify()
{
$insert_buffer = new \phpbb\db\sql_insert_buffer($this->db, $this->notification_emails_table);
/** @var \phpbb\notification\type\type_interface $notification */
foreach ($this->queue as $notification)
{
$data = self::clean_data($notification->get_insert_array());
$insert_buffer->insert($data);
}
$insert_buffer->flush();
return $this->notify_using_messenger(NOTIFY_EMAIL);
}
/**
* {@inheritdoc}
*/
public function mark_notifications($notification_type_id, $item_id, $user_id, $time = false, $mark_read = true)
{
$sql = 'DELETE FROM ' . $this->notification_emails_table . '
WHERE ' . ($notification_type_id !== false ? $this->db->sql_in_set('notification_type_id', $notification_type_id) : '1=1') .
($user_id !== false ? ' AND ' . $this->db->sql_in_set('user_id', $user_id) : '') .
($item_id !== false ? ' AND ' . $this->db->sql_in_set('item_id', $item_id) : '');
$this->db->sql_query($sql);
}
/**
* {@inheritdoc}
*/
public function mark_notifications_by_parent($notification_type_id, $item_parent_id, $user_id, $time = false, $mark_read = true)
{
$sql = 'DELETE FROM ' . $this->notification_emails_table . '
WHERE ' . ($notification_type_id !== false ? $this->db->sql_in_set('notification_type_id', $notification_type_id) : '1=1') .
($user_id !== false ? ' AND ' . $this->db->sql_in_set('user_id', $user_id) : '') .
($item_parent_id !== false ? ' AND ' . $this->db->sql_in_set('item_parent_id', $item_parent_id, false, true) : '');
$this->db->sql_query($sql);
}
/**
* Clean data to contain only what we need for email notifications table
*
* @param array $data Notification data
* @return array Cleaned notification data
*/
static public function clean_data(array $data)
{
$row = [
'notification_type_id' => null,
'item_id' => null,
'item_parent_id' => null,
'user_id' => null,
];
return array_intersect_key($data, $row);
}
}

View File

@@ -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
*/

View File

@@ -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']