<?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;
use DateTime;
use Exception;
use OCA\Social\Exceptions\ItemUnknownException;
use OCA\Social\Exceptions\StreamNotFoundException;
use OCA\Social\Model\ActivityPub\ACore;
use OCA\Social\Model\ActivityPub\Internal\SocialAppNotification;
use OCA\Social\Model\ActivityPub\Object\Document;
use OCA\Social\Model\ActivityPub\Object\Note;
use OCA\Social\Model\ActivityPub\Stream;
use OCA\Social\Model\Client\Options\ProbeOptions;
use OCA\Social\Service\ConfigService;
use OCA\Social\Service\MiscService;
use OCA\Social\Tools\Exceptions\DateTimeException;
use OCA\Social\Tools\Model\Cache;
use OCP\DB\Exception as DBException;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection;
use OCP\IURLGenerator;
use Psr\Log\LoggerInterface;
/**
* Class StreamRequest
*
* @package OCA\Social\Db
*/
class StreamRequest extends StreamRequestBuilder {
private const NID_LIMIT = 1000000;
private StreamDestRequest $streamDestRequest;
private StreamTagsRequest $streamTagsRequest;
public function __construct(
IDBConnection $connection, LoggerInterface $logger, IURLGenerator $urlGenerator,
StreamDestRequest $streamDestRequest, StreamTagsRequest $streamTagsRequest,
ConfigService $configService, MiscService $miscService
) {
parent::__construct($connection, $logger, $urlGenerator, $configService, $miscService);
$this->streamDestRequest = $streamDestRequest;
$this->streamTagsRequest = $streamTagsRequest;
}
public function save(Stream $stream): void {
$qb = $this->saveStream($stream);
if ($stream->getType() === Note::TYPE) {
/** @var Note $stream */
$attachments = [];
foreach ($stream->getAttachments() as $item) {
$attachments[] = $item->asLocal(); // get attachment ready for local
}
$qb->setValue('hashtags', $qb->createNamedParameter(json_encode($stream->getHashtags())))
->setValue(
'attachments', $qb->createNamedParameter(json_encode($attachments, JSON_UNESCAPED_SLASHES)
)
);
}
try {
$qb->executeStatement();
$this->streamDestRequest->generateStreamDest($stream);
$this->streamTagsRequest->generateStreamTags($stream);
} catch (DBException $e) {
if ($e->getReason() !== DBException::REASON_CONSTRAINT_VIOLATION) {
$this->logger->error("Couldn't save stream: " . $e->getMessage(), [
'exception' => $e,
]);
}
}
}
public function update(Stream $stream, bool $generateDest = false): void {
$qb = $this->getStreamUpdateSql();
$qb->set('details', $qb->createNamedParameter(json_encode($stream->getDetailsAll())));
$qb->set('to', $qb->createNamedParameter($stream->getTo()));
$qb->set(
'cc', $qb->createNamedParameter(json_encode($stream->getCcArray(), JSON_UNESCAPED_SLASHES))
);
$qb->set(
'to_array', $qb->createNamedParameter(json_encode($stream->getToArray(), JSON_UNESCAPED_SLASHES))
);
$qb->limitToIdPrim($qb->prim($stream->getId()));
$qb->executeStatement();
if ($generateDest) {
$this->streamDestRequest->generateStreamDest($stream);
}
}
public function updateCache(Stream $stream, Cache $cache): void {
$qb = $this->getStreamUpdateSql();
$qb->set('cache', $qb->createNamedParameter(json_encode($cache, JSON_UNESCAPED_SLASHES)));
$qb->limitToIdPrim($qb->prim