]> git.mxchange.org Git - friendica.git/blob - src/Model/UserItem.php
Issue 9099: Improve mentions from non followers
[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', 'parent-uri-id', 'uid', 'body', 'parent', 'gravity', 'tag',
55                         'private', '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                 $uids = [];
68                 $users = DBA::p("SELECT DISTINCT(`contact`.`uid`) FROM `item`
69                         INNER JOIN `contact` ON `contact`.`id` = `item`.`contact-id` AND `contact`.`uid` != 0
70                         WHERE `parent` IN (SELECT `parent` FROM `item` WHERE `id`=?)", $iid);
71                 while ($user = DBA::fetch($users)) {
72                         $uids[$user['uid']] = $user['uid'];
73                 }
74                 DBA::close($users);
75
76                 // Add item users
77                 $users = Item::select(['uid'], ["`parent-uri-id` = ? AND `uid` != ?", $item['parent-uri-id'], 0], ['group_by' => ['uid']]);
78                 while ($user = DBA::fetch($users)) {
79                         $uids[$user['uid']] = $user['uid'];
80                 }
81                 DBA::close($users);
82
83                 // Check for mentions to local users
84                 if (in_array($item['private'], [Item::PUBLIC, Item::UNLISTED])) {
85                         $mentions = Tag::getByURIId($item['uri-id'], [Tag::MENTION, Tag::IMPLICIT_MENTION, Tag::EXCLUSIVE_MENTION]);
86                         foreach ($mentions as $mention) {
87                                 $uid = User::getIdForURL($mention['url']);
88                                 if (!empty($uid)) {
89                                         $uids[$uid] = $uid;
90                                 }
91                         }
92                 }
93
94                 foreach ($uids as $uid) {
95                         self::setNotificationForUser($item, $uid);
96                 }
97         }
98
99         /**
100          * Checks an item for notifications for the given user and sets the "notification-type" field
101          *
102          * @param array $item Item array
103          * @param int   $uid  User ID
104          */
105         private static function setNotificationForUser(array $item, int $uid)
106         {
107                 $thread = Item::selectFirstThreadForUser($uid, ['ignored'], ['iid' => $item['parent'], 'deleted' => false]);
108                 if (!empty($thread['ignored'])) {
109                         return;
110                 }
111
112                 $notification_type = self::NOTIF_NONE;
113
114                 if (self::checkShared($item, $uid)) {
115                         $notification_type = $notification_type | self::NOTIF_SHARED;
116                 }
117
118                 $profiles = self::getProfileForUser($uid);
119
120                 // Fetch all contacts for the given profiles
121                 $contacts = [];
122                 $ret = DBA::select('contact', ['id'], ['uid' => 0, 'nurl' => $profiles]);
123                 while ($contact = DBA::fetch($ret)) {
124                         $contacts[] = $contact['id'];
125                 }
126                 DBA::close($ret);
127
128                 // Don't create notifications for user's posts
129                 if (in_array($item['author-id'], $contacts)) {
130                         return;
131                 }
132
133                 // Only create notifications for posts and comments, not for activities
134                 if (in_array($item['gravity'], [GRAVITY_PARENT, GRAVITY_COMMENT])) {
135                         if (self::checkImplicitMention($item, $profiles)) {
136                                 $notification_type = $notification_type | self::NOTIF_IMPLICIT_TAGGED;
137                         }
138
139                         if (self::checkExplicitMention($item, $profiles)) {
140                                 $notification_type = $notification_type | self::NOTIF_EXPLICIT_TAGGED;
141                         }
142
143                         if (self::checkCommentedThread($item, $contacts)) {
144                                 $notification_type = $notification_type | self::NOTIF_THREAD_COMMENT;
145                         }
146
147                         if (self::checkDirectComment($item, $contacts)) {
148                                 $notification_type = $notification_type | self::NOTIF_DIRECT_COMMENT;
149                         }
150
151                         if (self::checkDirectCommentedThread($item, $contacts)) {
152                                 $notification_type = $notification_type | self::NOTIF_DIRECT_THREAD_COMMENT;
153                         }
154
155                         if (self::checkCommentedParticipation($item, $contacts)) {
156                                 $notification_type = $notification_type | self::NOTIF_COMMENT_PARTICIPATION;
157                         }
158
159                         if (self::checkActivityParticipation($item, $contacts)) {
160                                 $notification_type = $notification_type | self::NOTIF_ACTIVITY_PARTICIPATION;
161                         }
162                 }
163
164                 if (empty($notification_type)) {
165                         return;
166                 }
167
168                 Logger::info('Set notification', ['iid' => $item['id'], 'uid' => $uid, 'notification-type' => $notification_type]);
169
170                 DBA::update('user-item', ['notification-type' => $notification_type], ['iid' => $item['id'], 'uid' => $uid], true);
171         }
172
173         /**
174          * Fetch all profiles (contact URL) of a given user
175          * @param int $uid User ID
176          *
177          * @return array Profile links
178          */
179         private static function getProfileForUser(int $uid)
180         {
181                 $notification_data = ['uid' => $uid, 'profiles' => []];
182                 Hook::callAll('check_item_notification', $notification_data);
183
184                 $profiles = $notification_data['profiles'];
185
186                 $user = DBA::selectFirst('user', ['nickname'], ['uid' => $uid]);
187                 if (!DBA::isResult($user)) {
188                         return [];
189                 }
190
191                 $owner = DBA::selectFirst('contact', ['url', 'alias'], ['self' => true, 'uid' => $uid]);
192                 if (!DBA::isResult($owner)) {
193                         return [];
194                 }
195
196                 // This is our regular URL format
197                 $profiles[] = $owner['url'];
198
199                 // Now the alias
200                 $profiles[] = $owner['alias'];
201
202                 // Notifications from Diaspora are often with an URL in the Diaspora format
203                 $profiles[] = DI::baseUrl() . '/u/' . $user['nickname'];
204
205                 // Validate and add profile links
206                 foreach ($profiles AS $key => $profile) {
207                         // Check for invalid profile urls (without scheme, host or path) and remove them
208                         if (empty(parse_url($profile, PHP_URL_SCHEME)) || empty(parse_url($profile, PHP_URL_HOST)) || empty(parse_url($profile, PHP_URL_PATH))) {
209                                 unset($profiles[$key]);
210                                 continue;
211                         }
212
213                         // Add the normalized form
214                         $profile = Strings::normaliseLink($profile);
215                         $profiles[] = $profile;
216
217                         // Add the SSL form
218                         $profile = str_replace('http://', 'https://', $profile);
219                         $profiles[] = $profile;
220                 }
221
222                 return array_unique($profiles);
223         }
224
225         /**
226          * Check for a "shared" notification for every new post of contacts from the given user
227          * @param array $item
228          * @param int   $uid  User ID
229          * @return bool A contact had shared something
230          */
231         private static function checkShared(array $item, int $uid)
232         {
233                 // Only check on original posts and reshare ("announce") activities, otherwise return
234                 if (($item['gravity'] != GRAVITY_PARENT) && ($item['verb'] != Activity::ANNOUNCE)) {
235                         return false;
236                 }
237
238                 // Check if the contact posted or shared something directly
239                 if (DBA::exists('contact', ['id' => $item['contact-id'], 'notify_new_posts' => true])) {
240                         return true;
241                 }
242
243                 // The following check doesn't make sense on activities, so quit here
244                 if ($item['verb'] == Activity::ANNOUNCE) {
245                         return false;
246                 }
247
248                 // Check if the contact is a mentioned forum
249                 $tags = DBA::select('tag-view', ['url'], ['uri-id' => $item['uri-id'], 'type' => [Tag::MENTION, Tag::EXCLUSIVE_MENTION]]);
250                 while ($tag = DBA::fetch($tags)) {
251                         $condition = ['nurl' => Strings::normaliseLink($tag['url']), 'uid' => $uid, 'notify_new_posts' => true, 'contact-type' => Contact::TYPE_COMMUNITY];
252                         if (DBA::exists('contact', $condition)) {
253                                 return true;
254                         }
255                 }
256                 DBA::close($tags);
257
258                 return false;
259         }
260
261         /**
262          * Check for an implicit mention (only tag, no body) of the given user
263          * @param array $item
264          * @param array $profiles Profile links
265          * @return bool The user is mentioned
266          */
267         private static function checkImplicitMention(array $item, array $profiles)
268         {
269                 $mentions = Tag::getByURIId($item['uri-id'], [Tag::IMPLICIT_MENTION]);
270                 foreach ($mentions as $mention) {
271                         foreach ($profiles as $profile) {
272                                 if (Strings::compareLink($profile, $mention['url'])) {
273                                         return true;
274                                 }
275                         }
276                 }
277
278                 return false;
279         }
280
281         /**
282          * Check for an explicit mention (tag and body) of the given user
283          * @param array $item
284          * @param array $profiles Profile links
285          * @return bool The user is mentioned
286          */
287         private static function checkExplicitMention(array $item, array $profiles)
288         {
289                 $mentions = Tag::getByURIId($item['uri-id'], [Tag::MENTION, Tag::EXCLUSIVE_MENTION]);
290                 foreach ($mentions as $mention) {
291                         foreach ($profiles as $profile) {
292                                 if (Strings::compareLink($profile, $mention['url'])) {
293                                         return true;
294                                 }
295                         }
296                 }
297
298                 return false;
299         }
300
301         /**
302          * Check if the given user had created this thread
303          * @param array $item
304          * @param array $contacts Array of contact IDs
305          * @return bool The user had created this thread
306          */
307         private static function checkCommentedThread(array $item, array $contacts)
308         {
309                 $condition = ['parent' => $item['parent'], 'author-id' => $contacts, 'deleted' => false, 'gravity' => GRAVITY_PARENT];
310                 return Item::exists($condition);
311         }
312
313         /**
314          * Check for a direct comment to a post of the given user
315          * @param array $item
316          * @param array $contacts Array of contact IDs
317          * @return bool The item is a direct comment to a user comment
318          */
319         private static function checkDirectComment(array $item, array $contacts)
320         {
321                 $condition = ['uri' => $item['thr-parent'], 'uid' => $item['uid'], 'author-id' => $contacts, 'deleted' => false, 'gravity' => GRAVITY_COMMENT];
322                 return Item::exists($condition);
323         }
324
325         /**
326          * Check for a direct comment to the starting post of the given user
327          * @param array $item
328          * @param array $contacts Array of contact IDs
329          * @return bool The user had created this thread
330          */
331         private static function checkDirectCommentedThread(array $item, array $contacts)
332         {
333                 $condition = ['uri' => $item['thr-parent'], 'uid' => $item['uid'], 'author-id' => $contacts, 'deleted' => false, 'gravity' => GRAVITY_PARENT];
334                 return Item::exists($condition);
335         }
336
337         /**
338          *  Check if the user had commented in this thread
339          * @param array $item
340          * @param array $contacts Array of contact IDs
341          * @return bool The user had commented in the thread
342          */
343         private static function checkCommentedParticipation(array $item, array $contacts)
344         {
345                 $condition = ['parent' => $item['parent'], 'author-id' => $contacts, 'deleted' => false, 'gravity' => GRAVITY_COMMENT];
346                 return Item::exists($condition);
347         }
348
349         /**
350          * Check if the user had interacted in this thread (Like, Dislike, ...)
351          * @param array $item
352          * @param array $contacts Array of contact IDs
353          * @return bool The user had interacted in the thread
354          */
355         private static function checkActivityParticipation(array $item, array $contacts)
356         {
357                 $condition = ['parent' => $item['parent'], 'author-id' => $contacts, 'deleted' => false, 'gravity' => GRAVITY_ACTIVITY];
358                 return Item::exists($condition);
359         }
360 }