2018-11-23 20:41:46 +00:00
|
|
|
<?php
|
2022-04-15 11:34:01 +00:00
|
|
|
|
2018-11-23 20:41:46 +00:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Nextcloud - Social Support
|
|
|
|
*
|
|
|
|
* This file is licensed under the Affero General Public License version 3 or
|
|
|
|
* later. See the COPYING file.
|
|
|
|
*
|
|
|
|
* @author Maxence Lange <maxence@artificial-owl.com>
|
|
|
|
* @copyright 2018, Maxence Lange <maxence@artificial-owl.com>
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OCA\Social\Db;
|
|
|
|
|
|
|
|
use DateTime;
|
2018-11-26 12:33:48 +00:00
|
|
|
use Exception;
|
2018-11-23 20:41:46 +00:00
|
|
|
use OCA\Social\Exceptions\CacheDocumentDoesNotExistException;
|
2018-12-13 09:40:27 +00:00
|
|
|
use OCA\Social\Model\ActivityPub\Object\Document;
|
2018-11-23 20:41:46 +00:00
|
|
|
use OCP\DB\QueryBuilder\IQueryBuilder;
|
|
|
|
|
|
|
|
class CacheDocumentsRequest extends CacheDocumentsRequestBuilder {
|
2022-04-15 11:34:01 +00:00
|
|
|
public const CACHING_TIMEOUT = 5; // 5 min
|
2018-11-26 12:33:48 +00:00
|
|
|
|
2022-04-15 16:13:33 +00:00
|
|
|
public function save(Document $document): void {
|
2018-11-23 20:41:46 +00:00
|
|
|
$qb = $this->getCacheDocumentsInsertSql();
|
|
|
|
$qb->setValue('id', $qb->createNamedParameter($document->getId()))
|
2022-11-22 23:16:36 +00:00
|
|
|
->setValue('id_prim', $qb->createNamedParameter($qb->prim($document->getId())))
|
2023-02-23 12:06:39 +00:00
|
|
|
->setValue('account', $qb->createNamedParameter($document->getAccount()))
|
2018-11-23 20:41:46 +00:00
|
|
|
->setValue('type', $qb->createNamedParameter($document->getType()))
|
|
|
|
->setValue('url', $qb->createNamedParameter($document->getUrl()))
|
2018-11-24 14:15:26 +00:00
|
|
|
->setValue('media_type', $qb->createNamedParameter($document->getMediaType()))
|
2018-11-27 14:19:38 +00:00
|
|
|
->setValue('mime_type', $qb->createNamedParameter($document->getMimeType()))
|
2018-11-27 15:13:10 +00:00
|
|
|
->setValue('error', $qb->createNamedParameter($document->getError()))
|
2018-11-23 20:41:46 +00:00
|
|
|
->setValue('local_copy', $qb->createNamedParameter($document->getLocalCopy()))
|
2019-07-17 21:34:28 +00:00
|
|
|
->setValue('resized_copy', $qb->createNamedParameter($document->getResizedCopy()))
|
2023-02-23 12:06:39 +00:00
|
|
|
->setValue('blurhash', $qb->createNamedParameter($document->getBlurHash()))
|
|
|
|
->setValue('description', $qb->createNamedParameter($document->getDescription()))
|
2019-01-18 15:45:56 +00:00
|
|
|
->setValue('parent_id', $qb->createNamedParameter($document->getParentId()))
|
2022-12-07 00:14:12 +00:00
|
|
|
->setValue('parent_id_prim', $qb->createNamedParameter($qb->prim($document->getParentId())))
|
2019-05-17 10:19:45 +00:00
|
|
|
->setValue('public', $qb->createNamedParameter(($document->isPublic()) ? '1' : '0'));
|
|
|
|
|
2023-03-07 15:30:12 +00:00
|
|
|
// generate Meta
|
|
|
|
$document->convertToMediaAttachment();
|
|
|
|
if ($document->getMeta() !== null) {
|
|
|
|
$qb->setValue('meta', $qb->createNamedParameter(json_encode($document->getMeta())));
|
|
|
|
}
|
|
|
|
|
2019-05-17 10:19:45 +00:00
|
|
|
try {
|
|
|
|
$qb->setValue(
|
|
|
|
'creation',
|
|
|
|
$qb->createNamedParameter(new DateTime('now'), IQueryBuilder::PARAM_DATE)
|
|
|
|
);
|
|
|
|
} catch (Exception $e) {
|
|
|
|
}
|
2019-03-05 12:28:14 +00:00
|
|
|
|
2022-04-15 16:13:33 +00:00
|
|
|
$qb->executeStatement();
|
2023-02-23 12:06:39 +00:00
|
|
|
$document->setNid($qb->getLastInsertId());
|
2018-11-23 20:41:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-01-18 16:55:10 +00:00
|
|
|
/**
|
2022-04-15 16:13:33 +00:00
|
|
|
* Insert cache about an Actor in database.
|
2019-01-18 16:55:10 +00:00
|
|
|
*/
|
2022-04-15 16:13:33 +00:00
|
|
|
public function update(Document $document): void {
|
2019-05-07 15:11:04 +00:00
|
|
|
$qb = $this->getCacheDocumentsUpdateSql();
|
2019-01-18 16:55:10 +00:00
|
|
|
$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()))
|
2019-07-17 21:34:28 +00:00
|
|
|
->set('resized_copy', $qb->createNamedParameter($document->getResizedCopy()))
|
2023-02-23 12:06:39 +00:00
|
|
|
->set('blurhash', $qb->createNamedParameter($document->getBlurHash()))
|
|
|
|
->set('description', $qb->createNamedParameter($document->getDescription()))
|
2019-01-18 16:55:10 +00:00
|
|
|
->set('parent_id', $qb->createNamedParameter($document->getParentId()))
|
2022-12-07 00:14:12 +00:00
|
|
|
->set('parent_id_prim', $qb->createNamedParameter($qb->prim($document->getParentId())))
|
2019-05-17 10:19:45 +00:00
|
|
|
->set('public', $qb->createNamedParameter(($document->isPublic()) ? '1' : '0'));
|
|
|
|
|
|
|
|
try {
|
|
|
|
$qb->set(
|
|
|
|
'creation',
|
|
|
|
$qb->createNamedParameter(new DateTime('now'), IQueryBuilder::PARAM_DATE)
|
|
|
|
);
|
|
|
|
} catch (Exception $e) {
|
|
|
|
}
|
2019-05-07 15:11:04 +00:00
|
|
|
|
2023-02-23 12:06:39 +00:00
|
|
|
$qb->limitToIdPrim($qb->prim($document->getId()));
|
2022-04-15 16:13:33 +00:00
|
|
|
$qb->executeStatement();
|
2019-01-18 16:55:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-11-24 14:15:26 +00:00
|
|
|
/**
|
2022-04-15 16:13:33 +00:00
|
|
|
* @throws \OCP\DB\Exception
|
2018-11-24 14:15:26 +00:00
|
|
|
*/
|
2022-04-15 16:13:33 +00:00
|
|
|
public function initCaching(Document $document): void {
|
2018-11-24 14:15:26 +00:00
|
|
|
$qb = $this->getCacheDocumentsUpdateSql();
|
|
|
|
$this->limitToIdString($qb, $document->getId());
|
2019-05-17 10:19:45 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
$qb->set(
|
|
|
|
'caching', $qb->createNamedParameter(new DateTime('now'), IQueryBuilder::PARAM_DATE)
|
|
|
|
);
|
|
|
|
} catch (Exception $e) {
|
|
|
|
}
|
2018-11-24 14:15:26 +00:00
|
|
|
|
2022-04-15 16:13:33 +00:00
|
|
|
$qb->executeStatement();
|
2018-11-24 14:15:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2022-04-15 16:13:33 +00:00
|
|
|
* @throws \OCP\DB\Exception
|
2018-11-24 14:15:26 +00:00
|
|
|
*/
|
2022-04-15 16:13:33 +00:00
|
|
|
public function endCaching(Document $document): void {
|
2018-11-24 14:15:26 +00:00
|
|
|
$qb = $this->getCacheDocumentsUpdateSql();
|
|
|
|
$this->limitToIdString($qb, $document->getId());
|
|
|
|
$qb->set('local_copy', $qb->createNamedParameter($document->getLocalCopy()));
|
2019-07-17 21:34:28 +00:00
|
|
|
$qb->set('resized_copy', $qb->createNamedParameter($document->getResizedCopy()));
|
2023-02-23 12:06:39 +00:00
|
|
|
$qb->set('blurhash', $qb->createNamedParameter($document->getBlurHash()));
|
|
|
|
$qb->set('description', $qb->createNamedParameter($document->getDescription()));
|
2018-11-25 12:50:12 +00:00
|
|
|
$qb->set('error', $qb->createNamedParameter($document->getError()));
|
2018-11-24 14:15:26 +00:00
|
|
|
|
2022-04-15 16:13:33 +00:00
|
|
|
$qb->executeStatement();
|
2018-11-24 14:15:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-11-23 20:41:46 +00:00
|
|
|
/**
|
|
|
|
* @param string $url
|
|
|
|
*
|
|
|
|
* @return Document
|
|
|
|
* @throws CacheDocumentDoesNotExistException
|
|
|
|
*/
|
2019-01-18 16:55:10 +00:00
|
|
|
public function getByUrl(string $url) {
|
2018-11-23 20:41:46 +00:00
|
|
|
$qb = $this->getCacheDocumentsSelectSql();
|
|
|
|
$this->limitToUrl($qb, $url);
|
|
|
|
|
2018-11-24 14:15:26 +00:00
|
|
|
$cursor = $qb->execute();
|
|
|
|
$data = $cursor->fetch();
|
|
|
|
$cursor->closeCursor();
|
|
|
|
|
|
|
|
if ($data === false) {
|
|
|
|
throw new CacheDocumentDoesNotExistException();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->parseCacheDocumentsSelectSql($data);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2023-02-23 12:06:39 +00:00
|
|
|
* @param array $mediaIds
|
|
|
|
* @param string $account - limit to account
|
2018-11-24 14:15:26 +00:00
|
|
|
*
|
2023-02-23 12:06:39 +00:00
|
|
|
* @return Document[]
|
|
|
|
*/
|
|
|
|
public function getFromArray(array $mediaIds, string $account = ''): array {
|
|
|
|
$qb = $this->getCacheDocumentsSelectSql();
|
|
|
|
$qb->limitToDBFieldArray('nid', $mediaIds);
|
|
|
|
$qb->limitToAccount($account);
|
|
|
|
|
|
|
|
$documents = [];
|
|
|
|
$cursor = $qb->execute();
|
|
|
|
while ($data = $cursor->fetch()) {
|
|
|
|
$documents[] = $this->parseCacheDocumentsSelectSql($data);
|
|
|
|
}
|
|
|
|
$cursor->closeCursor();
|
|
|
|
|
|
|
|
return $documents;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $id
|
2018-11-24 14:15:26 +00:00
|
|
|
* @param bool $public
|
2023-02-23 12:06:39 +00:00
|
|
|
* @param bool $useNid
|
2018-11-24 14:15:26 +00:00
|
|
|
*
|
|
|
|
* @return Document
|
|
|
|
* @throws CacheDocumentDoesNotExistException
|
|
|
|
*/
|
|
|
|
public function getById(string $id, bool $public = false) {
|
|
|
|
$qb = $this->getCacheDocumentsSelectSql();
|
2023-02-23 12:06:39 +00:00
|
|
|
$qb->limitToIdPrim($qb->prim($id));
|
2018-11-24 14:15:26 +00:00
|
|
|
|
|
|
|
if ($public === true) {
|
|
|
|
$this->limitToPublic($qb);
|
|
|
|
}
|
2018-11-23 20:41:46 +00:00
|
|
|
|
|
|
|
$cursor = $qb->execute();
|
|
|
|
$data = $cursor->fetch();
|
|
|
|
$cursor->closeCursor();
|
|
|
|
|
|
|
|
if ($data === false) {
|
|
|
|
throw new CacheDocumentDoesNotExistException();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->parseCacheDocumentsSelectSql($data);
|
|
|
|
}
|
|
|
|
|
2018-11-26 12:33:48 +00:00
|
|
|
|
2019-01-18 16:55:10 +00:00
|
|
|
/**
|
|
|
|
* @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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-11-26 12:33:48 +00:00
|
|
|
/**
|
|
|
|
* @return Document[]
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
public function getNotCachedDocuments() {
|
|
|
|
$qb = $this->getCacheDocumentsSelectSql();
|
|
|
|
$this->limitToDBFieldEmpty($qb, 'local_copy');
|
2018-11-28 21:30:45 +00:00
|
|
|
$this->limitToCaching($qb, self::CACHING_TIMEOUT);
|
2018-11-28 15:17:23 +00:00
|
|
|
$this->limitToDBFieldInt($qb, 'error', 0);
|
2018-11-26 12:33:48 +00:00
|
|
|
|
|
|
|
$documents = [];
|
|
|
|
$cursor = $qb->execute();
|
|
|
|
while ($data = $cursor->fetch()) {
|
|
|
|
$documents[] = $this->parseCacheDocumentsSelectSql($data);
|
|
|
|
}
|
|
|
|
$cursor->closeCursor();
|
|
|
|
|
|
|
|
return $documents;
|
|
|
|
}
|
|
|
|
|
2018-12-05 18:37:49 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $url
|
|
|
|
*/
|
|
|
|
public function deleteByUrl(string $url) {
|
|
|
|
$qb = $this->getCacheDocumentsDeleteSql();
|
|
|
|
$this->limitToUrl($qb, $url);
|
|
|
|
|
|
|
|
$qb->execute();
|
|
|
|
}
|
|
|
|
|
2018-12-29 14:07:57 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $id
|
|
|
|
*/
|
|
|
|
public function deleteById(string $id) {
|
|
|
|
$qb = $this->getCacheDocumentsDeleteSql();
|
|
|
|
$this->limitToIdString($qb, $id);
|
|
|
|
|
|
|
|
$qb->execute();
|
|
|
|
}
|
2022-12-07 00:14:12 +00:00
|
|
|
|
2022-12-07 10:45:17 +00:00
|
|
|
public function deleteByParent(string $parentId): void {
|
2022-12-07 00:14:12 +00:00
|
|
|
$qb = $this->getCacheDocumentsDeleteSql();
|
|
|
|
$qb->limitToDBField('parent_id_prim', $qb->prim($parentId));
|
|
|
|
|
|
|
|
$qb->executeStatement();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function moveAccount(string $actorId, string $newId): void {
|
|
|
|
$qb = $this->getCacheDocumentsUpdateSql();
|
|
|
|
$qb->set('parent_id', $qb->createNamedParameter($newId))
|
|
|
|
->set('parent_id_prim', $qb->createNamedParameter($qb->prim($newId)));
|
|
|
|
|
|
|
|
$qb->limitToDBField('parent_id_prim', $qb->prim($actorId));
|
|
|
|
|
2022-12-07 10:45:17 +00:00
|
|
|
$qb->executeStatement();
|
2022-12-07 00:14:12 +00:00
|
|
|
}
|
2023-04-13 14:39:22 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Document[]
|
|
|
|
* @deprecated in 0.7.x
|
|
|
|
*/
|
|
|
|
public function getOldFormatCopies(): array {
|
|
|
|
$qb = $this->getCacheDocumentsSelectSql();
|
|
|
|
|
|
|
|
$expr = $qb->expr();
|
|
|
|
$qb->andWhere(
|
|
|
|
$expr->orX(
|
|
|
|
$expr->iLike('local_copy', $qb->createNamedParameter('%/%')),
|
|
|
|
$expr->iLike('resized_copy', $qb->createNamedParameter('%/%'))
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$documents = [];
|
|
|
|
$cursor = $qb->execute();
|
|
|
|
while ($data = $cursor->fetch()) {
|
|
|
|
$documents[] = $this->parseCacheDocumentsSelectSql($data);
|
|
|
|
}
|
|
|
|
$cursor->closeCursor();
|
|
|
|
|
|
|
|
return $documents;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @deprecated in 0.7.x
|
|
|
|
*/
|
|
|
|
public function updateCopies(Document $document): void {
|
|
|
|
$qb = $this->getCacheDocumentsUpdateSql();
|
|
|
|
$qb->set('local_copy', $qb->createNamedParameter($document->getLocalCopy()))
|
|
|
|
->set('resized_copy', $qb->createNamedParameter($document->getResizedCopy()));
|
|
|
|
|
|
|
|
$qb->limitToIdPrim($qb->prim($document->getId()));
|
|
|
|
$qb->executeStatement();
|
|
|
|
}
|
2018-11-23 20:41:46 +00:00
|
|
|
}
|