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_DIRECT_THREAD_COMMENT = 64;
26 const NOTIF_SHARED = 128;
29 * Checks an item for notifications and sets the "notification-type" field
31 * - Check for mentions in posts with "uid=0" where the user hadn't interacted before
33 * @param int $iid Item ID
35 public static function setNotification(int $iid)
37 $fields = ['id', 'uid', 'body', 'parent', 'gravity', 'tag', 'contact-id', 'thr-parent', 'parent-uri', 'author-id'];
38 $item = Item::selectFirst($fields, ['id' => $iid, 'origin' => false]);
39 if (!DBA::isResult($item)) {
43 // fetch all users in the thread
44 $users = DBA::p("SELECT DISTINCT(`contact`.`uid`) FROM `item`
45 INNER JOIN `contact` ON `contact`.`id` = `item`.`contact-id` AND `contact`.`uid` != 0
46 WHERE `parent` IN (SELECT `parent` FROM `item` WHERE `id`=?)", $iid);
47 while ($user = DBA::fetch($users)) {
48 self::setNotificationForUser($item, $user['uid']);
54 * Checks an item for notifications for the given user and sets the "notification-type" field
56 * @param array $item Item array
57 * @param int $uid User ID
59 private static function setNotificationForUser(array $item, int $uid)
61 $thread = Item::selectFirstThreadForUser($uid, ['ignored'], ['iid' => $item['parent'], 'deleted' => false]);
62 if ($thread['ignored']) {
66 $notification_type = self::NOTIF_NONE;
68 if (self::checkShared($item, $uid)) {
69 $notification_type = $notification_type | self::NOTIF_SHARED;
72 $profiles = self::getProfileForUser($uid);
74 // Fetch all contacts for the given profiles
76 $ret = DBA::select('contact', ['id'], ['uid' => 0, 'nurl' => $profiles]);
77 while ($contact = DBA::fetch($ret)) {
78 $contacts[] = $contact['id'];
82 // Don't create notifications for user's posts
83 if (in_array($item['author-id'], $contacts)) {
87 if (self::checkImplicitMention($item, $profiles)) {
88 $notification_type = $notification_type | self::NOTIF_IMPLICIT_TAGGED;
91 if (self::checkExplicitMention($item, $profiles)) {
92 $notification_type = $notification_type | self::NOTIF_EXPLICIT_TAGGED;
95 if (self::checkCommentedThread($item, $contacts)) {
96 $notification_type = $notification_type | self::NOTIF_THREAD_COMMENT;
99 if (self::checkDirectComment($item, $contacts)) {
100 $notification_type = $notification_type | self::NOTIF_DIRECT_COMMENT;
103 if (self::checkDirectCommentedThread($item, $contacts)) {
104 $notification_type = $notification_type | self::NOTIF_DIRECT_THREAD_COMMENT;
107 if (self::checkCommentedParticipation($item, $contacts)) {
108 $notification_type = $notification_type | self::NOTIF_COMMENT_PARTICIPATION;
111 if (self::checkActivityParticipation($item, $contacts)) {
112 $notification_type = $notification_type | self::NOTIF_ACTIVITY_PARTICIPATION;
115 if (empty($notification_type)) {
119 Logger::info('Set notification', ['iid' => $item['id'], 'uid' => $uid, 'notification-type' => $notification_type]);
121 DBA::update('user-item', ['notification-type' => $notification_type], ['iid' => $item['id'], 'uid' => $uid], true);
125 * Fetch all profiles (contact URL) of a given user
126 * @param int $uid User ID
128 * @return array Profile links
130 private static function getProfileForUser(int $uid)
132 $notification_data = ['uid' => $uid, 'profiles' => []];
133 Hook::callAll('check_item_notification', $notification_data);
135 $profiles = $notification_data['profiles'];
137 $user = DBA::selectFirst('user', ['nickname'], ['uid' => $uid]);
138 if (!DBA::isResult($user)) {
142 $owner = DBA::selectFirst('contact', ['url', 'alias'], ['self' => true, 'uid' => $uid]);
143 if (!DBA::isResult($owner)) {
147 // This is our regular URL format
148 $profiles[] = $owner['url'];
151 $profiles[] = $owner['alias'];
153 // Notifications from Diaspora are often with an URL in the Diaspora format
154 $profiles[] = DI::baseUrl() . '/u/' . $user['nickname'];
156 // Validate and add profile links
157 foreach ($profiles AS $key => $profile) {
158 // Check for invalid profile urls (without scheme, host or path) and remove them
159 if (empty(parse_url($profile, PHP_URL_SCHEME)) || empty(parse_url($profile, PHP_URL_HOST)) || empty(parse_url($profile, PHP_URL_PATH))) {
160 unset($profiles[$key]);
164 // Add the normalized form
165 $profile = Strings::normaliseLink($profile);
166 $profiles[] = $profile;
169 $profile = str_replace('http://', 'https://', $profile);
170 $profiles[] = $profile;
173 return array_unique($profiles);
177 * Check for a "shared" notification for every new post of contacts from the given user
179 * @param int $uid User ID
180 * @return bool A contact had shared something
182 private static function checkShared(array $item, int $uid)
184 if ($item['gravity'] != GRAVITY_PARENT) {
188 // Either the contact had posted something directly
189 if (DBA::exists('contact', ['id' => $item['contact-id'], 'notify_new_posts' => true])) {
193 // Or the contact is a mentioned forum
194 $tags = DBA::select('term', ['url'], ['otype' => TERM_OBJ_POST, 'oid' => $item['id'], 'type' => TERM_MENTION, 'uid' => $uid]);
195 while ($tag = DBA::fetch($tags)) {
196 $condition = ['nurl' => Strings::normaliseLink($tag['url']), 'uid' => $uid, 'notify_new_posts' => true, 'contact-type' => Contact::TYPE_COMMUNITY];
197 if (DBA::exists('contact', $condition)) {
206 * Check for an implicit mention (only tag, no body) of the given user
208 * @param array $profiles Profile links
209 * @return bool The user is mentioned
211 private static function checkImplicitMention(array $item, array $profiles)
213 foreach ($profiles AS $profile) {
214 if (strpos($item['tag'], '=' . $profile.']') || strpos($item['body'], '=' . $profile . ']')) {
215 if (strpos($item['body'], $profile) === false) {
225 * Check for an explicit mention (tag and body) of the given user
227 * @param array $profiles Profile links
228 * @return bool The user is mentioned
230 private static function checkExplicitMention(array $item, array $profiles)
232 foreach ($profiles AS $profile) {
233 if (strpos($item['tag'], '=' . $profile.']') || strpos($item['body'], '=' . $profile . ']')) {
234 if (!(strpos($item['body'], $profile) === false)) {
244 * Check if the given user had created this thread
246 * @param array $contacts Array of contact IDs
247 * @return bool The user had created this thread
249 private static function checkCommentedThread(array $item, array $contacts)
251 $condition = ['parent' => $item['parent'], 'author-id' => $contacts, 'deleted' => false, 'gravity' => GRAVITY_PARENT];
252 return Item::exists($condition);
256 * Check for a direct comment to a post of the given user
258 * @param array $contacts Array of contact IDs
259 * @return bool The item is a direct comment to a user comment
261 private static function checkDirectComment(array $item, array $contacts)
263 $condition = ['uri' => $item['thr-parent'], 'uid' => $item['uid'], 'author-id' => $contacts, 'deleted' => false, 'gravity' => GRAVITY_COMMENT];
264 return Item::exists($condition);
268 * Check for a direct comment to the starting post of the given user
270 * @param array $contacts Array of contact IDs
271 * @return bool The user had created this thread
273 private static function checkDirectCommentedThread(array $item, array $contacts)
275 $condition = ['uri' => $item['thr-parent'], 'uid' => $item['uid'], 'author-id' => $contacts, 'deleted' => false, 'gravity' => GRAVITY_PARENT];
276 return Item::exists($condition);
280 * Check if the user had commented in this thread
282 * @param array $contacts Array of contact IDs
283 * @return bool The user had commented in the thread
285 private static function checkCommentedParticipation(array $item, array $contacts)
287 $condition = ['parent' => $item['parent'], 'author-id' => $contacts, 'deleted' => false, 'gravity' => GRAVITY_COMMENT];
288 return Item::exists($condition);
292 * Check if the user had interacted in this thread (Like, Dislike, ...)
294 * @param array $contacts Array of contact IDs
295 * @return bool The user had interacted in the thread
297 private static function checkActivityParticipation(array $item, array $contacts)
299 $condition = ['parent' => $item['parent'], 'author-id' => $contacts, 'deleted' => false, 'gravity' => GRAVITY_ACTIVITY];
300 return Item::exists($condition);