From 059711f28acceddc85966660204495d661829b75 Mon Sep 17 00:00:00 2001 From: Maxence Lange Date: Fri, 18 Jan 2019 14:45:56 -0100 Subject: [PATCH] add attachment and parent_id field Signed-off-by: Maxence Lange --- lib/Db/CacheDocumentsRequest.php | 1 + lib/Db/NotesRequest.php | 11 +++++++- lib/Db/NotesRequestBuilder.php | 5 ++-- lib/Interfaces/Object/DocumentInterface.php | 7 +++++ lib/Model/ActivityPub/Object/Document.php | 30 ++++++++++++++++++++- 5 files changed, 49 insertions(+), 5 deletions(-) diff --git a/lib/Db/CacheDocumentsRequest.php b/lib/Db/CacheDocumentsRequest.php index 9d7777c9..5c64dc68 100644 --- a/lib/Db/CacheDocumentsRequest.php +++ b/lib/Db/CacheDocumentsRequest.php @@ -56,6 +56,7 @@ class CacheDocumentsRequest extends CacheDocumentsRequestBuilder { ->setValue('mime_type', $qb->createNamedParameter($document->getMimeType())) ->setValue('error', $qb->createNamedParameter($document->getError())) ->setValue('local_copy', $qb->createNamedParameter($document->getLocalCopy())) + ->setValue('parent_id', $qb->createNamedParameter($document->getParentId())) ->setValue('public', $qb->createNamedParameter(($document->isPublic()) ? '1' : '0')) ->setValue( 'creation', diff --git a/lib/Db/NotesRequest.php b/lib/Db/NotesRequest.php index 306c85ff..4d8221c8 100644 --- a/lib/Db/NotesRequest.php +++ b/lib/Db/NotesRequest.php @@ -31,6 +31,7 @@ namespace OCA\Social\Db; use DateTime; +use Doctrine\DBAL\Exception\UniqueConstraintViolationException; use OCA\Social\Exceptions\NoteNotFoundException; use OCA\Social\Model\ActivityPub\ACore; use OCA\Social\Model\ActivityPub\Actor\Person; @@ -87,6 +88,11 @@ class NotesRequest extends NotesRequestBuilder { ) ->setValue('content', $qb->createNamedParameter($note->getContent())) ->setValue('summary', $qb->createNamedParameter($note->getSummary())) + ->setValue( + 'attachments', $qb->createNamedParameter( + json_encode($note->getAttachments(), JSON_UNESCAPED_SLASHES) + ) + ) ->setValue('published', $qb->createNamedParameter($note->getPublished())) ->setValue( 'published_time', $qb->createNamedParameter($dTime, IQueryBuilder::PARAM_DATE) @@ -105,7 +111,10 @@ class NotesRequest extends NotesRequestBuilder { $qb->createNamedParameter(new DateTime('now'), IQueryBuilder::PARAM_DATE) ); - $qb->execute(); + try { + $qb->execute(); + } catch (UniqueConstraintViolationException $e) { + } } diff --git a/lib/Db/NotesRequestBuilder.php b/lib/Db/NotesRequestBuilder.php index 5d770ddd..dccdb1e6 100644 --- a/lib/Db/NotesRequestBuilder.php +++ b/lib/Db/NotesRequestBuilder.php @@ -82,9 +82,8 @@ class NotesRequestBuilder extends CoreRequestBuilder { /** @noinspection PhpMethodParametersCountMismatchInspection */ $qb->select( 'sn.id', 'sn.type', 'sn.to', 'sn.to_array', 'sn.cc', 'sn.bcc', 'sn.content', - 'sn.summary', - 'sn.published', 'sn.published_time', 'sn.attributed_to', 'sn.in_reply_to', 'sn.source', - 'sn.local', 'sn.instances', 'sn.creation' + 'sn.summary', 'sn.attachments', 'sn.published', 'sn.published_time', 'sn.attributed_to', + 'sn.in_reply_to', 'sn.source', 'sn.local', 'sn.instances', 'sn.creation' ) ->from(self::TABLE_SERVER_NOTES, 'sn'); diff --git a/lib/Interfaces/Object/DocumentInterface.php b/lib/Interfaces/Object/DocumentInterface.php index 1ecb4903..d25ff56a 100644 --- a/lib/Interfaces/Object/DocumentInterface.php +++ b/lib/Interfaces/Object/DocumentInterface.php @@ -100,6 +100,13 @@ class DocumentInterface implements IActivityPubInterface { */ public function save(ACore $item) { /** @var Document $item */ + if ($item->getParent()) { + $item->setParentId( + $item->getParent() + ->getId() + ); + } + $this->cacheDocumentsRequest->save($item); } diff --git a/lib/Model/ActivityPub/Object/Document.php b/lib/Model/ActivityPub/Object/Document.php index 4797acf7..615ad54e 100644 --- a/lib/Model/ActivityPub/Object/Document.php +++ b/lib/Model/ActivityPub/Object/Document.php @@ -66,6 +66,8 @@ class Document extends ACore implements JsonSerializable { /** @var int */ private $error = 0; + /** @var string */ + private $parentId = ''; /** * Document constructor. @@ -155,6 +157,25 @@ class Document extends ACore implements JsonSerializable { } + /** + * @return string + */ + public function getParentId(): string { + return $this->parentId; + } + + /** + * @param string $parentId + * + * @return Document + */ + public function setParentId(string $parentId): Document { + $this->parentId = $parentId; + + return $this; + } + + /** * @return int */ @@ -220,6 +241,7 @@ class Document extends ACore implements JsonSerializable { $this->setLocalCopy($this->get('local_copy', $data, '')); $this->setMediaType($this->get('media_type', $data, '')); $this->setMimeType($this->get('mime_type', $data, '')); + $this->setParentId($this->get('parent_id', $data, '')); if ($this->get('caching', $data, '') === '') { $this->setCaching(0); @@ -233,7 +255,7 @@ class Document extends ACore implements JsonSerializable { * @return array */ public function jsonSerialize(): array { - return array_merge( + $result = array_merge( parent::jsonSerialize(), [ 'mediaType' => $this->getMediaType(), @@ -241,6 +263,12 @@ class Document extends ACore implements JsonSerializable { 'localCopy' => $this->getLocalCopy() ] ); + + if ($this->isCompleteDetails()) { + $result['parentId'] = $this->getParentId(); + } + + return $result; } }