[ticket/11103] Ability to query data before running create_insert_array()

Mark post/topic in queue notifications read when visiting mcp
Change post/topic in queue notification url to use MCP.

Fix the bug:
Approving a topic marks the topic as read, but before the notification
is created for the user approving the topic (if they would get a
notification that the topic has been made). This causes it to be
stuck "unread".

PHPBB3-11103
This commit is contained in:
Nathan Guse 2012-10-12 16:54:42 -05:00
parent 8b2f1127e4
commit 3d79ce2803
17 changed files with 169 additions and 36 deletions

View file

@ -34,6 +34,7 @@ class mcp_queue
{
global $auth, $db, $user, $template, $cache;
global $config, $phpbb_root_path, $phpEx, $action;
global $phpbb_notifications;
include_once($phpbb_root_path . 'includes/functions_posting.' . $phpEx);
@ -84,6 +85,9 @@ class mcp_queue
if (isset($topic_info[$topic_id]['topic_first_post_id']))
{
$post_id = (int) $topic_info[$topic_id]['topic_first_post_id'];
// Mark the notification as read
$phpbb_notifications->mark_notifications_read('topic_in_queue', $topic_id, $user->data['user_id']);
}
else
{
@ -91,6 +95,9 @@ class mcp_queue
}
}
// Mark the notification as read
$phpbb_notifications->mark_notifications_read('post_in_queue', $post_id, $user->data['user_id']);
$post_info = get_post_data(array($post_id), 'm_approve', true);
if (!sizeof($post_info))

View file

@ -324,8 +324,6 @@ class phpbb_notification_manager
// Make sure not to send new notifications to users who've already been notified about this item
// This may happen when an item was added, but now new users are able to see the item
// todo Users should not receive notifications from multiple events from the same item (ex: for a topic reply with a quote including your username)
// Probably should be handled within each type?
$sql = 'SELECT user_id
FROM ' . NOTIFICATIONS_TABLE . "
WHERE item_type = '" . $this->db->sql_escape($item_type) . "'
@ -342,6 +340,11 @@ class phpbb_notification_manager
return;
}
// Allow notifications to perform actions before creating the insert array (such as run a query to cache some data needed for all notifications)
$notification = $this->get_item_type_class($item_type_class_name);
$pre_create_data = $notification->pre_create_insert_array($data, $notify_users);
unset($notification);
// Go through each user so we can insert a row in the DB and then notify them by their desired means
foreach ($notify_users as $user => $methods)
{
@ -350,7 +353,7 @@ class phpbb_notification_manager
$notification->user_id = (int) $user;
// Store the creation array in our new rows that will be inserted later
$new_rows[] = $notification->create_insert_array($data);
$new_rows[] = $notification->create_insert_array($data, $pre_create_data);
// Users are needed to send notifications
$user_ids = array_merge($user_ids, $notification->users_to_query());

View file

@ -111,14 +111,15 @@ class phpbb_notification_type_approve_post extends phpbb_notification_type_post
* (The service handles insertion)
*
* @param array $post Data from submit_post
* @param array $pre_create_data Data from pre_create_insert_array()
*
* @return array Array of data ready to be inserted into the database
*/
public function create_insert_array($post)
public function create_insert_array($post, $pre_create_data = array())
{
$this->set_data('post_subject', $post['post_subject']);
$data = parent::create_insert_array($post);
$data = parent::create_insert_array($post, $pre_create_data);
$this->time = $data['time'] = time();

View file

@ -111,12 +111,13 @@ class phpbb_notification_type_approve_topic extends phpbb_notification_type_topi
* (The service handles insertion)
*
* @param array $post Data from submit_post
* @param array $pre_create_data Data from pre_create_insert_array()
*
* @return array Array of data ready to be inserted into the database
*/
public function create_insert_array($post)
public function create_insert_array($post, $pre_create_data = array())
{
$data = parent::create_insert_array($post);
$data = parent::create_insert_array($post, $pre_create_data);
$this->time = $data['time'] = time();

View file

@ -164,10 +164,11 @@ abstract class phpbb_notification_type_base implements phpbb_notification_type_i
* (The service handles insertion)
*
* @param array $type_data Data unique to this notification type
* @param array $pre_create_data Data from pre_create_insert_array()
*
* @return array Array of data ready to be inserted into the database
*/
public function create_insert_array($type_data)
public function create_insert_array($type_data, $pre_create_data = array())
{
// Defaults
$this->data = array_merge(array(
@ -257,6 +258,22 @@ abstract class phpbb_notification_type_base implements phpbb_notification_type_i
return true;
}
/**
* Pre create insert array function
* This allows you to perform certain actions, like run a query
* and load data, before create_insert_array() is run. The data
* returned from this function will be sent to create_insert_array().
*
* @param array $type_data Data unique to this notification type
* @param array $notify_users Notify users list
* Formated from find_users_for_notification()
* @return array Whatever you want to send to create_insert_array().
*/
public function pre_create_insert_array($type_data, $notify_users)
{
return array();
}
/**
* -------------- Helper functions -------------------
*/

View file

@ -98,10 +98,11 @@ class phpbb_notification_type_disapprove_post extends phpbb_notification_type_ap
* (The service handles insertion)
*
* @param array $post Data from submit_post
* @param array $pre_create_data Data from pre_create_insert_array()
*
* @return array Array of data ready to be inserted into the database
*/
public function create_insert_array($post)
public function create_insert_array($post, $pre_create_data = array())
{
$this->set_data('disapprove_reason', $post['disapprove_reason']);

View file

@ -98,14 +98,15 @@ class phpbb_notification_type_disapprove_topic extends phpbb_notification_type_a
* (The service handles insertion)
*
* @param array $post Data from submit_post
* @param array $pre_create_data Data from pre_create_insert_array()
*
* @return array Array of data ready to be inserted into the database
*/
public function create_insert_array($post)
public function create_insert_array($post, $pre_create_data = array())
{
$this->set_data('disapprove_reason', $post['disapprove_reason']);
$data = parent::create_insert_array($post);
$data = parent::create_insert_array($post, $pre_create_data);
$this->time = $data['time'] = time();

View file

@ -41,7 +41,9 @@ interface phpbb_notification_type_interface
public function mark_unread($return);
public function create_insert_array($type_data);
public function pre_create_insert_array($type_data, $notify_users);
public function create_insert_array($type_data, $pre_create_data);
public function users_to_query();

View file

@ -170,15 +170,16 @@ class phpbb_notification_type_pm extends phpbb_notification_type_base
* (The service handles insertion)
*
* @param array $post Data from submit_post
* @param array $pre_create_data Data from pre_create_insert_array()
*
* @return array Array of data ready to be inserted into the database
*/
public function create_insert_array($pm)
public function create_insert_array($pm, $pre_create_data = array())
{
$this->set_data('from_user_id', $pm['from_user_id']);
$this->set_data('message_subject', $pm['message_subject']);
return parent::create_insert_array($pm);
return parent::create_insert_array($pm, $pre_create_data);
}
}

View file

@ -242,11 +242,6 @@ class phpbb_notification_type_post extends phpbb_notification_type_base
*/
public function users_to_query()
{
/*$responders[] = array(
'poster_id' => $post['poster_id'],
'username' => (($post['poster_id'] == ANONYMOUS) ? $post['post_username'] : ''),
);*/
$responders = $this->get_data('responders');
$users = array(
$this->get_data('poster_id'),
@ -263,15 +258,47 @@ class phpbb_notification_type_post extends phpbb_notification_type_base
return $users;
}
/**
* Pre create insert array function
* This allows you to perform certain actions, like run a query
* and load data, before create_insert_array() is run. The data
* returned from this function will be sent to create_insert_array().
*
* @param array $post Post data from submit_post
* @param array $notify_users Notify users list
* Formated from find_users_for_notification()
* @return array Whatever you want to send to create_insert_array().
*/
public function pre_create_insert_array($post, $notify_users)
{
if (!sizeof($notify_users))
{
return array();
}
$tracking_data = array();
$sql = 'SELECT user_id, mark_time FROM ' . TOPICS_TRACK_TABLE . '
WHERE topic_id = ' . (int) $post['topic_id'] . '
AND ' . $this->db->sql_in_set('user_id', array_keys($notify_users));
$result = $this->db->sql_query($sql);
while ($row = $this->db->sql_fetchrow($result))
{
$tracking_data[$row['user_id']] = $row['mark_time'];
}
return $tracking_data;
}
/**
* Function for preparing the data for insertion in an SQL query
* (The service handles insertion)
*
* @param array $post Data from submit_post
* @param array $pre_create_data Data from pre_create_insert_array()
*
* @return array Array of data ready to be inserted into the database
*/
public function create_insert_array($post)
public function create_insert_array($post, $pre_create_data = array())
{
$this->set_data('poster_id', $post['poster_id']);
@ -287,7 +314,14 @@ class phpbb_notification_type_post extends phpbb_notification_type_base
$this->time = $post['post_time'];
return parent::create_insert_array($post);
// Topics can be "read" before they are public (while awaiting approval).
// Make sure that if the user has read the topic, it's marked as read in the notification
if (isset($pre_create_data[$this->user_id]) && $pre_create_data[$this->user_id] >= $this->time)
{
$this->unread = false;
}
return parent::create_insert_array($post, $pre_create_data);
}
/**

View file

@ -120,17 +120,28 @@ class phpbb_notification_type_post_in_queue extends phpbb_notification_type_post
return $notify_users;
}
/**
* Get the url to this item
*
* @return string URL
*/
public function get_url()
{
return append_sid($this->phpbb_root_path . 'mcp.' . $this->php_ext, "i=queue&mode=approve_details&f={$this->get_data('forum_id')}&p={$this->item_id}");
}
/**
* Function for preparing the data for insertion in an SQL query
* (The service handles insertion)
*
* @param array $post Data from submit_post
* @param array $pre_create_data Data from pre_create_insert_array()
*
* @return array Array of data ready to be inserted into the database
*/
public function create_insert_array($post)
public function create_insert_array($post, $pre_create_data = array())
{
$data = parent::create_insert_array($post);
$data = parent::create_insert_array($post, $pre_create_data);
$this->time = $data['time'] = time();

View file

@ -203,15 +203,16 @@ class phpbb_notification_type_report_pm extends phpbb_notification_type_pm
* (The service handles insertion)
*
* @param array $post Data from submit_post
* @param array $pre_create_data Data from pre_create_insert_array()
*
* @return array Array of data ready to be inserted into the database
*/
public function create_insert_array($post)
public function create_insert_array($post, $pre_create_data = array())
{
$this->set_data('reporter_id', $this->user->data['user_id']);
$this->set_data('reason_title', strtoupper($post['reason_title']));
$this->set_data('reason_description', $post['reason_description']);
return parent::create_insert_array($post);
return parent::create_insert_array($post, $pre_create_data);
}
}

View file

@ -124,14 +124,15 @@ class phpbb_notification_type_report_pm_closed extends phpbb_notification_type_p
* (The service handles insertion)
*
* @param array $post Data from submit_post
* @param array $pre_create_data Data from pre_create_insert_array()
*
* @return array Array of data ready to be inserted into the database
*/
public function create_insert_array($post)
public function create_insert_array($post, $pre_create_data = array())
{
$this->set_data('closer_id', $post['closer_id']);
$data = parent::create_insert_array($post);
$data = parent::create_insert_array($post, $pre_create_data);
$this->time = $data['time'] = time();

View file

@ -150,15 +150,16 @@ class phpbb_notification_type_report_post extends phpbb_notification_type_post_i
* (The service handles insertion)
*
* @param array $post Data from submit_post
* @param array $pre_create_data Data from pre_create_insert_array()
*
* @return array Array of data ready to be inserted into the database
*/
public function create_insert_array($post)
public function create_insert_array($post, $pre_create_data = array())
{
$this->set_data('reporter_id', $this->user->data['user_id']);
$this->set_data('reason_title', strtoupper($post['reason_title']));
$this->set_data('reason_description', $post['reason_description']);
return parent::create_insert_array($post);
return parent::create_insert_array($post, $pre_create_data);
}
}

View file

@ -124,14 +124,15 @@ class phpbb_notification_type_report_post_closed extends phpbb_notification_type
* (The service handles insertion)
*
* @param array $post Data from submit_post
* @param array $pre_create_data Data from pre_create_insert_array()
*
* @return array Array of data ready to be inserted into the database
*/
public function create_insert_array($post)
public function create_insert_array($post, $pre_create_data = array())
{
$this->set_data('closer_id', $post['closer_id']);
$data = parent::create_insert_array($post);
$data = parent::create_insert_array($post, $pre_create_data);
$this->time = $data['time'] = time();

View file

@ -76,7 +76,7 @@ class phpbb_notification_type_topic extends phpbb_notification_type_base
public function find_users_for_notification($topic, $options = array())
{
$options = array_merge(array(
'ignore_users' => array(),
'ignore_users' => array(),
), $options);
// Let's continue to use the phpBB subscriptions system, at least for now.
@ -207,15 +207,47 @@ class phpbb_notification_type_topic extends phpbb_notification_type_base
return array($this->data['poster_id']);
}
/**
* Pre create insert array function
* This allows you to perform certain actions, like run a query
* and load data, before create_insert_array() is run. The data
* returned from this function will be sent to create_insert_array().
*
* @param array $post Post data from submit_post
* @param array $notify_users Notify users list
* Formated from find_users_for_notification()
* @return array Whatever you want to send to create_insert_array().
*/
public function pre_create_insert_array($post, $notify_users)
{
if (!sizeof($notify_users))
{
return array();
}
$tracking_data = array();
$sql = 'SELECT user_id, mark_time FROM ' . TOPICS_TRACK_TABLE . '
WHERE topic_id = ' . (int) $post['topic_id'] . '
AND ' . $this->db->sql_in_set('user_id', array_keys($notify_users));
$result = $this->db->sql_query($sql);
while ($row = $this->db->sql_fetchrow($result))
{
$tracking_data[$row['user_id']] = $row['mark_time'];
}
return $tracking_data;
}
/**
* Function for preparing the data for insertion in an SQL query
* (The service handles insertion)
*
* @param array $post Data from submit_post
* @param array $pre_create_data Data from pre_create_insert_array()
*
* @return array Array of data ready to be inserted into the database
*/
public function create_insert_array($post)
public function create_insert_array($post, $pre_create_data = array())
{
$this->set_data('poster_id', $post['poster_id']);
@ -227,6 +259,13 @@ class phpbb_notification_type_topic extends phpbb_notification_type_base
$this->time = $post['post_time'];
return parent::create_insert_array($post);
// Topics can be "read" before they are public (while awaiting approval).
// Make sure that if the user has read the topic, it's marked as read in the notification
if (isset($pre_create_data[$this->user_id]) && $pre_create_data[$this->user_id] >= $this->time)
{
$this->unread = false;
}
return parent::create_insert_array($post, $pre_create_data);
}
}

View file

@ -113,17 +113,28 @@ class phpbb_notification_type_topic_in_queue extends phpbb_notification_type_top
return $notify_users;
}
/**
* Get the url to this item
*
* @return string URL
*/
public function get_url()
{
return append_sid($this->phpbb_root_path . 'mcp.' . $this->php_ext, "i=queue&mode=approve_details&f={$this->item_parent_id}&t={$this->item_id}");
}
/**
* Function for preparing the data for insertion in an SQL query
* (The service handles insertion)
*
* @param array $topic Data from submit_post
* @param array $pre_create_data Data from pre_create_insert_array()
*
* @return array Array of data ready to be inserted into the database
*/
public function create_insert_array($topic)
public function create_insert_array($topic, $pre_create_data = array())
{
$data = parent::create_insert_array($topic);
$data = parent::create_insert_array($topic, $pre_create_data);
$this->time = $data['time'] = time();