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;
18 const NOTIF_EXPLICIT_TAGGED = 1;
19 const NOTIF_IMPLICIT_TAGGED = 2;
20 const NOTIF_THREAD_COMMENT = 4;
21 const NOTIF_DIRECT_COMMENT = 8;
22 const NOTIF_COMMENT_PARTICIPATION = 16;
23 const NOTIF_ACTIVITY_PARTICIPATION = 32;
24 const NOTIF_SHARED = 128;
27 * Checks an item for notifications and sets the "notification-type" field
29 * @param array $item The message array that is checked for notifications
30 * @param int $uid User ID
32 public static function setNotification($iid, $uid)
34 $fields = ['id', 'body', 'origin', 'parent', 'gravity', 'tag', 'contact-id',
35 'thr-parent', 'parent-uri', 'mention'];
36 $item = Item::selectFirst($fields, ['id' => $iid]);
38 // Don't check for own posts
39 if ($item['origin'] || empty($uid)) {
43 $fields = ['ignored', 'mention'];
44 $thread = Item::selectFirstThreadForUser($uid, $fields, ['iid' => $item['parent'], 'deleted' => false]);
45 if ($thread['ignored']) {
49 $notification_type = self::NOTIF_NONE;
51 if (self::checkShared($item, $uid)) {
52 $notification_type = $notification_type | self::NOTIF_SHARED;
55 $profiles = self::getProfileForUser($uid);
57 if (self::checkImplicitMention($item, $uid, $profiles)) {
58 $notification_type = $notification_type | self::NOTIF_IMPLICIT_TAGGED;
61 if (self::checkExplicitMention($item, $uid, $profiles)) {
62 $notification_type = $notification_type | self::NOTIF_EXPLICIT_TAGGED;
66 $ret = DBA::select('contact', ['id'], ['uid' => 0, 'nurl' => $profiles]);
67 while ($contact = DBA::fetch($ret)) {
68 $contacts[] = $contact['id'];
72 if (self::checkCommentedThread($item, $uid, $contacts)) {
73 $notification_type = $notification_type | self::NOTIF_THREAD_COMMENT;
76 if (self::checkDirectComment($item, $uid, $contacts, $thread)) {
77 $notification_type = $notification_type | self::NOTIF_DIRECT_COMMENT;
80 if (self::checkCommentedParticipation($item, $uid, $contacts)) {
81 $notification_type = $notification_type | self::NOTIF_COMMENT_PARTICIPATION;
84 if (self::checkActivityParticipation($item, $uid, $contacts)) {
85 $notification_type = $notification_type | self::NOTIF_ACTIVITY_PARTICIPATION;
88 if (empty($notification_type)) {
92 Logger::info('Set notification', ['iid' => $item['id'], 'uid' => $uid, 'notification-type' => $notification_type]);
94 DBA::update('user-item', ['notification-type' => $notification_type], ['iid' => $item['id'], 'uid' => $uid], true);
97 // Fetch all contacts for the given profiles
98 private static function getProfileForUser($uid)
100 $notification_data = ['uid' => $uid, 'profiles' => []];
101 Hook::callAll('check_item_notification', $notification_data);
103 $profiles = $notification_data['profiles'];
105 $fields = ['nickname'];
106 $user = DBA::selectFirst('user', $fields, ['uid' => $uid]);
107 if (!DBA::isResult($user)) {
111 $owner = DBA::selectFirst('contact', ['url'], ['self' => true, 'uid' => $uid]);
112 if (!DBA::isResult($owner)) {
116 // This is our regular URL format
117 $profiles[] = $owner['url'];
119 // Notifications from Diaspora are often with an URL in the Diaspora format
120 $profiles[] = DI::baseUrl().'/u/'.$user['nickname'];
124 foreach ($profiles AS $profile) {
125 // Check for invalid profile urls. 13 should be the shortest possible profile length:
127 // Additionally check for invalid urls that would return the normalised value "http:"
128 if ((strlen($profile) >= 13) && (Strings::normaliseLink($profile) != 'http:')) {
129 if (!in_array($profile, $profiles2))
130 $profiles2[] = $profile;
132 $profile = Strings::normaliseLink($profile);
133 if (!in_array($profile, $profiles2))
134 $profiles2[] = $profile;
136 $profile = str_replace('http://', 'https://', $profile);
137 if (!in_array($profile, $profiles2))
138 $profiles2[] = $profile;
145 private static function checkShared($item, $uid)
147 if ($item['gravity'] != GRAVITY_PARENT) {
151 // Send a notification for every new post?
152 // Either the contact had posted something directly
153 if (DBA::exists('contact', ['id' => $item['contact-id'], 'notify_new_posts' => true])) {
157 // Or the contact is a mentioned forum
158 $tags = DBA::select('term', ['url'], ['otype' => TERM_OBJ_POST, 'oid' => $item['id'], 'type' => TERM_MENTION, 'uid' => $uid]);
159 while ($tag = DBA::fetch($tags)) {
160 $condition = ['nurl' => Strings::normaliseLink($tag['url']), 'uid' => $uid, 'notify_new_posts' => true, 'contact-type' => Contact::TYPE_COMMUNITY];
161 if (DBA::exists('contact', $condition)) {
169 // Is the user mentioned in this post?
170 private static function checkImplicitMention($item, $uid, $profiles)
172 foreach ($profiles AS $profile) {
173 if (strpos($item['tag'], '='.$profile.']') || strpos($item['body'], '='.$profile.']'))
180 private static function checkExplicitMention($item, $uid, $profiles)
182 foreach ($profiles AS $profile) {
183 if (strpos($item['tag'], '='.$profile.']') || strpos($item['body'], '='.$profile.']'))
184 return !(strpos($item['body'], $profile) === false);
190 // Is it a post that the user had started?
191 private static function checkCommentedThread($item, $uid, $contacts)
193 // Additional check for connector posts
194 $condition = ['parent' => $item['parent'], 'author-id' => $contacts, 'deleted' => false, 'gravity' => GRAVITY_PARENT];
195 return Item::exists($condition);
198 private static function checkDirectComment($item, $uid, $contacts)
200 // Additional check for connector posts
201 $condition = ['uri' => $item['thr-parent'], 'uid' => [0, $uid], 'author-id' => $contacts, 'deleted' => false, 'gravity' => GRAVITY_COMMENT];
202 return Item::exists($condition);
205 // Check for participation of one of our contacts in the thread
206 private static function checkCommentedParticipation($item, $uid, $contacts)
208 $condition = ['parent' => $item['parent'], 'author-id' => $contacts, 'deleted' => false, 'gravity' => GRAVITY_COMMENT];
209 return Item::exists($condition);
212 private static function checkActivityParticipation($item, $uid, $contacts)
214 $condition = ['parent' => $item['parent'], 'author-id' => $contacts, 'deleted' => false, 'gravity' => GRAVITY_ACTIVITY];
215 return Item::exists($condition);