]> git.mxchange.org Git - friendica.git/blob - src/Model/UserItem.php
Clarify the functionality
[friendica.git] / src / Model / UserItem.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Model;
23
24 use Friendica\Core\Logger;
25 use Friendica\Core\Hook;
26 use Friendica\Database\DBA;
27 use Friendica\DI;
28 use Friendica\Util\Strings;
29 use Friendica\Model\Tag;
30 use Friendica\Protocol\Activity;
31
32 class UserItem
33 {
34         // Notification types
35         const NOTIF_NONE = 0;
36         const NOTIF_EXPLICIT_TAGGED = 1;
37         const NOTIF_IMPLICIT_TAGGED = 2;
38         const NOTIF_THREAD_COMMENT = 4;
39         const NOTIF_DIRECT_COMMENT = 8;
40         const NOTIF_COMMENT_PARTICIPATION = 16;
41         const NOTIF_ACTIVITY_PARTICIPATION = 32;
42         const NOTIF_DIRECT_THREAD_COMMENT = 64;
43         const NOTIF_SHARED = 128;
44
45         /**
46          * Checks an item for notifications and sets the "notification-type" field
47          * @ToDo:
48          * - Check for mentions in posts with "uid=0" where the user hadn't interacted before
49          *
50          * @param int $iid Item ID
51          */
52         public static function setNotification(int $iid)
53         {
54                 $fields = ['id', 'uri-id', 'uid', 'body', 'parent', 'gravity', 'tag',
55                         'contact-id', 'thr-parent', 'parent-uri', 'author-id', 'verb'];
56                 $item = Item::selectFirst($fields, ['id' => $iid, 'origin' => false]);
57                 if (!DBA::isResult($item)) {
58                         return;
59                 }
60
61                 // "Activity::FOLLOW" is an automated activity, so we ignore it here
62                 if ($item['verb'] == Activity::FOLLOW) {
63                         return;
64                 }
65
66                 // fetch all users in the thread
67                 $users = DBA::p("SELECT DISTINCT(`contact`.`uid`) FROM `item`
68                         INNER JOIN `contact` ON `contact`.`id` = `item`.`contact-id` AND `contact`.`uid` != 0
69                         WHERE `parent` IN (SELECT `parent` FROM `item` WHERE `id`=?)", $iid);
70                 while ($user = DBA::fetch($users)) {
71                         self::setNotificationForUser($item, $user['uid']);
72                 }
73                 DBA::close($users);
74         }
75
76         /**
77          * Checks an item for notifications for the given user and sets the "notification-type" field
78          *
79          * @param array $item Item array
80          * @param int   $uid  User ID
81          */
82         private static function setNotificationForUser(array $item, int $uid)
83         {
84                 $thread = Item::selectFirstThreadForUser($uid, ['ignored'], ['iid' => $item['parent'], 'deleted' => false]);
85                 if (!empty($thread['ignored'])) {
86                         return;
87                 }
88
89                 $notification_type = self::NOTIF_NONE;
90
91                 if (self::checkShared($item, $uid)) {
92                         $notification_type = $notification_type | self::NOTIF_SHARED;
93                 }
94
95                 $profiles = self::getProfileForUser($uid);
96
97                 // Fetch all contacts for the given profiles
98                 $contacts = [];
99                 $ret = DBA::select('contact', ['id'], ['uid' => 0, 'nurl' => $profiles]);
100                 while ($contact = DBA::fetch($ret)) {
101                         $contacts[] = $contact['id'];
102                 }
103                 DBA::close($ret);
104
105                 // Don't create notifications for user's posts
106                 if (in_array($item['author-id'], $contacts)) {
107                         return;
108                 }
109
110                 // Only create notifications for posts and comments, not for activities
111                 if (in_array($item['gravity'], [GRAVITY_PARENT, GRAVITY_COMMENT])) {
112                         if (self::checkImplicitMention($item, $profiles)) {
113                                 $notification_type = $notification_type | self::NOTIF_IMPLICIT_TAGGED;
114                         }
115
116                         if (self::checkExplicitMention($item, $profiles)) {
117                                 $notification_type = $notification_type | self::NOTIF_EXPLICIT_TAGGED;
118                         }
119
120                         if (self::checkCommentedThread($item, $contacts)) {
121                                 $notification_type = $notification_type | self::NOTIF_THREAD_COMMENT;
122                         }
123
124                         if (self::checkDirectComment($item, $contacts)) {
125                                 $notification_type = $notification_type | self::NOTIF_DIRECT_COMMENT;
126                         }
127
128                         if (self::checkDirectCommentedThread($item, $contacts)) {
129                                 $notification_type = $notification_type | self::NOTIF_DIRECT_THREAD_COMMENT;
130                         }
131
132                         if (self::checkCommentedParticipation($item, $contacts)) {
133                                 $notification_type = $notification_type | self::NOTIF_COMMENT_PARTICIPATION;
134                         }
135
136                         if (self::checkActivityParticipation($item, $contacts)) {
137                                 $notification_type = $notification_type | self::NOTIF_ACTIVITY_PARTICIPATION;
138                         }
139                 }
140
141                 if (empty($notification_type)) {
142                         return;
143                 }
144
145                 Logger::info('Set notification', ['iid' => $item['id'], 'uid' => $uid, 'notification-type' => $notification_type]);
146
147                 DBA::update('user-item', ['notification-type' => $notification_type], ['iid' => $item['id'], 'uid' => $uid], true);
148         }
149
150         /**
151          * Fetch all profiles (contact URL) of a given user
152          * @param int $uid User ID
153          *
154          * @return array Profile links
155          */
156         private static function getProfileForUser(int $uid)
157         {
158                 $notification_data = ['uid' => $uid, 'profiles' => []];
159                 Hook::callAll('check_item_notification', $notification_data);
160
161                 $profiles = $notification_data['profiles'];
162
163                 $user = DBA::selectFirst('user', ['nickname'], ['uid' => $uid]);
164                 if (!DBA::isResult($user)) {
165                         return [];
166                 }
167
168                 $owner = DBA::selectFirst('contact', ['url', 'alias'], ['self' => true, 'uid' => $uid]);
169                 if (!DBA::isResult($owner)) {
170                         return [];
171                 }
172
173                 // This is our regular URL format
174                 $profiles[] = $owner['url'];
175
176                 // Now the alias
177                 $profiles[] = $owner['alias'];
178
179                 // Notifications from Diaspora are often with an URL in the Diaspora format
180                 $profiles[] = DI::baseUrl() . '/u/' . $user['nickname'];
181
182                 // Validate and add profile links
183                 foreach ($profiles AS $key => $profile) {
184                         // Check for invalid profile urls (without scheme, host or path) and remove them
185                         if (empty(parse_url($profile, PHP_URL_SCHEME)) || empty(parse_url($profile, PHP_URL_HOST)) || empty(parse_url($profile, PHP_URL_PATH))) {
186                                 unset($profiles[$key]);
187                                 continue;
188                         }
189
190                         // Add the normalized form
191                         $profile = Strings::normaliseLink($profile);
192                         $profiles[] = $profile;
193
194                         // Add the SSL form
195                         $profile = str_replace('http://', 'https://', $profile);
196                         $profiles[] = $profile;
197                 }
198
199                 return array_unique($profiles);
200         }
201
202         /**
203          * Check for a "shared" notification for every new post of contacts from the given user
204          * @param array $item
205          * @param int   $uid  User ID
206          * @return bool A contact had shared something
207          */
208         private static function checkShared(array $item, int $uid)
209         {
210                 // Only sheck on starting posts and reshare ("announce") activities, otherwise return
211                 if (($item['gravity'] != GRAVITY_PARENT) && ($item['verb'] != Activity::ANNOUNCE)) {
212                         return false;
213                 }
214
215                 // Check if the contact had posted or shared something directly
216                 if (DBA::exists('contact', ['id' => $item['contact-id'], 'notify_new_posts' => true])) {
217                         return true;
218                 }
219
220                 // Don't continue when the item had been an announce activity
221                 if ($item['verb'] == Activity::ANNOUNCE) {
222                         return false;
223                 }
224
225                 // Check if the contact is a mentioned forum
226                 $tags = DBA::select('tag-view', ['url'], ['uri-id' => $item['uri-id'], 'type' => [Tag::MENTION, Tag::EXCLUSIVE_MENTION]]);
227                 while ($tag = DBA::fetch($tags)) {
228                         $condition = ['nurl' => Strings::normaliseLink($tag['url']), 'uid' => $uid, 'notify_new_posts' => true, 'contact-type' => Contact::TYPE_COMMUNITY];
229                         if (DBA::exists('contact', $condition)) {
230                                 return true;
231                         }
232                 }
233                 DBA::close($tags);
234
235                 return false;
236         }
237
238         /**
239          * Check for an implicit mention (only tag, no body) of the given user
240          * @param array $item
241          * @param array $profiles Profile links
242          * @return bool The user is mentioned
243          */
244         private static function checkImplicitMention(array $item, array $profiles)
245         {
246                 $mentions = Tag::getByURIId($item['uri-id'], [Tag::IMPLICIT_MENTION]);
247                 foreach ($mentions as $mention) {
248                         foreach ($profiles as $profile) {
249                                 if (Strings::compareLink($profile, $mention['url'])) {
250                                         return true;
251                                 }
252                         }
253                 }
254
255                 return false;
256         }
257
258         /**
259          * Check for an explicit mention (tag and body) of the given user
260          * @param array $item
261          * @param array $profiles Profile links
262          * @return bool The user is mentioned
263          */
264         private static function checkExplicitMention(array $item, array $profiles)
265         {
266                 $mentions = Tag::getByURIId($item['uri-id'], [Tag::MENTION, Tag::EXCLUSIVE_MENTION]);
267                 foreach ($mentions as $mention) {
268                         foreach ($profiles as $profile) {
269                                 if (Strings::compareLink($profile, $mention['url'])) {
270                                         return true;
271                                 }
272                         }
273                 }
274
275                 return false;
276         }
277
278         /**
279          * Check if the given user had created this thread
280          * @param array $item
281          * @param array $contacts Array of contact IDs
282          * @return bool The user had created this thread
283          */
284         private static function checkCommentedThread(array $item, array $contacts)
285         {
286                 $condition = ['parent' => $item['parent'], 'author-id' => $contacts, 'deleted' => false, 'gravity' => GRAVITY_PARENT];
287                 return Item::exists($condition);
288         }
289
290         /**
291          * Check for a direct comment to a post of the given user
292          * @param array $item
293          * @param array $contacts Array of contact IDs
294          * @return bool The item is a direct comment to a user comment
295          */
296         private static function checkDirectComment(array $item, array $contacts)
297         {
298                 $condition = ['uri' => $item['thr-parent'], 'uid' => $item['uid'], 'author-id' => $contacts, 'deleted' => false, 'gravity' => GRAVITY_COMMENT];
299                 return Item::exists($condition);
300         }
301
302         /**
303          * Check for a direct comment to the starting post of the given user
304          * @param array $item
305          * @param array $contacts Array of contact IDs
306          * @return bool The user had created this thread
307          */
308         private static function checkDirectCommentedThread(array $item, array $contacts)
309         {
310                 $condition = ['uri' => $item['thr-parent'], 'uid' => $item['uid'], 'author-id' => $contacts, 'deleted' => false, 'gravity' => GRAVITY_PARENT];
311                 return Item::exists($condition);
312         }
313
314         /**
315          *  Check if the user had commented in this thread
316          * @param array $item
317          * @param array $contacts Array of contact IDs
318          * @return bool The user had commented in the thread
319          */
320         private static function checkCommentedParticipation(array $item, array $contacts)
321         {
322                 $condition = ['parent' => $item['parent'], 'author-id' => $contacts, 'deleted' => false, 'gravity' => GRAVITY_COMMENT];
323                 return Item::exists($condition);
324         }
325
326         /**
327          * Check if the user had interacted in this thread (Like, Dislike, ...)
328          * @param array $item
329          * @param array $contacts Array of contact IDs
330          * @return bool The user had interacted in the thread
331          */
332         private static function checkActivityParticipation(array $item, array $contacts)
333         {
334                 $condition = ['parent' => $item['parent'], 'author-id' => $contacts, 'deleted' => false, 'gravity' => GRAVITY_ACTIVITY];
335                 return Item::exists($condition);
336         }
337 }