2018-11-12 22:55:39 +00:00
|
|
|
<?php
|
2022-04-15 11:34:01 +00:00
|
|
|
|
2018-11-12 22:55:39 +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;
|
|
|
|
|
2022-06-17 14:29:54 +00:00
|
|
|
use OCA\Social\Tools\Exceptions\RowNotFoundException;
|
|
|
|
use OCA\Social\Tools\Traits\TArrayTools;
|
2020-09-18 11:55:47 +00:00
|
|
|
use OCA\Social\Exceptions\FollowNotFoundException;
|
2018-11-21 19:22:09 +00:00
|
|
|
use OCA\Social\Exceptions\InvalidResourceException;
|
2019-01-21 16:26:01 +00:00
|
|
|
use OCA\Social\Model\ActivityPub\Object\Follow;
|
2018-11-12 22:55:39 +00:00
|
|
|
use OCP\DB\QueryBuilder\IQueryBuilder;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class FollowsRequestBuilder
|
|
|
|
*
|
|
|
|
* @package OCA\Social\Db
|
|
|
|
*/
|
|
|
|
class FollowsRequestBuilder extends CoreRequestBuilder {
|
|
|
|
use TArrayTools;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Base of the Sql Insert request
|
|
|
|
*
|
2020-06-23 12:11:49 +00:00
|
|
|
* @return SocialQueryBuilder
|
2018-11-12 22:55:39 +00:00
|
|
|
*/
|
2020-06-23 12:11:49 +00:00
|
|
|
protected function getFollowsInsertSql(): SocialQueryBuilder {
|
|
|
|
$qb = $this->getQueryBuilder();
|
2019-05-07 15:11:04 +00:00
|
|
|
$qb->insert(self::TABLE_FOLLOWS);
|
2018-11-12 22:55:39 +00:00
|
|
|
|
|
|
|
return $qb;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Base of the Sql Update request
|
|
|
|
*
|
|
|
|
* @return IQueryBuilder
|
|
|
|
*/
|
|
|
|
protected function getFollowsUpdateSql(): IQueryBuilder {
|
2020-09-18 11:55:47 +00:00
|
|
|
$qb = $this->getQueryBuilder();
|
2019-05-07 15:11:04 +00:00
|
|
|
$qb->update(self::TABLE_FOLLOWS);
|
2018-11-12 22:55:39 +00:00
|
|
|
|
|
|
|
return $qb;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Base of the Sql Select request for Shares
|
|
|
|
*
|
2020-06-25 02:48:36 +00:00
|
|
|
* @return SocialQueryBuilder
|
2018-11-12 22:55:39 +00:00
|
|
|
*/
|
2020-06-25 02:48:36 +00:00
|
|
|
protected function getFollowsSelectSql(): SocialQueryBuilder {
|
|
|
|
$qb = $this->getQueryBuilder();
|
2018-11-12 22:55:39 +00:00
|
|
|
|
|
|
|
/** @noinspection PhpMethodParametersCountMismatchInspection */
|
2018-12-04 23:49:17 +00:00
|
|
|
$qb->select(
|
|
|
|
'f.id', 'f.type', 'f.actor_id', 'f.object_id', 'f.follow_id', 'f.accepted', 'f.creation'
|
|
|
|
)
|
2019-05-07 15:11:04 +00:00
|
|
|
->from(self::TABLE_FOLLOWS, 'f');
|
2018-11-12 22:55:39 +00:00
|
|
|
|
|
|
|
$this->defaultSelectAlias = 'f';
|
2020-06-25 02:48:36 +00:00
|
|
|
$qb->setDefaultSelectAlias('f');
|
2018-11-12 22:55:39 +00:00
|
|
|
|
|
|
|
return $qb;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-11-29 18:28:37 +00:00
|
|
|
/**
|
|
|
|
* Base of the Sql Select request for Shares
|
|
|
|
*
|
2019-10-02 16:40:38 +00:00
|
|
|
* @return SocialQueryBuilder
|
2018-11-29 18:28:37 +00:00
|
|
|
*/
|
2019-10-02 16:40:38 +00:00
|
|
|
protected function countFollowsSelectSql(): SocialQueryBuilder {
|
|
|
|
$qb = $this->getQueryBuilder();
|
2018-11-29 18:28:37 +00:00
|
|
|
$qb->selectAlias($qb->createFunction('COUNT(*)'), 'count')
|
2019-05-07 15:11:04 +00:00
|
|
|
->from(self::TABLE_FOLLOWS, 'f');
|
2018-11-29 18:28:37 +00:00
|
|
|
|
2020-06-25 02:48:36 +00:00
|
|
|
$qb->setDefaultSelectAlias('f');
|
2018-11-29 18:28:37 +00:00
|
|
|
$this->defaultSelectAlias = 'f';
|
|
|
|
|
|
|
|
return $qb;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-11-12 22:55:39 +00:00
|
|
|
/**
|
|
|
|
* Base of the Sql Delete request
|
|
|
|
*
|
|
|
|
* @return IQueryBuilder
|
|
|
|
*/
|
|
|
|
protected function getFollowsDeleteSql(): IQueryBuilder {
|
2020-09-18 11:55:47 +00:00
|
|
|
$qb = $this->getQueryBuilder();
|
2019-05-07 15:11:04 +00:00
|
|
|
$qb->delete(self::TABLE_FOLLOWS);
|
2018-11-12 22:55:39 +00:00
|
|
|
|
|
|
|
return $qb;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-09-18 11:55:47 +00:00
|
|
|
/**
|
|
|
|
* @param SocialQueryBuilder $qb
|
|
|
|
*
|
|
|
|
* @return Follow
|
|
|
|
* @throws FollowNotFoundException
|
|
|
|
*/
|
|
|
|
protected function getFollowFromRequest(SocialQueryBuilder $qb): Follow {
|
|
|
|
/** @var Follow $result */
|
|
|
|
try {
|
|
|
|
$result = $qb->getRow([$this, 'parseFollowsSelectSql']);
|
|
|
|
} catch (RowNotFoundException $e) {
|
|
|
|
throw new FollowNotFoundException($e->getMessage());
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param SocialQueryBuilder $qb
|
|
|
|
*
|
|
|
|
* @return Follow[]
|
|
|
|
*/
|
|
|
|
public function getFollowsFromRequest(SocialQueryBuilder $qb): array {
|
|
|
|
/** @var Follow[] $result */
|
|
|
|
$result = $qb->getRows([$this, 'parseFollowsSelectSql']);
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-11-12 22:55:39 +00:00
|
|
|
/**
|
|
|
|
* @param array $data
|
2020-09-02 13:07:57 +00:00
|
|
|
* @param SocialQueryBuilder $qb
|
2018-11-12 22:55:39 +00:00
|
|
|
*
|
|
|
|
* @return Follow
|
|
|
|
*/
|
2020-09-18 11:55:47 +00:00
|
|
|
public function parseFollowsSelectSql(array $data, SocialQueryBuilder $qb): Follow {
|
2018-11-12 22:55:39 +00:00
|
|
|
$follow = new Follow();
|
2018-11-30 10:54:48 +00:00
|
|
|
$follow->importFromDatabase($data);
|
2018-11-21 19:22:09 +00:00
|
|
|
|
|
|
|
try {
|
2020-09-02 13:07:57 +00:00
|
|
|
$actor = $qb->parseLeftJoinCacheActors($data);
|
2018-12-04 08:27:56 +00:00
|
|
|
$actor->setCompleteDetails(true);
|
|
|
|
$this->assignDetails($actor, $data);
|
|
|
|
|
2018-11-21 19:22:09 +00:00
|
|
|
$follow->setCompleteDetails(true);
|
|
|
|
$follow->setActor($actor);
|
2019-08-12 11:00:40 +00:00
|
|
|
|
|
|
|
return $follow;
|
|
|
|
} catch (InvalidResourceException $e) {
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
$actor = $this->parseAccountsLeftJoin($data);
|
|
|
|
$follow->setActor($actor);
|
2018-11-21 19:22:09 +00:00
|
|
|
} catch (InvalidResourceException $e) {
|
|
|
|
}
|
2018-11-12 22:55:39 +00:00
|
|
|
|
|
|
|
return $follow;
|
|
|
|
}
|
|
|
|
}
|