diff options
Diffstat (limited to 'app/Model/UserMentionModel.php')
-rw-r--r-- | app/Model/UserMentionModel.php | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/app/Model/UserMentionModel.php b/app/Model/UserMentionModel.php new file mode 100644 index 00000000..cdb9949e --- /dev/null +++ b/app/Model/UserMentionModel.php @@ -0,0 +1,62 @@ +<?php + +namespace Kanboard\Model; + +use Kanboard\Core\Base; +use Kanboard\Event\GenericEvent; + +/** + * User Mention + * + * @package Kanboard\Model + * @author Frederic Guillot + */ +class UserMentionModel 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(UserModel::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->taskFinderModel->getProjectId($event['task_id']); + } + + $users = $this->getMentionedUsers($content); + + foreach ($users as $user) { + if ($this->projectPermissionModel->isMember($event['project_id'], $user['id'])) { + $event['mention'] = $user; + $this->dispatcher->dispatch($eventName, $event); + } + } + } +} |