Send notifications on user mentions
This commit is contained in:
@@ -427,7 +427,7 @@ class Action extends Base
|
||||
return $this->board->getColumnIdByTitle($project_id, $column['title']) ?: false;
|
||||
case 'user_id':
|
||||
case 'owner_id':
|
||||
return $this->projectPermission->isMember($project_id, $param['value']) ? $param['value'] : false;
|
||||
return $this->projectPermission->isAssignable($project_id, $param['value']) ? $param['value'] : false;
|
||||
default:
|
||||
return $param['value'];
|
||||
}
|
||||
|
||||
@@ -26,8 +26,9 @@ class Comment extends Base
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
const EVENT_UPDATE = 'comment.update';
|
||||
const EVENT_CREATE = 'comment.create';
|
||||
const EVENT_UPDATE = 'comment.update';
|
||||
const EVENT_CREATE = 'comment.create';
|
||||
const EVENT_USER_MENTION = 'comment.user.mention';
|
||||
|
||||
/**
|
||||
* Get all comments for a given task
|
||||
@@ -110,7 +111,9 @@ class Comment extends Base
|
||||
$comment_id = $this->persist(self::TABLE, $values);
|
||||
|
||||
if ($comment_id) {
|
||||
$this->container['dispatcher']->dispatch(self::EVENT_CREATE, new CommentEvent(array('id' => $comment_id) + $values));
|
||||
$event = new CommentEvent(array('id' => $comment_id) + $values);
|
||||
$this->dispatcher->dispatch(self::EVENT_CREATE, $event);
|
||||
$this->userMention->fireEvents($values['comment'], self::EVENT_USER_MENTION, $event);
|
||||
}
|
||||
|
||||
return $comment_id;
|
||||
|
||||
@@ -74,6 +74,10 @@ class Notification extends Base
|
||||
return e('%s commented on the task #%d', $event_author, $event_data['task']['id']);
|
||||
case File::EVENT_CREATE:
|
||||
return e('%s attached a file to the task #%d', $event_author, $event_data['task']['id']);
|
||||
case Task::EVENT_USER_MENTION:
|
||||
return e('%s mentioned you in the task #%d', $event_author, $event_data['task']['id']);
|
||||
case Comment::EVENT_USER_MENTION:
|
||||
return e('%s mentioned you in a comment on the task #%d', $event_author, $event_data['task']['id']);
|
||||
default:
|
||||
return e('Notification');
|
||||
}
|
||||
@@ -91,52 +95,40 @@ class Notification extends Base
|
||||
{
|
||||
switch ($event_name) {
|
||||
case File::EVENT_CREATE:
|
||||
$title = e('New attachment on task #%d: %s', $event_data['file']['task_id'], $event_data['file']['name']);
|
||||
break;
|
||||
return e('New attachment on task #%d: %s', $event_data['file']['task_id'], $event_data['file']['name']);
|
||||
case Comment::EVENT_CREATE:
|
||||
$title = e('New comment on task #%d', $event_data['comment']['task_id']);
|
||||
break;
|
||||
return e('New comment on task #%d', $event_data['comment']['task_id']);
|
||||
case Comment::EVENT_UPDATE:
|
||||
$title = e('Comment updated on task #%d', $event_data['comment']['task_id']);
|
||||
break;
|
||||
return e('Comment updated on task #%d', $event_data['comment']['task_id']);
|
||||
case Subtask::EVENT_CREATE:
|
||||
$title = e('New subtask on task #%d', $event_data['subtask']['task_id']);
|
||||
break;
|
||||
return e('New subtask on task #%d', $event_data['subtask']['task_id']);
|
||||
case Subtask::EVENT_UPDATE:
|
||||
$title = e('Subtask updated on task #%d', $event_data['subtask']['task_id']);
|
||||
break;
|
||||
return e('Subtask updated on task #%d', $event_data['subtask']['task_id']);
|
||||
case Task::EVENT_CREATE:
|
||||
$title = e('New task #%d: %s', $event_data['task']['id'], $event_data['task']['title']);
|
||||
break;
|
||||
return e('New task #%d: %s', $event_data['task']['id'], $event_data['task']['title']);
|
||||
case Task::EVENT_UPDATE:
|
||||
$title = e('Task updated #%d', $event_data['task']['id']);
|
||||
break;
|
||||
return e('Task updated #%d', $event_data['task']['id']);
|
||||
case Task::EVENT_CLOSE:
|
||||
$title = e('Task #%d closed', $event_data['task']['id']);
|
||||
break;
|
||||
return e('Task #%d closed', $event_data['task']['id']);
|
||||
case Task::EVENT_OPEN:
|
||||
$title = e('Task #%d opened', $event_data['task']['id']);
|
||||
break;
|
||||
return e('Task #%d opened', $event_data['task']['id']);
|
||||
case Task::EVENT_MOVE_COLUMN:
|
||||
$title = e('Column changed for task #%d', $event_data['task']['id']);
|
||||
break;
|
||||
return e('Column changed for task #%d', $event_data['task']['id']);
|
||||
case Task::EVENT_MOVE_POSITION:
|
||||
$title = e('New position for task #%d', $event_data['task']['id']);
|
||||
break;
|
||||
return e('New position for task #%d', $event_data['task']['id']);
|
||||
case Task::EVENT_MOVE_SWIMLANE:
|
||||
$title = e('Swimlane changed for task #%d', $event_data['task']['id']);
|
||||
break;
|
||||
return e('Swimlane changed for task #%d', $event_data['task']['id']);
|
||||
case Task::EVENT_ASSIGNEE_CHANGE:
|
||||
$title = e('Assignee changed on task #%d', $event_data['task']['id']);
|
||||
break;
|
||||
return e('Assignee changed on task #%d', $event_data['task']['id']);
|
||||
case Task::EVENT_OVERDUE:
|
||||
$nb = count($event_data['tasks']);
|
||||
$title = $nb > 1 ? e('%d overdue tasks', $nb) : e('Task #%d is overdue', $event_data['tasks'][0]['id']);
|
||||
break;
|
||||
return $nb > 1 ? e('%d overdue tasks', $nb) : e('Task #%d is overdue', $event_data['tasks'][0]['id']);
|
||||
case Task::EVENT_USER_MENTION:
|
||||
return e('You were mentioned in the task #%d', $event_data['task']['id']);
|
||||
case Comment::EVENT_USER_MENTION:
|
||||
return e('You were mentioned in a comment on the task #%d', $event_data['task']['id']);
|
||||
default:
|
||||
$title = e('Notification');
|
||||
return e('Notification');
|
||||
}
|
||||
|
||||
return $title;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -86,11 +86,24 @@ class ProjectPermission extends Base
|
||||
* @param integer $user_id
|
||||
* @return boolean
|
||||
*/
|
||||
public function isMember($project_id, $user_id)
|
||||
public function isAssignable($project_id, $user_id)
|
||||
{
|
||||
return in_array($this->projectUserRole->getUserRole($project_id, $user_id), array(Role::PROJECT_MEMBER, Role::PROJECT_MANAGER));
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true if the user is member
|
||||
*
|
||||
* @access public
|
||||
* @param integer $project_id
|
||||
* @param integer $user_id
|
||||
* @return boolean
|
||||
*/
|
||||
public function isMember($project_id, $user_id)
|
||||
{
|
||||
return in_array($this->projectUserRole->getUserRole($project_id, $user_id), array(Role::PROJECT_MEMBER, Role::PROJECT_MANAGER, Role::PROJECT_VIEWER));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get active project ids by user
|
||||
*
|
||||
|
||||
@@ -41,6 +41,7 @@ class Task extends Base
|
||||
const EVENT_CREATE_UPDATE = 'task.create_update';
|
||||
const EVENT_ASSIGNEE_CHANGE = 'task.assignee_change';
|
||||
const EVENT_OVERDUE = 'task.overdue';
|
||||
const EVENT_USER_MENTION = 'task.user.mention';
|
||||
|
||||
/**
|
||||
* Recurrence: status
|
||||
|
||||
@@ -86,8 +86,13 @@ class TaskCreation extends Base
|
||||
*/
|
||||
private function fireEvents($task_id, array $values)
|
||||
{
|
||||
$values['task_id'] = $task_id;
|
||||
$this->container['dispatcher']->dispatch(Task::EVENT_CREATE_UPDATE, new TaskEvent($values));
|
||||
$this->container['dispatcher']->dispatch(Task::EVENT_CREATE, new TaskEvent($values));
|
||||
$event = new TaskEvent(array('task_id' => $task_id) + $values);
|
||||
|
||||
$this->dispatcher->dispatch(Task::EVENT_CREATE_UPDATE, $event);
|
||||
$this->dispatcher->dispatch(Task::EVENT_CREATE, $event);
|
||||
|
||||
if (! empty($values['description'])) {
|
||||
$this->userMention->fireEvents($values['description'], Task::EVENT_USER_MENTION, $event);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
61
app/Model/UserMention.php
Normal file
61
app/Model/UserMention.php
Normal file
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
namespace Kanboard\Model;
|
||||
|
||||
use Kanboard\Event\GenericEvent;
|
||||
|
||||
/**
|
||||
* User Mention
|
||||
*
|
||||
* @package model
|
||||
* @author Frederic Guillot
|
||||
*/
|
||||
class UserMention extends Base
|
||||
{
|
||||
/**
|
||||
* Get list of mentioned users
|
||||
*
|
||||
* @access public
|
||||
* @param string $content
|
||||
* @return array
|
||||
*/
|
||||
public function getMentionedUsers($content)
|
||||
{
|
||||
$users = array();
|
||||
|
||||
if (preg_match_all('/@([^\s]+)/', $content, $matches)) {
|
||||
$users = $this->db->table(User::TABLE)
|
||||
->columns('id', 'username', 'name', 'email', 'language')
|
||||
->eq('notifications_enabled', 1)
|
||||
->neq('id', $this->userSession->getId())
|
||||
->in('username', array_unique($matches[1]))
|
||||
->findAll();
|
||||
}
|
||||
|
||||
return $users;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fire events for user mentions
|
||||
*
|
||||
* @access public
|
||||
* @param string $content
|
||||
* @param string $eventName
|
||||
* @param GenericEvent $event
|
||||
*/
|
||||
public function fireEvents($content, $eventName, GenericEvent $event)
|
||||
{
|
||||
if (empty($event['project_id'])) {
|
||||
$event['project_id'] = $this->taskFinder->getProjectId($event['task_id']);
|
||||
}
|
||||
|
||||
$users = $this->getMentionedUsers($content);
|
||||
|
||||
foreach ($users as $user) {
|
||||
if ($this->projectPermission->isMember($event['project_id'], $user['id'])) {
|
||||
$event['mention'] = $user;
|
||||
$this->dispatcher->dispatch($eventName, $event);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -21,18 +21,12 @@ class UserNotification extends Base
|
||||
*/
|
||||
public function sendNotifications($event_name, array $event_data)
|
||||
{
|
||||
$logged_user_id = $this->userSession->isLogged() ? $this->userSession->getId() : 0;
|
||||
$users = $this->getUsersWithNotificationEnabled($event_data['task']['project_id'], $logged_user_id);
|
||||
$users = $this->getUsersWithNotificationEnabled($event_data['task']['project_id'], $this->userSession->getId());
|
||||
|
||||
if (! empty($users)) {
|
||||
foreach ($users as $user) {
|
||||
if ($this->userNotificationFilter->shouldReceiveNotification($user, $event_data)) {
|
||||
$this->sendUserNotification($user, $event_name, $event_data);
|
||||
}
|
||||
foreach ($users as $user) {
|
||||
if ($this->userNotificationFilter->shouldReceiveNotification($user, $event_data)) {
|
||||
$this->sendUserNotification($user, $event_name, $event_data);
|
||||
}
|
||||
|
||||
// Restore locales
|
||||
$this->config->setupTranslations();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -58,6 +52,9 @@ class UserNotification extends Base
|
||||
foreach ($this->userNotificationType->getSelectedTypes($user['id']) as $type) {
|
||||
$this->userNotificationType->getType($type)->notifyUser($user, $event_name, $event_data);
|
||||
}
|
||||
|
||||
// Restore locales
|
||||
$this->config->setupTranslations();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user