]> git.mxchange.org Git - friendica.git/blob - src/Model/UserItem.php
Functionality is now added
[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 array $item The message array that is checked for notifications
30          * @param int   $uid  User ID
31          */
32         public static function setNotification($iid, $uid)
33         {
34                 $fields = ['id', 'body', 'origin', 'parent', 'gravity', 'tag', 'contact-id',
35                         'thr-parent', 'parent-uri', 'mention'];
36                 $item = Item::selectFirst($fields, ['id' => $iid]);
37
38                 // Don't check for own posts
39                 if ($item['origin'] || empty($uid)) {
40                         return;
41                 }
42
43                 $fields = ['ignored', 'mention'];
44                 $thread = Item::selectFirstThreadForUser($uid, $fields, ['iid' => $item['parent'], 'deleted' => false]);
45                 if ($thread['ignored']) {
46                         return;
47                 }
48
49                 $notification_type = self::NOTIF_NONE;
50
51                 if (self::checkShared($item, $uid)) {
52                         $notification_type = $notification_type | self::NOTIF_SHARED;
53                 }
54
55                 $profiles = self::getProfileForUser($uid);
56
57                 if (self::checkImplicitMention($item, $uid, $profiles)) {
58                         $notification_type = $notification_type | self::NOTIF_IMPLICIT_TAGGED;
59                 }
60
61                 if (self::checkExplicitMention($item, $uid, $profiles)) {
62                         $notification_type = $notification_type | self::NOTIF_EXPLICIT_TAGGED;
63                 }
64
65                 $contacts = [];
66                 $ret = DBA::select('contact', ['id'], ['uid' => 0, 'nurl' => $profiles]);
67                 while ($contact = DBA::fetch($ret)) {
68                         $contacts[] = $contact['id'];
69                 }
70                 DBA::close($ret);
71
72                 if (self::checkCommentedThread($item, $uid, $contacts)) {
73                         $notification_type = $notification_type | self::NOTIF_THREAD_COMMENT;
74                 }
75
76                 if (self::checkDirectComment($item, $uid, $contacts, $thread)) {
77                         $notification_type = $notification_type | self::NOTIF_DIRECT_COMMENT;
78                 }
79
80                 if (self::checkCommentedParticipation($item, $uid, $contacts)) {
81                         $notification_type = $notification_type | self::NOTIF_COMMENT_PARTICIPATION;
82                 }
83
84                 if (self::checkActivityParticipation($item, $uid, $contacts)) {
85                         $notification_type = $notification_type | self::NOTIF_ACTIVITY_PARTICIPATION;
86                 }
87
88                 if (empty($notification_type)) {
89                         return;
90                 }
91
92                 Logger::info('Set notification', ['iid' => $item['id'], 'uid' => $uid, 'notification-type' => $notification_type]);
93
94                 DBA::update('user-item', ['notification-type' => $notification_type], ['iid' => $item['id'], 'uid' => $uid], true);
95         }
96
97         // Fetch all contacts for the given profiles
98         private static function getProfileForUser($uid)
99         {
100                 $notification_data = ['uid' => $uid, 'profiles' => []];
101                 Hook::callAll('check_item_notification', $notification_data);
102
103                 $profiles = $notification_data['profiles'];
104
105                 $fields = ['nickname'];
106                 $user = DBA::selectFirst('user', $fields, ['uid' => $uid]);
107                 if (!DBA::isResult($user)) {
108                         return false;
109                 }
110
111                 $owner = DBA::selectFirst('contact', ['url'], ['self' => true, 'uid' => $uid]);
112                 if (!DBA::isResult($owner)) {
113                         return false;
114                 }
115
116                 // This is our regular URL format
117                 $profiles[] = $owner['url'];
118
119                 // Notifications from Diaspora are often with an URL in the Diaspora format
120                 $profiles[] = DI::baseUrl().'/u/'.$user['nickname'];
121
122                 $profiles2 = [];
123
124                 foreach ($profiles AS $profile) {
125                         // Check for invalid profile urls. 13 should be the shortest possible profile length:
126                         // http://a.bc/d
127                         // Additionally check for invalid urls that would return the normalised value "http:"
128                         if ((strlen($profile) >= 13) && (Strings::normaliseLink($profile) != 'http:')) {
129                                 if (!in_array($profile, $profiles2))
130                                         $profiles2[] = $profile;
131
132                                 $profile = Strings::normaliseLink($profile);
133                                 if (!in_array($profile, $profiles2))
134                                         $profiles2[] = $profile;
135
136                                 $profile = str_replace('http://', 'https://', $profile);
137                                 if (!in_array($profile, $profiles2))
138                                         $profiles2[] = $profile;
139                         }
140                 }
141
142                 return $profiles2;
143         }
144
145         private static function checkShared($item, $uid)
146         {
147                 if ($item['gravity'] != GRAVITY_PARENT) {
148                         return false;
149                 }
150
151                 // Send a notification for every new post?
152                 // Either the contact had posted something directly
153                 if (DBA::exists('contact', ['id' => $item['contact-id'], 'notify_new_posts' => true])) {
154                         return true;
155                 }
156
157                 // Or the contact is a mentioned forum
158                 $tags = DBA::select('term', ['url'], ['otype' => TERM_OBJ_POST, 'oid' => $item['id'], 'type' => TERM_MENTION, 'uid' => $uid]);
159                 while ($tag = DBA::fetch($tags)) {
160                         $condition = ['nurl' => Strings::normaliseLink($tag['url']), 'uid' => $uid, 'notify_new_posts' => true, 'contact-type' => Contact::TYPE_COMMUNITY];
161                         if (DBA::exists('contact', $condition)) {
162                                 return true;
163                         }
164                 }
165
166                 return false;
167         }
168
169         // Is the user mentioned in this post?
170         private static function checkImplicitMention($item, $uid, $profiles)
171         {
172                 foreach ($profiles AS $profile) {
173                         if (strpos($item['tag'], '='.$profile.']') || strpos($item['body'], '='.$profile.']'))
174                                 return true;
175                 }
176
177                 return false;
178         }
179
180         private static function checkExplicitMention($item, $uid, $profiles)
181         {
182                 foreach ($profiles AS $profile) {
183                         if (strpos($item['tag'], '='.$profile.']') || strpos($item['body'], '='.$profile.']'))
184                                 return !(strpos($item['body'], $profile) === false);
185                 }
186
187                 return false;
188         }
189
190         // Is it a post that the user had started?
191         private static function checkCommentedThread($item, $uid, $contacts)
192         {
193                 // Additional check for connector posts
194                 $condition = ['parent' => $item['parent'], 'author-id' => $contacts, 'deleted' => false, 'gravity' => GRAVITY_PARENT];
195                 return Item::exists($condition);
196         }
197
198         private static function checkDirectComment($item, $uid, $contacts)
199         {
200                 // Additional check for connector posts
201                 $condition = ['uri' => $item['thr-parent'], 'uid' => [0, $uid], 'author-id' => $contacts, 'deleted' => false, 'gravity' => GRAVITY_COMMENT];
202                 return Item::exists($condition);
203         }
204
205         // Check for participation of one of our contacts in the thread
206         private static function checkCommentedParticipation($item, $uid, $contacts)
207         {
208                 $condition = ['parent' => $item['parent'], 'author-id' => $contacts, 'deleted' => false, 'gravity' => GRAVITY_COMMENT];
209                 return Item::exists($condition);
210         }
211
212         private static function checkActivityParticipation($item, $uid, $contacts)
213         {
214                 $condition = ['parent' => $item['parent'], 'author-id' => $contacts, 'deleted' => false, 'gravity' => GRAVITY_ACTIVITY];
215                 return Item::exists($condition);
216         }
217 }