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;
|
|
|
|
|
|
|
|
use DateTime;
|
|
|
|
use Exception;
|
2019-07-03 15:46:09 +00:00
|
|
|
use OCA\Social\Exceptions\ActionDoesNotExistException;
|
2019-06-29 20:35:41 +00:00
|
|
|
use OCA\Social\Model\ActivityPub\ACore;
|
2019-06-28 19:36:24 +00:00
|
|
|
use OCA\Social\Model\ActivityPub\Object\Like;
|
2022-11-22 23:16:36 +00:00
|
|
|
use OCA\Social\Tools\Traits\TArrayTools;
|
2019-06-28 19:36:24 +00:00
|
|
|
use OCP\DB\QueryBuilder\IQueryBuilder;
|
|
|
|
|
|
|
|
/**
|
2019-06-29 20:35:41 +00:00
|
|
|
* Class ActionsRequest
|
2019-06-28 19:36:24 +00:00
|
|
|
*
|
|
|
|
* @package OCA\Social\Db
|
|
|
|
*/
|
2019-06-29 20:35:41 +00:00
|
|
|
class ActionsRequest extends ActionsRequestBuilder {
|
2019-06-28 19:36:24 +00:00
|
|
|
use TArrayTools;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Insert a new Note in the database.
|
|
|
|
*/
|
2022-04-15 11:34:01 +00:00
|
|
|
public function save(ACore $like): void {
|
2019-06-29 20:35:41 +00:00
|
|
|
$qb = $this->getActionsInsertSql();
|
2019-06-28 19:36:24 +00:00
|
|
|
$qb->setValue('id', $qb->createNamedParameter($like->getId()))
|
2022-11-22 23:16:36 +00:00
|
|
|
->setValue('id_prim', $qb->createNamedParameter($qb->prim($like->getId())))
|
2019-06-28 19:36:24 +00:00
|
|
|
->setValue('actor_id', $qb->createNamedParameter($like->getActorId()))
|
2020-06-12 12:35:33 +00:00
|
|
|
->setValue('actor_id_prim', $qb->createNamedParameter($qb->prim($like->getActorId())))
|
2019-06-28 19:36:24 +00:00
|
|
|
->setValue('type', $qb->createNamedParameter($like->getType()))
|
2020-06-12 12:35:33 +00:00
|
|
|
->setValue('object_id', $qb->createNamedParameter($like->getObjectId()))
|
|
|
|
->setValue('object_id_prim', $qb->createNamedParameter($qb->prim($like->getObjectId())));
|
2019-06-28 19:36:24 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
$qb->setValue(
|
|
|
|
'creation',
|
|
|
|
$qb->createNamedParameter(new DateTime('now'), IQueryBuilder::PARAM_DATE)
|
|
|
|
);
|
|
|
|
} catch (Exception $e) {
|
|
|
|
}
|
|
|
|
|
2022-04-15 13:01:27 +00:00
|
|
|
$qb->executeStatement();
|
2019-06-28 19:36:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $actorId
|
|
|
|
* @param string $objectId
|
|
|
|
*
|
2019-06-29 20:35:41 +00:00
|
|
|
* @param string $type
|
|
|
|
*
|
|
|
|
* @return ACore
|
2019-07-03 15:46:09 +00:00
|
|
|
* @throws ActionDoesNotExistException
|
2019-06-28 19:36:24 +00:00
|
|
|
*/
|
2019-06-29 20:35:41 +00:00
|
|
|
public function getAction(string $actorId, string $objectId, string $type): ACore {
|
|
|
|
$qb = $this->getActionsSelectSql();
|
2020-06-12 12:35:33 +00:00
|
|
|
$qb->limitToActorIdPrim($qb->prim($actorId));
|
|
|
|
$qb->limitToObjectIdPrim($qb->prim($objectId));
|
|
|
|
$qb->limitToType($type);
|
2019-06-28 19:36:24 +00:00
|
|
|
|
2020-06-12 12:35:33 +00:00
|
|
|
return $this->getActionFromRequest($qb);
|
2019-06-28 19:36:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-07-08 14:42:16 +00:00
|
|
|
/**
|
|
|
|
* @param ACore $item
|
|
|
|
*
|
|
|
|
* @return ACore
|
|
|
|
* @throws ActionDoesNotExistException
|
|
|
|
*/
|
|
|
|
public function getActionFromItem(ACore $item): ACore {
|
|
|
|
$qb = $this->getActionsSelectSql();
|
|
|
|
|
2020-06-12 12:35:33 +00:00
|
|
|
$qb->limitToActorIdPrim($qb->prim($item->getActorId()));
|
|
|
|
$qb->limitToObjectIdPrim($qb->prim($item->getObjectId()));
|
|
|
|
$qb->limitToType($item->getType());
|
2019-07-08 14:42:16 +00:00
|
|
|
|
2020-06-12 12:35:33 +00:00
|
|
|
return $this->getActionFromRequest($qb);
|
2019-07-08 14:42:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-06-28 19:36:24 +00:00
|
|
|
/**
|
|
|
|
* @param string $objectId
|
2019-06-29 20:35:41 +00:00
|
|
|
* @param string $type
|
2019-06-28 19:36:24 +00:00
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
2019-06-29 20:35:41 +00:00
|
|
|
public function countActions(string $objectId, string $type): int {
|
|
|
|
$qb = $this->countActionsSelectSql();
|
2020-06-12 12:35:33 +00:00
|
|
|
$qb->limitToObjectIdPrim($qb->prim($objectId));
|
|
|
|
$qb->limitToType($type);
|
2019-06-28 19:36:24 +00:00
|
|
|
|
|
|
|
$cursor = $qb->execute();
|
|
|
|
$data = $cursor->fetch();
|
|
|
|
$cursor->closeCursor();
|
|
|
|
|
|
|
|
return $this->getInt('count', $data, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $objectId
|
|
|
|
*
|
|
|
|
* @return Like[]
|
|
|
|
*/
|
|
|
|
public function getByObjectId(string $objectId): array {
|
2019-06-29 20:35:41 +00:00
|
|
|
$qb = $this->getActionsSelectSql();
|
2020-06-12 12:35:33 +00:00
|
|
|
$qb->limitToObjectIdPrim($qb->prim($objectId));
|
2019-06-28 19:36:24 +00:00
|
|
|
$this->leftJoinCacheActors($qb, 'actor_id');
|
|
|
|
|
2020-06-12 12:35:33 +00:00
|
|
|
return $this->getActionsFromRequest($qb);
|
2019-06-28 19:36:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2019-07-03 15:46:09 +00:00
|
|
|
* @param ACore $item
|
2019-06-28 19:36:24 +00:00
|
|
|
*/
|
2019-07-03 15:46:09 +00:00
|
|
|
public function delete(ACore $item) {
|
2019-06-29 20:35:41 +00:00
|
|
|
$qb = $this->getActionsDeleteSql();
|
2019-07-03 15:46:09 +00:00
|
|
|
$this->limitToIdString($qb, $item->getId());
|
2019-07-08 14:42:16 +00:00
|
|
|
$this->limitToType($qb, $item->getType());
|
2019-06-28 19:36:24 +00:00
|
|
|
|
|
|
|
$qb->execute();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-12-07 00:14:12 +00:00
|
|
|
public function deleteByActor(string $actorId): void {
|
|
|
|
$qb = $this->getActionsDeleteSql();
|
|
|
|
$qb->limitToDBField('actor_id_prim', $qb->prim($actorId));
|
|
|
|
|
2022-12-07 10:45:17 +00:00
|
|
|
$qb->executeStatement();
|
2022-12-07 00:14:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function moveAccount(string $actorId, string $newId): void {
|
|
|
|
$qb = $this->getActionsUpdateSql();
|
|
|
|
$qb->set('actor_id', $qb->createNamedParameter($newId))
|
|
|
|
->set('actor_id_prim', $qb->createNamedParameter($qb->prim($newId)));
|
|
|
|
|
|
|
|
$qb->limitToDBField('actor_id_prim', $qb->prim($actorId));
|
|
|
|
|
2022-12-07 10:45:17 +00:00
|
|
|
$qb->executeStatement();
|
2022-12-07 00:14:12 +00:00
|
|
|
}
|
2019-06-28 19:36:24 +00:00
|
|
|
}
|