4 * @file src/Model/UserItem.php
7 namespace Friendica\Model;
9 use Friendica\Core\Logger;
10 use Friendica\Core\Hook;
11 use Friendica\Database\DBA;
13 use Friendica\Util\Strings;
19 const NOTIF_EXPLICIT_TAGGED = 1;
20 const NOTIF_IMPLICIT_TAGGED = 2;
21 const NOTIF_THREAD_COMMENT = 4;
22 const NOTIF_DIRECT_COMMENT = 8;
23 const NOTIF_COMMENT_PARTICIPATION = 16;
24 const NOTIF_ACTIVITY_PARTICIPATION = 32;
25 const NOTIF_SHARED = 128;
28 * Checks an item for notifications and sets the "notification-type" field
30 * @param int $iid Item ID
32 public static function setNotification(int $iid)
34 $fields = ['id', 'uid', 'body', 'parent', 'gravity', 'tag', 'contact-id', 'thr-parent', 'parent-uri', 'author-id'];
35 $item = Item::selectFirst($fields, ['id' => $iid, 'origin' => false]);
36 if (!DBA::isResult($item)) {
40 // fetch all users in the thread
41 $users = DBA::p("SELECT DISTINCT(`contact`.`uid`) FROM `item`
42 INNER JOIN `contact` ON `contact`.`id` = `item`.`contact-id` AND `contact`.`uid` != 0
43 WHERE `parent` IN (SELECT `parent` FROM `item` WHERE `id`=?)", $iid);
44 while ($user = DBA::fetch($users)) {
45 self::setNotificationForUser($item, $user['uid']);
51 * Checks an item for notifications for the given user and sets the "notification-type" field
53 * @param array $item Item array
54 * @param int $uid User ID
56 private static function setNotificationForUser(array $item, int $uid)
58 $thread = Item::selectFirstThreadForUser($uid, ['ignored'], ['iid' => $item['parent'], 'deleted' => false]);
59 if ($thread['ignored']) {
63 $notification_type = self::NOTIF_NONE;
65 if (self::checkShared($item, $uid)) {
66 $notification_type = $notification_type | self::NOTIF_SHARED;
69 $profiles = self::getProfileForUser($uid);
71 // Fetch all contacts for the given profiles
73 $ret = DBA::select('contact', ['id'], ['uid' => 0, 'nurl' => $profiles]);
74 while ($contact = DBA::fetch($ret)) {
75 $contacts[] = $contact['id'];
79 // Don't create notifications for user's posts
80 if (in_array($item['author-id'], $contacts)) {
84 if (self::checkImplicitMention($item, $profiles)) {
85 $notification_type = $notification_type | self::NOTIF_IMPLICIT_TAGGED;
88 if (self::checkExplicitMention($item, $profiles)) {
89 $notification_type = $notification_type | self::NOTIF_EXPLICIT_TAGGED;
92 if (self::checkCommentedThread($item, $contacts)) {
93 $notification_type = $notification_type | self::NOTIF_THREAD_COMMENT;
96 if (self::checkDirectComment($item, $uid, $contacts)) {
97 $notification_type = $notification_type | self::NOTIF_DIRECT_COMMENT;
100 if (self::checkCommentedParticipation($item, $contacts)) {
101 $notification_type = $notification_type | self::NOTIF_COMMENT_PARTICIPATION;
104 if (self::checkActivityParticipation($item, $contacts)) {
105 $notification_type = $notification_type | self::NOTIF_ACTIVITY_PARTICIPATION;
108 if (empty($notification_type)) {
112 Logger::info('Set notification', ['iid' => $item['id'], 'uid' => $uid, 'notification-type' => $notification_type]);
114 DBA::update('user-item', ['notification-type' => $notification_type], ['iid' => $item['id'], 'uid' => $uid], true);
118 * Fetch all profiles (contact URL) of a given user
119 * @param int $uid User ID
121 * @return array Profile links
123 private static function getProfileForUser(int $uid)
125 $notification_data = ['uid' => $uid, 'profiles' => []];
126 Hook::callAll('check_item_notification', $notification_data);
128 $raw_profiles = $notification_data['profiles'];
130 $user = DBA::selectFirst('user', ['nickname'], ['uid' => $uid]);
131 if (!DBA::isResult($user)) {
135 $owner = DBA::selectFirst('contact', ['url'], ['self' => true, 'uid' => $uid]);
136 if (!DBA::isResult($owner)) {
140 // This is our regular URL format
141 $raw_profiles[] = $owner['url'];
143 // Notifications from Diaspora are often with an URL in the Diaspora format
144 $raw_profiles[] = DI::baseUrl() . '/u/' . $user['nickname'];
148 // Validate and add profile links
149 foreach ($raw_profiles AS $profile) {
150 // Check for invalid profile urls. 13 should be the shortest possible profile length:
152 // Additionally check for invalid urls that would return the normalised value "http:"
153 if ((strlen($profile) < 13) || (Strings::normaliseLink($profile) == 'http:')) {
158 $profiles[] = $profile;
160 // Add the normalized form
161 $profile = Strings::normaliseLink($profile);
162 $profiles[] = $profile;
165 $profile = str_replace('http://', 'https://', $profile);
166 $profiles[] = $profile;
169 return array_unique($profiles);
173 * Check for a "shared" notification for every new post of contacts from the given user
175 * @param int $uid User ID
176 * @return bool A contact had shared something
178 private static function checkShared(array $item, int $uid)
180 if ($item['gravity'] != GRAVITY_PARENT) {
184 // Either the contact had posted something directly
185 if (DBA::exists('contact', ['id' => $item['contact-id'], 'notify_new_posts' => true])) {
189 // Or the contact is a mentioned forum
190 $tags = DBA::select('term', ['url'], ['otype' => TERM_OBJ_POST, 'oid' => $item['id'], 'type' => TERM_MENTION, 'uid' => $uid]);
191 while ($tag = DBA::fetch($tags)) {
192 $condition = ['nurl' => Strings::normaliseLink($tag['url']), 'uid' => $uid, 'notify_new_posts' => true, 'contact-type' => Contact::TYPE_COMMUNITY];
193 if (DBA::exists('contact', $condition)) {
202 * Check for an implicit mention (only tag, no body) of the given user
204 * @param array $profiles Profile links
205 * @return bool The user is mentioned
207 private static function checkImplicitMention(array $item, array $profiles)
209 foreach ($profiles AS $profile) {
210 if (strpos($item['tag'], '=' . $profile.']') || strpos($item['body'], '=' . $profile . ']')) {
211 if (strpos($item['body'], $profile) === false) {
221 * Check for an explicit mention (tag and body) of the given user
223 * @param array $profiles Profile links
224 * @return bool The user is mentioned
226 private static function checkExplicitMention(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 if the given user had created this thread
242 * @param array $contacts Array of contact IDs
243 * @return bool The user had created this thread
245 private static function checkCommentedThread(array $item, array $contacts)
247 $condition = ['parent' => $item['parent'], 'author-id' => $contacts, 'deleted' => false, 'gravity' => GRAVITY_PARENT];
248 return Item::exists($condition);
252 * Check for a direct comment to a post of the given user
254 * @param int $uid User ID
255 * @param array $contacts Array of contact IDs
256 * @return bool The item is a direct comment to a user comment
258 private static function checkDirectComment(array $item, int $uid, array $contacts)
260 $condition = ['uri' => $item['thr-parent'], 'uid' => [0, $uid], 'author-id' => $contacts, 'deleted' => false, 'gravity' => GRAVITY_COMMENT];
261 return Item::exists($condition);
265 * Check if the user had commented in this thread
267 * @param array $contacts Array of contact IDs
268 * @return bool The user had commented in the thread
270 private static function checkCommentedParticipation(array $item, array $contacts)
272 $condition = ['parent' => $item['parent'], 'author-id' => $contacts, 'deleted' => false, 'gravity' => GRAVITY_COMMENT];
273 return Item::exists($condition);
277 * Check if the user had interacted in this thread (Like, Dislike, ...)
279 * @param array $contacts Array of contact IDs
280 * @return bool The user had interacted in the thread
282 private static function checkActivityParticipation(array $item, array $contacts)
284 $condition = ['parent' => $item['parent'], 'author-id' => $contacts, 'deleted' => false, 'gravity' => GRAVITY_ACTIVITY];
285 return Item::exists($condition);