summaryrefslogtreecommitdiff
path: root/app/Model/UserMention.php
blob: 97a4e41981439dc7ac9c94646e30c3cfd737a533 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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);
            }
        }
    }
}