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