]> git.mxchange.org Git - friendica.git/commitdiff
Editing of media descriptions is now possible as well
authorMichael <heluecht@pirati.ca>
Tue, 28 Feb 2023 08:06:49 +0000 (08:06 +0000)
committerMichael <heluecht@pirati.ca>
Tue, 28 Feb 2023 08:06:49 +0000 (08:06 +0000)
src/Factory/Api/Mastodon/Attachment.php
src/Model/Post/Media.php
src/Module/Api/Mastodon/Media.php

index 197ade0fe29071a46034d1a80ed2a6f0364f7a53..2b53ae67d919504c81ad436a1e9b4d26035edbe6 100644 (file)
@@ -51,39 +51,63 @@ class Attachment extends BaseFactory
        {
                $attachments = [];
                foreach (Post\Media::getByURIId($uriId, [Post\Media::AUDIO, Post\Media::VIDEO, Post\Media::IMAGE]) as $attachment) {
-                       $filetype = !empty($attachment['mimetype']) ? strtolower(substr($attachment['mimetype'], 0, strpos($attachment['mimetype'], '/'))) : '';
-
-                       if (($filetype == 'audio') || ($attachment['type'] == Post\Media::AUDIO)) {
-                               $type = 'audio';
-                       } elseif (($filetype == 'video') || ($attachment['type'] == Post\Media::VIDEO)) {
-                               $type = 'video';
-                       } elseif ($attachment['mimetype'] == 'image/gif') {
-                               $type = 'gifv';
-                       } elseif (($filetype == 'image') || ($attachment['type'] == Post\Media::IMAGE)) {
-                               $type = 'image';
-                       } else {
-                               $type = 'unknown';
-                       }
+                       $attachments[] = $this->createFromMediaArray($attachment);
+               }
+
+               return $attachments;
+       }
+
+       /**
+        * @param int $id id of the media
+        * @return array
+        * @throws HTTPException\InternalServerErrorException
+        */
+       public function createFromId(int $id): array
+       {
+               $attachment = Post\Media::getById($id);
+               if (empty($attachment)) {
+                       return [];
+               }
+               return $this->createFromMediaArray($attachment);
+       }
 
-                       $remote = $attachment['url'];
-                       if ($type == 'image') {
-                               $url     = Post\Media::getPreviewUrlForId($attachment['id']);
+       /**
+        * @param array $attachment
+        * @return array
+        * @throws HTTPException\InternalServerErrorException
+        */
+       private function createFromMediaArray(array $attachment): array
+       {
+               $filetype = !empty($attachment['mimetype']) ? strtolower(substr($attachment['mimetype'], 0, strpos($attachment['mimetype'], '/'))) : '';
+
+               if (($filetype == 'audio') || ($attachment['type'] == Post\Media::AUDIO)) {
+                       $type = 'audio';
+               } elseif (($filetype == 'video') || ($attachment['type'] == Post\Media::VIDEO)) {
+                       $type = 'video';
+               } elseif ($attachment['mimetype'] == 'image/gif') {
+                       $type = 'gifv';
+               } elseif (($filetype == 'image') || ($attachment['type'] == Post\Media::IMAGE)) {
+                       $type = 'image';
+               } else {
+                       $type = 'unknown';
+               }
+
+               $remote = $attachment['url'];
+               if ($type == 'image') {
+                       $url     = Post\Media::getPreviewUrlForId($attachment['id']);
+                       $preview = Post\Media::getPreviewUrlForId($attachment['id'], Proxy::SIZE_SMALL);
+               } else {
+                       $url = $attachment['url'];
+
+                       if (!empty($attachment['preview'])) {
                                $preview = Post\Media::getPreviewUrlForId($attachment['id'], Proxy::SIZE_SMALL);
                        } else {
-                               $url = $attachment['url'];
-
-                               if (!empty($attachment['preview'])) {
-                                       $preview = Post\Media::getPreviewUrlForId($attachment['id'], Proxy::SIZE_SMALL);
-                               } else {
-                                       $preview = '';
-                               }
+                               $preview = '';
                        }
-
-                       $object        = new \Friendica\Object\Api\Mastodon\Attachment($attachment, $type, $url, $preview, $remote);
-                       $attachments[] = $object->toArray();
                }
 
-               return $attachments;
+               $object        = new \Friendica\Object\Api\Mastodon\Attachment($attachment, $type, $url, $preview, $remote);
+               return $object->toArray();
        }
 
        /**
index 6454cefda148ee338ee39e098363a4c14c32b4e3..8545760bef45d8874f48e8c6df95d35eee321461 100644 (file)
@@ -697,6 +697,30 @@ class Media
                return DBA::selectToArray('post-media', [], $condition, ['order' => ['id']]);
        }
 
+       /**
+        * Retrieves the media attachment with the provided media id.
+        *
+        * @param int $id  id
+        * @return array|bool Array on success, false on error
+        * @throws \Exception
+        */
+       public static function getById(int $id)
+       {
+               return DBA::selectFirst('post-media', [], ['id' => $id]);
+       }
+
+       /**
+        * Update post-media entries
+        *
+        * @param array $fields
+        * @param int $id
+        * @return bool
+        */
+       public static function updateById(array $fields, int $id): bool
+       {
+               return DBA::update('post-media', $fields, ['id' => $id]);
+       }
+
        /**
         * Checks if media attachments are associated with the provided item ID.
         *
index 2f71d53c27ec7f60250724248f6138e9664383d0..e26637601fefc7be061bcc34ef577746f33076d6 100644 (file)
@@ -25,6 +25,7 @@ use Friendica\Core\Logger;
 use Friendica\Core\System;
 use Friendica\DI;
 use Friendica\Model\Photo;
+use Friendica\Model\Post;
 use Friendica\Module\BaseApi;
 
 /**
@@ -71,7 +72,15 @@ class Media extends BaseApi
 
                $photo = Photo::selectFirst(['resource-id'], ['id' => $this->parameters['id'], 'uid' => $uid]);
                if (empty($photo['resource-id'])) {
-                       DI::mstdnError()->RecordNotFound();
+                       $media = Post\Media::getById($this->parameters['id']);
+                       if (empty($media['uri-id'])) {
+                               DI::mstdnError()->RecordNotFound();
+                       }
+                       if (!Post::exists(['uri-id' => $media['uri-id'], 'uid' => $uid, 'origin' => true])) {
+                               DI::mstdnError()->RecordNotFound();
+                       }
+                       Post\Media::updateById(['description' => $request['description']], $this->parameters['id']);
+                       System::jsonExit(DI::mstdnAttachment()->createFromId($this->parameters['id']));
                }
 
                Photo::update(['desc' => $request['description']], ['resource-id' => $photo['resource-id']]);