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