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\Term;
35 const NOTIF_EXPLICIT_TAGGED = 1;
36 const NOTIF_IMPLICIT_TAGGED = 2;
37 const NOTIF_THREAD_COMMENT = 4;
38 const NOTIF_DIRECT_COMMENT = 8;
39 const NOTIF_COMMENT_PARTICIPATION = 16;
40 const NOTIF_ACTIVITY_PARTICIPATION = 32;
41 const NOTIF_DIRECT_THREAD_COMMENT = 64;
42 const NOTIF_SHARED = 128;
45 * Checks an item for notifications and sets the "notification-type" field
47 * - Check for mentions in posts with "uid=0" where the user hadn't interacted before
49 * @param int $iid Item ID
51 public static function setNotification(int $iid)
53 $fields = ['id', 'uid', 'body', 'parent', 'gravity', 'tag', 'contact-id', 'thr-parent', 'parent-uri', 'author-id'];
54 $item = Item::selectFirst($fields, ['id' => $iid, 'origin' => false]);
55 if (!DBA::isResult($item)) {
59 // fetch all users in the thread
60 $users = DBA::p("SELECT DISTINCT(`contact`.`uid`) FROM `item`
61 INNER JOIN `contact` ON `contact`.`id` = `item`.`contact-id` AND `contact`.`uid` != 0
62 WHERE `parent` IN (SELECT `parent` FROM `item` WHERE `id`=?)", $iid);
63 while ($user = DBA::fetch($users)) {
64 self::setNotificationForUser($item, $user['uid']);
70 * Checks an item for notifications for the given user and sets the "notification-type" field
72 * @param array $item Item array
73 * @param int $uid User ID
75 private static function setNotificationForUser(array $item, int $uid)
77 $thread = Item::selectFirstThreadForUser($uid, ['ignored'], ['iid' => $item['parent'], 'deleted' => false]);
78 if ($thread['ignored']) {
82 $notification_type = self::NOTIF_NONE;
84 if (self::checkShared($item, $uid)) {
85 $notification_type = $notification_type | self::NOTIF_SHARED;
88 $profiles = self::getProfileForUser($uid);
90 // Fetch all contacts for the given profiles
92 $ret = DBA::select('contact', ['id'], ['uid' => 0, 'nurl' => $profiles]);
93 while ($contact = DBA::fetch($ret)) {
94 $contacts[] = $contact['id'];
98 // Don't create notifications for user's posts
99 if (in_array($item['author-id'], $contacts)) {
103 if (self::checkImplicitMention($item, $profiles)) {
104 $notification_type = $notification_type | self::NOTIF_IMPLICIT_TAGGED;
107 if (self::checkExplicitMention($item, $profiles)) {
108 $notification_type = $notification_type | self::NOTIF_EXPLICIT_TAGGED;
111 if (self::checkCommentedThread($item, $contacts)) {
112 $notification_type = $notification_type | self::NOTIF_THREAD_COMMENT;
115 if (self::checkDirectComment($item, $contacts)) {
116 $notification_type = $notification_type | self::NOTIF_DIRECT_COMMENT;
119 if (self::checkDirectCommentedThread($item, $contacts)) {
120 $notification_type = $notification_type | self::NOTIF_DIRECT_THREAD_COMMENT;
123 if (self::checkCommentedParticipation($item, $contacts)) {
124 $notification_type = $notification_type | self::NOTIF_COMMENT_PARTICIPATION;
127 if (self::checkActivityParticipation($item, $contacts)) {
128 $notification_type = $notification_type | self::NOTIF_ACTIVITY_PARTICIPATION;
131 if (empty($notification_type)) {
135 Logger::info('Set notification', ['iid' => $item['id'], 'uid' => $uid, 'notification-type' => $notification_type]);
137 DBA::update('user-item', ['notification-type' => $notification_type], ['iid' => $item['id'], 'uid' => $uid], true);
141 * Fetch all profiles (contact URL) of a given user
142 * @param int $uid User ID
144 * @return array Profile links
146 private static function getProfileForUser(int $uid)
148 $notification_data = ['uid' => $uid, 'profiles' => []];
149 Hook::callAll('check_item_notification', $notification_data);
151 $profiles = $notification_data['profiles'];
153 $user = DBA::selectFirst('user', ['nickname'], ['uid' => $uid]);
154 if (!DBA::isResult($user)) {
158 $owner = DBA::selectFirst('contact', ['url', 'alias'], ['self' => true, 'uid' => $uid]);
159 if (!DBA::isResult($owner)) {
163 // This is our regular URL format
164 $profiles[] = $owner['url'];
167 $profiles[] = $owner['alias'];
169 // Notifications from Diaspora are often with an URL in the Diaspora format
170 $profiles[] = DI::baseUrl() . '/u/' . $user['nickname'];
172 // Validate and add profile links
173 foreach ($profiles AS $key => $profile) {
174 // Check for invalid profile urls (without scheme, host or path) and remove them
175 if (empty(parse_url($profile, PHP_URL_SCHEME)) || empty(parse_url($profile, PHP_URL_HOST)) || empty(parse_url($profile, PHP_URL_PATH))) {
176 unset($profiles[$key]);
180 // Add the normalized form
181 $profile = Strings::normaliseLink($profile);
182 $profiles[] = $profile;
185 $profile = str_replace('http://', 'https://', $profile);
186 $profiles[] = $profile;
189 return array_unique($profiles);
193 * Check for a "shared" notification for every new post of contacts from the given user
195 * @param int $uid User ID
196 * @return bool A contact had shared something
198 private static function checkShared(array $item, int $uid)
200 if ($item['gravity'] != GRAVITY_PARENT) {
204 // Either the contact had posted something directly
205 if (DBA::exists('contact', ['id' => $item['contact-id'], 'notify_new_posts' => true])) {
209 // Or the contact is a mentioned forum
210 $tags = DBA::select('term', ['url'], ['otype' => term::OBJECT_TYPE_POST, 'oid' => $item['id'], 'type' => Term::MENTION, 'uid' => $uid]);
211 while ($tag = DBA::fetch($tags)) {
212 $condition = ['nurl' => Strings::normaliseLink($tag['url']), 'uid' => $uid, 'notify_new_posts' => true, 'contact-type' => Contact::TYPE_COMMUNITY];
213 if (DBA::exists('contact', $condition)) {
222 * Check for an implicit mention (only tag, no body) of the given user
224 * @param array $profiles Profile links
225 * @return bool The user is mentioned
227 private static function checkImplicitMention(array $item, array $profiles)
229 foreach ($profiles AS $profile) {
230 if (strpos($item['tag'], '=' . $profile.']') || strpos($item['body'], '=' . $profile . ']')) {
231 if (strpos($item['body'], $profile) === false) {
241 * Check for an explicit mention (tag and body) of the given user
243 * @param array $profiles Profile links
244 * @return bool The user is mentioned
246 private static function checkExplicitMention(array $item, array $profiles)
248 foreach ($profiles AS $profile) {
249 if (strpos($item['tag'], '=' . $profile.']') || strpos($item['body'], '=' . $profile . ']')) {
250 if (!(strpos($item['body'], $profile) === false)) {
260 * Check if the given user had created this thread
262 * @param array $contacts Array of contact IDs
263 * @return bool The user had created this thread
265 private static function checkCommentedThread(array $item, array $contacts)
267 $condition = ['parent' => $item['parent'], 'author-id' => $contacts, 'deleted' => false, 'gravity' => GRAVITY_PARENT];
268 return Item::exists($condition);
272 * Check for a direct comment to a post of the given user
274 * @param array $contacts Array of contact IDs
275 * @return bool The item is a direct comment to a user comment
277 private static function checkDirectComment(array $item, array $contacts)
279 $condition = ['uri' => $item['thr-parent'], 'uid' => $item['uid'], 'author-id' => $contacts, 'deleted' => false, 'gravity' => GRAVITY_COMMENT];
280 return Item::exists($condition);
284 * Check for a direct comment to the starting post of the given user
286 * @param array $contacts Array of contact IDs
287 * @return bool The user had created this thread
289 private static function checkDirectCommentedThread(array $item, array $contacts)
291 $condition = ['uri' => $item['thr-parent'], 'uid' => $item['uid'], 'author-id' => $contacts, 'deleted' => false, 'gravity' => GRAVITY_PARENT];
292 return Item::exists($condition);
296 * Check if the user had commented in this thread
298 * @param array $contacts Array of contact IDs
299 * @return bool The user had commented in the thread
301 private static function checkCommentedParticipation(array $item, array $contacts)
303 $condition = ['parent' => $item['parent'], 'author-id' => $contacts, 'deleted' => false, 'gravity' => GRAVITY_COMMENT];
304 return Item::exists($condition);
308 * Check if the user had interacted in this thread (Like, Dislike, ...)
310 * @param array $contacts Array of contact IDs
311 * @return bool The user had interacted in the thread
313 private static function checkActivityParticipation(array $item, array $contacts)
315 $condition = ['parent' => $item['parent'], 'author-id' => $contacts, 'deleted' => false, 'gravity' => GRAVITY_ACTIVITY];
316 return Item::exists($condition);