Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
40.00% covered (danger)
40.00%
12 / 30
50.00% covered (danger)
50.00%
4 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
mention
40.00% covered (danger)
40.00%
12 / 30
50.00% covered (danger)
50.00%
4 / 8
43.10
0.00% covered (danger)
0.00%
0 / 1
 get_type
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 is_available
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 find_users_for_notification
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
2
 update_notifications
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
12
 get_redirect_url
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 get_email_template
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 get_email_template_variables
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 set_helper
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3*
4* This file is part of the phpBB Forum Software package.
5*
6* @copyright (c) phpBB Limited <https://www.phpbb.com>
7* @license GNU General Public License, version 2 (GPL-2.0)
8*
9* For full copyright and license information, please see
10* the docs/CREDITS.txt file.
11*
12*/
13
14namespace phpbb\notification\type;
15
16use phpbb\textformatter\s9e\mention_helper;
17
18/**
19* Post mentioning notifications class
20* This class handles notifying users when they have been mentioned in a post
21*/
22
23class mention extends post
24{
25    /**
26    * @var mention_helper
27    */
28    protected $helper;
29
30    /**
31    * {@inheritDoc}
32    */
33    public function get_type()
34    {
35        return 'notification.type.mention';
36    }
37
38    /**
39     * {@inheritDoc}
40     */
41    protected $language_key = 'NOTIFICATION_MENTION';
42
43    /**
44     * {@inheritDoc}
45     */
46    public static $notification_option = [
47        'lang'    => 'NOTIFICATION_TYPE_MENTION',
48        'group'    => 'NOTIFICATION_GROUP_POSTING',
49    ];
50
51    /**
52     * {@inheritDoc}
53     */
54    public function is_available()
55    {
56        return $this->config['allow_mentions'] && $this->auth->acl_get('u_mention');
57    }
58
59    /**
60     * {@inheritDoc}
61     */
62    public function find_users_for_notification($type_data, $options = array())
63    {
64        $options = array_merge(array(
65            'ignore_users'        => array(),
66        ), $options);
67
68        $user_ids = $this->helper->get_mentioned_user_ids($type_data['post_text']);
69
70        $user_ids = array_unique($user_ids);
71
72        $user_ids = array_diff($user_ids, [(int) $type_data['poster_id']]);
73
74        if (empty($user_ids))
75        {
76            return array();
77        }
78
79        return $this->get_authorised_recipients($user_ids, $type_data['forum_id'], $options, true);
80    }
81
82    /**
83    * Update a notification
84    *
85    * @param array $post Data specific for this type that will be updated
86    * @return true
87    */
88    public function update_notifications($post)
89    {
90        $old_notifications = $this->notification_manager->get_notified_users($this->get_type(), array(
91            'item_id'    => static::get_item_id($post),
92        ));
93
94        // Find the new users to notify
95        $notifications = $this->find_users_for_notification($post);
96
97        // Find the notifications we must delete
98        $remove_notifications = array_diff(array_keys($old_notifications), array_keys($notifications));
99
100        // Find the notifications we must add
101        $add_notifications = array();
102        foreach (array_diff(array_keys($notifications), array_keys($old_notifications)) as $user_id)
103        {
104            $add_notifications[$user_id] = $notifications[$user_id];
105        }
106
107        // Add the necessary notifications
108        $this->notification_manager->add_notifications_for_users($this->get_type(), $post, $add_notifications);
109
110        // Remove the necessary notifications
111        if (!empty($remove_notifications))
112        {
113            $this->notification_manager->delete_notifications($this->get_type(), static::get_item_id($post), false, $remove_notifications);
114        }
115
116        // return true to continue with the update code in the notifications service (this will update the rest of the notifications)
117        return true;
118    }
119
120    /**
121    * {@inheritDoc}
122    */
123    public function get_redirect_url()
124    {
125        return $this->get_url();
126    }
127
128    /**
129     * {@inheritDoc}
130     */
131    public function get_email_template()
132    {
133        return 'mention';
134    }
135
136    /**
137     * {@inheritDoc}
138     */
139    public function get_email_template_variables()
140    {
141        $user_data = $this->user_loader->get_user($this->get_data('poster_id'));
142
143        return array_merge(parent::get_email_template_variables(), array(
144            'AUTHOR_NAME'        => htmlspecialchars_decode($user_data['username']),
145        ));
146    }
147
148    /**
149    * Set the helper service used to retrieve mentioned used
150    *
151    * @param mention_helper $helper
152    */
153    public function set_helper(mention_helper $helper): void
154    {
155        $this->helper = $helper;
156    }
157}