From 524c7aa14174c3d6c063b56fe79eeffa03de688d Mon Sep 17 00:00:00 2001 From: Maxence Lange Date: Fri, 18 Jan 2019 15:55:10 -0100 Subject: [PATCH] save/update Signed-off-by: Maxence Lange --- lib/Db/CacheDocumentsRequest.php | 44 ++++++++++++++++++++++- lib/Db/CoreRequestBuilder.php | 13 ++++++- lib/Model/ActivityPub/Object/Document.php | 4 +++ lib/Service/DocumentService.php | 6 ++-- 4 files changed, 62 insertions(+), 5 deletions(-) diff --git a/lib/Db/CacheDocumentsRequest.php b/lib/Db/CacheDocumentsRequest.php index 5c64dc68..7d51deec 100644 --- a/lib/Db/CacheDocumentsRequest.php +++ b/lib/Db/CacheDocumentsRequest.php @@ -66,6 +66,29 @@ class CacheDocumentsRequest extends CacheDocumentsRequestBuilder { } + /** + * insert cache about an Actor in database. + * + * @param Document $document + */ + public function update(Document $document) { + $qb = $this->getCacheDocumentsInsertSql(); + $qb->set('type', $qb->createNamedParameter($document->getType())) + ->set('url', $qb->createNamedParameter($document->getUrl())) + ->set('media_type', $qb->createNamedParameter($document->getMediaType())) + ->set('mime_type', $qb->createNamedParameter($document->getMimeType())) + ->set('error', $qb->createNamedParameter($document->getError())) + ->set('local_copy', $qb->createNamedParameter($document->getLocalCopy())) + ->set('parent_id', $qb->createNamedParameter($document->getParentId())) + ->set('public', $qb->createNamedParameter(($document->isPublic()) ? '1' : '0')) + ->set( + 'creation', + $qb->createNamedParameter(new DateTime('now'), IQueryBuilder::PARAM_DATE) + ); + $qb->execute(); + } + + /** * @param Document $document */ @@ -99,7 +122,7 @@ class CacheDocumentsRequest extends CacheDocumentsRequestBuilder { * @return Document * @throws CacheDocumentDoesNotExistException */ - public function getBySource(string $url) { + public function getByUrl(string $url) { $qb = $this->getCacheDocumentsSelectSql(); $this->limitToUrl($qb, $url); @@ -143,6 +166,24 @@ class CacheDocumentsRequest extends CacheDocumentsRequestBuilder { } + /** + * @param Document $item + * + * @return bool + */ + public function isDuplicate(Document $item): bool { + $qb = $this->getCacheDocumentsSelectSql(); + $this->limitToUrl($qb, $item->getUrl()); + $this->limitToParentId($qb, $item->getParentId()); + + $cursor = $qb->execute(); + $data = $cursor->fetch(); + $cursor->closeCursor(); + + return ($data !== false); + } + + /** * @return Document[] * @throws Exception @@ -185,5 +226,6 @@ class CacheDocumentsRequest extends CacheDocumentsRequestBuilder { $qb->execute(); } + } diff --git a/lib/Db/CoreRequestBuilder.php b/lib/Db/CoreRequestBuilder.php index d9ae63f1..66b5325e 100644 --- a/lib/Db/CoreRequestBuilder.php +++ b/lib/Db/CoreRequestBuilder.php @@ -63,7 +63,7 @@ class CoreRequestBuilder { const TABLE_CACHE_ACTORS = 'social_cache_actors'; const TABLE_CACHE_DOCUMENTS = 'social_cache_documents'; - + /** @var IDBConnection */ protected $dbConnection; @@ -346,6 +346,17 @@ class CoreRequestBuilder { } + /** + * Limit the request to the parent_id + * + * @param IQueryBuilder $qb + * @param string $parentId + */ + protected function limitToParentId(IQueryBuilder &$qb, string $parentId) { + $this->limitToDBField($qb, 'parent_id', $parentId); + } + + /** * @param IQueryBuilder $qb * @param int $since diff --git a/lib/Model/ActivityPub/Object/Document.php b/lib/Model/ActivityPub/Object/Document.php index 615ad54e..932392c7 100644 --- a/lib/Model/ActivityPub/Object/Document.php +++ b/lib/Model/ActivityPub/Object/Document.php @@ -33,6 +33,7 @@ namespace OCA\Social\Model\ActivityPub\Object; use DateTime; use JsonSerializable; +use OCA\Social\Exceptions\InvalidOriginException; use OCA\Social\Exceptions\UrlCloudException; use OCA\Social\Model\ActivityPub\ACore; @@ -218,6 +219,7 @@ class Document extends ACore implements JsonSerializable { * @param array $data * * @throws UrlCloudException + * @throws InvalidOriginException */ public function import(array $data) { parent::import($data); @@ -226,6 +228,8 @@ class Document extends ACore implements JsonSerializable { if ($this->getId() === '') { $this->generateUniqueId('/documents/g'); + } else { + $this->checkOrigin($this->getId()); } } diff --git a/lib/Service/DocumentService.php b/lib/Service/DocumentService.php index c250d0e3..e2579b95 100644 --- a/lib/Service/DocumentService.php +++ b/lib/Service/DocumentService.php @@ -238,14 +238,14 @@ class DocumentService { $icon->setMediaType(''); $icon->setLocalCopy('avatar'); - $this->cacheDocumentsRequest->deleteByUrl($icon->getUrl()); - $this->cacheDocumentsRequest->save($icon); + $interface = AP::$activityPub->getInterfaceFromType(Image::TYPE); + $interface->save($icon); $actor->setAvatarVersion($versionCurrent); $this->actorRequest->update($actor); } else { try { - $icon = $this->cacheDocumentsRequest->getBySource($url); + $icon = $this->cacheDocumentsRequest->getByUrl($url); } catch (CacheDocumentDoesNotExistException $e) { return ''; }