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;
|
|
|
|
|
|
|
|
|
2018-10-12 06:44:35 +00:00
|
|
|
use daita\MySmallPhpTools\Traits\TArrayTools;
|
2018-11-16 11:00:39 +00:00
|
|
|
use DateTime;
|
2018-11-22 11:46:29 +00:00
|
|
|
use Doctrine\DBAL\Query\QueryBuilder;
|
2018-11-14 12:04:33 +00:00
|
|
|
use OCA\Social\Exceptions\InvalidResourceException;
|
2018-09-28 11:41:24 +00:00
|
|
|
use OCA\Social\Model\ActivityPub\Note;
|
2018-11-21 19:22:46 +00:00
|
|
|
use OCA\Social\Model\InstancePath;
|
2018-09-28 11:41:24 +00:00
|
|
|
use OCP\DB\QueryBuilder\IQueryBuilder;
|
|
|
|
|
|
|
|
class NotesRequestBuilder extends CoreRequestBuilder {
|
|
|
|
|
|
|
|
|
|
|
|
use TArrayTools;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Base of the Sql Insert request
|
|
|
|
*
|
|
|
|
* @return IQueryBuilder
|
|
|
|
*/
|
2018-10-01 12:14:23 +00:00
|
|
|
protected function getNotesInsertSql(): IQueryBuilder {
|
2018-09-28 11:41:24 +00:00
|
|
|
$qb = $this->dbConnection->getQueryBuilder();
|
|
|
|
$qb->insert(self::TABLE_SERVER_NOTES);
|
|
|
|
|
|
|
|
return $qb;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Base of the Sql Update request
|
|
|
|
*
|
|
|
|
* @return IQueryBuilder
|
|
|
|
*/
|
2018-10-01 12:14:23 +00:00
|
|
|
protected function getNotesUpdateSql(): IQueryBuilder {
|
2018-09-28 11:41:24 +00:00
|
|
|
$qb = $this->dbConnection->getQueryBuilder();
|
|
|
|
$qb->update(self::TABLE_SERVER_NOTES);
|
|
|
|
|
|
|
|
return $qb;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Base of the Sql Select request for Shares
|
|
|
|
*
|
|
|
|
* @return IQueryBuilder
|
|
|
|
*/
|
2018-10-01 12:14:23 +00:00
|
|
|
protected function getNotesSelectSql(): IQueryBuilder {
|
2018-09-28 11:41:24 +00:00
|
|
|
$qb = $this->dbConnection->getQueryBuilder();
|
|
|
|
|
|
|
|
/** @noinspection PhpMethodParametersCountMismatchInspection */
|
|
|
|
$qb->select(
|
2018-12-04 23:52:02 +00:00
|
|
|
'sn.id', 'sn.type', 'sn.to', 'sn.to_array', 'sn.cc', 'sn.bcc', 'sn.content',
|
|
|
|
'sn.summary',
|
2018-11-16 10:47:59 +00:00
|
|
|
'sn.published', 'sn.published_time', 'sn.attributed_to', 'sn.in_reply_to', 'sn.source',
|
2018-11-21 19:22:46 +00:00
|
|
|
'sn.local', 'sn.instances', 'sn.creation'
|
2018-09-28 11:41:24 +00:00
|
|
|
)
|
|
|
|
->from(self::TABLE_SERVER_NOTES, 'sn');
|
|
|
|
|
|
|
|
$this->defaultSelectAlias = 'sn';
|
|
|
|
|
|
|
|
return $qb;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-11-29 18:28:37 +00:00
|
|
|
/**
|
|
|
|
* Base of the Sql Select request for Shares
|
|
|
|
*
|
|
|
|
* @return IQueryBuilder
|
|
|
|
*/
|
|
|
|
protected function countNotesSelectSql(): IQueryBuilder {
|
|
|
|
$qb = $this->dbConnection->getQueryBuilder();
|
|
|
|
$qb->selectAlias($qb->createFunction('COUNT(*)'), 'count')
|
|
|
|
->from(self::TABLE_SERVER_NOTES, 'sn');
|
|
|
|
|
|
|
|
$this->defaultSelectAlias = 'sn';
|
|
|
|
|
|
|
|
return $qb;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-09-28 11:41:24 +00:00
|
|
|
/**
|
|
|
|
* Base of the Sql Delete request
|
|
|
|
*
|
|
|
|
* @return IQueryBuilder
|
|
|
|
*/
|
2018-10-01 12:14:23 +00:00
|
|
|
protected function getNotesDeleteSql(): IQueryBuilder {
|
2018-09-28 11:41:24 +00:00
|
|
|
$qb = $this->dbConnection->getQueryBuilder();
|
|
|
|
$qb->delete(self::TABLE_SERVER_NOTES);
|
|
|
|
|
|
|
|
return $qb;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-11-22 11:46:29 +00:00
|
|
|
/**
|
|
|
|
* @param IQueryBuilder $qb
|
2018-12-05 23:53:54 +00:00
|
|
|
* @param string $actorId
|
2018-11-22 11:46:29 +00:00
|
|
|
*/
|
2018-12-05 23:53:54 +00:00
|
|
|
protected function rightJoinFollowing(IQueryBuilder $qb, string $actorId = '') {
|
2018-11-22 11:46:29 +00:00
|
|
|
if ($qb->getType() !== QueryBuilder::SELECT) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$expr = $qb->expr();
|
2018-11-22 12:21:47 +00:00
|
|
|
$func = $qb->func();
|
|
|
|
$pf = $this->defaultSelectAlias . '.';
|
|
|
|
|
2018-11-22 11:46:29 +00:00
|
|
|
$orX = $expr->orX();
|
2018-12-05 23:53:54 +00:00
|
|
|
if ($actorId !== '') {
|
|
|
|
$orX->add($this->exprLimitToDBField($qb, 'attributed_to', $actorId, false));
|
|
|
|
}
|
|
|
|
|
|
|
|
// list of possible follow (to, to_array, cc, ...)
|
|
|
|
$orXFollow = $expr->orX();
|
|
|
|
$orXFollow->add($expr->eq($func->lower($pf . 'to'), $func->lower('f.follow_id')));
|
|
|
|
$orXFollow->add(
|
2018-11-22 12:21:47 +00:00
|
|
|
$expr->like(
|
2018-12-04 23:52:02 +00:00
|
|
|
$func->lower($pf . 'to_array'), $func->concat(
|
2018-11-22 12:37:19 +00:00
|
|
|
$qb->createNamedParameter('%"'),
|
2018-12-04 23:52:02 +00:00
|
|
|
$func->concat($func->lower('f.follow_id'), $qb->createNamedParameter('"%'))
|
2018-11-22 12:21:47 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
2018-12-05 23:53:54 +00:00
|
|
|
$orXFollow->add(
|
2018-11-22 12:21:47 +00:00
|
|
|
$expr->like(
|
2018-12-04 23:52:02 +00:00
|
|
|
$func->lower($pf . 'cc'), $func->concat(
|
2018-11-22 12:37:19 +00:00
|
|
|
$qb->createNamedParameter('%"'),
|
2018-12-04 23:52:02 +00:00
|
|
|
$func->concat($func->lower('f.follow_id'), $qb->createNamedParameter('"%'))
|
2018-11-22 12:21:47 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
2018-12-05 23:53:54 +00:00
|
|
|
$orXFollow->add(
|
2018-11-22 12:21:47 +00:00
|
|
|
$expr->like(
|
2018-12-04 23:52:02 +00:00
|
|
|
$func->lower($pf . 'bcc'), $func->concat(
|
2018-11-22 12:37:19 +00:00
|
|
|
$qb->createNamedParameter('%"'),
|
2018-12-04 23:52:02 +00:00
|
|
|
$func->concat($func->lower('f.follow_id'), $qb->createNamedParameter('"%'))
|
2018-11-22 12:21:47 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2018-12-05 23:53:54 +00:00
|
|
|
// all possible follow, but linked by followers (actor_id) and accepted follow
|
|
|
|
$andXFollow = $expr->andX();
|
|
|
|
$andXFollow->add($orXFollow);
|
|
|
|
$andXFollow->add($this->exprLimitToDBField($qb, 'actor_id', $actorId, false, 'f'));
|
|
|
|
$andXFollow->add($this->exprLimitToDBFieldInt($qb, 'accepted', 1, 'f'));
|
|
|
|
|
|
|
|
$orX->add($andXFollow);
|
|
|
|
|
2018-12-05 12:20:23 +00:00
|
|
|
// TODO: SQLite does not support RIGHT JOIN
|
|
|
|
// $qb->rightJoin(
|
|
|
|
$qb->join(
|
2018-11-22 11:46:29 +00:00
|
|
|
$this->defaultSelectAlias, CoreRequestBuilder::TABLE_SERVER_FOLLOWS, 'f',
|
|
|
|
$orX
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-09-28 11:41:24 +00:00
|
|
|
/**
|
|
|
|
* @param array $data
|
|
|
|
*
|
|
|
|
* @return Note
|
|
|
|
*/
|
|
|
|
protected function parseNotesSelectSql($data): Note {
|
2018-11-16 11:00:39 +00:00
|
|
|
$dTime = new DateTime($this->get('published_time', $data, 'yesterday'));
|
|
|
|
|
2018-11-23 20:36:35 +00:00
|
|
|
// TODO - use $note->importFromDatabase() ?
|
2018-09-28 11:41:24 +00:00
|
|
|
$note = new Note();
|
|
|
|
$note->setId($data['id'])
|
|
|
|
->setTo($data['to'])
|
|
|
|
->setToArray(json_decode($data['to_array'], true))
|
2018-11-12 22:55:39 +00:00
|
|
|
->setCcArray(json_decode($data['cc'], true))
|
|
|
|
->setBccArray(json_decode($data['bcc']))
|
2018-11-21 19:22:46 +00:00
|
|
|
->setLocal(($data['local'] === '1') ? true : false)
|
2018-11-12 22:55:39 +00:00
|
|
|
->setPublished($data['published']);
|
2018-09-28 11:41:24 +00:00
|
|
|
$note->setContent($data['content'])
|
2018-11-16 11:00:39 +00:00
|
|
|
->setPublishedTime($dTime->getTimestamp())
|
2018-09-28 11:41:24 +00:00
|
|
|
->setAttributedTo($data['attributed_to'])
|
|
|
|
->setInReplyTo($data['in_reply_to']);
|
|
|
|
|
2018-11-21 19:22:46 +00:00
|
|
|
$instances = json_decode($data['instances'], true);
|
2018-11-22 04:38:29 +00:00
|
|
|
if (is_array($instances)) {
|
|
|
|
foreach ($instances as $instance) {
|
2018-11-27 16:59:19 +00:00
|
|
|
$instancePath = new InstancePath();
|
|
|
|
$instancePath->import($instance);
|
|
|
|
$note->addInstancePath($instancePath);
|
2018-11-22 04:38:29 +00:00
|
|
|
}
|
2018-11-21 19:22:46 +00:00
|
|
|
}
|
|
|
|
|
2018-11-14 12:04:33 +00:00
|
|
|
try {
|
|
|
|
$actor = $this->parseCacheActorsLeftJoin($data);
|
|
|
|
$note->setCompleteDetails(true);
|
|
|
|
$note->setActor($actor);
|
|
|
|
} catch (InvalidResourceException $e) {
|
|
|
|
}
|
2018-09-28 11:41:24 +00:00
|
|
|
|
2018-11-14 12:04:33 +00:00
|
|
|
return $note;
|
2018-11-12 22:55:39 +00:00
|
|
|
}
|
|
|
|
|
2018-09-28 11:41:24 +00:00
|
|
|
}
|
|
|
|
|