From dd38cc330f3131d5704c4efffda1da7dcdeb7e1c Mon Sep 17 00:00:00 2001 From: Michael Date: Fri, 7 May 2021 21:28:48 +0000 Subject: [PATCH 01/15] API: We now support two more timeline api endpoints --- src/Factory/Api/Mastodon/Status.php | 19 ++-- src/Model/Post.php | 10 +- src/Module/Api/Mastodon/Accounts.php | 2 +- src/Module/Api/Mastodon/Accounts/Statuses.php | 14 ++- src/Module/Api/Mastodon/Timelines/Home.php | 91 ++++++++++++++++ src/Module/Api/Mastodon/Timelines/Tag.php | 100 ++++++++++++++++++ static/routes.config.php | 4 +- 7 files changed, 220 insertions(+), 20 deletions(-) create mode 100644 src/Module/Api/Mastodon/Timelines/Home.php create mode 100644 src/Module/Api/Mastodon/Timelines/Tag.php diff --git a/src/Factory/Api/Mastodon/Status.php b/src/Factory/Api/Mastodon/Status.php index b157d809ac..310fa26bbe 100644 --- a/src/Factory/Api/Mastodon/Status.php +++ b/src/Factory/Api/Mastodon/Status.php @@ -23,6 +23,7 @@ namespace Friendica\Factory\Api\Mastodon; use Friendica\App\BaseURL; use Friendica\BaseFactory; +use Friendica\Content\ContactSelector; use Friendica\Content\Text\BBCode; use Friendica\Database\DBA; use Friendica\DI; @@ -60,9 +61,9 @@ class Status extends BaseFactory */ public function createFromUriId(int $uriId, $uid = 0) { - $fields = ['uri-id', 'uid', 'author-id', 'starred', 'app', 'title', 'body', 'raw-body', 'created', + $fields = ['uri-id', 'uid', 'author-id', 'author-link', 'starred', 'app', 'title', 'body', 'raw-body', 'created', 'network', 'thr-parent-id', 'parent-author-id', 'language', 'uri', 'plink', 'private', 'vid', 'gravity']; - $item = Post::selectFirst($fields, ['uri-id' => $uriId, 'uid' => $uid]); + $item = Post::selectFirst($fields, ['uri-id' => $uriId, 'uid' => [0, $uid]], ['order' => ['uid' => true]]); if (!$item) { throw new HTTPException\NotFoundException('Item with URI ID ' . $uriId . 'not found' . ($uid ? ' for user ' . $uid : '.')); } @@ -70,21 +71,21 @@ class Status extends BaseFactory $account = DI::mstdnAccount()->createFromContactId($item['author-id']); $counts = new \Friendica\Object\Api\Mastodon\Status\Counts( - Post::count(['thr-parent-id' => $uriId, 'uid' => $uid, 'gravity' => GRAVITY_COMMENT]), - Post::count(['thr-parent-id' => $uriId, 'uid' => $uid, 'gravity' => GRAVITY_ACTIVITY, 'vid' => Verb::getID(Activity::ANNOUNCE)]), - Post::count(['thr-parent-id' => $uriId, 'uid' => $uid, 'gravity' => GRAVITY_ACTIVITY, 'vid' => Verb::getID(Activity::LIKE)]) + Post::count(['thr-parent-id' => $uriId, 'gravity' => GRAVITY_COMMENT], [], false), + Post::count(['thr-parent-id' => $uriId, 'gravity' => GRAVITY_ACTIVITY, 'vid' => Verb::getID(Activity::ANNOUNCE)], [], false), + Post::count(['thr-parent-id' => $uriId, 'gravity' => GRAVITY_ACTIVITY, 'vid' => Verb::getID(Activity::LIKE)], [], false) ); $userAttributes = new \Friendica\Object\Api\Mastodon\Status\UserAttributes( Post::exists(['thr-parent-id' => $uriId, 'uid' => $uid, 'origin' => true, 'gravity' => GRAVITY_ACTIVITY, 'vid' => Verb::getID(Activity::LIKE)]), Post::exists(['thr-parent-id' => $uriId, 'uid' => $uid, 'origin' => true, 'gravity' => GRAVITY_ACTIVITY, 'vid' => Verb::getID(Activity::ANNOUNCE)]), - Post\ThreadUser::getIgnored($uriId, $item['uid']), + Post\ThreadUser::getIgnored($uriId, $uid), (bool)$item['starred'], - Post\ThreadUser::getPinned($uriId, $item['uid']) + Post\ThreadUser::getPinned($uriId, $uid) ); $sensitive = DBA::exists('tag-view', ['uri-id' => $uriId, 'name' => 'nsfw']); - $application = new \Friendica\Object\Api\Mastodon\Application($item['app'] ?? ''); + $application = new \Friendica\Object\Api\Mastodon\Application($item['app'] ?: ContactSelector::networkToName($item['network'], $item['author-link'])); $mentions = DI::mstdnMention()->createFromUriId($uriId); $tags = DI::mstdnTag()->createFromUriId($uriId); @@ -95,7 +96,7 @@ class Status extends BaseFactory if ($item['vid'] == Verb::getID(Activity::ANNOUNCE)) { $reshare = $this->createFromUriId($item['thr-parent-id'], $uid)->toArray(); - $reshared_item = Post::selectFirst(['title', 'body'], ['uri-id' => $item['thr-parent-id'], 'uid' => $uid]); + $reshared_item = Post::selectFirst(['title', 'body'], ['uri-id' => $item['thr-parent-id'], 'uid' => [0, $uid]]); $item['title'] = $reshared_item['title'] ?? $item['title']; $item['body'] = $reshared_item['body'] ?? $item['body']; } else { diff --git a/src/Model/Post.php b/src/Model/Post.php index 864e2a5742..9ce301ade0 100644 --- a/src/Model/Post.php +++ b/src/Model/Post.php @@ -127,12 +127,13 @@ class Post * Check if post data exists * * @param array $condition array of fields for condition + * @param bool $user_mode true = post-user-view, false = post-view * * @return boolean Are there rows for that condition? * @throws \Exception */ - public static function exists($condition) { - return DBA::exists('post-user-view', $condition); + public static function exists($condition, bool $user_mode = true) { + return DBA::exists($user_mode ? 'post-user-view' : 'post-view', $condition); } /** @@ -140,6 +141,7 @@ class Post * * @param array $condition array of fields for condition * @param array $params Array of several parameters + * @param bool $user_mode true = post-user-view, false = post-view * * @return int * @@ -151,9 +153,9 @@ class Post * $count = Post::count($condition); * @throws \Exception */ - public static function count(array $condition = [], array $params = []) + public static function count(array $condition = [], array $params = [], bool $user_mode = true) { - return DBA::count('post-user-view', $condition, $params); + return DBA::count($user_mode ? 'post-user-view' : 'post-view', $condition, $params); } /** diff --git a/src/Module/Api/Mastodon/Accounts.php b/src/Module/Api/Mastodon/Accounts.php index 6e868d1319..0d48744a40 100644 --- a/src/Module/Api/Mastodon/Accounts.php +++ b/src/Module/Api/Mastodon/Accounts.php @@ -46,7 +46,7 @@ class Accounts extends BaseApi DI::mstdnError()->RecordNotFound(); } - $account = DI::mstdnAccount()->createFromContactId($id); + $account = DI::mstdnAccount()->createFromContactId($id, self::getCurrentUserID()); System::jsonExit($account); } } diff --git a/src/Module/Api/Mastodon/Accounts/Statuses.php b/src/Module/Api/Mastodon/Accounts/Statuses.php index d21a1f535d..f6da2aebe2 100644 --- a/src/Module/Api/Mastodon/Accounts/Statuses.php +++ b/src/Module/Api/Mastodon/Accounts/Statuses.php @@ -64,8 +64,14 @@ class Statuses extends BaseApi $params = ['order' => ['uri-id' => true], 'limit' => $limit]; - $condition = ['author-id' => $id, 'private' => [Item::PUBLIC, Item::UNLISTED], - 'uid' => 0, 'network' => Protocol::FEDERATED]; + $uid = self::getCurrentUserID(); + + if (!$uid) { + $condition = ['author-id' => $id, 'private' => [Item::PUBLIC, Item::UNLISTED], + 'uid' => 0, 'network' => Protocol::FEDERATED]; + } else { + $condition = ["`author-id` = ? AND (`uid` = 0 OR (`uid` = ? AND NOT `global`))", $id, $uid]; + } $condition = DBA::mergeConditions($condition, ["(`gravity` IN (?, ?) OR (`gravity` = ? AND `vid` = ?))", GRAVITY_PARENT, GRAVITY_COMMENT, GRAVITY_ACTIVITY, Verb::getID(Activity::ANNOUNCE)]); @@ -83,11 +89,11 @@ class Statuses extends BaseApi $params['order'] = ['uri-id']; } - $items = Post::selectForUser(0, ['uri-id', 'uid'], $condition, $params); + $items = Post::selectForUser($uid, ['uri-id'], $condition, $params); $statuses = []; while ($item = Post::fetch($items)) { - $statuses[] = DI::mstdnStatus()->createFromUriId($item['uri-id'], $item['uid']); + $statuses[] = DI::mstdnStatus()->createFromUriId($item['uri-id'], $uid); } DBA::close($items); diff --git a/src/Module/Api/Mastodon/Timelines/Home.php b/src/Module/Api/Mastodon/Timelines/Home.php new file mode 100644 index 0000000000..dc1b7e0322 --- /dev/null +++ b/src/Module/Api/Mastodon/Timelines/Home.php @@ -0,0 +1,91 @@ +. + * + */ + +namespace Friendica\Module\Api\Mastodon\Timelines; + +use Friendica\Core\System; +use Friendica\Database\DBA; +use Friendica\DI; +use Friendica\Model\Post; +use Friendica\Module\BaseApi; +use Friendica\Network\HTTPException; + +/** + * @see https://docs.joinmastodon.org/methods/timelines/ + */ +class Home extends BaseApi +{ + /** + * @param array $parameters + * @throws HTTPException\InternalServerErrorException + */ + public static function rawContent(array $parameters = []) + { + self::login(); + $uid = self::getCurrentUserID(); + + // Return results older than id + $max_id = (int)!isset($_REQUEST['max_id']) ? 0 : $_REQUEST['max_id']; + // Return results newer than id + $since_id = (int)!isset($_REQUEST['since_id']) ? 0 : $_REQUEST['since_id']; + // Return results immediately newer than id + $min_id = (int)!isset($_REQUEST['min_id']) ? 0 : $_REQUEST['min_id']; + // Maximum number of results to return. Defaults to 20. + $limit = (int)!isset($_REQUEST['limit']) ? 20 : $_REQUEST['limit']; + // Return only local statuses? Defaults to false. + $local = (bool)!isset($_REQUEST['local']) ? false : ($_REQUEST['local'] == 'true'); + + $params = ['order' => ['uri-id' => true], 'limit' => $limit]; + + $condition = ['gravity' => [GRAVITY_PARENT, GRAVITY_COMMENT], 'uid' => $uid]; + + if ($local) { + $condition = DBA::mergeConditions($condition, ["`uri-id` IN (SELECT `uri-id` FROM `post-user` WHERE `origin`)"]); + } + + if (!empty($max_id)) { + $condition = DBA::mergeConditions($condition, ["`uri-id` < ?", $max_id]); + } + + if (!empty($since_id)) { + $condition = DBA::mergeConditions($condition, ["`uri-id` > ?", $since_id]); + } + + if (!empty($min_id)) { + $condition = DBA::mergeConditions($condition, ["`uri-id` > ?", $min_id]); + $params['order'] = ['uri-id']; + } + + $items = Post::selectForUser($uid, ['uri-id'], $condition, $params); + + $statuses = []; + while ($item = Post::fetch($items)) { + $statuses[] = DI::mstdnStatus()->createFromUriId($item['uri-id'], $uid); + } + DBA::close($items); + + if (!empty($min_id)) { + array_reverse($statuses); + } + + System::jsonExit($statuses); + } +} diff --git a/src/Module/Api/Mastodon/Timelines/Tag.php b/src/Module/Api/Mastodon/Timelines/Tag.php new file mode 100644 index 0000000000..28b62557b6 --- /dev/null +++ b/src/Module/Api/Mastodon/Timelines/Tag.php @@ -0,0 +1,100 @@ +. + * + */ + +namespace Friendica\Module\Api\Mastodon\Timelines; + +use Friendica\Core\Protocol; +use Friendica\Core\System; +use Friendica\Database\DBA; +use Friendica\DI; +use Friendica\Model\Post; +use Friendica\Module\BaseApi; +use Friendica\Network\HTTPException; + +/** + * @see https://docs.joinmastodon.org/methods/timelines/ + */ +class Tag extends BaseApi +{ + /** + * @param array $parameters + * @throws HTTPException\InternalServerErrorException + */ + public static function rawContent(array $parameters = []) + { + self::login(); + $uid = self::getCurrentUserID(); + + if (empty($parameters['hashtag'])) { + DI::mstdnError()->RecordNotFound(); + } + + // If true, return only local statuses. Defaults to false. + $local = (bool)!isset($_REQUEST['local']) ? false : ($_REQUEST['local'] == 'true'); + // If true, return only statuses with media attachments. Defaults to false. + $only_media = (bool)!isset($_REQUEST['only_media']) ? false : ($_REQUEST['only_media'] == 'true'); // Currently not supported + // Return results older than this ID. + $max_id = (int)!isset($_REQUEST['max_id']) ? 0 : $_REQUEST['max_id']; + // Return results newer than this ID. + $since_id = (int)!isset($_REQUEST['since_id']) ? 0 : $_REQUEST['since_id']; + // Return results immediately newer than this ID. + $min_id = (int)!isset($_REQUEST['min_id']) ? 0 : $_REQUEST['min_id']; + // Maximum number of results to return. Defaults to 20. + $limit = (int)!isset($_REQUEST['limit']) ? 20 : $_REQUEST['limit']; + + $params = ['order' => ['uri-id' => true], 'limit' => $limit]; + + $condition = ["`name` = ? AND (`uid` = ? OR (`uid` = ? AND NOT `global`)) + AND (`network` IN (?, ?, ?, ?) OR (`uid` = ? AND `uid` != ?))", + $parameters['hashtag'], 0, $uid, Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS, $uid, 0]; + + if ($local) { + $condition = DBA::mergeConditions($condition, ["`uri-id` IN (SELECT `uri-id` FROM `post-user` WHERE `origin`)"]); + } + + if (!empty($max_id)) { + $condition = DBA::mergeConditions($condition, ["`uri-id` < ?", $max_id]); + } + + if (!empty($since_id)) { + $condition = DBA::mergeConditions($condition, ["`uri-id` > ?", $since_id]); + } + + if (!empty($min_id)) { + $condition = DBA::mergeConditions($condition, ["`uri-id` > ?", $min_id]); + $params['order'] = ['uri-id']; + } + + $items = DBA::select('tag-search-view', ['uri-id'], $condition, $params); + + $statuses = []; + while ($item = Post::fetch($items)) { + $statuses[] = DI::mstdnStatus()->createFromUriId($item['uri-id'], $uid); + } + DBA::close($items); + + if (!empty($min_id)) { + array_reverse($statuses); + } + + System::jsonExit($statuses); + } +} diff --git a/static/routes.config.php b/static/routes.config.php index ec92b37adb..dd09a99dfc 100644 --- a/static/routes.config.php +++ b/static/routes.config.php @@ -142,10 +142,10 @@ return [ '/statuses/{id:\d+}/unpin' => [Module\Api\Mastodon\Unimplemented::class, [ R::POST]], '/suggestions' => [Module\Api\Mastodon\Unimplemented::class, [R::GET ]], '/suggestions/{id:\d+}' => [Module\Api\Mastodon\Unimplemented::class, [R::DELETE ]], - '/timelines/home' => [Module\Api\Mastodon\Unimplemented::class, [R::GET ]], + '/timelines/home' => [Module\Api\Mastodon\Timelines\Home::class, [R::GET ]], '/timelines/list/{id:\d+}' => [Module\Api\Mastodon\Unimplemented::class, [R::GET ]], '/timelines/public' => [Module\Api\Mastodon\Timelines\PublicTimeline::class, [R::GET ]], - '/timelines/tag/{hashtag}' => [Module\Api\Mastodon\Unimplemented::class, [R::GET ]], + '/timelines/tag/{hashtag}' => [Module\Api\Mastodon\Timelines\Tag::class, [R::GET ]], '/trends' => [Module\Api\Mastodon\Trends::class, [R::GET ]], ], '/v2' => [ From 0a25349fbf9e090e597dd1d3968c7614b87484eb Mon Sep 17 00:00:00 2001 From: Michael Date: Fri, 7 May 2021 21:33:42 +0000 Subject: [PATCH 02/15] Formatting --- src/Module/Api/Mastodon/Timelines/Tag.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Module/Api/Mastodon/Timelines/Tag.php b/src/Module/Api/Mastodon/Timelines/Tag.php index 28b62557b6..8e56f2f163 100644 --- a/src/Module/Api/Mastodon/Timelines/Tag.php +++ b/src/Module/Api/Mastodon/Timelines/Tag.php @@ -80,6 +80,7 @@ class Tag extends BaseApi if (!empty($min_id)) { $condition = DBA::mergeConditions($condition, ["`uri-id` > ?", $min_id]); + $params['order'] = ['uri-id']; } From 0468b7b3f9f627a65901016dd178b40dd52df0d5 Mon Sep 17 00:00:00 2001 From: Michael Date: Fri, 7 May 2021 21:35:07 +0000 Subject: [PATCH 03/15] Formatting --- src/Module/Api/Mastodon/Timelines/Home.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Module/Api/Mastodon/Timelines/Home.php b/src/Module/Api/Mastodon/Timelines/Home.php index dc1b7e0322..c46c485fa6 100644 --- a/src/Module/Api/Mastodon/Timelines/Home.php +++ b/src/Module/Api/Mastodon/Timelines/Home.php @@ -71,6 +71,7 @@ class Home extends BaseApi if (!empty($min_id)) { $condition = DBA::mergeConditions($condition, ["`uri-id` > ?", $min_id]); + $params['order'] = ['uri-id']; } From 0ea2241eed0ea9103b87ad72f0b104e37404645c Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 8 May 2021 01:52:23 +0000 Subject: [PATCH 04/15] Support the "only_media" parameter --- src/Module/Api/Mastodon/Accounts/Statuses.php | 5 +++++ src/Module/Api/Mastodon/Timelines/PublicTimeline.php | 5 +++++ src/Module/Api/Mastodon/Timelines/Tag.php | 5 +++++ 3 files changed, 15 insertions(+) diff --git a/src/Module/Api/Mastodon/Accounts/Statuses.php b/src/Module/Api/Mastodon/Accounts/Statuses.php index f6da2aebe2..14cd19e426 100644 --- a/src/Module/Api/Mastodon/Accounts/Statuses.php +++ b/src/Module/Api/Mastodon/Accounts/Statuses.php @@ -76,6 +76,11 @@ class Statuses extends BaseApi $condition = DBA::mergeConditions($condition, ["(`gravity` IN (?, ?) OR (`gravity` = ? AND `vid` = ?))", GRAVITY_PARENT, GRAVITY_COMMENT, GRAVITY_ACTIVITY, Verb::getID(Activity::ANNOUNCE)]); + if ($only_media) { + $condition = DBA::mergeConditions($condition, ["`uri-id` IN (SELECT `uri-id` FROM `post-media` WHERE `type` IN (?, ?, ?))", + Post\Media::AUDIO, Post\Media::IMAGE, Post\Media::VIDEO]); + } + if (!empty($max_id)) { $condition = DBA::mergeConditions($condition, ["`uri-id` < ?", $max_id]); } diff --git a/src/Module/Api/Mastodon/Timelines/PublicTimeline.php b/src/Module/Api/Mastodon/Timelines/PublicTimeline.php index 694ed46b88..a0a975d209 100644 --- a/src/Module/Api/Mastodon/Timelines/PublicTimeline.php +++ b/src/Module/Api/Mastodon/Timelines/PublicTimeline.php @@ -69,6 +69,11 @@ class PublicTimeline extends BaseApi $condition = DBA::mergeConditions($condition, ["NOT `uri-id` IN (SELECT `uri-id` FROM `post-user` WHERE `origin`)"]); } + if ($only_media) { + $condition = DBA::mergeConditions($condition, ["`uri-id` IN (SELECT `uri-id` FROM `post-media` WHERE `type` IN (?, ?, ?))", + Post\Media::AUDIO, Post\Media::IMAGE, Post\Media::VIDEO]); + } + if (!empty($max_id)) { $condition = DBA::mergeConditions($condition, ["`uri-id` < ?", $max_id]); } diff --git a/src/Module/Api/Mastodon/Timelines/Tag.php b/src/Module/Api/Mastodon/Timelines/Tag.php index 8e56f2f163..f61e3c3f74 100644 --- a/src/Module/Api/Mastodon/Timelines/Tag.php +++ b/src/Module/Api/Mastodon/Timelines/Tag.php @@ -70,6 +70,11 @@ class Tag extends BaseApi $condition = DBA::mergeConditions($condition, ["`uri-id` IN (SELECT `uri-id` FROM `post-user` WHERE `origin`)"]); } + if ($only_media) { + $condition = DBA::mergeConditions($condition, ["`uri-id` IN (SELECT `uri-id` FROM `post-media` WHERE `type` IN (?, ?, ?))", + Post\Media::AUDIO, Post\Media::IMAGE, Post\Media::VIDEO]); + } + if (!empty($max_id)) { $condition = DBA::mergeConditions($condition, ["`uri-id` < ?", $max_id]); } From d39983c8b7854d2087a8a648f5ef30e4ce736ead Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 8 May 2021 02:21:01 +0000 Subject: [PATCH 05/15] We now support lists as well --- .../Api/Mastodon/Timelines/ListTimeline.php | 91 +++++++++++++++++++ static/routes.config.php | 2 +- 2 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 src/Module/Api/Mastodon/Timelines/ListTimeline.php diff --git a/src/Module/Api/Mastodon/Timelines/ListTimeline.php b/src/Module/Api/Mastodon/Timelines/ListTimeline.php new file mode 100644 index 0000000000..df5c211220 --- /dev/null +++ b/src/Module/Api/Mastodon/Timelines/ListTimeline.php @@ -0,0 +1,91 @@ +. + * + */ + +namespace Friendica\Module\Api\Mastodon\Timelines; + +use Friendica\Core\System; +use Friendica\Database\DBA; +use Friendica\DI; +use Friendica\Model\Post; +use Friendica\Module\BaseApi; +use Friendica\Network\HTTPException; + +/** + * @see https://docs.joinmastodon.org/methods/timelines/ + */ +class ListTimeline extends BaseApi +{ + /** + * @param array $parameters + * @throws HTTPException\InternalServerErrorException + */ + public static function rawContent(array $parameters = []) + { + self::login(); + $uid = self::getCurrentUserID(); + + if (empty($parameters['id'])) { + DI::mstdnError()->RecordNotFound(); + } + + // Return results older than id + $max_id = (int)!isset($_REQUEST['max_id']) ? 0 : $_REQUEST['max_id']; + // Return results newer than id + $since_id = (int)!isset($_REQUEST['since_id']) ? 0 : $_REQUEST['since_id']; + // Return results immediately newer than id + $min_id = (int)!isset($_REQUEST['min_id']) ? 0 : $_REQUEST['min_id']; + // Maximum number of results to return. Defaults to 20. + $limit = (int)!isset($_REQUEST['limit']) ? 20 : $_REQUEST['limit']; + + $params = ['order' => ['uri-id' => true], 'limit' => $limit]; + + $condition = ["`uid` = ? AND `gravity` IN (?, ?) AND `contact-id` IN (SELECT `contact-id` FROM `group_member` WHERE `gid` = ?)", + $uid, GRAVITY_PARENT, GRAVITY_COMMENT, $parameters['id']]; + + if (!empty($max_id)) { + $condition = DBA::mergeConditions($condition, ["`uri-id` < ?", $max_id]); + } + + if (!empty($since_id)) { + $condition = DBA::mergeConditions($condition, ["`uri-id` > ?", $since_id]); + } + + if (!empty($min_id)) { + $condition = DBA::mergeConditions($condition, ["`uri-id` > ?", $min_id]); + + $params['order'] = ['uri-id']; + } + + $items = Post::selectForUser($uid, ['uri-id'], $condition, $params); + + $statuses = []; + while ($item = Post::fetch($items)) { + $statuses[] = DI::mstdnStatus()->createFromUriId($item['uri-id'], $uid); + } + DBA::close($items); + + if (!empty($min_id)) { + array_reverse($statuses); + } + + System::jsonExit($statuses); + } +} diff --git a/static/routes.config.php b/static/routes.config.php index dd09a99dfc..ff90dd9d58 100644 --- a/static/routes.config.php +++ b/static/routes.config.php @@ -143,7 +143,7 @@ return [ '/suggestions' => [Module\Api\Mastodon\Unimplemented::class, [R::GET ]], '/suggestions/{id:\d+}' => [Module\Api\Mastodon\Unimplemented::class, [R::DELETE ]], '/timelines/home' => [Module\Api\Mastodon\Timelines\Home::class, [R::GET ]], - '/timelines/list/{id:\d+}' => [Module\Api\Mastodon\Unimplemented::class, [R::GET ]], + '/timelines/list/{id:\d+}' => [Module\Api\Mastodon\Timelines\ListTimeline::class, [R::GET ]], '/timelines/public' => [Module\Api\Mastodon\Timelines\PublicTimeline::class, [R::GET ]], '/timelines/tag/{hashtag}' => [Module\Api\Mastodon\Timelines\Tag::class, [R::GET ]], '/trends' => [Module\Api\Mastodon\Trends::class, [R::GET ]], From 9300395c6ec2d8f1c349232ee25547c1299c150b Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 8 May 2021 02:52:44 +0000 Subject: [PATCH 06/15] "verify_credentials" is now partially supported --- .../Mastodon/Accounts/VerifyCredentials.php | 58 +++++++++++++++++++ static/routes.config.php | 2 +- 2 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 src/Module/Api/Mastodon/Accounts/VerifyCredentials.php diff --git a/src/Module/Api/Mastodon/Accounts/VerifyCredentials.php b/src/Module/Api/Mastodon/Accounts/VerifyCredentials.php new file mode 100644 index 0000000000..94fa32684d --- /dev/null +++ b/src/Module/Api/Mastodon/Accounts/VerifyCredentials.php @@ -0,0 +1,58 @@ +. + * + */ + +namespace Friendica\Module\Api\Mastodon\Accounts; + +use Friendica\Core\System; +use Friendica\DI; +use Friendica\Model\Contact; +use Friendica\Model\User; +use Friendica\Module\BaseApi; + +/** + * @see https://docs.joinmastodon.org/methods/accounts/ + */ +class VerifyCredentials extends BaseApi +{ + /** + * @param array $parameters + * @throws \Friendica\Network\HTTPException\InternalServerErrorException + */ + public static function rawContent(array $parameters = []) + { + self::login(); + $uid = self::getCurrentUserID(); + + $self = User::getOwnerDataById($uid); + if (empty($self)) { + DI::mstdnError()->RecordNotFound(); + } + + $cdata = Contact::getPublicAndUserContacID($self['id'], $uid); + if (empty($cdata)) { + DI::mstdnError()->RecordNotFound(); + } + + // @todo Support the source property, + $account = DI::mstdnAccount()->createFromContactId($cdata['user'], $uid); + System::jsonExit($account); + } +} diff --git a/static/routes.config.php b/static/routes.config.php index ff90dd9d58..e07befe027 100644 --- a/static/routes.config.php +++ b/static/routes.config.php @@ -75,7 +75,7 @@ return [ '/accounts/{id:\d+}/note' => [Module\Api\Mastodon\Unimplemented::class, [ R::POST]], '/accounts/relationships' => [Module\Api\Mastodon\Unimplemented::class, [R::GET ]], '/accounts/search' => [Module\Api\Mastodon\Unimplemented::class, [R::GET ]], - '/accounts/verify_credentials' => [Module\Api\Mastodon\Unimplemented::class, [R::GET ]], + '/accounts/verify_credentials' => [Module\Api\Mastodon\Accounts\VerifyCredentials::class, [R::GET ]], '/accounts/update_credentials' => [Module\Api\Mastodon\Unimplemented::class, [R::PATCH ]], '/admin/accounts' => [Module\Api\Mastodon\Unimplemented::class, [R::GET ]], '/admin/accounts/{id:\d+}' => [Module\Api\Mastodon\Unimplemented::class, [R::GET ]], From 4fdc1ae13ab4e584fd77846aea520ce9a8f01c90 Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 8 May 2021 05:55:06 +0000 Subject: [PATCH 07/15] "/statuses/id" is now supported --- src/Module/Api/Mastodon/Statuses.php | 45 ++++++++++++++++++++++++++++ static/routes.config.php | 2 +- 2 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 src/Module/Api/Mastodon/Statuses.php diff --git a/src/Module/Api/Mastodon/Statuses.php b/src/Module/Api/Mastodon/Statuses.php new file mode 100644 index 0000000000..ee64329bcc --- /dev/null +++ b/src/Module/Api/Mastodon/Statuses.php @@ -0,0 +1,45 @@ +. + * + */ + +namespace Friendica\Module\Api\Mastodon; + +use Friendica\Core\System; +use Friendica\DI; +use Friendica\Module\BaseApi; + +/** + * @see https://docs.joinmastodon.org/methods/statuses/ + */ +class Statuses extends BaseApi +{ + /** + * @param array $parameters + * @throws \Friendica\Network\HTTPException\InternalServerErrorException + */ + public static function rawContent(array $parameters = []) + { + if (empty($parameters['id'])) { + DI::mstdnError()->RecordNotFound(); + } + + System::jsonExit(DI::mstdnStatus()->createFromUriId($parameters['id'], self::getCurrentUserID())); + } +} diff --git a/static/routes.config.php b/static/routes.config.php index e07befe027..40c17912b4 100644 --- a/static/routes.config.php +++ b/static/routes.config.php @@ -126,7 +126,7 @@ return [ '/scheduled_statuses' => [Module\Api\Mastodon\Unimplemented::class, [R::GET ]], '/scheduled_statuses/{id:\d+}' => [Module\Api\Mastodon\Unimplemented::class, [R::GET, R::PUT, R::DELETE]], '/statuses' => [Module\Api\Mastodon\Unimplemented::class, [ R::POST]], - '/statuses/{id:\d+}' => [Module\Api\Mastodon\Unimplemented::class, [R::GET, R::DELETE]], + '/statuses/{id:\d+}' => [Module\Api\Mastodon\Statuses::class, [R::GET, R::DELETE]], '/statuses/{id:\d+}/context' => [Module\Api\Mastodon\Unimplemented::class, [R::GET ]], '/statuses/{id:\d+}/reblogged_by' => [Module\Api\Mastodon\Unimplemented::class, [R::GET ]], '/statuses/{id:\d+}/favourited_by' => [Module\Api\Mastodon\Unimplemented::class, [R::GET ]], From 8ae2e3caf880fd5d089581ea12c7904dec413276 Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 8 May 2021 08:39:00 +0000 Subject: [PATCH 08/15] Changed documentation --- doc/API-Mastodon.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/doc/API-Mastodon.md b/doc/API-Mastodon.md index 588947cc3a..e72b0c31a7 100644 --- a/doc/API-Mastodon.md +++ b/doc/API-Mastodon.md @@ -17,6 +17,7 @@ These endpoints use the [Mastodon API entities](https://docs.joinmastodon.org/en - [`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/#retrieve-information) +- [`GET /api/v1//accounts/verify_credentials`](https://docs.joinmastodon.org/methods/accounts) - [`GET /api/v1/custom_emojis`](https://docs.joinmastodon.org/methods/instance/custom_emojis/) - Doesn't return unicode emojis since they aren't using an image URL @@ -36,7 +37,11 @@ These endpoints use the [Mastodon API entities](https://docs.joinmastodon.org/en - [`GET /api/v1/instance`](https://docs.joinmastodon.org/methods/instance#fetch-instance) - [`GET /api/v1/instance/peers`](https://docs.joinmastodon.org/methods/instance#list-of-connected-domains) +- [`GET /api/v1/statuses/:id`](https://docs.joinmastodon.org/methods/statuses/) +- [`GET /api/v1/timelines/home`](https://docs.joinmastodon.org/methods/timelines/) +- [`GET /api/v1/timelines/list/:id`](https://docs.joinmastodon.org/methods/timelines/) - [`GET /api/v1/timelines/public`](https://docs.joinmastodon.org/methods/timelines/) +- [`GET /api/v1/timelines/tag/:hashtag`](https://docs.joinmastodon.org/methods/timelines/) - [`GET /api/v1/trends`](https://docs.joinmastodon.org/methods/instance/trends/) ## Non-implemented endpoints From 6236870aa4402fb63dd20f5fd81bccb5d6c71ad3 Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 8 May 2021 09:14:19 +0000 Subject: [PATCH 09/15] Add all required HTTP methods --- src/App/Module.php | 14 ++++++- src/BaseModule.php | 30 +++++++++++++ src/Module/Api/Mastodon/Statuses.php | 6 +++ src/Module/Api/Mastodon/Unimplemented.php | 46 ++++++++++++++++---- src/Module/BaseApi.php | 51 +++++++++++++++++++++++ 5 files changed, 137 insertions(+), 10 deletions(-) diff --git a/src/App/Module.php b/src/App/Module.php index 4771ecf589..7ad4261aa6 100644 --- a/src/App/Module.php +++ b/src/App/Module.php @@ -276,11 +276,23 @@ class Module $profiler->set(microtime(true) - $timestamp, 'init'); - if ($server['REQUEST_METHOD'] === 'POST') { + if ($server['REQUEST_METHOD'] === Router::DELETE) { + call_user_func([$this->module_class, 'delete'], $this->module_parameters); + } + + if ($server['REQUEST_METHOD'] === Router::PATCH) { + call_user_func([$this->module_class, 'patch'], $this->module_parameters); + } + + if ($server['REQUEST_METHOD'] === Router::POST) { Core\Hook::callAll($this->module . '_mod_post', $post); call_user_func([$this->module_class, 'post'], $this->module_parameters); } + if ($server['REQUEST_METHOD'] === Router::PUT) { + call_user_func([$this->module_class, 'put'], $this->module_parameters); + } + Core\Hook::callAll($this->module . '_mod_afterpost', $placeholder); call_user_func([$this->module_class, 'afterpost'], $this->module_parameters); diff --git a/src/BaseModule.php b/src/BaseModule.php index 19b58ad066..cb8f8c790d 100644 --- a/src/BaseModule.php +++ b/src/BaseModule.php @@ -72,6 +72,26 @@ abstract class BaseModule return $o; } + /** + * Module DELETE method to process submitted data + * + * Extend this method if the module is supposed to process DELETE requests. + * Doesn't display any content + */ + public static function delete(array $parameters = []) + { + } + + /** + * Module PATCH method to process submitted data + * + * Extend this method if the module is supposed to process PATCH requests. + * Doesn't display any content + */ + public static function patch(array $parameters = []) + { + } + /** * Module POST method to process submitted data * @@ -92,6 +112,16 @@ abstract class BaseModule { } + /** + * Module PUT method to process submitted data + * + * Extend this method if the module is supposed to process PUT requests. + * Doesn't display any content + */ + public static function put(array $parameters = []) + { + } + /* * Functions used to protect against Cross-Site Request Forgery * The security token has to base on at least one value that an attacker can't know - here it's the session ID and the private key. diff --git a/src/Module/Api/Mastodon/Statuses.php b/src/Module/Api/Mastodon/Statuses.php index ee64329bcc..d14ba1a063 100644 --- a/src/Module/Api/Mastodon/Statuses.php +++ b/src/Module/Api/Mastodon/Statuses.php @@ -21,6 +21,7 @@ namespace Friendica\Module\Api\Mastodon; +use Friendica\Core\Logger; use Friendica\Core\System; use Friendica\DI; use Friendica\Module\BaseApi; @@ -30,6 +31,11 @@ use Friendica\Module\BaseApi; */ class Statuses extends BaseApi { + public static function delete(array $parameters = []) + { + self::unsupported('delete'); + } + /** * @param array $parameters * @throws \Friendica\Network\HTTPException\InternalServerErrorException diff --git a/src/Module/Api/Mastodon/Unimplemented.php b/src/Module/Api/Mastodon/Unimplemented.php index 082bd38f0e..fa9618818a 100644 --- a/src/Module/Api/Mastodon/Unimplemented.php +++ b/src/Module/Api/Mastodon/Unimplemented.php @@ -21,9 +21,6 @@ namespace Friendica\Module\Api\Mastodon; -use Friendica\Core\Logger; -use Friendica\Core\System; -use Friendica\DI; use Friendica\Module\BaseApi; /** @@ -31,17 +28,48 @@ use Friendica\Module\BaseApi; */ class Unimplemented extends BaseApi { + /** + * @param array $parameters + * @throws \Friendica\Network\HTTPException\InternalServerErrorException + */ + public static function delete(array $parameters = []) + { + self::unsupported('delete'); + } + + /** + * @param array $parameters + * @throws \Friendica\Network\HTTPException\InternalServerErrorException + */ + public static function patch(array $parameters = []) + { + self::unsupported('patch'); + } + + /** + * @param array $parameters + * @throws \Friendica\Network\HTTPException\InternalServerErrorException + */ + public static function post(array $parameters = []) + { + self::unsupported('post'); + } + + /** + * @param array $parameters + * @throws \Friendica\Network\HTTPException\InternalServerErrorException + */ + public static function put(array $parameters = []) + { + self::unsupported('put'); + } + /** * @param array $parameters * @throws \Friendica\Network\HTTPException\InternalServerErrorException */ public static function rawContent(array $parameters = []) { - $path = DI::args()->getQueryString(); - Logger::info('Unimplemented API call', ['path' => $path]); - $error = DI::l10n()->t('API endpoint "%s" is not implemented', $path); - $error_description = DI::l10n()->t('The API endpoint is currently not implemented but might be in the future.');; - $errorobj = new \Friendica\Object\Api\Mastodon\Error($error, $error_description); - System::jsonError(501, $errorobj->toArray()); + self::unsupported('get'); } } diff --git a/src/Module/BaseApi.php b/src/Module/BaseApi.php index c161159e26..f6146ac56a 100644 --- a/src/Module/BaseApi.php +++ b/src/Module/BaseApi.php @@ -22,6 +22,8 @@ namespace Friendica\Module; use Friendica\BaseModule; +use Friendica\Core\Logger; +use Friendica\Core\System; use Friendica\DI; use Friendica\Network\HTTPException; @@ -53,6 +55,32 @@ class BaseApi extends BaseModule } } + public static function delete(array $parameters = []) + { + if (!api_user()) { + throw new HTTPException\UnauthorizedException(DI::l10n()->t('Permission denied.')); + } + + $a = DI::app(); + + if (!empty($a->user['uid']) && $a->user['uid'] != api_user()) { + throw new HTTPException\ForbiddenException(DI::l10n()->t('Permission denied.')); + } + } + + public static function patch(array $parameters = []) + { + if (!api_user()) { + throw new HTTPException\UnauthorizedException(DI::l10n()->t('Permission denied.')); + } + + $a = DI::app(); + + if (!empty($a->user['uid']) && $a->user['uid'] != api_user()) { + throw new HTTPException\ForbiddenException(DI::l10n()->t('Permission denied.')); + } + } + public static function post(array $parameters = []) { if (!api_user()) { @@ -66,6 +94,29 @@ class BaseApi extends BaseModule } } + public static function put(array $parameters = []) + { + if (!api_user()) { + throw new HTTPException\UnauthorizedException(DI::l10n()->t('Permission denied.')); + } + + $a = DI::app(); + + if (!empty($a->user['uid']) && $a->user['uid'] != api_user()) { + throw new HTTPException\ForbiddenException(DI::l10n()->t('Permission denied.')); + } + } + + public static function unsupported(string $method = 'all') + { + $path = DI::args()->getQueryString(); + Logger::info('Unimplemented API call', ['path' => $path, 'method' => $method]); + $error = DI::l10n()->t('API endpoint %s "%s" is not implemented', $method, $path); + $error_description = DI::l10n()->t('The API endpoint is currently not implemented but might be in the future.');; + $errorobj = new \Friendica\Object\Api\Mastodon\Error($error, $error_description); + System::jsonError(501, $errorobj->toArray()); + } + /** * Log in user via OAuth1 or Simple HTTP Auth. * From c97cfd8a4487a472a0bb19f38d7b827571a119a9 Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 8 May 2021 09:19:16 +0000 Subject: [PATCH 10/15] Removed unused use --- src/Module/Api/Mastodon/Statuses.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Module/Api/Mastodon/Statuses.php b/src/Module/Api/Mastodon/Statuses.php index d14ba1a063..042d038a25 100644 --- a/src/Module/Api/Mastodon/Statuses.php +++ b/src/Module/Api/Mastodon/Statuses.php @@ -21,7 +21,6 @@ namespace Friendica\Module\Api\Mastodon; -use Friendica\Core\Logger; use Friendica\Core\System; use Friendica\DI; use Friendica\Module\BaseApi; From d842a4ff871f5d9a43f7ee661c06b858ca4e7776 Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 8 May 2021 11:03:50 +0000 Subject: [PATCH 11/15] Suggestions are now supported as well --- doc/API-Mastodon.md | 1 + src/Model/Post/Media.php | 18 +++++++- src/Module/Api/Mastodon/Suggestions.php | 56 +++++++++++++++++++++++++ static/routes.config.php | 2 +- 4 files changed, 75 insertions(+), 2 deletions(-) create mode 100644 src/Module/Api/Mastodon/Suggestions.php diff --git a/doc/API-Mastodon.md b/doc/API-Mastodon.md index e72b0c31a7..574c5eb7f9 100644 --- a/doc/API-Mastodon.md +++ b/doc/API-Mastodon.md @@ -38,6 +38,7 @@ These endpoints use the [Mastodon API entities](https://docs.joinmastodon.org/en - [`GET /api/v1/instance`](https://docs.joinmastodon.org/methods/instance#fetch-instance) - [`GET /api/v1/instance/peers`](https://docs.joinmastodon.org/methods/instance#list-of-connected-domains) - [`GET /api/v1/statuses/:id`](https://docs.joinmastodon.org/methods/statuses/) +- [`GET /api/v1/suggestions`](https://docs.joinmastodon.org/methods/accounts/suggestions/) - [`GET /api/v1/timelines/home`](https://docs.joinmastodon.org/methods/timelines/) - [`GET /api/v1/timelines/list/:id`](https://docs.joinmastodon.org/methods/timelines/) - [`GET /api/v1/timelines/public`](https://docs.joinmastodon.org/methods/timelines/) diff --git a/src/Model/Post/Media.php b/src/Model/Post/Media.php index b975a09022..6f78b09051 100644 --- a/src/Model/Post/Media.php +++ b/src/Model/Post/Media.php @@ -21,7 +21,6 @@ namespace Friendica\Model\Post; -use Friendica\Content\PageInfo; use Friendica\Content\Text\BBCode; use Friendica\Core\Logger; use Friendica\Core\System; @@ -67,6 +66,11 @@ class Media return; } + if (DBA::exists('post-media', ['uri-id' => $media['uri-id'], 'preview' => $media['url']])) { + Logger::info('Media already exists as preview', ['uri-id' => $media['uri-id'], 'url' => $media['url'], 'callstack' => System::callstack()]); + return; + } + // "document" has got the lowest priority. So when the same file is both attached as document // and embedded as picture then we only store the picture or replace the document $found = DBA::selectFirst('post-media', ['type'], ['uri-id' => $media['uri-id'], 'url' => $media['url']]); @@ -499,6 +503,7 @@ class Media $height = 0; $selected = ''; + $previews = []; foreach ($media as $medium) { foreach ($links as $link) { @@ -507,6 +512,17 @@ class Media } } + // Avoid adding separate media entries for previews + foreach ($previews as $preview) { + if (Strings::compareLink($preview, $medium['url'])) { + continue 2; + } + } + + if (!empty($medium['preview'])) { + $previews[] = $medium['preview']; + } + $type = explode('/', current(explode(';', $medium['mimetype']))); if (count($type) < 2) { Logger::info('Unknown MimeType', ['type' => $type, 'media' => $medium]); diff --git a/src/Module/Api/Mastodon/Suggestions.php b/src/Module/Api/Mastodon/Suggestions.php new file mode 100644 index 0000000000..97cfa11d3e --- /dev/null +++ b/src/Module/Api/Mastodon/Suggestions.php @@ -0,0 +1,56 @@ +. + * + */ + +namespace Friendica\Module\Api\Mastodon; + +use Friendica\Core\System; +use Friendica\DI; +use Friendica\Model\Contact; +use Friendica\Module\BaseApi; + +/** + * @see https://docs.joinmastodon.org/methods/accounts/suggestions/ + */ +class Suggestions extends BaseApi +{ + /** + * @param array $parameters + * @throws \Friendica\Network\HTTPException\InternalServerErrorException + */ + public static function rawContent(array $parameters = []) + { + self::login(); + $uid = self::getCurrentUserID(); + + // Maximum number of results to return. Defaults to 40. + $limit = (int)!isset($_REQUEST['limit']) ? 40 : $_REQUEST['limit']; + + $suggestions = Contact\Relation::getSuggestions($uid, 0, $limit); + + $accounts = []; + + foreach ($suggestions as $suggestion) { + $accounts[] = DI::mstdnAccount()->createFromContactId($suggestion['id'], $uid); + } + + System::jsonExit($accounts); + } +} diff --git a/static/routes.config.php b/static/routes.config.php index 40c17912b4..6ca57082e5 100644 --- a/static/routes.config.php +++ b/static/routes.config.php @@ -140,7 +140,7 @@ return [ '/statuses/{id:\d+}/unmute' => [Module\Api\Mastodon\Unimplemented::class, [ R::POST]], '/statuses/{id:\d+}/pin' => [Module\Api\Mastodon\Unimplemented::class, [ R::POST]], '/statuses/{id:\d+}/unpin' => [Module\Api\Mastodon\Unimplemented::class, [ R::POST]], - '/suggestions' => [Module\Api\Mastodon\Unimplemented::class, [R::GET ]], + '/suggestions' => [Module\Api\Mastodon\Suggestions::class, [R::GET ]], '/suggestions/{id:\d+}' => [Module\Api\Mastodon\Unimplemented::class, [R::DELETE ]], '/timelines/home' => [Module\Api\Mastodon\Timelines\Home::class, [R::GET ]], '/timelines/list/{id:\d+}' => [Module\Api\Mastodon\Timelines\ListTimeline::class, [R::GET ]], From 054f6dedc1344a91b7c8d965d344d7a58aef57b2 Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 8 May 2021 11:46:24 +0000 Subject: [PATCH 12/15] /followes and /following is supported --- .../Api/Mastodon/Accounts/Followers.php | 85 +++++++++++++++++++ .../Api/Mastodon/Accounts/Following.php | 85 +++++++++++++++++++ static/routes.config.php | 4 +- 3 files changed, 172 insertions(+), 2 deletions(-) create mode 100644 src/Module/Api/Mastodon/Accounts/Followers.php create mode 100644 src/Module/Api/Mastodon/Accounts/Following.php diff --git a/src/Module/Api/Mastodon/Accounts/Followers.php b/src/Module/Api/Mastodon/Accounts/Followers.php new file mode 100644 index 0000000000..69e4e95eb1 --- /dev/null +++ b/src/Module/Api/Mastodon/Accounts/Followers.php @@ -0,0 +1,85 @@ +. + * + */ + +namespace Friendica\Module\Api\Mastodon\Accounts; + +use Friendica\Core\System; +use Friendica\Database\DBA; +use Friendica\DI; +use Friendica\Module\BaseApi; + +/** + * @see https://docs.joinmastodon.org/methods/accounts/ + */ +class Followers 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()->RecordNotFound(); + } + + $id = $parameters['id']; + if (!DBA::exists('contact', ['id' => $id, 'uid' => 0])) { + DI::mstdnError()->RecordNotFound(); + } + + // Return results older than this id + $max_id = (int)!isset($_REQUEST['max_id']) ? 0 : $_REQUEST['max_id']; + // Return results newer than this id + $since_id = (int)!isset($_REQUEST['since_id']) ? 0 : $_REQUEST['since_id']; + // Maximum number of results to return. Defaults to 20. + $limit = (int)!isset($_REQUEST['limit']) ? 20 : $_REQUEST['limit']; + + + $params = ['order' => ['cid' => true], 'limit' => $limit]; + + $condition = ['relation-cid' => $id, 'follows' => true]; + + if (!empty($max_id)) { + $condition = DBA::mergeConditions($condition, ["`cid` < ?", $max_id]); + } + + if (!empty($since_id)) { + $condition = DBA::mergeConditions($condition, ["`cid` > ?", $since_id]); + } + + if (!empty($min_id)) { + $condition = DBA::mergeConditions($condition, ["`cid` > ?", $min_id]); + $params['order'] = ['cid']; + } + + $followers = DBA::select('contact-relation', ['cid'], $condition, $parameters); + while ($follower = DBA::fetch($followers)) { + $accounts[] = DI::mstdnAccount()->createFromContactId($follower['cid'], $uid); + } + DBA::close($followers); + + System::jsonExit($accounts); + } +} diff --git a/src/Module/Api/Mastodon/Accounts/Following.php b/src/Module/Api/Mastodon/Accounts/Following.php new file mode 100644 index 0000000000..c36c990c95 --- /dev/null +++ b/src/Module/Api/Mastodon/Accounts/Following.php @@ -0,0 +1,85 @@ +. + * + */ + +namespace Friendica\Module\Api\Mastodon\Accounts; + +use Friendica\Core\System; +use Friendica\Database\DBA; +use Friendica\DI; +use Friendica\Module\BaseApi; + +/** + * @see https://docs.joinmastodon.org/methods/accounts/ + */ +class Following 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()->RecordNotFound(); + } + + $id = $parameters['id']; + if (!DBA::exists('contact', ['id' => $id, 'uid' => 0])) { + DI::mstdnError()->RecordNotFound(); + } + + // Return results older than this id + $max_id = (int)!isset($_REQUEST['max_id']) ? 0 : $_REQUEST['max_id']; + // Return results newer than this id + $since_id = (int)!isset($_REQUEST['since_id']) ? 0 : $_REQUEST['since_id']; + // Maximum number of results to return. Defaults to 20. + $limit = (int)!isset($_REQUEST['limit']) ? 20 : $_REQUEST['limit']; + + + $params = ['order' => ['relation-cid' => true], 'limit' => $limit]; + + $condition = ['cid' => $id, 'follows' => true]; + + if (!empty($max_id)) { + $condition = DBA::mergeConditions($condition, ["`relation-cid` < ?", $max_id]); + } + + if (!empty($since_id)) { + $condition = DBA::mergeConditions($condition, ["`relation-cid` > ?", $since_id]); + } + + if (!empty($min_id)) { + $condition = DBA::mergeConditions($condition, ["`relation-cid` > ?", $min_id]); + $params['order'] = ['cid']; + } + + $followers = DBA::select('contact-relation', ['relation-cid'], $condition, $parameters); + while ($follower = DBA::fetch($followers)) { + $accounts[] = DI::mstdnAccount()->createFromContactId($follower['relation-cid'], $uid); + } + DBA::close($followers); + + System::jsonExit($accounts); + } +} diff --git a/static/routes.config.php b/static/routes.config.php index 6ca57082e5..d661695590 100644 --- a/static/routes.config.php +++ b/static/routes.config.php @@ -60,8 +60,8 @@ return [ '/accounts' => [Module\Api\Mastodon\Unimplemented::class, [ R::POST]], '/accounts/{id:\d+}' => [Module\Api\Mastodon\Accounts::class, [R::GET ]], '/accounts/{id:\d+}/statuses' => [Module\Api\Mastodon\Accounts\Statuses::class, [R::GET ]], - '/accounts/{id:\d+}/followers' => [Module\Api\Mastodon\Unimplemented::class, [R::GET ]], - '/accounts/{id:\d+}/following' => [Module\Api\Mastodon\Unimplemented::class, [R::GET ]], + '/accounts/{id:\d+}/followers' => [Module\Api\Mastodon\Accounts\Followers::class, [R::GET ]], + '/accounts/{id:\d+}/following' => [Module\Api\Mastodon\Accounts\Followers::class, [R::GET ]], '/accounts/{id:\d+}/lists' => [Module\Api\Mastodon\Unimplemented::class, [R::GET ]], '/accounts/{id:\d+}/identity_proofs' => [Module\Api\Mastodon\Unimplemented::class, [R::GET ]], '/accounts/{id:\d+}/follow' => [Module\Api\Mastodon\Unimplemented::class, [ R::POST]], From 31d70e02e0e3c7f5b0329904cac0eb020f5164f1 Mon Sep 17 00:00:00 2001 From: Michael Vogel Date: Sat, 8 May 2021 14:23:47 +0200 Subject: [PATCH 13/15] Update src/Module/BaseApi.php Co-authored-by: Hypolite Petovan --- src/Module/BaseApi.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Module/BaseApi.php b/src/Module/BaseApi.php index f6146ac56a..c3f11b0eb9 100644 --- a/src/Module/BaseApi.php +++ b/src/Module/BaseApi.php @@ -111,7 +111,7 @@ class BaseApi extends BaseModule { $path = DI::args()->getQueryString(); Logger::info('Unimplemented API call', ['path' => $path, 'method' => $method]); - $error = DI::l10n()->t('API endpoint %s "%s" is not implemented', $method, $path); + $error = DI::l10n()->t('API endpoint %s %s is not implemented', strtoupper($method), $path); $error_description = DI::l10n()->t('The API endpoint is currently not implemented but might be in the future.');; $errorobj = new \Friendica\Object\Api\Mastodon\Error($error, $error_description); System::jsonError(501, $errorobj->toArray()); From dcff688efb58cfe901a2b78cfd93f9d04501ed24 Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 8 May 2021 12:25:52 +0000 Subject: [PATCH 14/15] messages.po updated --- view/lang/C/messages.po | 83 +++++++++++++++++++++-------------------- 1 file changed, 43 insertions(+), 40 deletions(-) diff --git a/view/lang/C/messages.po b/view/lang/C/messages.po index ca67882c86..2cf3158541 100644 --- a/view/lang/C/messages.po +++ b/view/lang/C/messages.po @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: 2021.06-dev\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2021-05-04 09:08-0400\n" +"POT-Creation-Date: 2021-05-08 12:25+0000\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -829,7 +829,10 @@ msgstr "" #: mod/unfollow.php:82 mod/wall_attach.php:78 mod/wall_attach.php:81 #: mod/wall_upload.php:99 mod/wall_upload.php:102 mod/wallmessage.php:35 #: mod/wallmessage.php:59 mod/wallmessage.php:96 mod/wallmessage.php:120 -#: src/Module/Attach.php:56 src/Module/BaseApi.php:59 src/Module/BaseApi.php:65 +#: src/Module/Attach.php:56 src/Module/BaseApi.php:61 src/Module/BaseApi.php:67 +#: src/Module/BaseApi.php:74 src/Module/BaseApi.php:80 +#: src/Module/BaseApi.php:87 src/Module/BaseApi.php:93 +#: src/Module/BaseApi.php:100 src/Module/BaseApi.php:106 #: src/Module/BaseNotifications.php:88 src/Module/Contact.php:385 #: src/Module/Contact/Advanced.php:43 src/Module/Delegation.php:118 #: src/Module/FollowConfirm.php:16 src/Module/FriendSuggest.php:44 @@ -1163,8 +1166,8 @@ msgstr "" #: mod/dfrn_request.php:600 mod/display.php:179 mod/photos.php:836 #: mod/videos.php:129 src/Module/Conversation/Community.php:188 #: src/Module/Debug/Probe.php:39 src/Module/Debug/WebFinger.php:38 -#: src/Module/Directory.php:49 src/Module/Search/Index.php:51 -#: src/Module/Search/Index.php:56 +#: src/Module/Directory.php:49 src/Module/Search/Index.php:50 +#: src/Module/Search/Index.php:55 msgid "Public access denied." msgstr "" @@ -3043,31 +3046,31 @@ msgstr "" msgid "Page not found." msgstr "" -#: src/BaseModule.php:150 +#: src/BaseModule.php:180 msgid "" "The form security token was not correct. This probably happened because the " "form has been opened for too long (>3 hours) before submitting it." msgstr "" -#: src/BaseModule.php:179 +#: src/BaseModule.php:209 msgid "All contacts" msgstr "" -#: src/BaseModule.php:184 src/Content/Widget.php:238 src/Core/ACL.php:183 +#: src/BaseModule.php:214 src/Content/Widget.php:238 src/Core/ACL.php:183 #: src/Module/Contact.php:860 src/Module/PermissionTooltip.php:77 #: src/Module/PermissionTooltip.php:99 msgid "Followers" msgstr "" -#: src/BaseModule.php:189 src/Content/Widget.php:239 src/Module/Contact.php:861 +#: src/BaseModule.php:219 src/Content/Widget.php:239 src/Module/Contact.php:861 msgid "Following" msgstr "" -#: src/BaseModule.php:194 src/Content/Widget.php:240 src/Module/Contact.php:862 +#: src/BaseModule.php:224 src/Content/Widget.php:240 src/Module/Contact.php:862 msgid "Mutual friends" msgstr "" -#: src/BaseModule.php:202 +#: src/BaseModule.php:232 msgid "Common" msgstr "" @@ -3504,7 +3507,7 @@ msgid "Addon applications, utilities, games" msgstr "" #: src/Content/Nav.php:220 src/Content/Text/HTML.php:899 -#: src/Module/Search/Index.php:100 +#: src/Module/Search/Index.php:99 msgid "Search" msgstr "" @@ -3662,39 +3665,39 @@ msgstr "" msgid "last" msgstr "" -#: src/Content/Text/BBCode.php:941 src/Content/Text/BBCode.php:1615 -#: src/Content/Text/BBCode.php:1616 +#: src/Content/Text/BBCode.php:942 src/Content/Text/BBCode.php:1605 +#: src/Content/Text/BBCode.php:1606 msgid "Image/photo" msgstr "" -#: src/Content/Text/BBCode.php:1063 +#: src/Content/Text/BBCode.php:1064 #, php-format msgid "" "%2$s %3$s" msgstr "" -#: src/Content/Text/BBCode.php:1088 src/Model/Item.php:3020 -#: src/Model/Item.php:3026 +#: src/Content/Text/BBCode.php:1089 src/Model/Item.php:3024 +#: src/Model/Item.php:3030 msgid "link to source" msgstr "" -#: src/Content/Text/BBCode.php:1533 src/Content/Text/HTML.php:951 +#: src/Content/Text/BBCode.php:1523 src/Content/Text/HTML.php:951 msgid "Click to open/close" msgstr "" -#: src/Content/Text/BBCode.php:1564 +#: src/Content/Text/BBCode.php:1554 msgid "$1 wrote:" msgstr "" -#: src/Content/Text/BBCode.php:1618 src/Content/Text/BBCode.php:1619 +#: src/Content/Text/BBCode.php:1608 src/Content/Text/BBCode.php:1609 msgid "Encrypted content" msgstr "" -#: src/Content/Text/BBCode.php:1832 +#: src/Content/Text/BBCode.php:1822 msgid "Invalid source protocol" msgstr "" -#: src/Content/Text/BBCode.php:1847 +#: src/Content/Text/BBCode.php:1837 msgid "Invalid link protocol" msgstr "" @@ -4846,15 +4849,15 @@ msgstr "" msgid "Content warning: %s" msgstr "" -#: src/Model/Item.php:2985 +#: src/Model/Item.php:2989 msgid "bytes" msgstr "" -#: src/Model/Item.php:3014 +#: src/Model/Item.php:3018 msgid "View on separate page" msgstr "" -#: src/Model/Item.php:3015 +#: src/Model/Item.php:3019 msgid "view on separate page" msgstr "" @@ -7218,16 +7221,6 @@ msgstr "" msgid "Deny" msgstr "" -#: src/Module/Api/Mastodon/Unimplemented.php:42 -#, php-format -msgid "API endpoint \"%s\" is not implemented" -msgstr "" - -#: src/Module/Api/Mastodon/Unimplemented.php:43 -msgid "" -"The API endpoint is currently not implemented but might be in the future." -msgstr "" - #: src/Module/Api/Twitter/ContactEndpoint.php:65 src/Module/Contact.php:400 msgid "Contact not found" msgstr "" @@ -7330,6 +7323,16 @@ msgstr "" msgid "User registrations waiting for confirmation" msgstr "" +#: src/Module/BaseApi.php:114 +#, php-format +msgid "API endpoint %s %s is not implemented" +msgstr "" + +#: src/Module/BaseApi.php:115 +msgid "" +"The API endpoint is currently not implemented but might be in the future." +msgstr "" + #: src/Module/BaseProfile.php:55 src/Module/Contact.php:947 msgid "Profile Details" msgstr "" @@ -7684,7 +7687,7 @@ msgstr "" msgid "Search your contacts" msgstr "" -#: src/Module/Contact.php:883 src/Module/Search/Index.php:193 +#: src/Module/Contact.php:883 src/Module/Search/Index.php:192 #, php-format msgid "Results for: %s" msgstr "" @@ -7924,8 +7927,8 @@ msgstr "" msgid "Hide" msgstr "" -#: src/Module/Conversation/Community.php:149 src/Module/Search/Index.php:140 -#: src/Module/Search/Index.php:180 +#: src/Module/Conversation/Community.php:149 src/Module/Search/Index.php:139 +#: src/Module/Search/Index.php:179 msgid "No results." msgstr "" @@ -9107,15 +9110,15 @@ msgid "" "or %s directly on your system." msgstr "" -#: src/Module/Search/Index.php:55 +#: src/Module/Search/Index.php:54 msgid "Only logged in users are permitted to perform a search." msgstr "" -#: src/Module/Search/Index.php:77 +#: src/Module/Search/Index.php:76 msgid "Only one search per minute is permitted for not logged in users." msgstr "" -#: src/Module/Search/Index.php:191 +#: src/Module/Search/Index.php:190 #, php-format msgid "Items tagged with: %s" msgstr "" From 7d1c43cf651c161e922bff0943961e458a562655 Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 8 May 2021 12:28:04 +0000 Subject: [PATCH 15/15] Formatting --- src/Module/Api/Mastodon/Accounts/Followers.php | 1 + src/Module/Api/Mastodon/Accounts/Following.php | 1 + 2 files changed, 2 insertions(+) diff --git a/src/Module/Api/Mastodon/Accounts/Followers.php b/src/Module/Api/Mastodon/Accounts/Followers.php index 69e4e95eb1..739341a2fa 100644 --- a/src/Module/Api/Mastodon/Accounts/Followers.php +++ b/src/Module/Api/Mastodon/Accounts/Followers.php @@ -71,6 +71,7 @@ class Followers extends BaseApi if (!empty($min_id)) { $condition = DBA::mergeConditions($condition, ["`cid` > ?", $min_id]); + $params['order'] = ['cid']; } diff --git a/src/Module/Api/Mastodon/Accounts/Following.php b/src/Module/Api/Mastodon/Accounts/Following.php index c36c990c95..10d73e9071 100644 --- a/src/Module/Api/Mastodon/Accounts/Following.php +++ b/src/Module/Api/Mastodon/Accounts/Following.php @@ -71,6 +71,7 @@ class Following extends BaseApi if (!empty($min_id)) { $condition = DBA::mergeConditions($condition, ["`relation-cid` > ?", $min_id]); + $params['order'] = ['cid']; }