]> git.mxchange.org Git - friendica.git/commitdiff
API: Fix relationships
authorMichael <heluecht@pirati.ca>
Sat, 15 May 2021 15:02:15 +0000 (15:02 +0000)
committerMichael <heluecht@pirati.ca>
Sat, 15 May 2021 15:02:15 +0000 (15:02 +0000)
14 files changed:
doc/API-Mastodon.md
src/Factory/Api/Mastodon/Relationship.php
src/Module/Api/Mastodon/Accounts/Block.php
src/Module/Api/Mastodon/Accounts/Mute.php
src/Module/Api/Mastodon/Accounts/Note.php [new file with mode: 0644]
src/Module/Api/Mastodon/Accounts/Relationships.php [new file with mode: 0644]
src/Module/Api/Mastodon/Accounts/Relationsships.php [deleted file]
src/Module/Api/Mastodon/Accounts/Statuses.php
src/Module/Api/Mastodon/Accounts/Unblock.php
src/Module/Api/Mastodon/Accounts/Unfollow.php
src/Module/Api/Mastodon/Accounts/Unmute.php
src/Module/Api/Mastodon/FollowRequests.php
src/Object/Api/Mastodon/Relationship.php
static/routes.config.php

index 278f5540ee61218511b06bcda68a0e79f150354e..d7ce5402d16ce34538b197b47d1320007ed9141b 100644 (file)
@@ -33,20 +33,17 @@ These endpoints use the [Mastodon API entities](https://docs.joinmastodon.org/en
 ## Implemented endpoints
 
 - [`GET /api/v1/accounts/:id`](https://docs.joinmastodon.org/methods/accounts/#retrieve-information)
-- [`GET /api/v1/accounts/:id/statuses`](https://docs.joinmastodon.org/methods/accounts/)
+- [`POST /api/v1/accounts/:id/block`](https://docs.joinmastodon.org/methods/accounts/)
+- [`POST /api/v1/accounts/:id/follow`](https://docs.joinmastodon.org/methods/accounts/)
 - [`GET /api/v1/accounts/:id/followers`](https://docs.joinmastodon.org/methods/accounts/)
 - [`GET /api/v1/accounts/:id/following`](https://docs.joinmastodon.org/methods/accounts/)
 - [`GET /api/v1/accounts/:id/lists`](https://docs.joinmastodon.org/methods/accounts/)
-- [`POST /api/v1/accounts/:id/follow`](https://docs.joinmastodon.org/methods/accounts/)
+- [`POST /api/v1/accounts/:id/mute`](https://docs.joinmastodon.org/methods/accounts/)
+- [`POST /api/v1/accounts/:id/note`](https://docs.joinmastodon.org/methods/accounts/)
+- [`GET /api/v1/accounts/:id/statuses`](https://docs.joinmastodon.org/methods/accounts/)
 - [`POST /api/v1/accounts/:id/unfollow`](https://docs.joinmastodon.org/methods/accounts/)
-- [`POST /api/v1/accounts/:id/block`](https://docs.joinmastodon.org/methods/accounts/)
 - [`POST /api/v1/accounts/:id/unblock`](https://docs.joinmastodon.org/methods/accounts/)
-- [`POST /api/v1/accounts/:id/mute`](https://docs.joinmastodon.org/methods/accounts/)
 - [`POST /api/v1/accounts/:id/unmute`](https://docs.joinmastodon.org/methods/accounts/)
-- [`GET /api/v1/accounts/:id/statuses`](https://docs.joinmastodon.org/methods/accounts/#retrieve-information)
-- [`GET /api/v1/accounts/:id/followers`](https://docs.joinmastodon.org/methods/accounts/)
-- [`GET /api/v1/accounts/:id/following`](https://docs.joinmastodon.org/methods/accounts/)
-- [`GET /api/v1/accounts/:id/lists`](https://docs.joinmastodon.org/methods/accounts/)
 - [`GET /api/v1/accounts/relationships`](https://docs.joinmastodon.org/methods/accounts/)
 - [`GET /api/v1/accounts/search`](https://docs.joinmastodon.org/methods/accounts)
 - [`GET /api/v1/accounts/verify_credentials`](https://docs.joinmastodon.org/methods/accounts)
@@ -115,7 +112,6 @@ These endpoints use the [Mastodon API entities](https://docs.joinmastodon.org/en
 
 These emdpoints are planned to be implemented
 
-- [`POST /api/v1/accounts/:id/note`](https://docs.joinmastodon.org/methods/accounts/)
 - [`PATCH /api/v1/accounts/update_credentials`](https://docs.joinmastodon.org/methods/accounts/)
 - [`GET /api/v1/apps/verify_credentials`](https://docs.joinmastodon.org/methods/apps/)
 - [`GET /api/v1/conversations`](https://docs.joinmastodon.org/methods/timelines/conversations/)
index a1d94a33235cedbd3d925bade36342a0824c6723..7111148344c17532ad27de8d090ec44fcdb71f63 100644 (file)
@@ -28,50 +28,21 @@ use Friendica\Model\Contact;
 class Relationship extends BaseFactory
 {
        /**
-        * @param int $userContactId Contact row id with uid != 0
-        * @return RelationshipEntity
-        * @throws \Exception
-        */
-       public function createFromContactId(int $userContactId)
-       {
-               return $this->createFromContact(Contact::getById($userContactId));
-       }
-
-       /**
-        * @param int $publicContactId Contact row id with uid = 0
+        * @param int $contactId Contact ID (public or user contact)
         * @param int $uid User ID
         * @return RelationshipEntity
         * @throws \Exception
         */
-       public function createFromPublicContactId(int $publicContactId, int $uid)
+       public function createFromContactId(int $contactId, int $uid)
        {
-               $cdata = Contact::getPublicAndUserContacID($publicContactId, $uid);
+               $cdata = Contact::getPublicAndUserContacID($contactId, $uid);
                if (!empty($cdata)) {
                        $cid = $cdata['user'];
                } else {
-                       $cid = $publicContactId;
+                       $cid = $contactId;
                }
 
-               return $this->createFromContact(Contact::getById($cid));
-       }
-
-       /**
-        * @param array $userContact Full contact row record with uid != 0
-        * @return RelationshipEntity
-        */
-       public function createFromContact(array $userContact)
-       {
-               return new RelationshipEntity($userContact['id'], $userContact,
-                       Contact\User::isBlocked($userContact['id'], $userContact['uid']),
-                       Contact\User::isIgnored($userContact['id'], $userContact['uid']));
-       }
-
-       /**
-        * @param int $userContactId Contact row id with uid != 0
-        * @return RelationshipEntity
-        */
-       public function createDefaultFromContactId(int $userContactId)
-       {
-               return new RelationshipEntity($userContactId);
+               return new RelationshipEntity($cdata['public'], Contact::getById($cid),
+                       Contact\User::isBlocked($cid, $uid), Contact\User::isIgnored($cid, $uid));
        }
 }
index 50434524493127827c6088885b9c03a86b3de78c..edbae8a1d33822d03f2a35b86cc613a3d5079701 100644 (file)
@@ -42,6 +42,6 @@ class Block extends BaseApi
 
                Contact\User::setBlocked($parameters['id'], $uid, true);
 
-               System::jsonExit(DI::mstdnRelationship()->createFromPublicContactId($parameters['id'], $uid)->toArray());
+               System::jsonExit(DI::mstdnRelationship()->createFromContactId($parameters['id'], $uid)->toArray());
        }
 }
index 6dda625f0ef676c15acb48ab59a2b6dfc413944c..e3975771faa96173c46e34f171d4d02dadfbe61a 100644 (file)
@@ -42,6 +42,6 @@ class Mute extends BaseApi
 
                Contact\User::setIgnored($parameters['id'], $uid, true);
 
-               System::jsonExit(DI::mstdnRelationship()->createFromPublicContactId($parameters['id'], $uid)->toArray());
+               System::jsonExit(DI::mstdnRelationship()->createFromContactId($parameters['id'], $uid)->toArray());
        }
 }
diff --git a/src/Module/Api/Mastodon/Accounts/Note.php b/src/Module/Api/Mastodon/Accounts/Note.php
new file mode 100644 (file)
index 0000000..bbb53d9
--- /dev/null
@@ -0,0 +1,53 @@
+<?php
+/**
+ * @copyright Copyright (C) 2010-2021, the Friendica project
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ *
+ */
+
+namespace Friendica\Module\Api\Mastodon\Accounts;
+
+use Friendica\Core\System;
+use Friendica\Database\DBA;
+use Friendica\DI;
+use Friendica\Model\Contact;
+use Friendica\Module\BaseApi;
+
+/**
+ * @see https://docs.joinmastodon.org/methods/accounts/
+ */
+class Note extends BaseApi
+{
+       public static function post(array $parameters = [])
+       {
+               self::login();
+               $uid = self::getCurrentUserID();
+
+               if (empty($parameters['id'])) {
+                       DI::mstdnError()->UnprocessableEntity();
+               }
+
+               $cdata = Contact::getPublicAndUserContacID($parameters['id'], $uid);
+               if (empty($cdata['user'])) {
+                       DI::mstdnError()->RecordNotFound();
+               }
+
+               DBA::update('contact', ['info' => $_REQUEST['comment'] ?? ''], ['id' => $cdata['user']]);
+
+               System::jsonExit(DI::mstdnRelationship()->createFromContactId($parameters['id'], $uid)->toArray());
+       }
+}
diff --git a/src/Module/Api/Mastodon/Accounts/Relationships.php b/src/Module/Api/Mastodon/Accounts/Relationships.php
new file mode 100644 (file)
index 0000000..a989460
--- /dev/null
@@ -0,0 +1,55 @@
+<?php
+/**
+ * @copyright Copyright (C) 2010-2021, the Friendica project
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ *
+ */
+
+namespace Friendica\Module\Api\Mastodon\Accounts;
+
+use Friendica\Core\Logger;
+use Friendica\Core\System;
+use Friendica\DI;
+use Friendica\Module\BaseApi;
+
+/**
+ * @see https://docs.joinmastodon.org/methods/accounts/
+ */
+class Relationships extends BaseApi
+{
+       /**
+        * @param array $parameters
+        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
+        */
+       public static function rawContent(array $parameters = [])
+       {
+               self::login();
+               $uid = self::getCurrentUserID();
+
+               if (empty($_REQUEST['id']) || !is_array($_REQUEST['id'])) {
+                       DI::mstdnError()->UnprocessableEntity();
+               }
+
+               $relationsships = [];
+
+               foreach ($_REQUEST['id'] as $id) {
+                       $relationsships[] = DI::mstdnRelationship()->createFromContactId($id, $uid);
+               }
+
+               System::jsonExit($relationsships);
+       }
+}
diff --git a/src/Module/Api/Mastodon/Accounts/Relationsships.php b/src/Module/Api/Mastodon/Accounts/Relationsships.php
deleted file mode 100644 (file)
index d485a06..0000000
+++ /dev/null
@@ -1,55 +0,0 @@
-<?php
-/**
- * @copyright Copyright (C) 2010-2021, the Friendica project
- *
- * @license GNU AGPL version 3 or any later version
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as
- * published by the Free Software Foundation, either version 3 of the
- * License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program.  If not, see <https://www.gnu.org/licenses/>.
- *
- */
-
-namespace Friendica\Module\Api\Mastodon\Accounts;
-
-use Friendica\Core\System;
-use Friendica\DI;
-use Friendica\Module\BaseApi;
-
-/**
- * @see https://docs.joinmastodon.org/methods/accounts/
- */
-class Relationships extends BaseApi
-{
-       /**
-        * @param array $parameters
-        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
-        */
-       public static function rawContent(array $parameters = [])
-       {
-               self::login();
-               $uid = self::getCurrentUserID();
-
-
-               if (empty($parameters['id'])) {
-                       DI::mstdnError()->UnprocessableEntity();
-               }
-
-               $relationsships = [];
-
-               foreach ($parameters['id'] as $id) {
-                       $relationsships[] = DI::mstdnRelationship()->createFromPublicContactId($id, $uid);
-               }
-
-               System::jsonExit($relationsships);
-       }
-}
index e234b7ee1d8c97fa269f5c04b6783859487901da..f824abc9ad91c18ffee1a34043b774bd250f1991 100644 (file)
@@ -62,6 +62,9 @@ class Statuses extends BaseApi
                // Maximum number of results to return. Defaults to 20.
                $limit = (int)!isset($_REQUEST['limit']) ? 20 : $_REQUEST['limit'];
 
+               $pinned = (bool)!isset($_REQUEST['pinned']) ? false : ($_REQUEST['pinned'] == 'true');
+               $exclude_replies = (bool)!isset($_REQUEST['exclude_replies']) ? false : ($_REQUEST['exclude_replies'] == 'true');
+
                $params = ['order' => ['uri-id' => true], 'limit' => $limit];
 
                $uid = self::getCurrentUserID();
@@ -94,6 +97,14 @@ class Statuses extends BaseApi
                        $params['order'] = ['uri-id'];
                }
 
+               if ($pinned) {
+                       $condition = DBA::mergeConditions($condition, ['pinned' => true]);
+               }
+
+               if ($exclude_replies) {
+                       $condition = DBA::mergeConditions($condition, ['gravity' => GRAVITY_PARENT]);
+               }
+
                $items = Post::selectForUser($uid, ['uri-id'], $condition, $params);
 
                $statuses = [];
index 51997453fc41ac392cf3dc9c48135e22772465dd..7de5a4cfbcc89dd83a247733088fa09f06379356 100644 (file)
@@ -42,6 +42,6 @@ class Unblock extends BaseApi
 
                Contact\User::setBlocked($parameters['id'], $uid, false);
 
-               System::jsonExit(DI::mstdnRelationship()->createFromPublicContactId($parameters['id'], $uid)->toArray());
+               System::jsonExit(DI::mstdnRelationship()->createFromContactId($parameters['id'], $uid)->toArray());
        }
 }
index ef681e67f61bbd7a2da81bba709c94fa430ef4f2..b2efde7b01bf02195219d0153638342a72d0d670 100644 (file)
@@ -42,6 +42,6 @@ class Unfollow extends BaseApi
 
                Contact::unfollow($parameters['id'], $uid);
 
-               System::jsonExit(DI::mstdnRelationship()->createFromPublicContactId($parameters['id'], $uid)->toArray());
+               System::jsonExit(DI::mstdnRelationship()->createFromContactId($parameters['id'], $uid)->toArray());
        }
 }
index c870da0aa2f55e58accf19600dcf284f57fd898c..15892cff42afacf3bfd2fca4177256a7cea66caf 100644 (file)
@@ -42,6 +42,6 @@ class Unmute extends BaseApi
 
                Contact\User::setIgnored($parameters['id'], $uid, false);
 
-               System::jsonExit(DI::mstdnRelationship()->createFromPublicContactId($parameters['id'], $uid)->toArray());
+               System::jsonExit(DI::mstdnRelationship()->createFromContactId($parameters['id'], $uid)->toArray());
        }
 }
index 009094057782bdef047b12ff044e87b05f01005f..8a59120d312ebf120640cd7640329f266ffcf64c 100644 (file)
@@ -31,15 +31,6 @@ use Friendica\Network\HTTPException;
  */
 class FollowRequests extends BaseApi
 {
-       public static function init(array $parameters = [])
-       {
-               parent::init($parameters);
-
-               if (!self::login()) {
-                       throw new HTTPException\UnauthorizedException();
-               }
-       }
-
        /**
         * @param array $parameters
         * @throws HTTPException\BadRequestException
@@ -54,9 +45,10 @@ class FollowRequests extends BaseApi
         */
        public static function post(array $parameters = [])
        {
-               parent::post($parameters);
+               self::login();
+               $uid = self::getCurrentUserID();
 
-               $introduction = DI::intro()->selectFirst(['id' => $parameters['id'], 'uid' => self::$current_user_id]);
+               $introduction = DI::intro()->selectFirst(['id' => $parameters['id'], 'uid' => $uid]);
 
                $contactId = $introduction->{'contact-id'};
 
@@ -64,17 +56,17 @@ class FollowRequests extends BaseApi
                        case 'authorize':
                                $introduction->confirm();
 
-                               $relationship = DI::mstdnRelationship()->createFromContactId($contactId);
+                               $relationship = DI::mstdnRelationship()->createFromContactId($contactId, $uid);
                                break;
                        case 'ignore':
                                $introduction->ignore();
 
-                               $relationship = DI::mstdnRelationship()->createDefaultFromContactId($contactId);
+                               $relationship = DI::mstdnRelationship()->createFromContactId($contactId, $uid);
                                break;
                        case 'reject':
                                $introduction->discard();
 
-                               $relationship = DI::mstdnRelationship()->createDefaultFromContactId($contactId);
+                               $relationship = DI::mstdnRelationship()->createFromContactId($contactId, $uid);
                                break;
                        default:
                                throw new HTTPException\BadRequestException('Unexpected action parameter, expecting "authorize", "ignore" or "reject"');
@@ -91,6 +83,9 @@ class FollowRequests extends BaseApi
         */
        public static function rawContent(array $parameters = [])
        {
+               self::login();
+               $uid = self::getCurrentUserID();
+
                $min_id = $_GET['min_id'] ?? null;
                $max_id = $_GET['max_id'] ?? null;
                $limit = intval($_GET['limit'] ?? 40);
@@ -98,7 +93,7 @@ class FollowRequests extends BaseApi
                $baseUrl = DI::baseUrl();
 
                $introductions = DI::intro()->selectByBoundaries(
-                       ['`uid` = ? AND NOT `ignore`', self::$current_user_id],
+                       ['`uid` = ? AND NOT `ignore`', $uid],
                        ['order' => ['id' => 'DESC']],
                        $min_id,
                        $max_id,
index 463eb97e04c5df2fa14e615d5743129af0dcaba4..870acb54466178407f8fc9747df59de963cd41de 100644 (file)
@@ -28,7 +28,7 @@ use Friendica\Util\Network;
 /**
  * Class Relationship
  *
- * @see https://docs.joinmastodon.org/api/entities/#relationship
+ * @see https://docs.joinmastodon.org/entities/relationship/
  */
 class Relationship extends BaseDataTransferObject
 {
@@ -72,27 +72,37 @@ class Relationship extends BaseDataTransferObject
        protected $note = '';
 
        /**
-        * @param int   $userContactId Contact row Id with uid != 0
-        * @param array $userContact   Full Contact table record with uid != 0
+        * @param int   $contactId Contact row Id with uid != 0
+        * @param array $contactRecord   Full Contact table record with uid != 0
         * @param bool  $blocked "true" if user is blocked
         * @param bool  $muted "true" if user is muted
         */
-       public function __construct(int $userContactId, array $userContact = [], bool $blocked = false, bool $muted = false)
+       public function __construct(int $contactId, array $contactRecord = [], bool $blocked = false, bool $muted = false)
        {
-               $this->id                   = $userContactId;
-               $this->following            = in_array($userContact['rel'] ?? 0, [Contact::SHARING, Contact::FRIEND]);
-               $this->requested            = (bool)$userContact['pending'] ?? false;
+               $this->id                   = $contactId;
+               $this->following            = false;
+               $this->requested            = false;
                $this->endorsed             = false;
-               $this->followed_by          = in_array($userContact['rel'] ?? 0, [Contact::FOLLOWER, Contact::FRIEND]);
-               $this->muting               = (bool)($userContact['readonly'] ?? false) || $muted;
-               $this->muting_notifications = $this->muting;
+               $this->followed_by          = false;
+               $this->muting               = $muted;
+               $this->muting_notifications = false;
                $this->showing_reblogs      = true;
-               $this->notifying            = (bool)$userContact['notify_new_posts'] ?? false;
-               $this->blocking             = (bool)($userContact['blocked'] ?? false) || $blocked;
-               $this->domain_blocking      = Network::isUrlBlocked($userContact['url'] ?? '');
+               $this->notifying            = false;
+               $this->blocking             = $blocked;
+               $this->domain_blocking      = Network::isUrlBlocked($contactRecord['url'] ?? '');
                $this->blocked_by           = false;
                $this->note                 = '';
 
+               if ($contactRecord['uid'] != 0) {
+                       $this->following   = in_array($contactRecord['rel'] ?? 0, [Contact::SHARING, Contact::FRIEND]);
+                       $this->requested   = (bool)($contactRecord['pending'] ?? false);
+                       $this->followed_by = in_array($contactRecord['rel'] ?? 0, [Contact::FOLLOWER, Contact::FRIEND]);
+                       $this->muting      = (bool)($contactRecord['readonly'] ?? false) || $muted;
+                       $this->notifying   = (bool)$contactRecord['notify_new_posts'] ?? false;
+                       $this->blocking    = (bool)($contactRecord['blocked'] ?? false) || $blocked;
+                       $this->note        = $contactRecord['info'];
+               }
+
                return $this;
        }
 }
index ada9de8a564d755f0bd4d85ddfbcf535aab27874..6dee2164cc7126fe5f8247d8af07e230dc583ca5 100644 (file)
@@ -72,11 +72,11 @@ return [
                        '/accounts/{id:\d+}/unmute'          => [Module\Api\Mastodon\Accounts\Unmute::class,          [        R::POST]],
                        '/accounts/{id:\d+}/pin'             => [Module\Api\Mastodon\Unimplemented::class,            [        R::POST]], // not supported
                        '/accounts/{id:\d+}/unpin'           => [Module\Api\Mastodon\Unimplemented::class,            [        R::POST]], // not supported
-                       '/accounts/{id:\d+}/note'            => [Module\Api\Mastodon\Unimplemented::class,            [        R::POST]], // @todo
+                       '/accounts/{id:\d+}/note'            => [Module\Api\Mastodon\Accounts\Note::class,            [        R::POST]],
                        '/accounts/relationships'            => [Module\Api\Mastodon\Accounts\Relationships::class,   [R::GET         ]],
                        '/accounts/search'                   => [Module\Api\Mastodon\Accounts\Search::class,          [R::GET         ]],
-                       '/accounts/verify_credentials'       => [Module\Api\Mastodon\Accounts\VerifyCredentials::class, [R::GET       ]],
                        '/accounts/update_credentials'       => [Module\Api\Mastodon\Accounts\UpdateCredentials::class, [R::PATCH     ]],
+                       '/accounts/verify_credentials'       => [Module\Api\Mastodon\Accounts\VerifyCredentials::class, [R::GET       ]],
                        '/admin/accounts'                    => [Module\Api\Mastodon\Unimplemented::class,            [R::GET         ]], // not supported
                        '/admin/accounts/{id:\d+}'           => [Module\Api\Mastodon\Unimplemented::class,            [R::GET         ]], // not supported
                        '/admin/accounts/{id:\d+}/{action}'  => [Module\Api\Mastodon\Unimplemented::class,            [        R::POST]], // not supported