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', 'uid', 'body', 'parent', 'gravity', 'tag',
55 '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 // fetch all users in the thread
67 $users = DBA::p("SELECT DISTINCT(`contact`.`uid`) FROM `item`
68 INNER JOIN `contact` ON `contact`.`id` = `item`.`contact-id` AND `contact`.`uid` != 0
69 WHERE `parent` IN (SELECT `parent` FROM `item` WHERE `id`=?)", $iid);
70 while ($user = DBA::fetch($users)) {
71 self::setNotificationForUser($item, $user['uid']);
77 * Checks an item for notifications for the given user and sets the "notification-type" field
79 * @param array $item Item array
80 * @param int $uid User ID
82 private static function setNotificationForUser(array $item, int $uid)
84 $thread = Item::selectFirstThreadForUser($uid, ['ignored'], ['iid' => $item['parent'], 'deleted' => false]);
85 if (!empty($thread['ignored'])) {
89 $notification_type = self::NOTIF_NONE;
91 if (self::checkShared($item, $uid)) {
92 $notification_type = $notification_type | self::NOTIF_SHARED;
95 $profiles = self::getProfileForUser($uid);
97 // Fetch all contacts for the given profiles
99 $ret = DBA::select('contact', ['id'], ['uid' => 0, 'nurl' => $profiles]);
100 while ($contact = DBA::fetch($ret)) {
101 $contacts[] = $contact['id'];
105 // Don't create notifications for user's posts
106 if (in_array($item['author-id'], $contacts)) {
110 // Only create notifications for posts and comments, not for activities
111 if (in_array($item['gravity'], [GRAVITY_PARENT, GRAVITY_COMMENT])) {
112 if (self::checkImplicitMention($item, $profiles)) {
113 $notification_type = $notification_type | self::NOTIF_IMPLICIT_TAGGED;
116 if (self::checkExplicitMention($item, $profiles)) {
117 $notification_type = $notification_type | self::NOTIF_EXPLICIT_TAGGED;
120 if (self::checkCommentedThread($item, $contacts)) {
121 $notification_type = $notification_type | self::NOTIF_THREAD_COMMENT;
124 if (self::checkDirectComment($item, $contacts)) {
125 $notification_type = $notification_type | self::NOTIF_DIRECT_COMMENT;
128 if (self::checkDirectCommentedThread($item, $contacts)) {
129 $notification_type = $notification_type | self::NOTIF_DIRECT_THREAD_COMMENT;
132 if (self::checkCommentedParticipation($item, $contacts)) {
133 $notification_type = $notification_type | self::NOTIF_COMMENT_PARTICIPATION;
136 if (self::checkActivityParticipation($item, $contacts)) {
137 $notification_type = $notification_type | self::NOTIF_ACTIVITY_PARTICIPATION;
141 if (empty($notification_type)) {
145 Logger::info('Set notification', ['iid' => $item['id'], 'uid' => $uid, 'notification-type' => $notification_type]);
147 DBA::update('user-item', ['notification-type' => $notification_type], ['iid' => $item['id'], 'uid' => $uid], true);
151 * Fetch all profiles (contact URL) of a given user
152 * @param int $uid User ID
154 * @return array Profile links
156 private static function getProfileForUser(int $uid)
158 $notification_data = ['uid' => $uid, 'profiles' => []];
159 Hook::callAll('check_item_notification', $notification_data);
161 $profiles = $notification_data['profiles'];
163 $user = DBA::selectFirst('user', ['nickname'], ['uid' => $uid]);
164 if (!DBA::isResult($user)) {
168 $owner = DBA::selectFirst('contact', ['url', 'alias'], ['self' => true, 'uid' => $uid]);
169 if (!DBA::isResult($owner)) {
173 // This is our regular URL format
174 $profiles[] = $owner['url'];
177 $profiles[] = $owner['alias'];
179 // Notifications from Diaspora are often with an URL in the Diaspora format
180 $profiles[] = DI::baseUrl() . '/u/' . $user['nickname'];
182 // Validate and add profile links
183 foreach ($profiles AS $key => $profile) {
184 // Check for invalid profile urls (without scheme, host or path) and remove them
185 if (empty(parse_url($profile, PHP_URL_SCHEME)) || empty(parse_url($profile, PHP_URL_HOST)) || empty(parse_url($profile, PHP_URL_PATH))) {
186 unset($profiles[$key]);
190 // Add the normalized form
191 $profile = Strings::normaliseLink($profile);
192 $profiles[] = $profile;
195 $profile = str_replace('http://', 'https://', $profile);
196 $profiles[] = $profile;
199 return array_unique($profiles);
203 * Check for a "shared" notification for every new post of contacts from the given user
205 * @param int $uid User ID
206 * @return bool A contact had shared something
208 private static function checkShared(array $item, int $uid)
210 if (($item['gravity'] != GRAVITY_PARENT) && ($item['verb'] != Activity::ANNOUNCE)) {
214 // Either the contact had posted something directly
215 if (DBA::exists('contact', ['id' => $item['contact-id'], 'notify_new_posts' => true])) {
219 if ($item['gravity'] != GRAVITY_PARENT) {
223 // Or the contact is a mentioned forum
224 $tags = DBA::select('tag-view', ['url'], ['uri-id' => $item['uri-id'], 'type' => [Tag::MENTION, Tag::EXCLUSIVE_MENTION]]);
225 while ($tag = DBA::fetch($tags)) {
226 $condition = ['nurl' => Strings::normaliseLink($tag['url']), 'uid' => $uid, 'notify_new_posts' => true, 'contact-type' => Contact::TYPE_COMMUNITY];
227 if (DBA::exists('contact', $condition)) {
237 * Check for an implicit mention (only tag, no body) of the given user
239 * @param array $profiles Profile links
240 * @return bool The user is mentioned
242 private static function checkImplicitMention(array $item, array $profiles)
244 $mentions = Tag::getByURIId($item['uri-id'], [Tag::IMPLICIT_MENTION]);
245 foreach ($mentions as $mention) {
246 foreach ($profiles as $profile) {
247 if (Strings::compareLink($profile, $mention['url'])) {
257 * Check for an explicit mention (tag and body) of the given user
259 * @param array $profiles Profile links
260 * @return bool The user is mentioned
262 private static function checkExplicitMention(array $item, array $profiles)
264 $mentions = Tag::getByURIId($item['uri-id'], [Tag::MENTION, Tag::EXCLUSIVE_MENTION]);
265 foreach ($mentions as $mention) {
266 foreach ($profiles as $profile) {
267 if (Strings::compareLink($profile, $mention['url'])) {
277 * Check if the given user had created this thread
279 * @param array $contacts Array of contact IDs
280 * @return bool The user had created this thread
282 private static function checkCommentedThread(array $item, array $contacts)
284 $condition = ['parent' => $item['parent'], 'author-id' => $contacts, 'deleted' => false, 'gravity' => GRAVITY_PARENT];
285 return Item::exists($condition);
289 * Check for a direct comment to a post of the given user
291 * @param array $contacts Array of contact IDs
292 * @return bool The item is a direct comment to a user comment
294 private static function checkDirectComment(array $item, array $contacts)
296 $condition = ['uri' => $item['thr-parent'], 'uid' => $item['uid'], 'author-id' => $contacts, 'deleted' => false, 'gravity' => GRAVITY_COMMENT];
297 return Item::exists($condition);
301 * Check for a direct comment to the starting post of the given user
303 * @param array $contacts Array of contact IDs
304 * @return bool The user had created this thread
306 private static function checkDirectCommentedThread(array $item, array $contacts)
308 $condition = ['uri' => $item['thr-parent'], 'uid' => $item['uid'], 'author-id' => $contacts, 'deleted' => false, 'gravity' => GRAVITY_PARENT];
309 return Item::exists($condition);
313 * Check if the user had commented in this thread
315 * @param array $contacts Array of contact IDs
316 * @return bool The user had commented in the thread
318 private static function checkCommentedParticipation(array $item, array $contacts)
320 $condition = ['parent' => $item['parent'], 'author-id' => $contacts, 'deleted' => false, 'gravity' => GRAVITY_COMMENT];
321 return Item::exists($condition);
325 * Check if the user had interacted in this thread (Like, Dislike, ...)
327 * @param array $contacts Array of contact IDs
328 * @return bool The user had interacted in the thread
330 private static function checkActivityParticipation(array $item, array $contacts)
332 $condition = ['parent' => $item['parent'], 'author-id' => $contacts, 'deleted' => false, 'gravity' => GRAVITY_ACTIVITY];
333 return Item::exists($condition);