Make user notifications pluggable

This commit is contained in:
Frederic Guillot
2015-10-17 09:51:15 -04:00
parent 98b203fe69
commit 73a5b9bc75
25 changed files with 407 additions and 258 deletions

View File

@@ -1,123 +0,0 @@
<?php
namespace Kanboard\Model;
use Kanboard\Core\NotificationInterface;
/**
* Email Notification model
*
* @package model
* @author Frederic Guillot
*/
class EmailNotification extends Base implements NotificationInterface
{
/**
* Send email notification to someone
*
* @access public
* @param array $user
* @param string $event_name
* @param array $event_data
*/
public function send(array $user, $event_name, array $event_data)
{
if (! empty($user['email'])) {
$this->emailClient->send(
$user['email'],
$user['name'] ?: $user['username'],
$this->getMailSubject($event_name, $event_data),
$this->getMailContent($event_name, $event_data)
);
}
}
/**
* Get the mail content for a given template name
*
* @access public
* @param string $event_name Event name
* @param array $event_data Event data
* @return string
*/
public function getMailContent($event_name, array $event_data)
{
return $this->template->render(
'notification/'.str_replace('.', '_', $event_name),
$event_data + array('application_url' => $this->config->get('application_url'))
);
}
/**
* Get the mail subject for a given template name
*
* @access public
* @param string $event_name Event name
* @param array $event_data Event data
* @return string
*/
public function getMailSubject($event_name, array $event_data)
{
switch ($event_name) {
case File::EVENT_CREATE:
$subject = $this->getStandardMailSubject(e('New attachment'), $event_data);
break;
case Comment::EVENT_CREATE:
$subject = $this->getStandardMailSubject(e('New comment'), $event_data);
break;
case Comment::EVENT_UPDATE:
$subject = $this->getStandardMailSubject(e('Comment updated'), $event_data);
break;
case Subtask::EVENT_CREATE:
$subject = $this->getStandardMailSubject(e('New subtask'), $event_data);
break;
case Subtask::EVENT_UPDATE:
$subject = $this->getStandardMailSubject(e('Subtask updated'), $event_data);
break;
case Task::EVENT_CREATE:
$subject = $this->getStandardMailSubject(e('New task'), $event_data);
break;
case Task::EVENT_UPDATE:
$subject = $this->getStandardMailSubject(e('Task updated'), $event_data);
break;
case Task::EVENT_CLOSE:
$subject = $this->getStandardMailSubject(e('Task closed'), $event_data);
break;
case Task::EVENT_OPEN:
$subject = $this->getStandardMailSubject(e('Task opened'), $event_data);
break;
case Task::EVENT_MOVE_COLUMN:
$subject = $this->getStandardMailSubject(e('Column change'), $event_data);
break;
case Task::EVENT_MOVE_POSITION:
$subject = $this->getStandardMailSubject(e('Position change'), $event_data);
break;
case Task::EVENT_MOVE_SWIMLANE:
$subject = $this->getStandardMailSubject(e('Swimlane change'), $event_data);
break;
case Task::EVENT_ASSIGNEE_CHANGE:
$subject = $this->getStandardMailSubject(e('Assignee change'), $event_data);
break;
case Task::EVENT_OVERDUE:
$subject = e('[%s] Overdue tasks', $event_data['project_name']);
break;
default:
$subject = e('Notification');
}
return $subject;
}
/**
* Get the mail subject for a given label
*
* @access private
* @param string $label Label
* @param array $data Template data
* @return string
*/
private function getStandardMailSubject($label, array $data)
{
return sprintf('[%s][%s] %s (#%d)', $data['task']['project_name'], $label, $data['task']['title'], $data['task']['id']);
}
}

View File

@@ -2,72 +2,82 @@
namespace Kanboard\Model;
use Pimple\Container;
/**
* Notification Type model
* Notification Type
*
* @package model
* @author Frederic Guillot
*/
class NotificationType extends Base
abstract class NotificationType extends Base
{
/**
* SQL table name
* Mail transport instances
*
* @var string
* @access private
* @var \Pimple\Container
*/
const TABLE = 'user_has_notification_types';
private $classes;
/**
* Types
* Mail transport instances
*
* @var string
* @access private
* @var array
*/
const TYPE_WEB = 'web';
const TYPE_EMAIL = 'email';
private $labels = array();
/**
* Get all notification types
* Constructor
*
* @access public
* @param \Pimple\Container $container
*/
public function __construct(Container $container)
{
parent::__construct($container);
$this->classes = new Container;
}
/**
* Add a new notification type
*
* @access public
* @param string $type
* @param string $label
* @param string $class
*/
public function setType($type, $label, $class)
{
$container = $this->container;
$this->labels[$type] = $label;
$this->classes[$type] = function() use ($class, $container) {
return new $class($container);
};
}
/**
* Get mail notification type instance
*
* @access public
* @param string $type
* @return NotificationInterface
*/
public function getType($type)
{
return $this->classes[$type];
}
/**
* Get all notification types with labels
*
* @access public
* @return array
*/
public function getTypes()
{
return array(
self::TYPE_EMAIL => t('Email'),
self::TYPE_WEB => t('Web'),
);
}
/**
* Get selected notification types for a given user
*
* @access public
* @param integer $user_id
* @return array
*/
public function getUserSelectedTypes($user_id)
{
return $this->db->table(self::TABLE)->eq('user_id', $user_id)->asc('notification_type')->findAllByColumn('notification_type');
}
/**
* Save notification types for a given user
*
* @access public
* @param integer $user_id
* @param string[] $types
* @return boolean
*/
public function saveUserSelectedTypes($user_id, array $types)
{
$results = array();
$this->db->table(self::TABLE)->eq('user_id', $user_id)->remove();
foreach ($types as $type) {
$results[] = $this->db->table(self::TABLE)->insert(array('user_id' => $user_id, 'notification_type' => $type));
}
return ! in_array(false, $results);
return $this->labels;
}
}

View File

@@ -22,7 +22,7 @@ class OverdueNotification extends Base
foreach ($this->groupByColumn($tasks, 'project_id') as $project_id => $project_tasks) {
// Get the list of users that should receive notifications for each projects
$users = $this->notification->getUsersWithNotificationEnabled($project_id);
$users = $this->userNotification->getUsersWithNotificationEnabled($project_id);
foreach ($users as $user) {
$this->sendUserOverdueTaskNotifications($user, $project_tasks);
@@ -44,13 +44,13 @@ class OverdueNotification extends Base
$user_tasks = array();
foreach ($tasks as $task) {
if ($this->notificationFilter->shouldReceiveNotification($user, array('task' => $task))) {
if ($this->userNotificationFilter->shouldReceiveNotification($user, array('task' => $task))) {
$user_tasks[] = $task;
}
}
if (! empty($user_tasks)) {
$this->notification->sendUserNotification(
$this->userNotification->sendUserNotification(
$user,
Task::EVENT_OVERDUE,
array('tasks' => $user_tasks, 'project_name' => $tasks[0]['project_name'])

View File

@@ -5,12 +5,12 @@ namespace Kanboard\Model;
use Kanboard\Core\Translator;
/**
* Notification model
* User Notification
*
* @package model
* @author Frederic Guillot
*/
class Notification extends Base
class UserNotification extends Base
{
/**
* Send notifications to people
@@ -22,12 +22,11 @@ class Notification extends Base
public function sendNotifications($event_name, array $event_data)
{
$logged_user_id = $this->userSession->isLogged() ? $this->userSession->getId() : 0;
$users = $this->notification->getUsersWithNotificationEnabled($event_data['task']['project_id'], $logged_user_id);
$users = $this->getUsersWithNotificationEnabled($event_data['task']['project_id'], $logged_user_id);
if (! empty($users)) {
foreach ($users as $user) {
if ($this->notificationFilter->shouldReceiveNotification($user, $event_data)) {
if ($this->userNotificationFilter->shouldReceiveNotification($user, $event_data)) {
$this->sendUserNotification($user, $event_name, $event_data);
}
}
@@ -57,9 +56,8 @@ class Notification extends Base
Translator::load($this->config->get('application_language', 'en_US'));
}
foreach ($this->notificationType->getUserSelectedTypes($user['id']) as $type) {
$className = strtolower($type).'Notification';
$this->$className->send($user, $event_name, $event_data);
foreach ($this->userNotificationType->getSelectedTypes($user['id']) as $type) {
$this->userNotificationType->getType($type)->notifyUser($user, $event_name, $event_data);
}
}
@@ -118,13 +116,13 @@ class Notification extends Base
if (isset($values['notifications_enabled']) && $values['notifications_enabled'] == 1) {
$this->enableNotification($user_id);
$filter = empty($values['notifications_filter']) ? NotificationFilter::FILTER_BOTH : $values['notifications_filter'];
$filter = empty($values['notifications_filter']) ? UserNotificationFilter::FILTER_BOTH : $values['notifications_filter'];
$projects = empty($values['notification_projects']) ? array() : array_keys($values['notification_projects']);
$types = empty($values['notification_types']) ? array() : array_keys($values['notification_types']);
$this->notificationFilter->saveUserFilter($user_id, $filter);
$this->notificationFilter->saveUserSelectedProjects($user_id, $projects);
$this->notificationType->saveUserSelectedTypes($user_id, $types);
$this->userNotificationFilter->saveFilter($user_id, $filter);
$this->userNotificationFilter->saveSelectedProjects($user_id, $projects);
$this->userNotificationType->saveSelectedTypes($user_id, $types);
}
else {
$this->disableNotification($user_id);
@@ -143,8 +141,8 @@ class Notification extends Base
public function readSettings($user_id)
{
$values = $this->db->table(User::TABLE)->eq('id', $user_id)->columns('notifications_enabled', 'notifications_filter')->findOne();
$values['notification_types'] = $this->notificationType->getUserSelectedTypes($user_id);
$values['notification_projects'] = $this->notificationFilter->getUserSelectedProjects($user_id);
$values['notification_types'] = $this->userNotificationType->getSelectedTypes($user_id);
$values['notification_projects'] = $this->userNotificationFilter->getSelectedProjects($user_id);
return $values;
}

View File

@@ -3,12 +3,12 @@
namespace Kanboard\Model;
/**
* Notification Filter model
* User Notification Filter
*
* @package model
* @author Frederic Guillot
*/
class NotificationFilter extends Base
class UserNotificationFilter extends Base
{
/**
* SQL table name
@@ -50,7 +50,7 @@ class NotificationFilter extends Base
* @param integer $user_id
* @return integer
*/
public function getUserSelectedFilter($user_id)
public function getSelectedFilter($user_id)
{
return $this->db->table(User::TABLE)->eq('id', $user_id)->findOneColumn('notifications_filter');
}
@@ -62,7 +62,7 @@ class NotificationFilter extends Base
* @param integer $user_id
* @param string $filter
*/
public function saveUserFilter($user_id, $filter)
public function saveFilter($user_id, $filter)
{
$this->db->table(User::TABLE)->eq('id', $user_id)->update(array(
'notifications_filter' => $filter,
@@ -76,7 +76,7 @@ class NotificationFilter extends Base
* @param integer $user_id
* @return array
*/
public function getUserSelectedProjects($user_id)
public function getSelectedProjects($user_id)
{
return $this->db->table(self::PROJECT_TABLE)->eq('user_id', $user_id)->findAllByColumn('project_id');
}
@@ -88,7 +88,7 @@ class NotificationFilter extends Base
* @param integer $user_id
* @param integer[] $project_ids
*/
public function saveUserSelectedProjects($user_id, array $project_ids)
public function saveSelectedProjects($user_id, array $project_ids)
{
$this->db->table(self::PROJECT_TABLE)->eq('user_id', $user_id)->remove();
@@ -188,7 +188,7 @@ class NotificationFilter extends Base
*/
public function filterProject(array $user, array $event_data)
{
$projects = $this->getUserSelectedProjects($user['id']);
$projects = $this->getSelectedProjects($user['id']);
if (! empty($projects)) {
return in_array($event_data['task']['project_id'], $projects);

View File

@@ -0,0 +1,51 @@
<?php
namespace Kanboard\Model;
/**
* User Notification Type
*
* @package model
* @author Frederic Guillot
*/
class UserNotificationType extends NotificationType
{
/**
* SQL table name
*
* @var string
*/
const TABLE = 'user_has_notification_types';
/**
* Get selected notification types for a given user
*
* @access public
* @param integer $user_id
* @return array
*/
public function getSelectedTypes($user_id)
{
return $this->db->table(self::TABLE)->eq('user_id', $user_id)->asc('notification_type')->findAllByColumn('notification_type');
}
/**
* Save notification types for a given user
*
* @access public
* @param integer $user_id
* @param string[] $types
* @return boolean
*/
public function saveSelectedTypes($user_id, array $types)
{
$results = array();
$this->db->table(self::TABLE)->eq('user_id', $user_id)->remove();
foreach ($types as $type) {
$results[] = $this->db->table(self::TABLE)->insert(array('user_id' => $user_id, 'notification_type' => $type));
}
return ! in_array(false, $results);
}
}

View File

@@ -2,15 +2,13 @@
namespace Kanboard\Model;
use Kanboard\Core\NotificationInterface;
/**
* Web Notification model
* User Unread Notification
*
* @package model
* @author Frederic Guillot
*/
class WebNotification extends Base implements NotificationInterface
class UserUnreadNotification extends Base
{
/**
* SQL table name
@@ -23,14 +21,14 @@ class WebNotification extends Base implements NotificationInterface
* Add unread notification to someone
*
* @access public
* @param array $user
* @param integer $user_id
* @param string $event_name
* @param array $event_data
*/
public function send(array $user, $event_name, array $event_data)
public function create($user_id, $event_name, array $event_data)
{
$this->db->table(self::TABLE)->insert(array(
'user_id' => $user['id'],
'user_id' => $user_id,
'date_creation' => time(),
'event_name' => $event_name,
'event_data' => json_encode($event_data),