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