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 * Checks an item for notifications and sets the "notification-type" field
19 * @param array $item The message array that is checked for notifications
20 * @param int $uid User ID
22 public static function setNotification($item, $uid)
24 if (self::checkShared($item, $uid)) {
28 $profiles = self::getProfileForUser($uid);
30 if (self::checkTagged($item, $uid, $profiles)) {
34 if (self::checkCommented($item, $uid, $profiles)) {
39 private static function getProfileForUser($uid)
41 $notification_data = ["uid" => $uid, "profiles" => []];
42 Hook::callAll('check_item_notification', $notification_data);
44 $profiles = $notification_data["profiles"];
46 $fields = ['nickname'];
47 $user = DBA::selectFirst('user', $fields, ['uid' => $uid]);
48 if (!DBA::isResult($user)) {
52 $owner = DBA::selectFirst('contact', ['url'], ['self' => true, 'uid' => $uid]);
53 if (!DBA::isResult($owner)) {
57 // This is our regular URL format
58 $profiles[] = $owner["url"];
60 // Notifications from Diaspora are often with an URL in the Diaspora format
61 $profiles[] = DI::baseUrl()."/u/".$user["nickname"];
65 foreach ($profiles AS $profile) {
66 // Check for invalid profile urls. 13 should be the shortest possible profile length:
68 // Additionally check for invalid urls that would return the normalised value "http:"
69 if ((strlen($profile) >= 13) && (Strings::normaliseLink($profile) != "http:")) {
70 if (!in_array($profile, $profiles2))
71 $profiles2[] = $profile;
73 $profile = Strings::normaliseLink($profile);
74 if (!in_array($profile, $profiles2))
75 $profiles2[] = $profile;
77 $profile = str_replace("http://", "https://", $profile);
78 if (!in_array($profile, $profiles2))
79 $profiles2[] = $profile;
86 private static function checkShared($item, $uid)
88 if ($item['gravity'] != GRAVITY_PARENT) {
92 // Send a notification for every new post?
93 // Either the contact had posted something directly
94 if (DBA::exists('contact', ['id' => $item['contact-id'], 'notify_new_posts' => true])) {
98 // Or the contact is a mentioned forum
99 $tags = DBA::select('term', ['url'], ['otype' => TERM_OBJ_POST, 'oid' => $itemid, 'type' => TERM_MENTION, 'uid' => $uid]);
100 while ($tag = DBA::fetch($tags)) {
101 $condition = ['nurl' => Strings::normaliseLink($tag["url"]), 'uid' => $uid, 'notify_new_posts' => true, 'contact-type' => Contact::TYPE_COMMUNITY];
102 if (DBA::exists('contact', $condition)) {
110 // Is the user mentioned in this post?
111 private static function checkTagged($item, $uid, $profiles)
113 foreach ($profiles AS $profile) {
114 if (strpos($item["tag"], "=".$profile."]") || strpos($item["body"], "=".$profile."]"))
121 // Fetch all contacts for the given profiles
122 private static function checkCommented($item, $uid, $profiles)
124 // Is it a post that the user had started?
125 $fields = ['ignored', 'mention'];
126 $thread = Item::selectFirstThreadForUser($uid, $fields, ['iid' => $item["parent"], 'deleted' => false]);
127 if ($thread['mention'] && !$thread['ignored']) {
132 $ret = DBA::select('contact', ['id'], ['uid' => 0, 'nurl' => $profiles]);
133 while ($contact = DBA::fetch($ret)) {
134 $contacts[] = $contact['id'];
138 // And now we check for participation of one of our contacts in the thread
139 $condition = ['parent' => $item["parent"], 'author-id' => $contacts, 'deleted' => false];
141 if (!$thread['ignored'] && Item::exists($condition)) {