Document, Image and Caching

Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
pull/53/head
Maxence Lange 2018-11-23 19:41:46 -01:00 zatwierdzone przez Julius Härtl
rodzic 0944d07da6
commit 9aacccf719
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 4C614C6ED2CDE6DF
8 zmienionych plików z 694 dodań i 0 usunięć

Wyświetl plik

@ -374,5 +374,72 @@
</declaration>
</table>
<table>
<name>*dbprefix*social_cache_documents</name>
<declaration>
<field>
<name>id</name>
<type>string</type>
<length>127</length>
<notnull>true</notnull>
<primary>true</primary>
</field>
<field>
<name>type</name>
<type>text</type>
<length>31</length>
<notnull>true</notnull>
</field>
<field>
<name>media_type</name>
<type>text</type>
<length>63</length>
<notnull>true</notnull>
</field>
<field>
<name>mime_type</name>
<type>text</type>
<length>63</length>
<notnull>true</notnull>
</field>
<field>
<name>url</name>
<type>text</type>
<length>127</length>
<notnull>true</notnull>
</field>
<field>
<name>local_copy</name>
<type>text</type>
<length>127</length>
<notnull>true</notnull>
</field>
<field>
<name>creation</name>
<type>timestamp</type>
</field>
<field>
<name>caching</name>
<type>timestamp</type>
</field>
<index>
<name>unique_url</name>
<unique>true</unique>
<field>
<name>url</name>
</field>
</index>
</declaration>
</table>
</database>

Wyświetl plik

@ -0,0 +1,87 @@
<?php
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;
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;
class CacheDocumentsRequest extends CacheDocumentsRequestBuilder {
/**
* insert cache about an Actor in database.
*
* @param Document $document
*/
public function save(Document $document) {
$qb = $this->getCacheDocumentsInsertSql();
$qb->setValue('id', $qb->createNamedParameter($document->getId()))
->setValue('type', $qb->createNamedParameter($document->getType()))
->setValue('url', $qb->createNamedParameter($document->getUrl()))
->setValue('local_copy', $qb->createNamedParameter($document->getLocalCopy()))
->setValue(
'creation',
$qb->createNamedParameter(new DateTime('now'), IQueryBuilder::PARAM_DATE)
);
$qb->execute();
}
/**
* @param string $url
*
* @return Document
* @throws CacheDocumentDoesNotExistException
* @throws SocialAppConfigException
* @throws UrlCloudException
*/
public function getFromSource(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);
}
}

Wyświetl plik

@ -0,0 +1,121 @@
<?php
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 daita\MySmallPhpTools\Traits\TArrayTools;
use OCA\Social\Exceptions\SocialAppConfigException;
use OCA\Social\Exceptions\UrlCloudException;
use OCA\Social\Model\ActivityPub\Document;
use OCP\DB\QueryBuilder\IQueryBuilder;
class CacheDocumentsRequestBuilder extends CoreRequestBuilder {
use TArrayTools;
/**
* Base of the Sql Insert request
*
* @return IQueryBuilder
*/
protected function getCacheDocumentsInsertSql(): IQueryBuilder {
$qb = $this->dbConnection->getQueryBuilder();
$qb->insert(self::TABLE_CACHE_DOCUMENTS);
return $qb;
}
/**
* Base of the Sql Update request
*
* @return IQueryBuilder
*/
protected function getCacheDocumentsUpdateSql(): IQueryBuilder {
$qb = $this->dbConnection->getQueryBuilder();
$qb->update(self::TABLE_CACHE_DOCUMENTS);
return $qb;
}
/**
* Base of the Sql Select request for Shares
*
* @return IQueryBuilder
*/
protected function getCacheDocumentsSelectSql(): IQueryBuilder {
$qb = $this->dbConnection->getQueryBuilder();
/** @noinspection PhpMethodParametersCountMismatchInspection */
$qb->select(
'cd.id', 'cd.type', 'cd.media_type', 'cd.mime_type', 'cd.url', 'cd.local_copy',
'cd.creation', 'cd.caching'
)
->from(self::TABLE_CACHE_DOCUMENTS, 'cd');
$this->defaultSelectAlias = 'cd';
return $qb;
}
/**
* Base of the Sql Delete request
*
* @return IQueryBuilder
*/
protected function getCacheDocumentsDeleteSql(): IQueryBuilder {
$qb = $this->dbConnection->getQueryBuilder();
$qb->delete(self::TABLE_CACHE_DOCUMENTS);
return $qb;
}
/**
* @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;
}
}

Wyświetl plik

@ -446,6 +446,60 @@ class CoreRequestBuilder {
return $actor;
}
/**
* @param IQueryBuilder $qb
* @param string $fieldDocumentId
*/
protected function leftJoinCacheDocuments(IQueryBuilder &$qb, string $fieldDocumentId) {
if ($qb->getType() !== QueryBuilder::SELECT) {
return;
}
$expr = $qb->expr();
$pf = $this->defaultSelectAlias;
// /** @noinspection PhpMethodParametersCountMismatchInspection */
$qb->selectAlias('cd.id', 'cachedocument_id')
->selectAlias('cd.type', 'cachedocument_type')
->selectAlias('cd.mime_type', 'cachedocument_mime_type')
->selectAlias('cd.media_type', 'cachedocument_media_type')
->selectAlias('cd.url', 'cachedocument_url')
->selectAlias('cd.local_copy', 'cachedocument_local_copy')
->selectAlias('cd.caching', 'cachedocument_caching')
->selectAlias('ca.creation', 'cachedocument_creation')
->leftJoin(
$this->defaultSelectAlias, CoreRequestBuilder::TABLE_CACHE_DOCUMENTS, 'cd',
$expr->eq($pf . '.' . $fieldDocumentId, 'cd.id')
);
}
/**
* @param array $data
*
* @return Document
* @throws InvalidResourceException
*/
protected function parseCacheDocumentsLeftJoin(array $data): Document {
$new = [];
foreach ($data as $k => $v) {
if (substr($k, 0, 14) === 'cachedocument_') {
$new[substr($k, 14)] = $v;
}
}
$document = new Document();
$document->importFromDatabase($new);
if ($document->getType() !== Image::TYPE) {
throw new InvalidResourceException();
}
return $document;
}
}

Wyświetl plik

@ -0,0 +1,8 @@
<?php
namespace OCA\Social\Exceptions;
class CacheDocumentDoesNotExistException extends \Exception {
}

Wyświetl plik

@ -0,0 +1,192 @@
<?php
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\Model\ActivityPub;
use JsonSerializable;
use OCA\Social\Exceptions\UrlCloudException;
/**
* Class Document
*
* @package OCA\Social\Model\ActivityPub
*/
class Document extends ACore implements JsonSerializable {
const TYPE = 'Document';
/** @var string */
private $mediaType = '';
/** @var string */
private $mimeType = '';
/** @var string */
private $localCopy = '';
/** @var string */
private $caching = '';
/**
* Document constructor.
*
* @param ACore $parent
*/
public function __construct($parent = null) {
parent::__construct($parent);
$this->setType(self::TYPE);
}
/**
* @return string
*/
public function getMediaType(): string {
return $this->mediaType;
}
/**
* @param string $mediaType
*
* @return ACore
*/
public function setMediaType(string $mediaType): ACore {
$this->mediaType = $mediaType;
return $this;
}
/**
* @return string
*/
public function getMimeType(): string {
return $this->mimeType;
}
/**
* @param string $mimeType
*
* @return ACore
*/
public function setMimeType(string $mimeType): ACore {
$this->mimeType = $mimeType;
return $this;
}
/**
* @return string
*/
public function getLocalCopy(): string {
return $this->localCopy;
}
/**
* @param string $localCopy
*
* @return Document
*/
public function setLocalCopy(string $localCopy): Document {
$this->localCopy = $localCopy;
return $this;
}
/**
* @return string
*/
public function getCaching(): string {
return $this->caching;
}
/**
* @param string $caching
*
* @return Document
*/
public function setCaching(string $caching): Document {
$this->caching = $caching;
return $this;
}
/**
* @param array $data
*
* @throws UrlCloudException
*/
public function import(array $data) {
parent::import($data);
$this->setMediaType($this->get('mediaType', $data, ''));
if ($this->getId() === '') {
$this->generateUniqueId('/documents/g');
}
}
/**
* @param array $data
*/
public function importFromDatabase(array $data) {
parent::importFromDatabase($data);
$this->setMediaType($this->get('media_type', $data, ''));
$this->setMimeType($this->get('mime_type', $data, ''));
$this->setCaching($this->get('caching', $data, ''));
}
/**
* @return array
*/
public function jsonSerialize(): array {
return array_merge(
parent::jsonSerialize(),
[
'mediaType' => $this->getMediaType(),
'mimeType' => $this->getMimeType(),
'localCopy' => $this->getLocalCopy()
]
);
}
}

Wyświetl plik

@ -0,0 +1,83 @@
<?php
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\Model\ActivityPub;
use JsonSerializable;
use OCA\Social\Exceptions\UrlCloudException;
/**
* Class Image
*
* @package OCA\Social\Model\ActivityPub
*/
class Image extends Document implements JsonSerializable {
const TYPE = 'Image';
/**
* Image constructor.
*
* @param ACore $parent
*/
public function __construct($parent = null) {
parent::__construct($parent);
$this->setType(self::TYPE);
}
/**
* @param array $data
*
* @throws UrlCloudException
*/
public function import(array $data) {
parent::import($data);
}
/**
* @return array
*/
public function jsonSerialize(): array {
return array_merge(
parent::jsonSerialize(),
[
]
);
}
}

Wyświetl plik

@ -0,0 +1,82 @@
<?php
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\Service\ActivityPub;
use OCA\Social\Db\CacheDocumentsRequest;
use OCA\Social\Model\ActivityPub\ACore;
use OCA\Social\Service\ICoreService;
use OCA\Social\Service\MiscService;
class DocumentService implements ICoreService {
/** @var CacheDocumentsRequest */
private $cacheDocumentsRequest;
/** @var MiscService */
private $miscService;
/**
* DocumentService constructor.
*
* @param CacheDocumentsRequest $cacheDocumentsRequest
* @param MiscService $miscService
*/
public function __construct(
CacheDocumentsRequest $cacheDocumentsRequest, MiscService $miscService
) {
$this->cacheDocumentsRequest = $cacheDocumentsRequest;
$this->miscService = $miscService;
}
public function getFromCache(array $documents) {
}
/**
* @param ACore $item
*/
public function parse(ACore $item) {
// TODO: Implement parse() method.
}
/**
* @param ACore $item
*/
public function delete(ACore $item) {
// TODO: Implement delete() method.
}
}