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