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