]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/UserItem.php
New "post-user" structure, new update functionality
[friendica.git] / src / Model / UserItem.php
index 1680dd2727219e52b0141561f26e10a9b11ff973..b4346c2215676a53180d9586b93d2ac05e30d03f 100644 (file)
@@ -51,9 +51,9 @@ class UserItem
         */
        public static function setNotification(int $iid)
        {
-               $fields = ['id', 'uri-id', 'uid', 'body', 'parent', 'gravity', 'tag',
-                       'contact-id', 'thr-parent', 'parent-uri', 'author-id', 'verb'];
-               $item = Item::selectFirst($fields, ['id' => $iid, 'origin' => false]);
+               $fields = ['id', 'uri-id', 'parent-uri-id', 'uid', 'body', 'parent', 'gravity',
+                       'private', 'contact-id', 'thr-parent', 'parent-uri', 'author-id', 'verb'];
+               $item = Post::selectFirst($fields, ['id' => $iid, 'origin' => false]);
                if (!DBA::isResult($item)) {
                        return;
                }
@@ -63,14 +63,25 @@ class UserItem
                        return;
                }
 
-               // fetch all users in the thread
-               $users = DBA::p("SELECT DISTINCT(`contact`.`uid`) FROM `item`
-                       INNER JOIN `contact` ON `contact`.`id` = `item`.`contact-id` AND `contact`.`uid` != 0
-                       WHERE `parent` IN (SELECT `parent` FROM `item` WHERE `id`=?)", $iid);
+               if ($item['uid'] == 0) {
+                       $uids = [];
+               } else {
+                       // Always include the item user
+                       $uids = [$item['uid']];
+               }
+
+               // Add every user who participated so far in this thread
+               // This can only happen with participations on global items. (means: uid = 0) 
+               $users = DBA::p("SELECT DISTINCT(`contact-uid`) AS `uid` FROM `post-view`
+                       WHERE `contact-uid` != 0 AND `parent` IN (SELECT `parent` FROM `post-view` WHERE `id` = ?)", $iid);
                while ($user = DBA::fetch($users)) {
-                       self::setNotificationForUser($item, $user['uid']);
+                       $uids[] = $user['uid'];
                }
                DBA::close($users);
+
+               foreach (array_unique($uids) as $uid) {
+                       self::setNotificationForUser($item, $uid);
+               }
        }
 
        /**
@@ -81,7 +92,7 @@ class UserItem
         */
        private static function setNotificationForUser(array $item, int $uid)
        {
-               $thread = Item::selectFirstThreadForUser($uid, ['ignored'], ['iid' => $item['parent'], 'deleted' => false]);
+               $thread = Post::selectFirstThreadForUser($uid, ['ignored'], ['iid' => $item['parent'], 'deleted' => false]);
                if (!empty($thread['ignored'])) {
                        return;
                }
@@ -144,7 +155,9 @@ class UserItem
 
                Logger::info('Set notification', ['iid' => $item['id'], 'uid' => $uid, 'notification-type' => $notification_type]);
 
-               DBA::update('user-item', ['notification-type' => $notification_type], ['iid' => $item['id'], 'uid' => $uid], true);
+               $fields = ['notification-type' => $notification_type];
+               Post\User::update($item['uri-id'], $uid, $fields);
+               DBA::update('user-item', $fields, ['iid' => $item['id'], 'uid' => $uid], true);
        }
 
        /**
@@ -207,20 +220,22 @@ class UserItem
         */
        private static function checkShared(array $item, int $uid)
        {
+               // Only check on original posts and reshare ("announce") activities, otherwise return
                if (($item['gravity'] != GRAVITY_PARENT) && ($item['verb'] != Activity::ANNOUNCE)) {
                        return false;
                }
 
-               // Either the contact had posted something directly
+               // Check if the contact posted or shared something directly
                if (DBA::exists('contact', ['id' => $item['contact-id'], 'notify_new_posts' => true])) {
                        return true;
                }
 
-               if ($item['gravity'] != GRAVITY_PARENT) {
+               // The following check doesn't make sense on activities, so quit here
+               if ($item['verb'] == Activity::ANNOUNCE) {
                        return false;
                }
 
-               // Or the contact is a mentioned forum
+               // Check if the contact is a mentioned forum
                $tags = DBA::select('tag-view', ['url'], ['uri-id' => $item['uri-id'], 'type' => [Tag::MENTION, Tag::EXCLUSIVE_MENTION]]);
                while ($tag = DBA::fetch($tags)) {
                        $condition = ['nurl' => Strings::normaliseLink($tag['url']), 'uid' => $uid, 'notify_new_posts' => true, 'contact-type' => Contact::TYPE_COMMUNITY];
@@ -282,7 +297,7 @@ class UserItem
        private static function checkCommentedThread(array $item, array $contacts)
        {
                $condition = ['parent' => $item['parent'], 'author-id' => $contacts, 'deleted' => false, 'gravity' => GRAVITY_PARENT];
-               return Item::exists($condition);
+               return Post::exists($condition);
        }
 
        /**
@@ -294,7 +309,7 @@ class UserItem
        private static function checkDirectComment(array $item, array $contacts)
        {
                $condition = ['uri' => $item['thr-parent'], 'uid' => $item['uid'], 'author-id' => $contacts, 'deleted' => false, 'gravity' => GRAVITY_COMMENT];
-               return Item::exists($condition);
+               return Post::exists($condition);
        }
 
        /**
@@ -306,7 +321,7 @@ class UserItem
        private static function checkDirectCommentedThread(array $item, array $contacts)
        {
                $condition = ['uri' => $item['thr-parent'], 'uid' => $item['uid'], 'author-id' => $contacts, 'deleted' => false, 'gravity' => GRAVITY_PARENT];
-               return Item::exists($condition);
+               return Post::exists($condition);
        }
 
        /**
@@ -318,7 +333,7 @@ class UserItem
        private static function checkCommentedParticipation(array $item, array $contacts)
        {
                $condition = ['parent' => $item['parent'], 'author-id' => $contacts, 'deleted' => false, 'gravity' => GRAVITY_COMMENT];
-               return Item::exists($condition);
+               return Post::exists($condition);
        }
 
        /**
@@ -330,6 +345,6 @@ class UserItem
        private static function checkActivityParticipation(array $item, array $contacts)
        {
                $condition = ['parent' => $item['parent'], 'author-id' => $contacts, 'deleted' => false, 'gravity' => GRAVITY_ACTIVITY];
-               return Item::exists($condition);
+               return Post::exists($condition);
        }
 }