diff options
author | Maxence Lange <maxence@artificial-owl.com> | 2023-03-07 14:30:12 -0100 |
---|---|---|
committer | Maxence Lange <maxence@artificial-owl.com> | 2023-03-07 14:30:22 -0100 |
commit | a8e7de151fa96b42be4cbd0776abc0f4aa84857c (patch) | |
tree | 99ee73d31eeb34cd3d0091e12fb68b35a129b7a2 | |
parent | 0a6aea1fe4e49bedff57992f08911940f3a60680 (diff) |
add meta to attachments
Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
-rw-r--r-- | lib/Db/CacheDocumentsRequest.php | 6 | ||||
-rw-r--r-- | lib/Db/CacheDocumentsRequestBuilder.php | 4 | ||||
-rw-r--r-- | lib/Db/CoreRequestBuilder.php | 1 | ||||
-rw-r--r-- | lib/Migration/Version1000Date20221118000001.php | 7 | ||||
-rw-r--r-- | lib/Migration/Version1000Date20230217000002.php | 10 | ||||
-rw-r--r-- | lib/Model/ActivityPub/Object/Document.php | 31 | ||||
-rw-r--r-- | lib/Model/Client/AttachmentMeta.php | 17 | ||||
-rw-r--r-- | lib/Model/Client/AttachmentMetaDim.php | 21 | ||||
-rw-r--r-- | lib/Model/Client/AttachmentMetaFocus.php | 20 | ||||
-rw-r--r-- | lib/Service/PostService.php | 2 |
10 files changed, 99 insertions, 20 deletions
diff --git a/lib/Db/CacheDocumentsRequest.php b/lib/Db/CacheDocumentsRequest.php index a26e43a4..b16a3217 100644 --- a/lib/Db/CacheDocumentsRequest.php +++ b/lib/Db/CacheDocumentsRequest.php @@ -57,6 +57,12 @@ class CacheDocumentsRequest extends CacheDocumentsRequestBuilder { ->setValue('parent_id_prim', $qb->createNamedParameter($qb->prim($document->getParentId()))) ->setValue('public', $qb->createNamedParameter(($document->isPublic()) ? '1' : '0')); + // generate Meta + $document->convertToMediaAttachment(); + if ($document->getMeta() !== null) { + $qb->setValue('meta', $qb->createNamedParameter(json_encode($document->getMeta()))); + } + try { $qb->setValue( 'creation', diff --git a/lib/Db/CacheDocumentsRequestBuilder.php b/lib/Db/CacheDocumentsRequestBuilder.php index 3043345e..546f5a94 100644 --- a/lib/Db/CacheDocumentsRequestBuilder.php +++ b/lib/Db/CacheDocumentsRequestBuilder.php @@ -62,8 +62,8 @@ class CacheDocumentsRequestBuilder extends CoreRequestBuilder { $qb->select( 'cd.nid', 'cd.id', 'cd.type', 'cd.parent_id', 'cd.account', 'cd.media_type', 'cd.mime_type', 'cd.url', 'cd.local_copy', 'cd.public', - 'cd.error', 'cd.creation', 'cd.caching', 'cd.resized_copy', 'cd.blurhash', - 'cd.description' + 'cd.error', 'cd.creation', 'cd.caching', 'cd.resized_copy', 'cd.meta', + 'cd.blurhash', 'cd.description' ) ->from(self::TABLE_CACHE_DOCUMENTS, 'cd'); diff --git a/lib/Db/CoreRequestBuilder.php b/lib/Db/CoreRequestBuilder.php index 8bac6d18..cf46bb0e 100644 --- a/lib/Db/CoreRequestBuilder.php +++ b/lib/Db/CoreRequestBuilder.php @@ -130,6 +130,7 @@ class CoreRequestBuilder { 'url', 'local_copy', 'resized_copy', + 'meta', 'blurhash', 'description', 'public', diff --git a/lib/Migration/Version1000Date20221118000001.php b/lib/Migration/Version1000Date20221118000001.php index cb0a791e..a6b535c4 100644 --- a/lib/Migration/Version1000Date20221118000001.php +++ b/lib/Migration/Version1000Date20221118000001.php @@ -987,6 +987,13 @@ class Version1000Date20221118000001 extends SimpleMigrationStep { ] ); $table->addColumn( + 'meta', Types::TEXT, + [ + 'notnull' => true, + 'default' => '[]' + ] + ); + $table->addColumn( 'blurhash', Types::STRING, [ 'notnull' => true, diff --git a/lib/Migration/Version1000Date20230217000002.php b/lib/Migration/Version1000Date20230217000002.php index e2969158..67ce0d4c 100644 --- a/lib/Migration/Version1000Date20230217000002.php +++ b/lib/Migration/Version1000Date20230217000002.php @@ -81,6 +81,16 @@ class Version1000Date20230217000002 extends SimpleMigrationStep { ); } + if (!$table->hasColumn('meta')) { + $table->addColumn( + 'meta', Types::TEXT, + [ + 'notnull' => true, + 'default' => '[]' + ] + ); + } + if (!$table->hasColumn('blurhash')) { $table->addColumn( 'blurhash', Types::STRING, diff --git a/lib/Model/ActivityPub/Object/Document.php b/lib/Model/ActivityPub/Object/Document.php index 364824bc..402cde95 100644 --- a/lib/Model/ActivityPub/Object/Document.php +++ b/lib/Model/ActivityPub/Object/Document.php @@ -57,6 +57,7 @@ class Document extends ACore implements JsonSerializable { private string $localCopy = ''; private string $resizedCopy = ''; private string $blurHash = ''; + private ?AttachmentMeta $meta = null; private string $description = ''; private int $caching = 0; private bool $public = false; @@ -193,6 +194,16 @@ class Document extends ACore implements JsonSerializable { return $this->blurHash; } + public function setMeta(AttachmentMeta $meta): self { + $this->meta = $meta; + + return $this; + } + + public function getMeta(): ?AttachmentMeta { + return $this->meta; + } + public function setDescription(string $description): self { $this->description = $description; @@ -326,6 +337,12 @@ class Document extends ACore implements JsonSerializable { } catch (Exception $e) { } } + + if ($this->get('meta', $data) !== '') { + $meta = new AttachmentMeta(); + $meta->import($this->getArray('meta', $data)); + $this->setMeta($meta); + } } /** @@ -375,12 +392,16 @@ class Document extends ACore implements JsonSerializable { $media->setRemoteUrl($this->getUrl()); } - $meta = new AttachmentMeta(); - $meta->setOriginal(new AttachmentMetaDim($this->getLocalCopySize())) - ->setSmall(new AttachmentMetaDim($this->getResizedCopySize())) - ->setFocus(new AttachmentMetaFocus(0, 0)); + if ($this->getMeta() === null) { + $meta = new AttachmentMeta(); + $meta->setOriginal(new AttachmentMetaDim($this->getLocalCopySize())) + ->setSmall(new AttachmentMetaDim($this->getResizedCopySize())) + ->setFocus(new AttachmentMetaFocus(0, 0)); + + $this->setMeta($meta); + } - $media->setMeta($meta) + $media->setMeta($this->getMeta()) ->setDescription($this->getDescription()) ->setBlurHash($this->getBlurHash()); diff --git a/lib/Model/Client/AttachmentMeta.php b/lib/Model/Client/AttachmentMeta.php index 95477b6f..c3424735 100644 --- a/lib/Model/Client/AttachmentMeta.php +++ b/lib/Model/Client/AttachmentMeta.php @@ -204,6 +204,23 @@ class AttachmentMeta implements JsonSerializable { return $this->blurHash; } + + public function import(array $data): self { + $original = new AttachmentMetaDim(); + $original->import($this->getArray('original', $data)); + $this->setOriginal($original); + + $small = new AttachmentMetaDim(); + $small->import($this->getArray('small', $data)); + $this->setSmall($small); + + $focus = new AttachmentMetaFocus($this->getInt('focus.x', $data), $this->getInt('focus.y', $data)); + $this->setFocus($focus); + + return $this; + } + + public function jsonSerialize(): array { return array_filter( [ diff --git a/lib/Model/Client/AttachmentMetaDim.php b/lib/Model/Client/AttachmentMetaDim.php index d4d8d895..0fb68df8 100644 --- a/lib/Model/Client/AttachmentMetaDim.php +++ b/lib/Model/Client/AttachmentMetaDim.php @@ -30,8 +30,11 @@ declare(strict_types=1); namespace OCA\Social\Model\Client; use JsonSerializable; +use OCA\Social\Tools\Traits\TArrayTools; class AttachmentMetaDim implements JsonSerializable { + use TArrayTools; + private ?int $width = null; private ?int $height = null; private string $size = ''; @@ -40,7 +43,11 @@ class AttachmentMetaDim implements JsonSerializable { private ?float $bitrate = null; private ?float $frameRate = null; - public function __construct(array $dim) { + public function __construct(array $dim = []) { + if (sizeof($dim) !== 2) { + return; + } + $width = (int)$dim[0]; $height = (int)$dim[1]; @@ -122,6 +129,18 @@ class AttachmentMetaDim implements JsonSerializable { return $this->frameRate; } + public function import(array $data): self { + $this->setWidth($this->getInt('width', $data)) + ->setHeight($this->getInt('height', $data)) + ->setSize($this->get('size', $data)) + ->setAspect($this->getFloat('aspect', $data)) + ->setDuration($this->getInt('duration', $data)) + ->setBitrate($this->getInt('bitrate', $data)) + ->setFrameRate($this->getFloat('frame_rate', $data)); + + return $this; + } + public function jsonSerialize(): array { return array_filter( [ diff --git a/lib/Model/Client/AttachmentMetaFocus.php b/lib/Model/Client/AttachmentMetaFocus.php index 2eb3ff11..22541dcf 100644 --- a/lib/Model/Client/AttachmentMetaFocus.php +++ b/lib/Model/Client/AttachmentMetaFocus.php @@ -32,10 +32,10 @@ namespace OCA\Social\Model\Client; use JsonSerializable; class AttachmentMetaFocus implements JsonSerializable { - private ?float $x; - private ?float $y; + private float $x; + private float $y; - public function __construct(?float $x = null, ?float $y = null) { + public function __construct(float $x = 0, float $y = 0) { $this->x = $x; $this->y = $y; } @@ -46,7 +46,7 @@ class AttachmentMetaFocus implements JsonSerializable { return $this; } - public function getX(): ?float { + public function getX(): float { return $this->x; } @@ -56,16 +56,14 @@ class AttachmentMetaFocus implements JsonSerializable { return $this; } - public function getY(): ?float { + public function getY(): float { return $this->y; } public function jsonSerialize(): array { - return array_filter( - [ - 'x' => $this->getX(), - 'y' => $this->getY() - ] - ); + return [ + 'x' => $this->getX(), + 'y' => $this->getY() + ]; } } diff --git a/lib/Service/PostService.php b/lib/Service/PostService.php index cd10fce9..27000306 100644 --- a/lib/Service/PostService.php +++ b/lib/Service/PostService.php @@ -119,7 +119,7 @@ class PostService { $token = $this->activityService->createActivity($actor, $note, $activity); $this->accountService->cacheLocalActorDetailCount($actor); - $this->miscService->log('Activity: ' . json_encode($activity)); + $this->logger->debug('Activity: ' . json_encode($activity)); return $activity; } |