diff options
author | Maxence Lange <maxence@artificial-owl.com> | 2018-11-24 13:15:26 -0100 |
---|---|---|
committer | Maxence Lange <maxence@artificial-owl.com> | 2018-11-24 13:15:26 -0100 |
commit | a031d9f294750dedd014f9b60666cd5457568e95 (patch) | |
tree | 3269d4cfd91771b4cb638223aa38b72ce8f949fe /lib/Db | |
parent | 7aeca5c395a964f4088a5eaf4bca62feac2911be (diff) |
Caching
Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
Diffstat (limited to 'lib/Db')
-rw-r--r-- | lib/Db/CacheDocumentsRequest.php | 61 | ||||
-rw-r--r-- | lib/Db/CacheDocumentsRequestBuilder.php | 5 | ||||
-rw-r--r-- | lib/Db/CoreRequestBuilder.php | 12 |
3 files changed, 68 insertions, 10 deletions
diff --git a/lib/Db/CacheDocumentsRequest.php b/lib/Db/CacheDocumentsRequest.php index 7837ad5d..fa6f8935 100644 --- a/lib/Db/CacheDocumentsRequest.php +++ b/lib/Db/CacheDocumentsRequest.php @@ -32,8 +32,6 @@ namespace OCA\Social\Db; use DateTime; use OCA\Social\Exceptions\CacheDocumentDoesNotExistException; -use OCA\Social\Exceptions\SocialAppConfigException; -use OCA\Social\Exceptions\UrlCloudException; use OCA\Social\Model\ActivityPub\Document; use OCP\DB\QueryBuilder\IQueryBuilder; @@ -50,7 +48,9 @@ class CacheDocumentsRequest extends CacheDocumentsRequestBuilder { $qb->setValue('id', $qb->createNamedParameter($document->getId())) ->setValue('type', $qb->createNamedParameter($document->getType())) ->setValue('url', $qb->createNamedParameter($document->getUrl())) + ->setValue('media_type', $qb->createNamedParameter($document->getMediaType())) ->setValue('local_copy', $qb->createNamedParameter($document->getLocalCopy())) + ->setValue('public', $qb->createNamedParameter(($document->isPublic()) ? '1' : '0')) ->setValue( 'creation', $qb->createNamedParameter(new DateTime('now'), IQueryBuilder::PARAM_DATE) @@ -60,17 +60,68 @@ class CacheDocumentsRequest extends CacheDocumentsRequestBuilder { /** + * @param Document $document + */ + public function initCaching(Document $document) { + $qb = $this->getCacheDocumentsUpdateSql(); + $this->limitToIdString($qb, $document->getId()); + $qb->set( + 'caching', $qb->createNamedParameter(new DateTime('now'), IQueryBuilder::PARAM_DATE) + ); + + $qb->execute(); + } + + + /** + * @param Document $document + */ + public function endCaching(Document $document) { + $qb = $this->getCacheDocumentsUpdateSql(); + $this->limitToIdString($qb, $document->getId()); + $qb->set('local_copy', $qb->createNamedParameter($document->getLocalCopy())); + + $qb->execute(); + } + + + /** * @param string $url * * @return Document * @throws CacheDocumentDoesNotExistException - * @throws SocialAppConfigException - * @throws UrlCloudException */ - public function getFromSource(string $url) { + public function getBySource(string $url) { $qb = $this->getCacheDocumentsSelectSql(); $this->limitToUrl($qb, $url); + $cursor = $qb->execute(); + $data = $cursor->fetch(); + $cursor->closeCursor(); + + if ($data === false) { + throw new CacheDocumentDoesNotExistException(); + } + + return $this->parseCacheDocumentsSelectSql($data); + } + + + /** + * @param string $id + * + * @param bool $public + * + * @return Document + * @throws CacheDocumentDoesNotExistException + */ + public function getById(string $id, bool $public = false) { + $qb = $this->getCacheDocumentsSelectSql(); + $this->limitToIdString($qb, $id); + + if ($public === true) { + $this->limitToPublic($qb); + } $cursor = $qb->execute(); $data = $cursor->fetch(); diff --git a/lib/Db/CacheDocumentsRequestBuilder.php b/lib/Db/CacheDocumentsRequestBuilder.php index 853ed0f4..6e52293e 100644 --- a/lib/Db/CacheDocumentsRequestBuilder.php +++ b/lib/Db/CacheDocumentsRequestBuilder.php @@ -79,7 +79,7 @@ class CacheDocumentsRequestBuilder extends CoreRequestBuilder { /** @noinspection PhpMethodParametersCountMismatchInspection */ $qb->select( 'cd.id', 'cd.type', 'cd.media_type', 'cd.mime_type', 'cd.url', 'cd.local_copy', - 'cd.creation', 'cd.caching' + 'cd.public', 'cd.creation', 'cd.caching' ) ->from(self::TABLE_CACHE_DOCUMENTS, 'cd'); @@ -106,12 +106,9 @@ class CacheDocumentsRequestBuilder extends CoreRequestBuilder { * @param array $data * * @return Document - * @throws UrlCloudException - * @throws SocialAppConfigException */ protected function parseCacheDocumentsSelectSql(array $data): Document { $document = new Document(); - $document->setUrlCloud($this->configService->getCloudAddress()); $document->importFromDatabase($data); return $document; diff --git a/lib/Db/CoreRequestBuilder.php b/lib/Db/CoreRequestBuilder.php index 902b0888..f5d2053c 100644 --- a/lib/Db/CoreRequestBuilder.php +++ b/lib/Db/CoreRequestBuilder.php @@ -33,7 +33,6 @@ namespace OCA\Social\Db; use Doctrine\DBAL\Query\QueryBuilder; use OCA\Social\Exceptions\InvalidResourceException; -use OCA\Social\Exceptions\UrlCloudException; use OCA\Social\Model\ActivityPub\Document; use OCA\Social\Model\ActivityPub\Image; use OCA\Social\Model\ActivityPub\Person; @@ -150,6 +149,17 @@ class CoreRequestBuilder { * Limit the request to the ActorId * * @param IQueryBuilder $qb + */ + protected function limitToPublic(IQueryBuilder &$qb) { + $this->limitToDBFieldInt($qb, 'public', 1); + } + + + + /** + * Limit the request to the ActorId + * + * @param IQueryBuilder $qb * @param string $actorId * @param string $alias */ |