4 * @file src/Model/UserItem.php
7 namespace Friendica\Model;
9 use Friendica\Core\Hook;
10 use Friendica\Database\DBA;
12 use Friendica\Util\Strings;
17 const NOTIF_EXPLICIT_TAGGED = 1;
18 const NOTIF_IMPLICIT_TAGGED = 2;
19 const NOTIF_THREAD_COMMENT = 4;
20 const NOTIF_DIRECT_COMMENT = 8;
21 const NOTIF_COMMENT_PARTICIPATION = 16;
22 const NOTIF_ACTIVITY_PARTICIPATION = 32;
23 const NOTIF_SHARED = 128;
26 * Checks an item for notifications and sets the "notification-type" field
28 * @param array $item The message array that is checked for notifications
29 * @param int $uid User ID
31 public static function setNotification($item, $uid)
33 // Don't check for own posts
34 if ($item['origin'] || empty($uid)) {
38 $fields = ['ignored', 'mention'];
39 $thread = Item::selectFirstThreadForUser($uid, $fields, ['iid' => $item['parent'], 'deleted' => false]);
40 if ($thread['ignored']) {
44 $notification_type = self::NOTIF_NONE;
46 if (self::checkShared($item, $uid)) {
47 $notification_type = $notification_type | self::NOTIF_SHARED;
50 $profiles = self::getProfileForUser($uid);
52 if (self::checkImplicitMention($item, $uid, $profiles)) {
53 $notification_type = $notification_type | self::NOTIF_IMPLICIT_TAGGED;
56 if (self::checkExplicitMention($item, $uid, $profiles)) {
57 $notification_type = $notification_type | self::NOTIF_EXPLICIT_TAGGED;
61 $ret = DBA::select('contact', ['id'], ['uid' => 0, 'nurl' => $profiles]);
62 while ($contact = DBA::fetch($ret)) {
63 $contacts[] = $contact['id'];
67 if (self::checkCommentedThread($item, $uid, $contacts)) {
68 $notification_type = $notification_type | self::NOTIF_THREAD_COMMENT;
71 if (self::checkDirectComment($item, $uid, $contacts, $thread)) {
72 $notification_type = $notification_type | self::NOTIF_DIRECT_COMMENT;
75 if (self::checkCommentedParticipation($item, $uid, $contacts)) {
76 $notification_type = $notification_type | self::NOTIF_COMMENT_PARTICIPATION;
79 if (self::checkActivityParticipation($item, $uid, $contacts)) {
80 $notification_type = $notification_type | self::NOTIF_ACTIVITY_PARTICIPATION;
83 DBA::update('user-item', ['notification-type' => $notification_type], ['iid' => $item['id'], 'uid' => $uid], true);
86 // Fetch all contacts for the given profiles
87 private static function getProfileForUser($uid)
89 $notification_data = ['uid' => $uid, 'profiles' => []];
90 Hook::callAll('check_item_notification', $notification_data);
92 $profiles = $notification_data['profiles'];
94 $fields = ['nickname'];
95 $user = DBA::selectFirst('user', $fields, ['uid' => $uid]);
96 if (!DBA::isResult($user)) {
100 $owner = DBA::selectFirst('contact', ['url'], ['self' => true, 'uid' => $uid]);
101 if (!DBA::isResult($owner)) {
105 // This is our regular URL format
106 $profiles[] = $owner['url'];
108 // Notifications from Diaspora are often with an URL in the Diaspora format
109 $profiles[] = DI::baseUrl().'/u/'.$user['nickname'];
113 foreach ($profiles AS $profile) {
114 // Check for invalid profile urls. 13 should be the shortest possible profile length:
116 // Additionally check for invalid urls that would return the normalised value "http:"
117 if ((strlen($profile) >= 13) && (Strings::normaliseLink($profile) != 'http:')) {
118 if (!in_array($profile, $profiles2))
119 $profiles2[] = $profile;
121 $profile = Strings::normaliseLink($profile);
122 if (!in_array($profile, $profiles2))
123 $profiles2[] = $profile;
125 $profile = str_replace('http://', 'https://', $profile);
126 if (!in_array($profile, $profiles2))
127 $profiles2[] = $profile;
134 private static function checkShared($item, $uid)
136 if ($item['gravity'] != GRAVITY_PARENT) {
140 // Send a notification for every new post?
141 // Either the contact had posted something directly
142 if (DBA::exists('contact', ['id' => $item['contact-id'], 'notify_new_posts' => true])) {
146 // Or the contact is a mentioned forum
147 $tags = DBA::select('term', ['url'], ['otype' => TERM_OBJ_POST, 'oid' => $itemid, 'type' => TERM_MENTION, 'uid' => $uid]);
148 while ($tag = DBA::fetch($tags)) {
149 $condition = ['nurl' => Strings::normaliseLink($tag['url']), 'uid' => $uid, 'notify_new_posts' => true, 'contact-type' => Contact::TYPE_COMMUNITY];
150 if (DBA::exists('contact', $condition)) {
158 // Is the user mentioned in this post?
159 private static function checkImplicitMention($item, $uid, $profiles)
161 foreach ($profiles AS $profile) {
162 if (strpos($item['tag'], '='.$profile.']') || strpos($item['body'], '='.$profile.']'))
169 private static function checkExplicitMention($item, $uid, $profiles)
171 foreach ($profiles AS $profile) {
172 if (strpos($item['tag'], '='.$profile.']') || strpos($item['body'], '='.$profile.']'))
173 return !(strpos($item['body'], $profile) === false);
179 // Is it a post that the user had started?
180 private static function checkCommentedThread($item, $uid, $contacts)
182 // Additional check for connector posts
183 $condition = ['parent' => $item['parent'], 'author-id' => $contacts, 'deleted' => false, 'gravity' => GRAVITY_PARENT];
184 return Item::exists($condition);
187 private static function checkDirectComment($item, $uid, $contacts)
189 // Additional check for connector posts
190 $condition = ['uri' => $item['thr-parent'], 'uid' => [0, $uid], 'author-id' => $contacts, 'deleted' => false, 'gravity' => GRAVITY_COMMENT];
191 return Item::exists($condition);
194 // Check for participation of one of our contacts in the thread
195 private static function checkCommentedParticipation($item, $uid, $contacts)
197 $condition = ['parent' => $item['parent'], 'author-id' => $contacts, 'deleted' => false, 'gravity' => GRAVITY_COMMENT];
198 return Item::exists($condition);
201 private static function checkActivityParticipation($item, $uid, $contacts)
203 $condition = ['parent' => $item['parent'], 'author-id' => $contacts, 'deleted' => false, 'gravity' => GRAVITY_ACTIVITY];
204 return Item::exists($condition);