2023-03-07 10:09:41 +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 2023, 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\Service;
|
|
|
|
|
|
|
|
use OCA\Social\Exceptions\InvalidActionException;
|
|
|
|
use OCA\Social\Exceptions\StreamNotFoundException;
|
2023-03-21 00:24:53 +00:00
|
|
|
use OCA\Social\Model\ActivityPub\Actor\Person;
|
2023-03-07 10:09:41 +00:00
|
|
|
use OCA\Social\Model\ActivityPub\Stream;
|
|
|
|
use OCA\Social\Tools\Traits\TStringTools;
|
|
|
|
|
|
|
|
class ActionService {
|
|
|
|
use TStringTools;
|
|
|
|
|
|
|
|
private StreamService $streamService;
|
2023-03-21 00:24:53 +00:00
|
|
|
private BoostService $boostService;
|
2023-03-07 10:09:41 +00:00
|
|
|
private StreamActionService $streamActionService;
|
|
|
|
|
|
|
|
private const TRANSLATE = 'translate';
|
|
|
|
private const FAVOURITE = 'favourite';
|
|
|
|
private const UNFAVOURITE = 'unfavourite';
|
|
|
|
private const REBLOG = 'reblog';
|
|
|
|
private const UNREBLOG = 'unreblog';
|
|
|
|
private const BOOKMARK = 'bookmark';
|
|
|
|
private const UNBOOKMARK = 'unbookmark';
|
|
|
|
private const MUTE = 'mute';
|
|
|
|
private const UNMUTE = 'unmute';
|
|
|
|
private const PIN = 'pin';
|
|
|
|
private const UNPIN = 'unpin';
|
|
|
|
|
|
|
|
private static array $availableStatusAction = [
|
|
|
|
self::TRANSLATE,
|
|
|
|
self::FAVOURITE,
|
|
|
|
self::UNFAVOURITE,
|
|
|
|
self::REBLOG,
|
|
|
|
self::UNREBLOG,
|
|
|
|
self::BOOKMARK,
|
|
|
|
self::UNBOOKMARK,
|
|
|
|
self::MUTE,
|
|
|
|
self::UNMUTE,
|
|
|
|
self::PIN,
|
|
|
|
self::UNPIN
|
|
|
|
];
|
|
|
|
|
|
|
|
public function __construct(
|
|
|
|
StreamService $streamService,
|
2023-03-21 00:24:53 +00:00
|
|
|
BoostService $boostService,
|
2023-03-07 10:09:41 +00:00
|
|
|
StreamActionService $streamActionService
|
|
|
|
) {
|
|
|
|
$this->streamService = $streamService;
|
2023-03-21 00:24:53 +00:00
|
|
|
$this->boostService = $boostService;
|
2023-03-07 10:09:41 +00:00
|
|
|
$this->streamActionService = $streamActionService;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* should return null
|
|
|
|
* will return Stream only with translate action
|
|
|
|
*
|
|
|
|
* @param int $nid
|
|
|
|
* @param string $action
|
|
|
|
*
|
|
|
|
* @return Stream|null
|
|
|
|
* @throws InvalidActionException
|
|
|
|
*/
|
2023-03-21 00:24:53 +00:00
|
|
|
public function action(Person $actor, int $nid, string $action): ?Stream {
|
2023-03-07 10:09:41 +00:00
|
|
|
if (!in_array($action, self::$availableStatusAction)) {
|
|
|
|
throw new InvalidActionException();
|
|
|
|
}
|
|
|
|
|
|
|
|
$post = $this->streamService->getStreamByNid($nid);
|
|
|
|
|
|
|
|
switch ($action) {
|
|
|
|
case self::TRANSLATE:
|
|
|
|
return $this->translate($nid);
|
|
|
|
|
|
|
|
case self::FAVOURITE:
|
2023-03-21 00:24:53 +00:00
|
|
|
$this->favourite($actor, $post->getId());
|
2023-03-07 10:09:41 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case self::UNFAVOURITE:
|
2023-03-21 00:24:53 +00:00
|
|
|
$this->favourite($actor, $post->getId(), false);
|
2023-03-07 10:09:41 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case self::REBLOG:
|
2023-03-21 00:24:53 +00:00
|
|
|
$this->reblog($actor, $post->getId());
|
2023-03-07 10:09:41 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case self::UNREBLOG:
|
2023-03-21 00:24:53 +00:00
|
|
|
$this->reblog($actor, $post->getId(), false);
|
2023-03-07 10:09:41 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* TODO: returns a translated version of the Status
|
|
|
|
*
|
|
|
|
* @param int $nid
|
|
|
|
*
|
|
|
|
* @return Stream
|
|
|
|
* @throws StreamNotFoundException
|
|
|
|
*/
|
|
|
|
private function translate(int $nid): Stream {
|
|
|
|
return $this->streamService->getStreamByNid($nid);
|
|
|
|
}
|
|
|
|
|
2023-03-21 00:24:53 +00:00
|
|
|
private function favourite(Person $actor, string $postId, bool $enabled = true): void {
|
|
|
|
$this->boostService->delete($actor, $postId);
|
|
|
|
// $this->streamActionService->setActionBool($actor->getId(), $postId, StreamAction::LIKED, $enabled);
|
2023-03-07 10:09:41 +00:00
|
|
|
}
|
|
|
|
|
2023-03-21 00:24:53 +00:00
|
|
|
private function reblog(Person $actor, string $postId, bool $enabled = true): void {
|
2023-03-21 11:25:11 +00:00
|
|
|
if ($enabled) {
|
|
|
|
$this->boostService->create($actor, $postId);
|
|
|
|
} else {
|
|
|
|
$this->boostService->delete($actor, $postId);
|
|
|
|
}
|
2023-03-07 10:09:41 +00:00
|
|
|
}
|
|
|
|
}
|