3 * @copyright Copyright (C) 2020, Friendica
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Model;
24 use Friendica\Core\Logger;
25 use Friendica\Core\Hook;
26 use Friendica\Database\DBA;
28 use Friendica\Util\Strings;
29 use Friendica\Model\Tag;
30 use Friendica\Protocol\Activity;
36 const NOTIF_EXPLICIT_TAGGED = 1;
37 const NOTIF_IMPLICIT_TAGGED = 2;
38 const NOTIF_THREAD_COMMENT = 4;
39 const NOTIF_DIRECT_COMMENT = 8;
40 const NOTIF_COMMENT_PARTICIPATION = 16;
41 const NOTIF_ACTIVITY_PARTICIPATION = 32;
42 const NOTIF_DIRECT_THREAD_COMMENT = 64;
43 const NOTIF_SHARED = 128;
46 * Checks an item for notifications and sets the "notification-type" field
48 * - Check for mentions in posts with "uid=0" where the user hadn't interacted before
50 * @param int $iid Item ID
52 public static function setNotification(int $iid)
54 $fields = ['id', 'uri-id', 'parent-uri-id', 'uid', 'body', 'parent', 'gravity', 'tag',
55 'private', 'contact-id', 'thr-parent', 'parent-uri', 'author-id', 'verb'];
56 $item = Item::selectFirst($fields, ['id' => $iid, 'origin' => false]);
57 if (!DBA::isResult($item)) {
61 // "Activity::FOLLOW" is an automated activity, so we ignore it here
62 if ($item['verb'] == Activity::FOLLOW) {
66 if ($item['uid'] == 0) {
69 // Always include the item user
70 $uids = [$item['uid']];
73 // Add every user who participated so far in this thread
74 // This can only happen with participations on global items. (means: uid = 0)
75 $users = DBA::p("SELECT DISTINCT(`contact`.`uid`) FROM `item`
76 INNER JOIN `contact` ON `contact`.`id` = `item`.`contact-id` AND `contact`.`uid` != 0
77 WHERE `parent` IN (SELECT `parent` FROM `item` WHERE `id`=?)", $iid);
78 while ($user = DBA::fetch($users)) {
79 $uids[] = $user['uid'];
83 foreach (array_unique($uids) as $uid) {
84 self::setNotificationForUser($item, $uid);
89 * Checks an item for notifications for the given user and sets the "notification-type" field
91 * @param array $item Item array
92 * @param int $uid User ID
94 private static function setNotificationForUser(array $item, int $uid)
96 $thread = Item::selectFirstThreadForUser($uid, ['ignored'], ['iid' => $item['parent'], 'deleted' => false]);
97 if (!empty($thread['ignored'])) {
101 $notification_type = self::NOTIF_NONE;
103 if (self::checkShared($item, $uid)) {
104 $notification_type = $notification_type | self::NOTIF_SHARED;
107 $profiles = self::getProfileForUser($uid);
109 // Fetch all contacts for the given profiles
111 $ret = DBA::select('contact', ['id'], ['uid' => 0, 'nurl' => $profiles]);
112 while ($contact = DBA::fetch($ret)) {
113 $contacts[] = $contact['id'];
117 // Don't create notifications for user's posts
118 if (in_array($item['author-id'], $contacts)) {
122 // Only create notifications for posts and comments, not for activities
123 if (in_array($item['gravity'], [GRAVITY_PARENT, GRAVITY_COMMENT])) {
124 if (self::checkImplicitMention($item, $profiles)) {
125 $notification_type = $notification_type | self::NOTIF_IMPLICIT_TAGGED;
128 if (self::checkExplicitMention($item, $profiles)) {
129 $notification_type = $notification_type | self::NOTIF_EXPLICIT_TAGGED;
132 if (self::checkCommentedThread($item, $contacts)) {
133 $notification_type = $notification_type | self::NOTIF_THREAD_COMMENT;
136 if (self::checkDirectComment($item, $contacts)) {
137 $notification_type = $notification_type | self::NOTIF_DIRECT_COMMENT;
140 if (self::checkDirectCommentedThread($item, $contacts)) {
141 $notification_type = $notification_type | self::NOTIF_DIRECT_THREAD_COMMENT;
144 if (self::checkCommentedParticipation($item, $contacts)) {
145 $notification_type = $notification_type | self::NOTIF_COMMENT_PARTICIPATION;
148 if (self::checkActivityParticipation($item, $contacts)) {
149 $notification_type = $notification_type | self::NOTIF_ACTIVITY_PARTICIPATION;
153 if (empty($notification_type)) {
157 Logger::info('Set notification', ['iid' => $item['id'], 'uid' => $uid, 'notification-type' => $notification_type]);
159 $fields = ['notification-type' => $notification_type];
160 Post\User::update($item['uri-id'], $uid, $fields);
161 DBA::update('user-item', $fields, ['iid' => $item['id'], 'uid' => $uid], true);
165 * Fetch all profiles (contact URL) of a given user
166 * @param int $uid User ID
168 * @return array Profile links
170 private static function getProfileForUser(int $uid)
172 $notification_data = ['uid' => $uid, 'profiles' => []];
173 Hook::callAll('check_item_notification', $notification_data);
175 $profiles = $notification_data['profiles'];
177 $user = DBA::selectFirst('user', ['nickname'], ['uid' => $uid]);
178 if (!DBA::isResult($user)) {
182 $owner = DBA::selectFirst('contact', ['url', 'alias'], ['self' => true, 'uid' => $uid]);
183 if (!DBA::isResult($owner)) {
187 // This is our regular URL format
188 $profiles[] = $owner['url'];
191 $profiles[] = $owner['alias'];
193 // Notifications from Diaspora are often with an URL in the Diaspora format
194 $profiles[] = DI::baseUrl() . '/u/' . $user['nickname'];
196 // Validate and add profile links
197 foreach ($profiles AS $key => $profile) {
198 // Check for invalid profile urls (without scheme, host or path) and remove them
199 if (empty(parse_url($profile, PHP_URL_SCHEME)) || empty(parse_url($profile, PHP_URL_HOST)) || empty(parse_url($profile, PHP_URL_PATH))) {
200 unset($profiles[$key]);
204 // Add the normalized form
205 $profile = Strings::normaliseLink($profile);
206 $profiles[] = $profile;
209 $profile = str_replace('http://', 'https://', $profile);
210 $profiles[] = $profile;
213 return array_unique($profiles);
217 * Check for a "shared" notification for every new post of contacts from the given user
219 * @param int $uid User ID
220 * @return bool A contact had shared something
222 private static function checkShared(array $item, int $uid)
224 // Only check on original posts and reshare ("announce") activities, otherwise return
225 if (($item['gravity'] != GRAVITY_PARENT) && ($item['verb'] != Activity::ANNOUNCE)) {
229 // Check if the contact posted or shared something directly
230 if (DBA::exists('contact', ['id' => $item['contact-id'], 'notify_new_posts' => true])) {
234 // The following check doesn't make sense on activities, so quit here
235 if ($item['verb'] == Activity::ANNOUNCE) {
239 // Check if the contact is a mentioned forum
240 $tags = DBA::select('tag-view', ['url'], ['uri-id' => $item['uri-id'], 'type' => [Tag::MENTION, Tag::EXCLUSIVE_MENTION]]);
241 while ($tag = DBA::fetch($tags)) {
242 $condition = ['nurl' => Strings::normaliseLink($tag['url']), 'uid' => $uid, 'notify_new_posts' => true, 'contact-type' => Contact::TYPE_COMMUNITY];
243 if (DBA::exists('contact', $condition)) {
253 * Check for an implicit mention (only tag, no body) of the given user
255 * @param array $profiles Profile links
256 * @return bool The user is mentioned
258 private static function checkImplicitMention(array $item, array $profiles)
260 $mentions = Tag::getByURIId($item['uri-id'], [Tag::IMPLICIT_MENTION]);
261 foreach ($mentions as $mention) {
262 foreach ($profiles as $profile) {
263 if (Strings::compareLink($profile, $mention['url'])) {
273 * Check for an explicit mention (tag and body) of the given user
275 * @param array $profiles Profile links
276 * @return bool The user is mentioned
278 private static function checkExplicitMention(array $item, array $profiles)
280 $mentions = Tag::getByURIId($item['uri-id'], [Tag::MENTION, Tag::EXCLUSIVE_MENTION]);
281 foreach ($mentions as $mention) {
282 foreach ($profiles as $profile) {
283 if (Strings::compareLink($profile, $mention['url'])) {
293 * Check if the given user had created this thread
295 * @param array $contacts Array of contact IDs
296 * @return bool The user had created this thread
298 private static function checkCommentedThread(array $item, array $contacts)
300 $condition = ['parent' => $item['parent'], 'author-id' => $contacts, 'deleted' => false, 'gravity' => GRAVITY_PARENT];
301 return Item::exists($condition);
305 * Check for a direct comment to a post of the given user
307 * @param array $contacts Array of contact IDs
308 * @return bool The item is a direct comment to a user comment
310 private static function checkDirectComment(array $item, array $contacts)
312 $condition = ['uri' => $item['thr-parent'], 'uid' => $item['uid'], 'author-id' => $contacts, 'deleted' => false, 'gravity' => GRAVITY_COMMENT];
313 return Item::exists($condition);
317 * Check for a direct comment to the starting post of the given user
319 * @param array $contacts Array of contact IDs
320 * @return bool The user had created this thread
322 private static function checkDirectCommentedThread(array $item, array $contacts)
324 $condition = ['uri' => $item['thr-parent'], 'uid' => $item['uid'], 'author-id' => $contacts, 'deleted' => false, 'gravity' => GRAVITY_PARENT];
325 return Item::exists($condition);
329 * Check if the user had commented in this thread
331 * @param array $contacts Array of contact IDs
332 * @return bool The user had commented in the thread
334 private static function checkCommentedParticipation(array $item, array $contacts)
336 $condition = ['parent' => $item['parent'], 'author-id' => $contacts, 'deleted' => false, 'gravity' => GRAVITY_COMMENT];
337 return Item::exists($condition);
341 * Check if the user had interacted in this thread (Like, Dislike, ...)
343 * @param array $contacts Array of contact IDs
344 * @return bool The user had interacted in the thread
346 private static function checkActivityParticipation(array $item, array $contacts)
348 $condition = ['parent' => $item['parent'], 'author-id' => $contacts, 'deleted' => false, 'gravity' => GRAVITY_ACTIVITY];
349 return Item::exists($condition);