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