2018-09-28 11:41:24 +00:00
|
|
|
<?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;
|
|
|
|
|
|
|
|
|
2019-01-02 11:20:11 +00:00
|
|
|
use DateTime;
|
2019-05-17 10:19:45 +00:00
|
|
|
use Exception;
|
2018-09-28 11:41:24 +00:00
|
|
|
use OCA\Social\Exceptions\ActorDoesNotExistException;
|
2018-11-15 11:26:25 +00:00
|
|
|
use OCA\Social\Exceptions\SocialAppConfigException;
|
2018-12-13 09:40:27 +00:00
|
|
|
use OCA\Social\Model\ActivityPub\Actor\Person;
|
2018-09-28 11:41:24 +00:00
|
|
|
use OCA\Social\Service\ConfigService;
|
|
|
|
use OCA\Social\Service\MiscService;
|
2019-01-02 11:20:11 +00:00
|
|
|
use OCP\DB\QueryBuilder\IQueryBuilder;
|
2018-09-28 11:41:24 +00:00
|
|
|
use OCP\IDBConnection;
|
|
|
|
|
|
|
|
class ActorsRequest extends ActorsRequestBuilder {
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2018-10-01 12:14:23 +00:00
|
|
|
* ActorsRequest constructor.
|
2018-09-28 11:41:24 +00:00
|
|
|
*
|
|
|
|
* @param IDBConnection $connection
|
|
|
|
* @param ConfigService $configService
|
|
|
|
* @param MiscService $miscService
|
|
|
|
*/
|
|
|
|
public function __construct(
|
|
|
|
IDBConnection $connection, ConfigService $configService, MiscService $miscService
|
|
|
|
) {
|
|
|
|
parent::__construct($connection, $configService, $miscService);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2018-11-12 22:55:39 +00:00
|
|
|
* create a new Person in the database.
|
2018-10-01 12:14:23 +00:00
|
|
|
*
|
2018-11-12 22:55:39 +00:00
|
|
|
* @param Person $actor
|
2018-09-28 11:41:24 +00:00
|
|
|
*
|
2018-11-14 13:37:54 +00:00
|
|
|
* @return string
|
2018-11-28 15:39:25 +00:00
|
|
|
* @throws SocialAppConfigException
|
2018-09-28 11:41:24 +00:00
|
|
|
*/
|
2018-11-14 13:37:54 +00:00
|
|
|
public function create(Person $actor): string {
|
2018-11-12 22:55:39 +00:00
|
|
|
|
2018-11-23 20:36:35 +00:00
|
|
|
$id = $this->configService->getUrlSocial() . '@' . $actor->getPreferredUsername();
|
2018-11-28 15:39:25 +00:00
|
|
|
$qb = $this->getActorsInsertSql();
|
2018-11-12 22:55:39 +00:00
|
|
|
|
2018-11-28 15:39:25 +00:00
|
|
|
$qb->setValue('id', $qb->createNamedParameter($id))
|
2018-11-12 22:55:39 +00:00
|
|
|
// ->setValue('type', $qb->createNamedParameter($actor->getType()))
|
2018-11-28 15:39:25 +00:00
|
|
|
->setValue('user_id', $qb->createNamedParameter($actor->getUserId()))
|
|
|
|
->setValue('name', $qb->createNamedParameter($actor->getName()))
|
|
|
|
->setValue('summary', $qb->createNamedParameter($actor->getSummary()))
|
2018-12-05 18:37:49 +00:00
|
|
|
->setValue('avatar_version', $qb->createNamedParameter($actor->getAvatarVersion()))
|
2018-11-28 15:39:25 +00:00
|
|
|
->setValue(
|
|
|
|
'preferred_username', $qb->createNamedParameter($actor->getPreferredUsername())
|
|
|
|
)
|
|
|
|
->setValue('public_key', $qb->createNamedParameter($actor->getPublicKey()))
|
2019-01-02 11:20:11 +00:00
|
|
|
->setValue('private_key', $qb->createNamedParameter($actor->getPrivateKey()))
|
|
|
|
->setValue(
|
|
|
|
'creation',
|
|
|
|
$qb->createNamedParameter(new DateTime('now'), IQueryBuilder::PARAM_DATE)
|
|
|
|
);
|
2018-11-28 15:39:25 +00:00
|
|
|
|
2019-03-05 12:28:14 +00:00
|
|
|
$this->generatePrimaryKey($qb, $id);
|
|
|
|
|
2018-11-28 15:39:25 +00:00
|
|
|
$qb->execute();
|
|
|
|
|
|
|
|
return $id;
|
2018-09-28 11:41:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-01-02 11:20:11 +00:00
|
|
|
/**
|
|
|
|
* @param Person $actor
|
|
|
|
*/
|
2018-12-05 18:37:49 +00:00
|
|
|
public function update(Person $actor) {
|
|
|
|
$qb = $this->getActorsUpdateSql();
|
|
|
|
$qb->set('avatar_version', $qb->createNamedParameter($actor->getAvatarVersion()));
|
|
|
|
$this->limitToIdString($qb, $actor->getId());
|
|
|
|
|
|
|
|
$qb->execute();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-01-02 11:20:11 +00:00
|
|
|
/**
|
|
|
|
* @param Person $actor
|
|
|
|
*/
|
|
|
|
public function refreshKeys(Person $actor) {
|
|
|
|
$qb = $this->getActorsUpdateSql();
|
|
|
|
$qb->set('public_key', $qb->createNamedParameter($actor->getPublicKey()))
|
2019-05-17 10:19:45 +00:00
|
|
|
->set('private_key', $qb->createNamedParameter($actor->getPrivateKey()));
|
|
|
|
|
|
|
|
try {
|
|
|
|
$qb->set(
|
|
|
|
'creation',
|
|
|
|
$qb->createNamedParameter(new DateTime('now'), IQueryBuilder::PARAM_DATE)
|
|
|
|
);
|
|
|
|
} catch (Exception $e) {
|
|
|
|
}
|
|
|
|
|
2019-01-02 11:20:11 +00:00
|
|
|
$this->limitToIdString($qb, $actor->getId());
|
|
|
|
|
|
|
|
$qb->execute();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-09-28 11:41:24 +00:00
|
|
|
/**
|
2018-10-01 12:14:23 +00:00
|
|
|
* return Actor from database based on the username
|
2018-09-28 11:41:24 +00:00
|
|
|
*
|
|
|
|
* @param string $username
|
|
|
|
*
|
2018-11-12 22:55:39 +00:00
|
|
|
* @return Person
|
2018-09-28 11:41:24 +00:00
|
|
|
* @throws ActorDoesNotExistException
|
2018-11-15 11:26:25 +00:00
|
|
|
* @throws SocialAppConfigException
|
2018-09-28 11:41:24 +00:00
|
|
|
*/
|
2018-11-12 22:55:39 +00:00
|
|
|
public function getFromUsername(string $username): Person {
|
2018-09-28 11:41:24 +00:00
|
|
|
$qb = $this->getActorsSelectSql();
|
|
|
|
$this->limitToPreferredUsername($qb, $username);
|
|
|
|
|
|
|
|
$cursor = $qb->execute();
|
|
|
|
$data = $cursor->fetch();
|
|
|
|
$cursor->closeCursor();
|
|
|
|
|
2018-11-13 14:30:48 +00:00
|
|
|
if ($data === false) {
|
|
|
|
throw new ActorDoesNotExistException('Actor not found');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->parseActorsSelectSql($data);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-11-14 13:37:54 +00:00
|
|
|
* @param string $id
|
|
|
|
*
|
2018-11-13 14:30:48 +00:00
|
|
|
* @return Person
|
|
|
|
* @throws ActorDoesNotExistException
|
2018-11-15 11:26:25 +00:00
|
|
|
* @throws SocialAppConfigException
|
2018-11-13 14:30:48 +00:00
|
|
|
*/
|
|
|
|
public function getFromId(string $id): Person {
|
|
|
|
$qb = $this->getActorsSelectSql();
|
|
|
|
$this->limitToIdString($qb, $id);
|
|
|
|
|
|
|
|
$cursor = $qb->execute();
|
|
|
|
$data = $cursor->fetch();
|
|
|
|
$cursor->closeCursor();
|
|
|
|
|
2018-09-28 11:41:24 +00:00
|
|
|
if ($data === false) {
|
|
|
|
throw new ActorDoesNotExistException('Actor not found');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->parseActorsSelectSql($data);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2018-10-01 12:14:23 +00:00
|
|
|
* return Actor from database, based on the userId of the owner.
|
2018-09-28 11:41:24 +00:00
|
|
|
*
|
|
|
|
* @param string $userId
|
|
|
|
*
|
2018-11-12 22:55:39 +00:00
|
|
|
* @return Person
|
2018-09-28 11:41:24 +00:00
|
|
|
* @throws ActorDoesNotExistException
|
2018-11-15 11:26:25 +00:00
|
|
|
* @throws SocialAppConfigException
|
2018-09-28 11:41:24 +00:00
|
|
|
*/
|
2018-11-12 22:55:39 +00:00
|
|
|
public function getFromUserId(string $userId): Person {
|
2018-09-28 11:41:24 +00:00
|
|
|
$qb = $this->getActorsSelectSql();
|
|
|
|
$this->limitToUserId($qb, $userId);
|
|
|
|
|
|
|
|
$cursor = $qb->execute();
|
|
|
|
$data = $cursor->fetch();
|
|
|
|
$cursor->closeCursor();
|
|
|
|
|
|
|
|
if ($data === false) {
|
|
|
|
throw new ActorDoesNotExistException('Actor not found');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->parseActorsSelectSql($data);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-11-26 12:33:48 +00:00
|
|
|
/**
|
|
|
|
* @return Person[]
|
|
|
|
* @throws SocialAppConfigException
|
|
|
|
*/
|
|
|
|
public function getAll(): array {
|
|
|
|
$qb = $this->getActorsSelectSql();
|
|
|
|
|
|
|
|
$accounts = [];
|
|
|
|
$cursor = $qb->execute();
|
|
|
|
while ($data = $cursor->fetch()) {
|
|
|
|
$accounts[] = $this->parseActorsSelectSql($data);
|
|
|
|
}
|
|
|
|
$cursor->closeCursor();
|
|
|
|
|
|
|
|
return $accounts;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-11-14 11:00:05 +00:00
|
|
|
/**
|
|
|
|
* @param string $search
|
|
|
|
*
|
|
|
|
* @return Person[]
|
2018-11-15 11:26:25 +00:00
|
|
|
* @throws SocialAppConfigException
|
2018-11-14 11:00:05 +00:00
|
|
|
*/
|
|
|
|
public function searchFromUsername(string $search): array {
|
|
|
|
$qb = $this->getActorsSelectSql();
|
|
|
|
$this->searchInPreferredUsername($qb, $search);
|
|
|
|
|
|
|
|
$accounts = [];
|
|
|
|
$cursor = $qb->execute();
|
|
|
|
while ($data = $cursor->fetch()) {
|
|
|
|
$accounts[] = $this->parseActorsSelectSql($data);
|
|
|
|
}
|
|
|
|
$cursor->closeCursor();
|
|
|
|
|
|
|
|
return $accounts;
|
|
|
|
}
|
|
|
|
|
2018-09-28 11:41:24 +00:00
|
|
|
}
|
2018-10-01 12:14:23 +00:00
|
|
|
|