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