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