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 * - Check for mentions in posts with "uid=0" where the user hadn't interacted before
32 * @param int $iid Item ID
34 public static function setNotification(int $iid)
36 $fields = ['id', 'uid', 'body', 'parent', 'gravity', 'tag', 'contact-id', 'thr-parent', 'parent-uri', 'author-id'];
37 $item = Item::selectFirst($fields, ['id' => $iid, 'origin' => false]);
38 if (!DBA::isResult($item)) {
42 // fetch all users in the thread
43 $users = DBA::p("SELECT DISTINCT(`contact`.`uid`) FROM `item`
44 INNER JOIN `contact` ON `contact`.`id` = `item`.`contact-id` AND `contact`.`uid` != 0
45 WHERE `parent` IN (SELECT `parent` FROM `item` WHERE `id`=?)", $iid);
46 while ($user = DBA::fetch($users)) {
47 self::setNotificationForUser($item, $user['uid']);
53 * Checks an item for notifications for the given user and sets the "notification-type" field
55 * @param array $item Item array
56 * @param int $uid User ID
58 private static function setNotificationForUser(array $item, int $uid)
60 $thread = Item::selectFirstThreadForUser($uid, ['ignored'], ['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 // Fetch all contacts for the given profiles
75 $ret = DBA::select('contact', ['id'], ['uid' => 0, 'nurl' => $profiles]);
76 while ($contact = DBA::fetch($ret)) {
77 $contacts[] = $contact['id'];
81 // Don't create notifications for user's posts
82 if (in_array($item['author-id'], $contacts)) {
86 if (self::checkImplicitMention($item, $profiles)) {
87 $notification_type = $notification_type | self::NOTIF_IMPLICIT_TAGGED;
90 if (self::checkExplicitMention($item, $profiles)) {
91 $notification_type = $notification_type | self::NOTIF_EXPLICIT_TAGGED;
94 if (self::checkCommentedThread($item, $contacts)) {
95 $notification_type = $notification_type | self::NOTIF_THREAD_COMMENT;
98 if (self::checkDirectComment($item, $contacts)) {
99 $notification_type = $notification_type | self::NOTIF_DIRECT_COMMENT;
102 if (self::checkCommentedParticipation($item, $contacts)) {
103 $notification_type = $notification_type | self::NOTIF_COMMENT_PARTICIPATION;
106 if (self::checkActivityParticipation($item, $contacts)) {
107 $notification_type = $notification_type | self::NOTIF_ACTIVITY_PARTICIPATION;
110 if (empty($notification_type)) {
114 Logger::info('Set notification', ['iid' => $item['id'], 'uid' => $uid, 'notification-type' => $notification_type]);
116 DBA::update('user-item', ['notification-type' => $notification_type], ['iid' => $item['id'], 'uid' => $uid], true);
120 * Fetch all profiles (contact URL) of a given user
121 * @param int $uid User ID
123 * @return array Profile links
125 private static function getProfileForUser(int $uid)
127 $notification_data = ['uid' => $uid, 'profiles' => []];
128 Hook::callAll('check_item_notification', $notification_data);
130 $profiles = $notification_data['profiles'];
132 $user = DBA::selectFirst('user', ['nickname'], ['uid' => $uid]);
133 if (!DBA::isResult($user)) {
137 $owner = DBA::selectFirst('contact', ['url', 'alias'], ['self' => true, 'uid' => $uid]);
138 if (!DBA::isResult($owner)) {
142 // This is our regular URL format
143 $profiles[] = $owner['url'];
146 $profiles[] = $owner['alias'];
148 // Notifications from Diaspora are often with an URL in the Diaspora format
149 $profiles[] = DI::baseUrl() . '/u/' . $user['nickname'];
151 // Validate and add profile links
152 foreach ($profiles AS $key => $profile) {
153 // Check for invalid profile urls (without scheme, host or path) and remove them
154 if (empty(parse_url($profile, PHP_URL_SCHEME)) || empty(parse_url($profile, PHP_URL_HOST)) || empty(parse_url($profile, PHP_URL_PATH))) {
155 unset($profiles[$key]);
159 // Add the normalized form
160 $profile = Strings::normaliseLink($profile);
161 $profiles[] = $profile;
164 $profile = str_replace('http://', 'https://', $profile);
165 $profiles[] = $profile;
168 return array_unique($profiles);
172 * Check for a "shared" notification for every new post of contacts from the given user
174 * @param int $uid User ID
175 * @return bool A contact had shared something
177 private static function checkShared(array $item, int $uid)
179 if ($item['gravity'] != GRAVITY_PARENT) {
183 // Either the contact had posted something directly
184 if (DBA::exists('contact', ['id' => $item['contact-id'], 'notify_new_posts' => true])) {
188 // Or the contact is a mentioned forum
189 $tags = DBA::select('term', ['url'], ['otype' => TERM_OBJ_POST, 'oid' => $item['id'], 'type' => TERM_MENTION, 'uid' => $uid]);
190 while ($tag = DBA::fetch($tags)) {
191 $condition = ['nurl' => Strings::normaliseLink($tag['url']), 'uid' => $uid, 'notify_new_posts' => true, 'contact-type' => Contact::TYPE_COMMUNITY];
192 if (DBA::exists('contact', $condition)) {
201 * Check for an implicit mention (only tag, no body) of the given user
203 * @param array $profiles Profile links
204 * @return bool The user is mentioned
206 private static function checkImplicitMention(array $item, array $profiles)
208 foreach ($profiles AS $profile) {
209 if (strpos($item['tag'], '=' . $profile.']') || strpos($item['body'], '=' . $profile . ']')) {
210 if (strpos($item['body'], $profile) === false) {
220 * Check for an explicit mention (tag and body) of the given user
222 * @param array $profiles Profile links
223 * @return bool The user is mentioned
225 private static function checkExplicitMention(array $item, array $profiles)
227 foreach ($profiles AS $profile) {
228 if (strpos($item['tag'], '=' . $profile.']') || strpos($item['body'], '=' . $profile . ']')) {
229 if (!(strpos($item['body'], $profile) === false)) {
239 * Check if the given user had created this thread
241 * @param array $contacts Array of contact IDs
242 * @return bool The user had created this thread
244 private static function checkCommentedThread(array $item, array $contacts)
246 $condition = ['parent' => $item['parent'], 'author-id' => $contacts, 'deleted' => false, 'gravity' => GRAVITY_PARENT];
247 return Item::exists($condition);
251 * Check for a direct comment to a post of the given user
253 * @param array $contacts Array of contact IDs
254 * @return bool The item is a direct comment to a user comment
256 private static function checkDirectComment(array $item, array $contacts)
258 $condition = ['uri' => $item['thr-parent'], 'uid' => $item['uid'], 'author-id' => $contacts, 'deleted' => false, 'gravity' => GRAVITY_COMMENT];
259 return Item::exists($condition);
263 * Check if the user had commented in this thread
265 * @param array $contacts Array of contact IDs
266 * @return bool The user had commented in the thread
268 private static function checkCommentedParticipation(array $item, array $contacts)
270 $condition = ['parent' => $item['parent'], 'author-id' => $contacts, 'deleted' => false, 'gravity' => GRAVITY_COMMENT];
271 return Item::exists($condition);
275 * Check if the user had interacted in this thread (Like, Dislike, ...)
277 * @param array $contacts Array of contact IDs
278 * @return bool The user had interacted in the thread
280 private static function checkActivityParticipation(array $item, array $contacts)
282 $condition = ['parent' => $item['parent'], 'author-id' => $contacts, 'deleted' => false, 'gravity' => GRAVITY_ACTIVITY];
283 return Item::exists($condition);