summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMaxence Lange <maxence@artificial-owl.com>2019-01-18 15:55:10 -0100
committerMaxence Lange <maxence@artificial-owl.com>2019-01-19 11:14:38 -0100
commit524c7aa14174c3d6c063b56fe79eeffa03de688d (patch)
tree9040ce9e8414ab1305845f5f4d5b4ab0f0fd1035
parent898993a67d7c17da3f19be34a6d9824e8c6a5489 (diff)
save/update
Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
-rw-r--r--lib/Db/CacheDocumentsRequest.php44
-rw-r--r--lib/Db/CoreRequestBuilder.php13
-rw-r--r--lib/Model/ActivityPub/Object/Document.php4
-rw-r--r--lib/Service/DocumentService.php6
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
@@ -67,6 +67,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
*/
public function initCaching(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);
@@ -144,6 +167,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;
@@ -347,6 +347,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
* @param int $limit
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 '';
}