Editing of media descriptions is now possible as well

This commit is contained in:
Michael 2023-02-28 08:06:49 +00:00
parent 98aed4dde3
commit 3a0b0ffce5
3 changed files with 88 additions and 31 deletions

View File

@ -51,6 +51,33 @@ class Attachment extends BaseFactory
{
$attachments = [];
foreach (Post\Media::getByURIId($uriId, [Post\Media::AUDIO, Post\Media::VIDEO, Post\Media::IMAGE]) as $attachment) {
$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);
}
/**
* @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)) {
@ -80,10 +107,7 @@ class Attachment extends BaseFactory
}
$object = new \Friendica\Object\Api\Mastodon\Attachment($attachment, $type, $url, $preview, $remote);
$attachments[] = $object->toArray();
}
return $attachments;
return $object->toArray();
}
/**

View 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.
*

View 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,8 +72,16 @@ class Media extends BaseApi
$photo = Photo::selectFirst(['resource-id'], ['id' => $this->parameters['id'], 'uid' => $uid]);
if (empty($photo['resource-id'])) {
$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']]);