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;
34 const NOTIF_EXPLICIT_TAGGED = 1;
35 const NOTIF_IMPLICIT_TAGGED = 2;
36 const NOTIF_THREAD_COMMENT = 4;
37 const NOTIF_DIRECT_COMMENT = 8;
38 const NOTIF_COMMENT_PARTICIPATION = 16;
39 const NOTIF_ACTIVITY_PARTICIPATION = 32;
40 const NOTIF_DIRECT_THREAD_COMMENT = 64;
41 const NOTIF_SHARED = 128;
44 * Checks an item for notifications and sets the "notification-type" field
46 * - Check for mentions in posts with "uid=0" where the user hadn't interacted before
48 * @param int $iid Item ID
50 public static function setNotification(int $iid)
52 $fields = ['id', 'uid', 'body', 'parent', 'gravity', 'tag', 'contact-id', 'thr-parent', 'parent-uri', 'author-id'];
53 $item = Item::selectFirst($fields, ['id' => $iid, 'origin' => false]);
54 if (!DBA::isResult($item)) {
58 // fetch all users in the thread
59 $users = DBA::p("SELECT DISTINCT(`contact`.`uid`) FROM `item`
60 INNER JOIN `contact` ON `contact`.`id` = `item`.`contact-id` AND `contact`.`uid` != 0
61 WHERE `parent` IN (SELECT `parent` FROM `item` WHERE `id`=?)", $iid);
62 while ($user = DBA::fetch($users)) {
63 self::setNotificationForUser($item, $user['uid']);
69 * Checks an item for notifications for the given user and sets the "notification-type" field
71 * @param array $item Item array
72 * @param int $uid User ID
74 private static function setNotificationForUser(array $item, int $uid)
76 $thread = Item::selectFirstThreadForUser($uid, ['ignored'], ['iid' => $item['parent'], 'deleted' => false]);
77 if ($thread['ignored']) {
81 $notification_type = self::NOTIF_NONE;
83 if (self::checkShared($item, $uid)) {
84 $notification_type = $notification_type | self::NOTIF_SHARED;
87 $profiles = self::getProfileForUser($uid);
89 // Fetch all contacts for the given profiles
91 $ret = DBA::select('contact', ['id'], ['uid' => 0, 'nurl' => $profiles]);
92 while ($contact = DBA::fetch($ret)) {
93 $contacts[] = $contact['id'];
97 // Don't create notifications for user's posts
98 if (in_array($item['author-id'], $contacts)) {
102 if (self::checkImplicitMention($item, $profiles)) {
103 $notification_type = $notification_type | self::NOTIF_IMPLICIT_TAGGED;
106 if (self::checkExplicitMention($item, $profiles)) {
107 $notification_type = $notification_type | self::NOTIF_EXPLICIT_TAGGED;
110 if (self::checkCommentedThread($item, $contacts)) {
111 $notification_type = $notification_type | self::NOTIF_THREAD_COMMENT;
114 if (self::checkDirectComment($item, $contacts)) {
115 $notification_type = $notification_type | self::NOTIF_DIRECT_COMMENT;
118 if (self::checkDirectCommentedThread($item, $contacts)) {
119 $notification_type = $notification_type | self::NOTIF_DIRECT_THREAD_COMMENT;
122 if (self::checkCommentedParticipation($item, $contacts)) {
123 $notification_type = $notification_type | self::NOTIF_COMMENT_PARTICIPATION;
126 if (self::checkActivityParticipation($item, $contacts)) {
127 $notification_type = $notification_type | self::NOTIF_ACTIVITY_PARTICIPATION;
130 if (empty($notification_type)) {
134 Logger::info('Set notification', ['iid' => $item['id'], 'uid' => $uid, 'notification-type' => $notification_type]);
136 DBA::update('user-item', ['notification-type' => $notification_type], ['iid' => $item['id'], 'uid' => $uid], true);
140 * Fetch all profiles (contact URL) of a given user
141 * @param int $uid User ID
143 * @return array Profile links
145 private static function getProfileForUser(int $uid)
147 $notification_data = ['uid' => $uid, 'profiles' => []];
148 Hook::callAll('check_item_notification', $notification_data);
150 $profiles = $notification_data['profiles'];
152 $user = DBA::selectFirst('user', ['nickname'], ['uid' => $uid]);
153 if (!DBA::isResult($user)) {
157 $owner = DBA::selectFirst('contact', ['url', 'alias'], ['self' => true, 'uid' => $uid]);
158 if (!DBA::isResult($owner)) {
162 // This is our regular URL format
163 $profiles[] = $owner['url'];
166 $profiles[] = $owner['alias'];
168 // Notifications from Diaspora are often with an URL in the Diaspora format
169 $profiles[] = DI::baseUrl() . '/u/' . $user['nickname'];
171 // Validate and add profile links
172 foreach ($profiles AS $key => $profile) {
173 // Check for invalid profile urls (without scheme, host or path) and remove them
174 if (empty(parse_url($profile, PHP_URL_SCHEME)) || empty(parse_url($profile, PHP_URL_HOST)) || empty(parse_url($profile, PHP_URL_PATH))) {
175 unset($profiles[$key]);
179 // Add the normalized form
180 $profile = Strings::normaliseLink($profile);
181 $profiles[] = $profile;
184 $profile = str_replace('http://', 'https://', $profile);
185 $profiles[] = $profile;
188 return array_unique($profiles);
192 * Check for a "shared" notification for every new post of contacts from the given user
194 * @param int $uid User ID
195 * @return bool A contact had shared something
197 private static function checkShared(array $item, int $uid)
199 if ($item['gravity'] != GRAVITY_PARENT) {
203 // Either the contact had posted something directly
204 if (DBA::exists('contact', ['id' => $item['contact-id'], 'notify_new_posts' => true])) {
208 // Or the contact is a mentioned forum
209 $tags = DBA::select('term', ['url'], ['otype' => TERM_OBJ_POST, 'oid' => $item['id'], 'type' => TERM_MENTION, 'uid' => $uid]);
210 while ($tag = DBA::fetch($tags)) {
211 $condition = ['nurl' => Strings::normaliseLink($tag['url']), 'uid' => $uid, 'notify_new_posts' => true, 'contact-type' => Contact::TYPE_COMMUNITY];
212 if (DBA::exists('contact', $condition)) {
221 * Check for an implicit mention (only tag, no body) of the given user
223 * @param array $profiles Profile links
224 * @return bool The user is mentioned
226 private static function checkImplicitMention(array $item, array $profiles)
228 foreach ($profiles AS $profile) {
229 if (strpos($item['tag'], '=' . $profile.']') || strpos($item['body'], '=' . $profile . ']')) {
230 if (strpos($item['body'], $profile) === false) {
240 * Check for an explicit mention (tag and body) of the given user
242 * @param array $profiles Profile links
243 * @return bool The user is mentioned
245 private static function checkExplicitMention(array $item, array $profiles)
247 foreach ($profiles AS $profile) {
248 if (strpos($item['tag'], '=' . $profile.']') || strpos($item['body'], '=' . $profile . ']')) {
249 if (!(strpos($item['body'], $profile) === false)) {
259 * Check if the given user had created this thread
261 * @param array $contacts Array of contact IDs
262 * @return bool The user had created this thread
264 private static function checkCommentedThread(array $item, array $contacts)
266 $condition = ['parent' => $item['parent'], 'author-id' => $contacts, 'deleted' => false, 'gravity' => GRAVITY_PARENT];
267 return Item::exists($condition);
271 * Check for a direct comment to a post of the given user
273 * @param array $contacts Array of contact IDs
274 * @return bool The item is a direct comment to a user comment
276 private static function checkDirectComment(array $item, array $contacts)
278 $condition = ['uri' => $item['thr-parent'], 'uid' => $item['uid'], 'author-id' => $contacts, 'deleted' => false, 'gravity' => GRAVITY_COMMENT];
279 return Item::exists($condition);
283 * Check for a direct comment to the starting post of the given user
285 * @param array $contacts Array of contact IDs
286 * @return bool The user had created this thread
288 private static function checkDirectCommentedThread(array $item, array $contacts)
290 $condition = ['uri' => $item['thr-parent'], 'uid' => $item['uid'], 'author-id' => $contacts, 'deleted' => false, 'gravity' => GRAVITY_PARENT];
291 return Item::exists($condition);
295 * Check if the user had commented in this thread
297 * @param array $contacts Array of contact IDs
298 * @return bool The user had commented in the thread
300 private static function checkCommentedParticipation(array $item, array $contacts)
302 $condition = ['parent' => $item['parent'], 'author-id' => $contacts, 'deleted' => false, 'gravity' => GRAVITY_COMMENT];
303 return Item::exists($condition);
307 * Check if the user had interacted in this thread (Like, Dislike, ...)
309 * @param array $contacts Array of contact IDs
310 * @return bool The user had interacted in the thread
312 private static function checkActivityParticipation(array $item, array $contacts)
314 $condition = ['parent' => $item['parent'], 'author-id' => $contacts, 'deleted' => false, 'gravity' => GRAVITY_ACTIVITY];
315 return Item::exists($condition);