2019-06-28 19:36:24 +00:00
|
|
|
<?php
|
2022-04-15 11:34:01 +00:00
|
|
|
|
2019-06-28 19:36:24 +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;
|
|
|
|
|
2020-06-12 12:35:33 +00:00
|
|
|
use OCA\Social\Exceptions\ActionDoesNotExistException;
|
2019-06-28 19:36:24 +00:00
|
|
|
use OCA\Social\Exceptions\InvalidResourceException;
|
2019-06-29 20:35:41 +00:00
|
|
|
use OCA\Social\Model\ActivityPub\ACore;
|
2022-12-07 00:14:12 +00:00
|
|
|
use OCA\Social\Tools\Exceptions\RowNotFoundException;
|
|
|
|
use OCA\Social\Tools\Traits\TArrayTools;
|
2019-06-28 19:36:24 +00:00
|
|
|
|
|
|
|
/**
|
2019-06-29 20:35:41 +00:00
|
|
|
* Class ActionsRequestBuilder
|
2019-06-28 19:36:24 +00:00
|
|
|
*
|
|
|
|
* @package OCA\Social\Db
|
|
|
|
*/
|
2019-06-29 20:35:41 +00:00
|
|
|
class ActionsRequestBuilder extends CoreRequestBuilder {
|
2019-06-28 19:36:24 +00:00
|
|
|
use TArrayTools;
|
|
|
|
|
2020-06-12 12:35:33 +00:00
|
|
|
protected function getActionsInsertSql(): SocialQueryBuilder {
|
|
|
|
$qb = $this->getQueryBuilder();
|
2019-06-29 20:35:41 +00:00
|
|
|
$qb->insert(self::TABLE_ACTIONS);
|
2019-06-28 19:36:24 +00:00
|
|
|
|
|
|
|
return $qb;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Base of the Sql Update request
|
|
|
|
*/
|
2020-06-12 12:35:33 +00:00
|
|
|
protected function getActionsUpdateSql(): SocialQueryBuilder {
|
|
|
|
$qb = $this->getQueryBuilder();
|
2019-06-29 20:35:41 +00:00
|
|
|
$qb->update(self::TABLE_ACTIONS);
|
2019-06-28 19:36:24 +00:00
|
|
|
|
|
|
|
return $qb;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Base of the Sql Select request for Shares
|
|
|
|
*/
|
2020-06-12 12:35:33 +00:00
|
|
|
protected function getActionsSelectSql(): SocialQueryBuilder {
|
|
|
|
$qb = $this->getQueryBuilder();
|
2019-06-28 19:36:24 +00:00
|
|
|
|
|
|
|
/** @noinspection PhpMethodParametersCountMismatchInspection */
|
2019-06-29 20:35:41 +00:00
|
|
|
$qb->select('a.id', 'a.type', 'a.actor_id', 'a.object_id', 'a.creation')
|
|
|
|
->from(self::TABLE_ACTIONS, 'a');
|
2019-06-28 19:36:24 +00:00
|
|
|
|
2019-06-29 20:35:41 +00:00
|
|
|
$this->defaultSelectAlias = 'a';
|
2020-06-25 02:48:36 +00:00
|
|
|
$qb->setDefaultSelectAlias('a');
|
2019-06-28 19:36:24 +00:00
|
|
|
|
|
|
|
return $qb;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Base of the Sql Select request for Shares
|
|
|
|
*
|
2020-06-12 12:35:33 +00:00
|
|
|
* @return SocialQueryBuilder
|
2019-06-28 19:36:24 +00:00
|
|
|
*/
|
2020-06-12 12:35:33 +00:00
|
|
|
protected function countActionsSelectSql(): SocialQueryBuilder {
|
|
|
|
$qb = $this->getQueryBuilder();
|
2019-06-28 19:36:24 +00:00
|
|
|
$qb->selectAlias($qb->createFunction('COUNT(*)'), 'count')
|
2019-06-29 20:35:41 +00:00
|
|
|
->from(self::TABLE_ACTIONS, 'a');
|
2019-06-28 19:36:24 +00:00
|
|
|
|
2019-06-29 20:35:41 +00:00
|
|
|
$this->defaultSelectAlias = 'a';
|
2020-06-25 02:48:36 +00:00
|
|
|
$qb->setDefaultSelectAlias('a');
|
2019-06-28 19:36:24 +00:00
|
|
|
|
|
|
|
return $qb;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Base of the Sql Delete request
|
|
|
|
*
|
2020-06-12 12:35:33 +00:00
|
|
|
* @return SocialQueryBuilder
|
2019-06-28 19:36:24 +00:00
|
|
|
*/
|
2020-06-12 12:35:33 +00:00
|
|
|
protected function getActionsDeleteSql(): SocialQueryBuilder {
|
|
|
|
$qb = $this->getQueryBuilder();
|
2022-12-07 00:14:12 +00:00
|
|
|
$qb->delete(self::TABLE_ACTIONS)
|
|
|
|
->setDefaultSelectAlias('a');
|
2019-06-28 19:36:24 +00:00
|
|
|
|
|
|
|
return $qb;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-06-12 12:35:33 +00:00
|
|
|
/**
|
|
|
|
* @param SocialQueryBuilder $qb
|
|
|
|
*
|
|
|
|
* @return ACore
|
|
|
|
* @throws ActionDoesNotExistException
|
|
|
|
*/
|
|
|
|
protected function getActionFromRequest(SocialQueryBuilder $qb): ACore {
|
|
|
|
try {
|
2022-04-15 13:01:27 +00:00
|
|
|
/** @var ACore $result */
|
2020-06-12 12:35:33 +00:00
|
|
|
$result = $qb->getRow([$this, 'parseActionsSelectSql']);
|
|
|
|
} catch (RowNotFoundException $e) {
|
|
|
|
throw new ActionDoesNotExistException($e->getMessage());
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param SocialQueryBuilder $qb
|
|
|
|
*
|
|
|
|
* @return ACore[]
|
|
|
|
*/
|
|
|
|
public function getActionsFromRequest(SocialQueryBuilder $qb): array {
|
|
|
|
/** @var ACore[] $result */
|
|
|
|
$result = $qb->getRows([$this, 'parseActionsSelectSql']);
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-06-28 19:36:24 +00:00
|
|
|
/**
|
|
|
|
* @param array $data
|
2020-09-02 13:07:57 +00:00
|
|
|
* @param SocialQueryBuilder $qb
|
2019-06-28 19:36:24 +00:00
|
|
|
*
|
2019-06-29 20:35:41 +00:00
|
|
|
* @return ACore
|
2019-06-28 19:36:24 +00:00
|
|
|
*/
|
2020-09-02 13:07:57 +00:00
|
|
|
public function parseActionsSelectSql($data, SocialQueryBuilder $qb): ACore {
|
2019-06-29 20:35:41 +00:00
|
|
|
$item = new ACore();
|
|
|
|
$item->importFromDatabase($data);
|
2019-06-28 19:36:24 +00:00
|
|
|
|
|
|
|
try {
|
2020-09-02 13:07:57 +00:00
|
|
|
$actor = $qb->parseLeftJoinCacheActors($data);
|
2019-06-28 19:36:24 +00:00
|
|
|
$actor->setCompleteDetails(true);
|
|
|
|
|
2019-06-29 20:35:41 +00:00
|
|
|
$item->setActor($actor);
|
2019-06-28 19:36:24 +00:00
|
|
|
} catch (InvalidResourceException $e) {
|
|
|
|
}
|
|
|
|
|
2019-06-29 20:35:41 +00:00
|
|
|
return $item;
|
2019-06-28 19:36:24 +00:00
|
|
|
}
|
|
|
|
}
|