]> git.mxchange.org Git - friendica.git/commitdiff
Issue 13041: API activities for reshared posts are now performed on the original...
authorMichael <heluecht@pirati.ca>
Mon, 1 May 2023 04:01:11 +0000 (04:01 +0000)
committerMichael <heluecht@pirati.ca>
Mon, 1 May 2023 04:01:11 +0000 (04:01 +0000)
17 files changed:
src/Factory/Api/Mastodon/StatusSource.php
src/Model/Post.php
src/Module/Api/Mastodon/Statuses.php
src/Module/Api/Mastodon/Statuses/Bookmark.php
src/Module/Api/Mastodon/Statuses/Card.php
src/Module/Api/Mastodon/Statuses/Context.php
src/Module/Api/Mastodon/Statuses/Favourite.php
src/Module/Api/Mastodon/Statuses/FavouritedBy.php
src/Module/Api/Mastodon/Statuses/Mute.php
src/Module/Api/Mastodon/Statuses/Pin.php
src/Module/Api/Mastodon/Statuses/Reblog.php
src/Module/Api/Mastodon/Statuses/RebloggedBy.php
src/Module/Api/Mastodon/Statuses/Unbookmark.php
src/Module/Api/Mastodon/Statuses/Unfavourite.php
src/Module/Api/Mastodon/Statuses/Unmute.php
src/Module/Api/Mastodon/Statuses/Unpin.php
src/Module/Api/Mastodon/Statuses/Unreblog.php

index 13d6145ca6968932ace95b7a59cb48016b36f722..170b66e1b017859e7060f56bb01403ec0a635046 100644 (file)
@@ -38,7 +38,7 @@ class StatusSource extends BaseFactory
         */
        public function createFromUriId(int $uriId, int $uid): \Friendica\Object\Api\Mastodon\StatusSource
        {
-               $post = Post::selectFirst(['uri-id', 'raw-body', 'body', 'title'], ['uri-id' => $uriId, 'uid' => [0, $uid]]);
+               $post = Post::selectOriginal(['uri-id', 'raw-body', 'body', 'title'], ['uri-id' => $uriId, 'uid' => [0, $uid]]);
 
                $spoiler_text = $post['title'] ?: BBCode::toPlaintext(BBCode::getAbstract($post['body'], Protocol::ACTIVITYPUB));
                $body         = BBCode::toMarkdown(Post\Media::removeFromEndOfBody($post['body']));
index 062f22db9c966937da6652726fe37e97fb39b032..770ab083178f41b3ee8d3a14021bbb433581ca33 100644 (file)
@@ -226,6 +226,46 @@ class Post
                }
        }
 
+       /**
+        * Retrieve a single record from the post-user-view view and returns it in an associative array
+        * When the requested record is a reshare activity, the system fetches the reshared original post.
+        * Otherwise the function reacts similar to selectFirst
+        *
+        * @param array $fields
+        * @param array $condition
+        * @param array $params
+        * @param bool  $user_mode true = post-user-view, false = post-view
+        * @return bool|array
+        * @throws \Exception
+        * @see   DBA::select
+        */
+       public static function selectOriginal(array $fields = [], array $condition = [], array $params = [])
+       {
+               $original_fields = $fields;
+               $remove = [];
+               if (!empty($fields)) {
+                       foreach (['gravity', 'verb', 'thr-parent-id', 'uid'] as $field) {
+                               if (!in_array($field, $fields)) {
+                                       $fields[] = $field;
+                                       $remove[] = $field;
+                               }
+                       }
+               }
+               $result = self::selectFirst($fields, $condition, $params);
+               if (empty($result)) {
+                       return $result;
+               }
+
+               if (($result['gravity'] != Item::GRAVITY_ACTIVITY) || ($result['verb'] != Activity::ANNOUNCE)) {
+                       foreach ($remove as $field) {
+                               unset($result[$field]);
+                       }
+                       return $result;
+               }
+
+               return self::selectFirst($original_fields, ['uri-id' => $result['thr-parent-id'], 'uid' => [0, $result['uid']]], $params);
+       }
+
        /**
         * Retrieve a single record from the post-view view and returns it in an associative array
         *
@@ -505,6 +545,46 @@ class Post
                }
        }
 
+       /**
+        * Retrieve a single record from the post-user-view view for a given user and returns it in an associative array
+        * When the requested record is a reshare activity, the system fetches the reshared original post.
+        * Otherwise the function reacts similar to selectFirstForUser
+        *
+        * @param integer $uid User ID
+        * @param array   $selected
+        * @param array   $condition
+        * @param array   $params
+        * @return bool|array
+        * @throws \Exception
+        * @see   DBA::select
+        */
+       public static function selectOriginalForUser(int $uid, array $selected = [], array $condition = [], array $params = [])
+       {
+               $original_selected = $selected;
+               $remove = [];
+               if (!empty($selected)) {
+                       foreach (['gravity', 'verb', 'thr-parent-id'] as $field) {
+                               if (!in_array($field, $selected)) {
+                                       $selected[] = $field;
+                                       $remove[]   = $field;
+                               }
+                       }
+               }
+               $result = self::selectFirstForUser($uid, $selected, $condition, $params);
+               if (empty($result)) {
+                       return $result;
+               }
+
+               if (($result['gravity'] != Item::GRAVITY_ACTIVITY) || ($result['verb'] != Activity::ANNOUNCE)) {
+                       foreach ($remove as $field) {
+                               unset($result[$field]);
+                       }
+                       return $result;
+               }
+
+               return self::selectFirstForUser($uid, $original_selected, ['uri-id' => $result['thr-parent-id'], 'uid' => [0, $uid]], $params);
+       }
+
        /**
         * Update existing post entries
         *
index a232112de8bfa45e538172197182bd9876680268..72641235d7e1457291170d5b3293871bbb126cec 100644 (file)
@@ -268,7 +268,7 @@ class Statuses extends BaseApi
                }
 
                if ($request['in_reply_to_id']) {
-                       $parent = Post::selectFirst(['uri'], ['uri-id' => $request['in_reply_to_id'], 'uid' => [0, $uid]]);
+                       $parent = Post::selectOriginal(['uri'], ['uri-id' => $request['in_reply_to_id'], 'uid' => [0, $uid]]);
                        if (empty($parent)) {
                                throw new HTTPException\NotFoundException('Item with URI ID ' . $request['in_reply_to_id'] . ' not found for user ' . $uid . '.');
                        }
index b5e8a540d01d23e7b733c001bf41c934d32c18bf..255b459c333252daf134a20b944cd8732f623d23 100644 (file)
@@ -42,7 +42,7 @@ class Bookmark extends BaseApi
                        DI::mstdnError()->UnprocessableEntity();
                }
 
-               $item = Post::selectFirst(['uid', 'id', 'gravity'], ['uri-id' => $this->parameters['id'], 'uid' => [$uid, 0]], ['order' => ['uid' => true]]);
+               $item = Post::selectOriginal(['uid', 'id', 'gravity'], ['uri-id' => $this->parameters['id'], 'uid' => [$uid, 0]], ['order' => ['uid' => true]]);
                if (!DBA::isResult($item)) {
                        DI::mstdnError()->RecordNotFound();
                }
@@ -52,7 +52,7 @@ class Bookmark extends BaseApi
                }
 
                if ($item['uid'] == 0) {
-                       $stored = Item::storeForUserByUriId($this->parameters['id'], $uid, ['post-reason' => Item::PR_ACTIVITY]);
+                       $stored = Item::storeForUserByUriId($item['id'], $uid, ['post-reason' => Item::PR_ACTIVITY]);
                        if (!empty($stored)) {
                                $item = Post::selectFirst(['id', 'gravity'], ['id' => $stored]);
                                if (!DBA::isResult($item)) {
index e1012d8ec33a3a6fb84341df228dfb3a396536e7..6d29ee879e2cefe3f33c30d5c18ff757a778188f 100644 (file)
@@ -43,13 +43,11 @@ class Card extends BaseApi
                        DI::mstdnError()->UnprocessableEntity();
                }
 
-               $id = $this->parameters['id'];
-
-               if (!Post::exists(['uri-id' => $id, 'uid' => [0, $uid]])) {
-                       throw new HTTPException\NotFoundException('Item with URI ID ' . $id . ' not found' . ($uid ? ' for user ' . $uid : '.'));
+               if (!$post = Post::selectOriginal(['id'], ['uri-id' => $this->parameters['id'], 'uid' => [0, $uid]])) {
+                       throw new HTTPException\NotFoundException('Item with URI ID ' . $this->parameters['id'] . ' not found' . ($uid ? ' for user ' . $uid : '.'));
                }
 
-               $card = DI::mstdnCard()->createFromUriId($id);
+               $card = DI::mstdnCard()->createFromUriId($post['id']);
 
                System::jsonExit($card->toArray());
        }
index f683b01751910ab1e2011a3752065e63ca6c6acd..0b2e7bda6d6b3c1c2b3e9e0c3545e71ab7b2421f 100644 (file)
@@ -57,8 +57,9 @@ class Context extends BaseApi
                $parents  = [];
                $children = [];
 
-               $parent = Post::selectFirst(['parent-uri-id'], ['uri-id' => $id]);
+               $parent = Post::selectOriginal(['uri-id', 'parent-uri-id'], ['uri-id' => $id]);
                if (DBA::isResult($parent)) {
+                       $id = $parent['uri-id'];
                        $params    = ['order' => ['uri-id' => true]];
                        $condition = ['parent-uri-id' => $parent['parent-uri-id'], 'gravity' => [Item::GRAVITY_PARENT, Item::GRAVITY_COMMENT]];
 
index de4c0feec02e834fb0c92ef64a931b8340fee32b..d1a68862ca3dc61ed6907ad59ee3aa2d25c86788 100644 (file)
@@ -42,7 +42,7 @@ class Favourite extends BaseApi
                        DI::mstdnError()->UnprocessableEntity();
                }
 
-               $item = Post::selectFirstForUser($uid, ['id'], ['uri-id' => $this->parameters['id'], 'uid' => [$uid, 0]]);
+               $item = Post::selectOriginalForUser($uid, ['id'], ['uri-id' => $this->parameters['id'], 'uid' => [$uid, 0]]);
                if (!DBA::isResult($item)) {
                        DI::mstdnError()->RecordNotFound();
                }
index 8f9fe02f171989a26b2d31790b2227b79784a95e..326e48d823f54bb052b7801339d9134810302076 100644 (file)
@@ -44,12 +44,11 @@ class FavouritedBy extends BaseApi
                        DI::mstdnError()->UnprocessableEntity();
                }
 
-               $id = $this->parameters['id'];
-               if (!Post::exists(['uri-id' => $id, 'uid' => [0, $uid]])) {
+               if (!$post = Post::selectOriginal(['id'], ['uri-id' => $this->parameters['id'], 'uid' => [0, $uid]])) {
                        DI::mstdnError()->RecordNotFound();
                }
 
-               $activities = Post::selectPosts(['author-id'], ['thr-parent-id' => $id, 'gravity' => Item::GRAVITY_ACTIVITY, 'verb' => Activity::LIKE, 'deleted' => false]);
+               $activities = Post::selectPosts(['author-id'], ['thr-parent-id' => $post['id'], 'gravity' => Item::GRAVITY_ACTIVITY, 'verb' => Activity::LIKE, 'deleted' => false]);
 
                $accounts = [];
 
index fa6cd120e95f5f0464a0520d18709778d4cc1f0b..0c1068490c463a5477eced1a66d40572bac2f46b 100644 (file)
@@ -42,7 +42,7 @@ class Mute extends BaseApi
                        DI::mstdnError()->UnprocessableEntity();
                }
 
-               $item = Post::selectFirstForUser($uid, ['id', 'gravity'], ['uri-id' => $this->parameters['id'], 'uid' => [$uid, 0]]);
+               $item = Post::selectOriginalForUser($uid, ['id', 'gravity'], ['uri-id' => $this->parameters['id'], 'uid' => [$uid, 0]]);
                if (!DBA::isResult($item)) {
                        DI::mstdnError()->RecordNotFound();
                }
@@ -51,7 +51,7 @@ class Mute extends BaseApi
                        DI::mstdnError()->UnprocessableEntity(DI::l10n()->t('Only starting posts can be muted'));
                }
 
-               Post\ThreadUser::setIgnored($this->parameters['id'], $uid, true);
+               Post\ThreadUser::setIgnored($item['id'], $uid, true);
 
                System::jsonExit(DI::mstdnStatus()->createFromUriId($this->parameters['id'], $uid, self::appSupportsQuotes())->toArray());
        }
index aa692c6b764c62045b883e0195f1233c789d8582..d092b4255f1f4020e563a9e45d990da468fbf1d6 100644 (file)
@@ -41,12 +41,12 @@ class Pin extends BaseApi
                        DI::mstdnError()->UnprocessableEntity();
                }
 
-               $item = Post::selectFirstForUser($uid, ['id', 'gravity', 'author-id'], ['uri-id' => $this->parameters['id'], 'uid' => [$uid, 0]]);
+               $item = Post::selectOriginalForUser($uid, ['id', 'gravity', 'author-id'], ['uri-id' => $this->parameters['id'], 'uid' => [$uid, 0]]);
                if (!DBA::isResult($item)) {
                        DI::mstdnError()->RecordNotFound();
                }
 
-               Post\Collection::add($this->parameters['id'], Post\Collection::FEATURED, $item['author-id'], $uid);
+               Post\Collection::add($item['id'], Post\Collection::FEATURED, $item['author-id'], $uid);
 
                System::jsonExit(DI::mstdnStatus()->createFromUriId($this->parameters['id'], $uid, self::appSupportsQuotes())->toArray());
        }
index 254144df98d5fedee27d63a2b0b92223237053b4..f1922d72712783790608baa61ac142b8f91e20f6 100644 (file)
@@ -45,7 +45,7 @@ class Reblog extends BaseApi
                        DI::mstdnError()->UnprocessableEntity();
                }
 
-               $item = Post::selectFirstForUser($uid, ['id', 'network'], ['uri-id' => $this->parameters['id'], 'uid' => [$uid, 0]]);
+               $item = Post::selectOriginalForUser($uid, ['id', 'network'], ['uri-id' => $this->parameters['id'], 'uid' => [$uid, 0]]);
                if (!DBA::isResult($item)) {
                        DI::mstdnError()->RecordNotFound();
                }
index d247a28566f696046abbfde6394345d71f91dc49..b225d17439c1d28a181024102072ae98d147bb60 100644 (file)
@@ -44,12 +44,11 @@ class RebloggedBy extends BaseApi
                        DI::mstdnError()->UnprocessableEntity();
                }
 
-               $id = $this->parameters['id'];
-               if (!Post::exists(['uri-id' => $id, 'uid' => [0, $uid]])) {
+               if (!$post = Post::selectOriginal(['id'], ['uri-id' => $this->parameters['id'], 'uid' => [0, $uid]])) {
                        DI::mstdnError()->RecordNotFound();
                }
 
-               $activities = Post::selectPosts(['author-id'], ['thr-parent-id' => $id, 'gravity' => Item::GRAVITY_ACTIVITY, 'verb' => Activity::ANNOUNCE]);
+               $activities = Post::selectPosts(['author-id'], ['thr-parent-id' => $post['id'], 'gravity' => Item::GRAVITY_ACTIVITY, 'verb' => Activity::ANNOUNCE]);
 
                $accounts = [];
 
index dd6a59fed20390fcc27724838ec54923d7e22bdc..4f6db11fd33b60bc94bcd6dc1c971829e3656ce9 100644 (file)
@@ -42,7 +42,7 @@ class Unbookmark extends BaseApi
                        DI::mstdnError()->UnprocessableEntity();
                }
 
-               $item = Post::selectFirst(['uid', 'id', 'gravity'], ['uri-id' => $this->parameters['id'], 'uid' => [$uid, 0]], ['order' => ['uid' => true]]);
+               $item = Post::selectOriginal(['uid', 'id', 'gravity'], ['uri-id' => $this->parameters['id'], 'uid' => [$uid, 0]], ['order' => ['uid' => true]]);
                if (!DBA::isResult($item)) {
                        DI::mstdnError()->RecordNotFound();
                }
@@ -52,7 +52,7 @@ class Unbookmark extends BaseApi
                }
 
                if ($item['uid'] == 0) {
-                       $stored = Item::storeForUserByUriId($this->parameters['id'], $uid, ['post-reason' => Item::PR_ACTIVITY]);
+                       $stored = Item::storeForUserByUriId($item['id'], $uid, ['post-reason' => Item::PR_ACTIVITY]);
                        if (!empty($stored)) {
                                $item = Post::selectFirst(['id', 'gravity'], ['id' => $stored]);
                                if (!DBA::isResult($item)) {
index 06b5c6c615e2f15dd273bf72131f8688d8472622..a3760b45442568da5fcadeda1d3aa0f1d08e1e26 100644 (file)
@@ -42,7 +42,7 @@ class Unfavourite extends BaseApi
                        DI::mstdnError()->UnprocessableEntity();
                }
 
-               $item = Post::selectFirstForUser($uid, ['id'], ['uri-id' => $this->parameters['id'], 'uid' => [$uid, 0]]);
+               $item = Post::selectOriginalForUser($uid, ['id'], ['uri-id' => $this->parameters['id'], 'uid' => [$uid, 0]]);
                if (!DBA::isResult($item)) {
                        DI::mstdnError()->RecordNotFound();
                }
index 658b6be8ba4a6ecd5d367442e6293e5d7d2ed7c9..b7a24beed24adba1a5b29d263068dd3a4405a795 100644 (file)
@@ -42,7 +42,7 @@ class Unmute extends BaseApi
                        DI::mstdnError()->UnprocessableEntity();
                }
 
-               $item = Post::selectFirstForUser($uid, ['id', 'gravity'], ['uri-id' => $this->parameters['id'], 'uid' => [$uid, 0]]);
+               $item = Post::selectOriginalForUser($uid, ['id', 'gravity'], ['uri-id' => $this->parameters['id'], 'uid' => [$uid, 0]]);
                if (!DBA::isResult($item)) {
                        DI::mstdnError()->RecordNotFound();
                }
@@ -51,7 +51,7 @@ class Unmute extends BaseApi
                        DI::mstdnError()->UnprocessableEntity(DI::l10n()->t('Only starting posts can be unmuted'));
                }
 
-               Post\ThreadUser::setIgnored($this->parameters['id'], $uid, false);
+               Post\ThreadUser::setIgnored($item['id'], $uid, false);
 
                System::jsonExit(DI::mstdnStatus()->createFromUriId($this->parameters['id'], $uid, self::appSupportsQuotes())->toArray());
        }
index f9efcdee66e71b146a1ed729628b273cdf3dfcf6..523ce4d554d416290160ddf86ae9abb52af128a3 100644 (file)
@@ -41,12 +41,12 @@ class Unpin extends BaseApi
                        DI::mstdnError()->UnprocessableEntity();
                }
 
-               $item = Post::selectFirstForUser($uid, ['id', 'gravity'], ['uri-id' => $this->parameters['id'], 'uid' => [$uid, 0]]);
+               $item = Post::selectOriginalForUser($uid, ['id', 'gravity'], ['uri-id' => $this->parameters['id'], 'uid' => [$uid, 0]]);
                if (!DBA::isResult($item)) {
                        DI::mstdnError()->RecordNotFound();
                }
 
-               Post\Collection::remove($this->parameters['id'], Post\Collection::FEATURED, $uid);
+               Post\Collection::remove($item['id'], Post\Collection::FEATURED, $uid);
 
                System::jsonExit(DI::mstdnStatus()->createFromUriId($this->parameters['id'], $uid, self::appSupportsQuotes())->toArray());
        }
index 94de3e9c6ac5e4a6e5c28312c20fda4710c75c90..54a18823a77168bc278777ae7fdb9e95e16e54ff 100644 (file)
@@ -44,7 +44,7 @@ class Unreblog extends BaseApi
                        DI::mstdnError()->UnprocessableEntity();
                }
 
-               $item = Post::selectFirstForUser($uid, ['id', 'network'], ['uri-id' => $this->parameters['id'], 'uid' => [$uid, 0]]);
+               $item = Post::selectOriginalForUser($uid, ['id', 'network'], ['uri-id' => $this->parameters['id'], 'uid' => [$uid, 0]]);
                if (!DBA::isResult($item)) {
                        DI::mstdnError()->RecordNotFound();
                }