kopia lustrzana https://github.com/nextcloud/social
257 wiersze
6.5 KiB
PHP
257 wiersze
6.5 KiB
PHP
<?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 OCA\Social\AP;
|
|
use OCA\Social\Exceptions\InvalidResourceException;
|
|
use OCA\Social\Exceptions\ItemUnknownException;
|
|
use OCA\Social\Exceptions\SocialAppConfigException;
|
|
use OCA\Social\Exceptions\StreamNotFoundException;
|
|
use OCA\Social\Model\ActivityPub\ACore;
|
|
use OCA\Social\Model\ActivityPub\Object\Announce;
|
|
use OCA\Social\Model\ActivityPub\Stream;
|
|
use OCA\Social\Model\InstancePath;
|
|
use OCA\Social\Tools\Exceptions\CacheItemNotFoundException;
|
|
use OCA\Social\Tools\Exceptions\RowNotFoundException;
|
|
use OCA\Social\Tools\Traits\TArrayTools;
|
|
|
|
/**
|
|
* Class StreamRequestBuilder
|
|
*
|
|
* @package OCA\Social\Db
|
|
*/
|
|
class StreamRequestBuilder extends CoreRequestBuilder {
|
|
use TArrayTools;
|
|
|
|
|
|
/**
|
|
* Base of the Sql Insert request
|
|
*
|
|
* @return SocialQueryBuilder
|
|
*/
|
|
protected function getStreamInsertSql(): SocialQueryBuilder {
|
|
$qb = $this->getQueryBuilder();
|
|
$qb->insert(self::TABLE_STREAM);
|
|
|
|
return $qb;
|
|
}
|
|
|
|
|
|
/**
|
|
* Base of the Sql Update request
|
|
*
|
|
* @return SocialQueryBuilder
|
|
*/
|
|
protected function getStreamUpdateSql(): SocialQueryBuilder {
|
|
$qb = $this->getQueryBuilder();
|
|
$qb->update(self::TABLE_STREAM);
|
|
|
|
return $qb;
|
|
}
|
|
|
|
|
|
/**
|
|
* Base of the Sql Select request for Shares
|
|
*
|
|
* @param int $format
|
|
*
|
|
* @return SocialQueryBuilder
|
|
*/
|
|
protected function getStreamSelectSql(int $format = Stream::FORMAT_ACTIVITYPUB): SocialQueryBuilder {
|
|
$qb = $this->getQueryBuilder();
|
|
$qb->setFormat($format);
|
|
|
|
$qb->selectDistinct('s.id')
|
|
->from(self::TABLE_STREAM, 's');
|
|
foreach (self::$tables[self::TABLE_STREAM] as $field) {
|
|
if ($field === 'id') {
|
|
continue;
|
|
}
|
|
$qb->addSelect('s.' . $field);
|
|
}
|
|
|
|
$qb->setDefaultSelectAlias('s');
|
|
|
|
return $qb;
|
|
}
|
|
|
|
|
|
/**
|
|
* Base of the Sql Select request for Shares
|
|
*
|
|
* @return SocialQueryBuilder
|
|
*/
|
|
protected function countNotesSelectSql(): SocialQueryBuilder {
|
|
$qb = $this->getQueryBuilder();
|
|
$qb->selectAlias($qb->createFunction('COUNT(*)'), 'count')
|
|
->from(self::TABLE_STREAM, 's');
|
|
|
|
$qb->setDefaultSelectAlias('s');
|
|
|
|
return $qb;
|
|
}
|
|
|
|
|
|
/**
|
|
* Base of the Sql Delete request
|
|
*
|
|
* @return SocialQueryBuilder
|
|
*/
|
|
protected function getStreamDeleteSql(): SocialQueryBuilder {
|
|
$qb = $this->getQueryBuilder();
|
|
$qb->delete(self::TABLE_STREAM);
|
|
|
|
return $qb;
|
|
}
|
|
|
|
|
|
/**
|
|
* @param SocialQueryBuilder $qb
|
|
* @param string $alias
|
|
* @param string $aliasFollow
|
|
*/
|
|
protected function timelineHomeLinkCacheActor(
|
|
SocialQueryBuilder $qb, string $alias = 'ca', string $aliasFollow = 'f'
|
|
) {
|
|
$qb->linkToCacheActors($alias, 's.attributed_to_prim');
|
|
|
|
$expr = $qb->expr();
|
|
$orX = $expr->orX();
|
|
|
|
$follow = $expr->andX();
|
|
$follow->add($expr->eq($aliasFollow . '.type', $qb->createNamedParameter('Follow')));
|
|
// might be overkill to check object_id and also seems to filter boosted message
|
|
// $follow->add($expr->eq($alias . '.id_prim', $aliasFollow . '.object_id_prim'));
|
|
$orX->add($follow);
|
|
|
|
$loopback = $expr->andX();
|
|
$loopback->add($expr->eq($aliasFollow . '.type', $qb->createNamedParameter('Loopback')));
|
|
$loopback->add($expr->eq($alias . '.id_prim', $qb->getDefaultSelectAlias() . '.attributed_to_prim'));
|
|
$orX->add($loopback);
|
|
|
|
$qb->andWhere($orX);
|
|
}
|
|
|
|
|
|
/**
|
|
* @param SocialQueryBuilder $qb
|
|
*
|
|
* @return Stream
|
|
* @throws StreamNotFoundException
|
|
*/
|
|
protected function getStreamFromRequest(SocialQueryBuilder $qb): Stream {
|
|
/** @var Stream $result */
|
|
try {
|
|
$result = $qb->getRow([$this, 'parseStreamSelectSql']);
|
|
} catch (RowNotFoundException $e) {
|
|
throw new StreamNotFoundException('stream not found');
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
|
|
|
|
/**
|
|
* @param SocialQueryBuilder $qb
|
|
*
|
|
* @return Stream[]
|
|
*/
|
|
public function getStreamsFromRequest(SocialQueryBuilder $qb): array {
|
|
/** @var Stream[] $result */
|
|
$result = $qb->getRows([$this, 'parseStreamSelectSql']);
|
|
|
|
return $result;
|
|
}
|
|
|
|
|
|
/**
|
|
* @param array $data
|
|
* @param SocialQueryBuilder $qb
|
|
*
|
|
* @return Stream
|
|
* @throws ItemUnknownException
|
|
* @throws SocialAppConfigException
|
|
*/
|
|
public function parseStreamSelectSql(array $data, SocialQueryBuilder $qb): Stream {
|
|
$as = $this->get('type', $data, Stream::TYPE);
|
|
|
|
/** @var Stream $item */
|
|
$item = AP::$activityPub->getItemFromType($as);
|
|
$item->importFromDatabase($data);
|
|
$item->setExportFormat($qb->getFormat());
|
|
$instances = json_decode($this->get('instances', $data, '[]'), true);
|
|
if (is_array($instances)) {
|
|
foreach ($instances as $instance) {
|
|
$instancePath = new InstancePath();
|
|
$instancePath->import($instance);
|
|
$item->addInstancePath($instancePath);
|
|
}
|
|
}
|
|
|
|
try {
|
|
$actor = $qb->parseLeftJoinCacheActors($data, 'cacheactor_', $qb->getFormat());
|
|
$actor->setExportFormat($qb->getFormat());
|
|
$item->setCompleteDetails(true);
|
|
$item->setActor($actor);
|
|
} catch (InvalidResourceException $e) {
|
|
}
|
|
|
|
try {
|
|
$object = $qb->parseLeftJoinStream($data, 'objectstream_', ACore::FORMAT_LOCAL);
|
|
$item->setObject($object);
|
|
} catch (InvalidResourceException $e) {
|
|
}
|
|
|
|
$action = $this->parseStreamActionsLeftJoin($data);
|
|
$item->setAction($action);
|
|
|
|
if ($item->hasCache()) {
|
|
$cache = $item->getCache();
|
|
try {
|
|
$cachedItem = $cache->getItem($action->getStreamId());
|
|
$cachedObject = $cachedItem->getObject();
|
|
$cachedObject['action'] = $action;
|
|
$cachedItem->setContent(json_encode($cachedObject));
|
|
$cache->updateItem($cachedItem, false);
|
|
} catch (CacheItemNotFoundException $e) {
|
|
}
|
|
}
|
|
|
|
|
|
if ($item->getType() === Announce::TYPE) {
|
|
$item->setAttributedTo($this->get('following_actor_id', $data, ''));
|
|
}
|
|
|
|
return $item;
|
|
}
|
|
}
|