]> git.mxchange.org Git - friendica.git/commitdiff
Media is now supported as well
authorMichael <heluecht@pirati.ca>
Sun, 9 May 2021 12:59:23 +0000 (12:59 +0000)
committerMichael <heluecht@pirati.ca>
Sun, 9 May 2021 12:59:23 +0000 (12:59 +0000)
doc/API-Mastodon.md
src/Factory/Api/Mastodon/Attachment.php
src/Module/Api/Mastodon/Media.php [new file with mode: 0644]
static/routes.config.php

index 2efa9a9ffae7b2fe5a4f3141ba0a32b8ebd38a00..7f5b55797aeae5f02ab088601f64f7b55dbd4937 100644 (file)
@@ -47,6 +47,7 @@ These endpoints use the [Mastodon API entities](https://docs.joinmastodon.org/en
 - [`GET /api/v1/lists`](https://docs.joinmastodon.org/methods/timelines/lists/)
 - [`GET /api/v1/lists/:id`](https://docs.joinmastodon.org/methods/timelines/lists/)
 - [`GET /api/v1/lists/:id/accounts`](https://docs.joinmastodon.org/methods/timelines/lists/)
+- [`GET /api/v1/media/:id`](https://docs.joinmastodon.org/methods/statuses/media/)
 - [`GET /api/v1/mutes`](https://docs.joinmastodon.org/methods/accounts/mutes/)
 - [`GET /api/v1/notifications`](https://docs.joinmastodon.org/methods/notifications/)
 - [`GET /api/v1/notifications/:id`](https://docs.joinmastodon.org/methods/notifications/)
index 893b4c9f95e53db3c03e3a5cd9ae8da450085dfb..7ac45f354e65e4a206aac5bfb34f820e08829f29 100644 (file)
@@ -23,9 +23,12 @@ namespace Friendica\Factory\Api\Mastodon;
 
 use Friendica\App\BaseURL;
 use Friendica\BaseFactory;
+use Friendica\DI;
+use Friendica\Model\Photo;
 use Friendica\Network\HTTPException;
 use Friendica\Model\Post;
 use Friendica\Repository\ProfileField;
+use Friendica\Util\Images;
 use Friendica\Util\Proxy;
 use Psr\Log\LoggerInterface;
 
@@ -93,4 +96,36 @@ class Attachment extends BaseFactory
 
                return $attachments;
        }
+
+       /**
+        * @param int $id id of the photo
+        * @return array
+        * @throws HTTPException\InternalServerErrorException
+        * @throws \ImagickException
+        */
+       public function createFromPhoto(int $id)
+       {
+               $photo = Photo::selectFirst(['resource-id', 'uid', 'id', 'title', 'type'], ['id' => $id]);
+               if (empty($photo)) {
+                       return null;
+               }
+
+               $attachment = ['id' => $photo['id'], 'description' => $photo['title']];
+
+               $phototypes = Images::supportedTypes();
+               $ext = $phototypes[$photo['type']];
+
+               $url = DI::baseUrl() . '/photo/' . $photo['resource-id'] . '-0.' . $ext;
+
+               $preview = Photo::selectFirst(['scale'], ["`resource-id` = ? AND `uid` = ? AND `scale` > ?", $photo['resource-id'], $photo['uid'], 0], ['order' => ['scale']]);
+               if (empty($scale)) {
+                       $preview_url = DI::baseUrl() . '/photo/' . $photo['resource-id'] . '-' . $preview['scale'] . '.' . $ext;
+               } else {
+                       $preview_url = '';
+               }
+
+
+               $object = new \Friendica\Object\Api\Mastodon\Attachment($attachment, 'image', $url, $preview_url, '');
+               return $object->toArray();
+       }
 }
diff --git a/src/Module/Api/Mastodon/Media.php b/src/Module/Api/Mastodon/Media.php
new file mode 100644 (file)
index 0000000..c5fb82a
--- /dev/null
@@ -0,0 +1,62 @@
+<?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;
+
+use Friendica\Core\System;
+use Friendica\Database\DBA;
+use Friendica\DI;
+use Friendica\Model\Contact;
+use Friendica\Model\Notification;
+use Friendica\Model\Photo;
+use Friendica\Module\BaseApi;
+
+/**
+ * @see https://docs.joinmastodon.org/methods/statuses/media/
+ */
+class Media extends BaseApi
+{
+       public static function put(array $parameters = [])
+       {
+               self::unsupported('put');
+       }
+
+       /**
+        * @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 (!Photo::exists(['id' => $id, 'uid' => $uid])) {
+                       DI::mstdnError()->RecordNotFound();
+               }
+
+               System::jsonExit(DI::mstdnAttachment()->createFromPhoto($id));
+       }
+}
index e00a7f0e3400e44fe0e5495561df77efbffd7947..a21ff336da81f61d87c181ecfcebcc1183ec9275 100644 (file)
@@ -90,9 +90,9 @@ return [
                        '/apps/verify_credentials'           => [Module\Api\Mastodon\Unimplemented::class,            [R::GET         ]],
                        '/blocks'                            => [Module\Api\Mastodon\Blocks::class,                   [R::GET         ]],
                        '/bookmarks'                         => [Module\Api\Mastodon\Bookmarks::class,                [R::GET         ]],
-                       '/conversations'                     => [Module\Api\Mastodon\Unimplemented::class,            [R::GET         ]], // @todo
-                       '/conversations/{id:\d+}'            => [Module\Api\Mastodon\Unimplemented::class,            [R::DELETE      ]],
-                       '/conversations/{id:\d+}/read'       => [Module\Api\Mastodon\Unimplemented::class,            [R::POST        ]],
+                       '/conversations'                     => [Module\Api\Mastodon\Unimplemented::class,            [R::GET         ]], // not implemented
+                       '/conversations/{id:\d+}'            => [Module\Api\Mastodon\Unimplemented::class,            [R::DELETE      ]], // not implemented
+                       '/conversations/{id:\d+}/read'       => [Module\Api\Mastodon\Unimplemented::class,            [R::POST        ]], // not implemented
                        '/custom_emojis'                     => [Module\Api\Mastodon\CustomEmojis::class,             [R::GET         ]],
                        '/domain_blocks'                     => [Module\Api\Mastodon\Unimplemented::class,            [R::GET, R::POST, R::DELETE]], // not implemented
                        '/directory'                         => [Module\Api\Mastodon\Directory::class,                [R::GET         ]],
@@ -113,7 +113,7 @@ return [
                        '/lists/{id:\d+}/accounts'           => [Module\Api\Mastodon\Lists\Accounts::class,           [R::GET, R::POST, R::DELETE]],
                        '/markers'                           => [Module\Api\Mastodon\Unimplemented::class,            [R::GET, R::POST]], // not implemented
                        '/media'                             => [Module\Api\Mastodon\Unimplemented::class,            [        R::POST]],
-                       '/media/{id:\d+}'                    => [Module\Api\Mastodon\Unimplemented::class,            [R::GET, R::PUT]], // @todo
+                       '/media/{id:\d+}'                    => [Module\Api\Mastodon\Media::class,                    [R::GET, R::PUT]],
                        '/mutes'                             => [Module\Api\Mastodon\Mutes::class,                    [R::GET         ]],
                        '/notifications'                     => [Module\Api\Mastodon\Notifications::class,            [R::GET         ]],
                        '/notifications/{id:\d+}'            => [Module\Api\Mastodon\Notifications::class,            [R::GET         ]],