summaryrefslogtreecommitdiff
path: root/app/Job/UserMentionJob.php
blob: 355095bba7d5245ac67a93388efe2b00720aa423 (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
62
63
64
65
66
67
68
69
70
71
72
73
<?php

namespace Kanboard\Job;

use Kanboard\Event\GenericEvent;
use Kanboard\Model\UserModel;

/**
 * Class UserMentionJob
 *
 * @package Kanboard\Job
 * @author  Frederic Guillot
 */
class UserMentionJob extends BaseJob
{
    /**
     * Set job parameters
     *
     * @param  string       $text
     * @param  string       $eventName
     * @param  GenericEvent $event
     * @return $this
     */
    public function withParams($text, $eventName, GenericEvent $event)
    {
        $this->jobParams = array($text, $eventName, $event->getAll());
        return $this;
    }

    /**
     * Execute job
     *
     * @param string       $text
     * @param string       $eventName
     * @param array        $eventData
     */
    public function execute($text, $eventName, array $eventData)
    {
        $event = new GenericEvent($eventData);
        $users = $this->getMentionedUsers($text);

        foreach ($users as $user) {
            if ($this->projectPermissionModel->isMember($event->getProjectId(), $user['id'])) {
                $event['mention'] = $user;
                $this->dispatcher->dispatch($eventName, $event);
            }
        }
    }

    /**
     * Get list of mentioned users
     *
     * @access public
     * @param  string $text
     * @return array
     */
    public function getMentionedUsers($text)
    {
        $users = array();

        if (preg_match_all('/@([^\s,!:?]+)/', $text, $matches)) {
            array_walk($matches[1], function (&$username) { $username = rtrim($username, '.'); });
            $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;
    }
}