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',
35 'thr-parent', 'parent-uri', 'mention'];
36 $item = Item::selectFirst($fields, ['id' => $iid, 'origin' => false]);
37 if (!DBA::isResult($item)) {
41 // fetch all users in the thread
42 $users = DBA::p("SELECT DISTINCT(`contact`.`uid`) FROM `item`
43 INNER JOIN `contact` ON `contact`.`id` = `item`.`contact-id` AND `contact`.`uid` != 0
44 WHERE `parent` IN (SELECT `parent` FROM `item` WHERE `id`=?)", $iid);
45 while ($user = DBA::fetch($users)) {
46 self::setNotificationForUser($item, $user['uid']);
52 * Checks an item for notifications for the given user and sets the "notification-type" field
54 * @param array $item Item array
55 * @param int $uid User ID
57 private static function setNotificationForUser(array $item, int $uid)
59 $fields = ['ignored', 'mention'];
60 $thread = Item::selectFirstThreadForUser($uid, $fields, ['iid' => $item['parent'], 'deleted' => false]);
61 if ($thread['ignored']) {
65 $notification_type = self::NOTIF_NONE;
67 if (self::checkShared($item, $uid)) {
68 $notification_type = $notification_type | self::NOTIF_SHARED;
71 $profiles = self::getProfileForUser($uid);
73 if (self::checkImplicitMention($item, $uid, $profiles)) {
74 $notification_type = $notification_type | self::NOTIF_IMPLICIT_TAGGED;
77 if (self::checkExplicitMention($item, $uid, $profiles)) {
78 $notification_type = $notification_type | self::NOTIF_EXPLICIT_TAGGED;
81 // Fetch all contacts for the given profiles
83 $ret = DBA::select('contact', ['id'], ['uid' => 0, 'nurl' => $profiles]);
84 while ($contact = DBA::fetch($ret)) {
85 $contacts[] = $contact['id'];
89 if (self::checkCommentedThread($item, $uid, $contacts)) {
90 $notification_type = $notification_type | self::NOTIF_THREAD_COMMENT;
93 if (self::checkDirectComment($item, $uid, $contacts, $thread)) {
94 $notification_type = $notification_type | self::NOTIF_DIRECT_COMMENT;
97 if (self::checkCommentedParticipation($item, $contacts)) {
98 $notification_type = $notification_type | self::NOTIF_COMMENT_PARTICIPATION;
101 if (self::checkActivityParticipation($item, $contacts)) {
102 $notification_type = $notification_type | self::NOTIF_ACTIVITY_PARTICIPATION;
105 if (empty($notification_type)) {
109 Logger::info('Set notification', ['iid' => $item['id'], 'uid' => $uid, 'notification-type' => $notification_type]);
111 DBA::update('user-item', ['notification-type' => $notification_type], ['iid' => $item['id'], 'uid' => $uid], true);
115 * Fetch all profiles (contact URL) of a given user
116 * @param int $uid User ID
118 * @return array Profiles
120 private static function getProfileForUser($uid)
122 $notification_data = ['uid' => $uid, 'profiles' => []];
123 Hook::callAll('check_item_notification', $notification_data);
125 $profiles = $notification_data['profiles'];
127 $user = DBA::selectFirst('user', ['nickname'], ['uid' => $uid]);
128 if (!DBA::isResult($user)) {
132 $owner = DBA::selectFirst('contact', ['url'], ['self' => true, 'uid' => $uid]);
133 if (!DBA::isResult($owner)) {
137 // This is our regular URL format
138 $profiles[] = $owner['url'];
140 // Notifications from Diaspora are often with an URL in the Diaspora format
141 $profiles[] = DI::baseUrl().'/u/'.$user['nickname'];
145 foreach ($profiles AS $profile) {
146 // Check for invalid profile urls. 13 should be the shortest possible profile length:
148 // Additionally check for invalid urls that would return the normalised value "http:"
149 if ((strlen($profile) >= 13) && (Strings::normaliseLink($profile) != 'http:')) {
150 if (!in_array($profile, $profiles2))
151 $profiles2[] = $profile;
153 $profile = Strings::normaliseLink($profile);
154 if (!in_array($profile, $profiles2))
155 $profiles2[] = $profile;
157 $profile = str_replace('http://', 'https://', $profile);
158 if (!in_array($profile, $profiles2))
159 $profiles2[] = $profile;
167 * Check for a "shared" notification for every new post of contacts from the given user
169 * @param int $uid User ID
170 * @return bool A contact had shared something
172 private static function checkShared($item, $uid)
174 if ($item['gravity'] != GRAVITY_PARENT) {
178 // Either the contact had posted something directly
179 if (DBA::exists('contact', ['id' => $item['contact-id'], 'notify_new_posts' => true])) {
183 // Or the contact is a mentioned forum
184 $tags = DBA::select('term', ['url'], ['otype' => TERM_OBJ_POST, 'oid' => $item['id'], 'type' => TERM_MENTION, 'uid' => $uid]);
185 while ($tag = DBA::fetch($tags)) {
186 $condition = ['nurl' => Strings::normaliseLink($tag['url']), 'uid' => $uid, 'notify_new_posts' => true, 'contact-type' => Contact::TYPE_COMMUNITY];
187 if (DBA::exists('contact', $condition)) {
196 * Check for an implicit mention (only tag, no body) of the given user
198 * @param int $uid User ID
199 * @return bool The user is mentioned
201 private static function checkImplicitMention($item, $uid, $profiles)
203 foreach ($profiles AS $profile) {
204 if (strpos($item['tag'], '='.$profile.']') || strpos($item['body'], '='.$profile.']')) {
205 if (strpos($item['body'], $profile) === false) {
215 * Check for an explicit mention (tag and body) of the given user
217 * @param int $uid User ID
218 * @return bool The user is mentioned
220 private static function checkExplicitMention($item, $uid, $profiles)
222 foreach ($profiles AS $profile) {
223 if (strpos($item['tag'], '='.$profile.']') || strpos($item['body'], '='.$profile.']')) {
224 if (!(strpos($item['body'], $profile) === false)) {
234 * Check if the given user had created this thread
236 * @param int $uid User ID
237 * @return bool The user had created this thread
239 private static function checkCommentedThread($item, $uid, $contacts)
241 $condition = ['parent' => $item['parent'], 'author-id' => $contacts, 'deleted' => false, 'gravity' => GRAVITY_PARENT];
242 return Item::exists($condition);
246 * Check for a direct comment to a post of the given user
248 * @param int $uid User ID
249 * @param array $contacts Array of contacts
250 * @return bool The item is a direct comment to a user comment
252 private static function checkDirectComment($item, $uid, $contacts)
254 $condition = ['uri' => $item['thr-parent'], 'uid' => [0, $uid], 'author-id' => $contacts, 'deleted' => false, 'gravity' => GRAVITY_COMMENT];
255 return Item::exists($condition);
259 * Check if the user had commented in this thread
261 * @param array $contacts Array of contacts
262 * @return bool The user had commented in the thread
264 private static function checkCommentedParticipation($item, $contacts)
266 $condition = ['parent' => $item['parent'], 'author-id' => $contacts, 'deleted' => false, 'gravity' => GRAVITY_COMMENT];
267 return Item::exists($condition);
271 * Check if the user had interacted in this thread (Like, Dislike, ...)
273 * @param array $contacts Array of contacts
274 * @return bool The user had interacted in the thread
276 private static function checkActivityParticipation($item, $contacts)
278 $condition = ['parent' => $item['parent'], 'author-id' => $contacts, 'deleted' => false, 'gravity' => GRAVITY_ACTIVITY];
279 return Item::exists($condition);