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 ]],