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