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 int $iid Item ID
31 public static function setNotification(int $iid)
33 $fields = ['id', 'uid', 'body', 'parent', 'gravity', 'tag', 'contact-id',
34 'thr-parent', 'parent-uri', 'mention'];
35 $item = Item::selectFirst($fields, ['id' => $iid, 'origin' => false]);
36 if (!DBA::isResult($item)) {
40 if (!empty($item['uid'])) {
41 self::setNotificationForUser($item, $item['uid']);
44 // Alle user des Threads ermitteln
47 private static function setNotificationForUser(array $item, int $uid)
49 $fields = ['ignored', 'mention'];
50 $thread = Item::selectFirstThreadForUser($uid, $fields, ['iid' => $item['parent'], 'deleted' => false]);
51 if ($thread['ignored']) {
55 $notification_type = self::NOTIF_NONE;
57 if (self::checkShared($item, $uid)) {
58 $notification_type = $notification_type | self::NOTIF_SHARED;
61 $profiles = self::getProfileForUser($uid);
63 if (self::checkImplicitMention($item, $uid, $profiles)) {
64 $notification_type = $notification_type | self::NOTIF_IMPLICIT_TAGGED;
67 if (self::checkExplicitMention($item, $uid, $profiles)) {
68 $notification_type = $notification_type | self::NOTIF_EXPLICIT_TAGGED;
71 // Fetch all contacts for the given profiles
73 $ret = DBA::select('contact', ['id'], ['uid' => 0, 'nurl' => $profiles]);
74 while ($contact = DBA::fetch($ret)) {
75 $contacts[] = $contact['id'];
79 if (self::checkCommentedThread($item, $uid, $contacts)) {
80 $notification_type = $notification_type | self::NOTIF_THREAD_COMMENT;
83 if (self::checkDirectComment($item, $uid, $contacts, $thread)) {
84 $notification_type = $notification_type | self::NOTIF_DIRECT_COMMENT;
87 if (self::checkCommentedParticipation($item, $contacts)) {
88 $notification_type = $notification_type | self::NOTIF_COMMENT_PARTICIPATION;
91 if (self::checkActivityParticipation($item, $contacts)) {
92 $notification_type = $notification_type | self::NOTIF_ACTIVITY_PARTICIPATION;
95 if (empty($notification_type)) {
99 Logger::info('Set notification', ['iid' => $item['id'], 'uid' => $uid, 'notification-type' => $notification_type]);
101 DBA::update('user-item', ['notification-type' => $notification_type], ['iid' => $item['id'], 'uid' => $uid], true);
105 * Fetch all profiles of a given user
106 * @param int $uid User ID
108 * @return array Profiles
110 private static function getProfileForUser($uid)
112 $notification_data = ['uid' => $uid, 'profiles' => []];
113 Hook::callAll('check_item_notification', $notification_data);
115 $profiles = $notification_data['profiles'];
117 $fields = ['nickname'];
118 $user = DBA::selectFirst('user', $fields, ['uid' => $uid]);
119 if (!DBA::isResult($user)) {
123 $owner = DBA::selectFirst('contact', ['url'], ['self' => true, 'uid' => $uid]);
124 if (!DBA::isResult($owner)) {
128 // This is our regular URL format
129 $profiles[] = $owner['url'];
131 // Notifications from Diaspora are often with an URL in the Diaspora format
132 $profiles[] = DI::baseUrl().'/u/'.$user['nickname'];
136 foreach ($profiles AS $profile) {
137 // Check for invalid profile urls. 13 should be the shortest possible profile length:
139 // Additionally check for invalid urls that would return the normalised value "http:"
140 if ((strlen($profile) >= 13) && (Strings::normaliseLink($profile) != 'http:')) {
141 if (!in_array($profile, $profiles2))
142 $profiles2[] = $profile;
144 $profile = Strings::normaliseLink($profile);
145 if (!in_array($profile, $profiles2))
146 $profiles2[] = $profile;
148 $profile = str_replace('http://', 'https://', $profile);
149 if (!in_array($profile, $profiles2))
150 $profiles2[] = $profile;
158 * Check for a "shared" notification for the given item and user
160 * @param int $uid User ID
162 private static function checkShared($item, $uid)
164 if ($item['gravity'] != GRAVITY_PARENT) {
168 // Send a notification for every new post?
169 // Either the contact had posted something directly
170 if (DBA::exists('contact', ['id' => $item['contact-id'], 'notify_new_posts' => true])) {
174 // Or the contact is a mentioned forum
175 $tags = DBA::select('term', ['url'], ['otype' => TERM_OBJ_POST, 'oid' => $item['id'], 'type' => TERM_MENTION, 'uid' => $uid]);
176 while ($tag = DBA::fetch($tags)) {
177 $condition = ['nurl' => Strings::normaliseLink($tag['url']), 'uid' => $uid, 'notify_new_posts' => true, 'contact-type' => Contact::TYPE_COMMUNITY];
178 if (DBA::exists('contact', $condition)) {
186 // Is the user mentioned in this post?
188 * Check for a "shared" notification for the given item and user
190 * @param int $uid User ID
192 private static function checkImplicitMention($item, $uid, $profiles)
194 foreach ($profiles AS $profile) {
195 if (strpos($item['tag'], '='.$profile.']') || strpos($item['body'], '='.$profile.']')) {
196 if (strpos($item['body'], $profile) === false) {
206 * Check for a "shared" notification for the given item and user
208 * @param int $uid User ID
210 private static function checkExplicitMention($item, $uid, $profiles)
212 foreach ($profiles AS $profile) {
213 if (strpos($item['tag'], '='.$profile.']') || strpos($item['body'], '='.$profile.']')) {
214 if (!(strpos($item['body'], $profile) === false)) {
223 // Is it a post that the user had started?
225 * Check for a "shared" notification for the given item and user
227 * @param int $uid User ID
229 private static function checkCommentedThread($item, $uid, $contacts)
231 // Additional check for connector posts
232 $condition = ['parent' => $item['parent'], 'author-id' => $contacts, 'deleted' => false, 'gravity' => GRAVITY_PARENT];
233 return Item::exists($condition);
237 * Check for a direct comment to a post of the given user
239 * @param int $uid User ID
240 * @param array $contacts Array of contacts
242 private static function checkDirectComment($item, $uid, $contacts)
244 // Additional check for connector posts
245 $condition = ['uri' => $item['thr-parent'], 'uid' => [0, $uid], 'author-id' => $contacts, 'deleted' => false, 'gravity' => GRAVITY_COMMENT];
246 return Item::exists($condition);
250 * Check if the user had commented in this thread
252 * @param array $contacts Array of contacts
254 private static function checkCommentedParticipation($item, $contacts)
256 $condition = ['parent' => $item['parent'], 'author-id' => $contacts, 'deleted' => false, 'gravity' => GRAVITY_COMMENT];
257 return Item::exists($condition);
261 * Check if the user had interacted in this thread (Like, Dislike, ...)
263 * @param array $contacts Array of contacts
265 private static function checkActivityParticipation($item, $contacts)
267 $condition = ['parent' => $item['parent'], 'author-id' => $contacts, 'deleted' => false, 'gravity' => GRAVITY_ACTIVITY];
268 return Item::exists($condition);