2018-11-12 22:57:32 +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/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2018-11-19 10:34:54 +00:00
|
|
|
|
2018-11-12 22:57:32 +00:00
|
|
|
namespace OCA\Social\Service;
|
|
|
|
|
|
|
|
|
|
|
|
use daita\MySmallPhpTools\Traits\TArrayTools;
|
2018-11-19 10:34:54 +00:00
|
|
|
use Exception;
|
2018-12-03 10:09:19 +00:00
|
|
|
use OCA\Social\Exceptions\ActivityPubFormatException;
|
2018-12-05 21:21:37 +00:00
|
|
|
use OCA\Social\Exceptions\InvalidResourceEntryException;
|
2018-11-23 20:53:01 +00:00
|
|
|
use OCA\Social\Exceptions\SocialAppConfigException;
|
2018-11-12 22:57:32 +00:00
|
|
|
use OCA\Social\Exceptions\UnknownItemException;
|
2018-11-23 20:53:01 +00:00
|
|
|
use OCA\Social\Exceptions\UrlCloudException;
|
2018-11-12 22:57:32 +00:00
|
|
|
use OCA\Social\Model\ActivityPub\ACore;
|
2018-11-19 10:34:54 +00:00
|
|
|
use OCA\Social\Model\ActivityPub\Activity\Accept;
|
2018-12-13 09:40:27 +00:00
|
|
|
use OCA\Social\Model\ActivityPub\Activity\Add;
|
|
|
|
use OCA\Social\Model\ActivityPub\Activity\Block;
|
2018-11-19 10:34:54 +00:00
|
|
|
use OCA\Social\Model\ActivityPub\Activity\Create;
|
2018-11-20 12:52:24 +00:00
|
|
|
use OCA\Social\Model\ActivityPub\Activity\Delete;
|
2018-12-13 09:40:27 +00:00
|
|
|
use OCA\Social\Model\ActivityPub\Activity\Follow;
|
|
|
|
use OCA\Social\Model\ActivityPub\Activity\Like;
|
2018-11-19 10:34:54 +00:00
|
|
|
use OCA\Social\Model\ActivityPub\Activity\Reject;
|
2018-12-13 09:40:27 +00:00
|
|
|
use OCA\Social\Model\ActivityPub\Activity\Remove;
|
2018-11-19 10:34:54 +00:00
|
|
|
use OCA\Social\Model\ActivityPub\Activity\Undo;
|
2018-12-13 09:40:27 +00:00
|
|
|
use OCA\Social\Model\ActivityPub\Activity\Update;
|
|
|
|
use OCA\Social\Model\ActivityPub\Object\Document;
|
|
|
|
use OCA\Social\Model\ActivityPub\Object\Image;
|
|
|
|
use OCA\Social\Model\ActivityPub\Object\Note;
|
|
|
|
use OCA\Social\Model\ActivityPub\Actor\Person;
|
|
|
|
use OCA\Social\Model\ActivityPub\Object\Tombstone;
|
|
|
|
use OCA\Social\Service\ActivityPub\Activity\AcceptService;
|
|
|
|
use OCA\Social\Service\ActivityPub\Activity\AddService;
|
|
|
|
use OCA\Social\Service\ActivityPub\Activity\BlockService;
|
|
|
|
use OCA\Social\Service\ActivityPub\Activity\CreateService;
|
|
|
|
use OCA\Social\Service\ActivityPub\Activity\DeleteService;
|
|
|
|
use OCA\Social\Service\ActivityPub\Activity\FollowService;
|
|
|
|
use OCA\Social\Service\ActivityPub\Activity\LikeService;
|
|
|
|
use OCA\Social\Service\ActivityPub\Activity\RejectService;
|
|
|
|
use OCA\Social\Service\ActivityPub\Activity\RemoveService;
|
|
|
|
use OCA\Social\Service\ActivityPub\Activity\UndoService;
|
|
|
|
use OCA\Social\Service\ActivityPub\Activity\UpdateService;
|
|
|
|
use OCA\Social\Service\ActivityPub\ICoreService;
|
|
|
|
use OCA\Social\Service\ActivityPub\Object\NoteService;
|
|
|
|
use OCA\Social\Service\ActivityPub\Actor\PersonService;
|
2018-11-19 10:34:54 +00:00
|
|
|
|
2018-11-12 22:57:32 +00:00
|
|
|
|
|
|
|
class ImportService {
|
|
|
|
|
|
|
|
|
|
|
|
use TArrayTools;
|
|
|
|
|
2018-12-13 09:40:27 +00:00
|
|
|
/** @var AcceptService */
|
|
|
|
private $acceptService;
|
|
|
|
|
|
|
|
/** @var AddService */
|
|
|
|
private $addService;
|
|
|
|
|
|
|
|
/** @var BlockService */
|
|
|
|
private $blockService;
|
|
|
|
|
|
|
|
/** @var CreateService */
|
|
|
|
private $createService;
|
|
|
|
|
|
|
|
/** @var DeleteService */
|
|
|
|
private $deleteService;
|
|
|
|
|
|
|
|
/** @var FollowService */
|
|
|
|
private $followService;
|
|
|
|
|
|
|
|
/** @var LikeService */
|
|
|
|
private $likeService;
|
|
|
|
|
|
|
|
/** @var PersonService */
|
|
|
|
private $personService;
|
2018-11-12 22:57:32 +00:00
|
|
|
|
2018-11-19 10:34:54 +00:00
|
|
|
/** @var NoteService */
|
|
|
|
private $noteService;
|
|
|
|
|
2018-12-13 09:40:27 +00:00
|
|
|
/** @var RejectService */
|
|
|
|
private $rejectService;
|
|
|
|
|
|
|
|
/** @var RemoveService */
|
|
|
|
private $removeService;
|
|
|
|
|
2018-11-19 10:34:54 +00:00
|
|
|
/** @var UndoService */
|
|
|
|
private $undoService;
|
|
|
|
|
2018-12-13 09:40:27 +00:00
|
|
|
/** @var UpdateService */
|
|
|
|
private $updateService;
|
2018-11-20 12:52:24 +00:00
|
|
|
|
2018-11-27 16:54:08 +00:00
|
|
|
/** @var ConfigService */
|
2018-11-23 20:53:01 +00:00
|
|
|
private $configService;
|
|
|
|
|
2018-11-12 22:57:32 +00:00
|
|
|
/** @var MiscService */
|
|
|
|
private $miscService;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2018-11-19 10:34:54 +00:00
|
|
|
* ImportService constructor.
|
2018-11-12 22:57:32 +00:00
|
|
|
*
|
2018-12-13 09:40:27 +00:00
|
|
|
* @param AcceptService $acceptService
|
|
|
|
* @param AddService $addService
|
|
|
|
* @param BlockService $blockService
|
|
|
|
* @param CreateService $createService
|
|
|
|
* @param DeleteService $deleteService
|
|
|
|
* @param FollowService $followService
|
2018-11-19 10:34:54 +00:00
|
|
|
* @param NoteService $noteService
|
2018-12-13 09:40:27 +00:00
|
|
|
* @param LikeService $likeService
|
|
|
|
* @param PersonService $personService
|
|
|
|
* @param RejectService $rejectService
|
|
|
|
* @param RemoveService $removeService
|
2018-11-19 10:34:54 +00:00
|
|
|
* @param UndoService $undoService
|
2018-12-13 09:40:27 +00:00
|
|
|
* @param UpdateService $updateService
|
2018-11-23 20:53:01 +00:00
|
|
|
* @param ConfigService $configService
|
2018-11-12 22:57:32 +00:00
|
|
|
* @param MiscService $miscService
|
|
|
|
*/
|
2018-11-19 10:34:54 +00:00
|
|
|
public function __construct(
|
2018-12-13 09:40:27 +00:00
|
|
|
AcceptService $acceptService, AddService $addService, BlockService $blockService,
|
|
|
|
CreateService $createService, DeleteService $deleteService, FollowService $followService,
|
|
|
|
NoteService $noteService, LikeService $likeService, PersonService $personService,
|
|
|
|
RejectService $rejectService, RemoveService $removeService,
|
|
|
|
UndoService $undoService, UpdateService $updateService,
|
|
|
|
ConfigService $configService, MiscService $miscService
|
2018-11-19 10:34:54 +00:00
|
|
|
) {
|
2018-12-13 09:40:27 +00:00
|
|
|
$this->acceptService = $acceptService;
|
|
|
|
$this->addService = $addService;
|
|
|
|
$this->blockService = $blockService;
|
|
|
|
$this->createService = $createService;
|
|
|
|
$this->deleteService = $deleteService;
|
|
|
|
$this->followService = $followService;
|
|
|
|
$this->likeService = $likeService;
|
|
|
|
$this->rejectService = $rejectService;
|
|
|
|
$this->removeService = $removeService;
|
|
|
|
$this->personService = $personService;
|
2018-11-19 10:34:54 +00:00
|
|
|
$this->noteService = $noteService;
|
|
|
|
$this->undoService = $undoService;
|
2018-12-13 09:40:27 +00:00
|
|
|
$this->updateService = $updateService;
|
2018-11-23 20:53:01 +00:00
|
|
|
$this->configService = $configService;
|
2018-11-12 22:57:32 +00:00
|
|
|
$this->miscService = $miscService;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $json
|
|
|
|
*
|
|
|
|
* @return ACore
|
|
|
|
* @throws UnknownItemException
|
2018-11-23 20:53:01 +00:00
|
|
|
* @throws UrlCloudException
|
|
|
|
* @throws SocialAppConfigException
|
2018-12-03 10:09:19 +00:00
|
|
|
* @throws ActivityPubFormatException
|
2018-11-12 22:57:32 +00:00
|
|
|
*/
|
2018-12-03 10:09:19 +00:00
|
|
|
public function importFromJson(string $json) {
|
2018-11-12 22:57:32 +00:00
|
|
|
$data = json_decode($json, true);
|
2018-12-03 10:09:19 +00:00
|
|
|
if (!is_array($data)) {
|
|
|
|
throw new ActivityPubFormatException();
|
|
|
|
}
|
|
|
|
$activity = $this->importFromData($data, null);
|
2018-11-12 22:57:32 +00:00
|
|
|
|
|
|
|
return $activity;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $data
|
|
|
|
* @param ACore $root
|
|
|
|
*
|
|
|
|
* @return ACore
|
|
|
|
* @throws UnknownItemException
|
2018-11-23 20:53:01 +00:00
|
|
|
* @throws UrlCloudException
|
|
|
|
* @throws SocialAppConfigException
|
2018-12-05 21:21:37 +00:00
|
|
|
* @throws InvalidResourceEntryException
|
2018-11-12 22:57:32 +00:00
|
|
|
*/
|
2018-12-03 10:09:19 +00:00
|
|
|
private function importFromData(array $data, $root = null): ACore {
|
|
|
|
|
|
|
|
// TODO - missing : Person (why not ?), OrderCollection (not yet), Document (should ?)
|
|
|
|
switch ($this->get('type', $data, '')) {
|
|
|
|
case Accept::TYPE:
|
|
|
|
$item = new Accept($root);
|
|
|
|
break;
|
2018-11-12 22:57:32 +00:00
|
|
|
|
2018-12-13 09:40:27 +00:00
|
|
|
case Add::TYPE:
|
|
|
|
$item = new Add($root);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Block::TYPE:
|
|
|
|
$item = new Block($root);
|
|
|
|
break;
|
|
|
|
|
2018-11-19 10:34:54 +00:00
|
|
|
case Create::TYPE:
|
|
|
|
$item = new Create($root);
|
|
|
|
break;
|
|
|
|
|
2018-11-20 12:52:24 +00:00
|
|
|
case Delete::TYPE:
|
|
|
|
$item = new Delete($root);
|
|
|
|
break;
|
|
|
|
|
2018-12-03 10:09:19 +00:00
|
|
|
case Follow::TYPE:
|
|
|
|
$item = new Follow($root);
|
2018-11-12 22:57:32 +00:00
|
|
|
break;
|
|
|
|
|
2018-11-23 20:52:14 +00:00
|
|
|
case Image::TYPE:
|
|
|
|
$item = new Image($root);
|
|
|
|
break;
|
|
|
|
|
2018-12-13 09:40:27 +00:00
|
|
|
case Like::TYPE:
|
|
|
|
$item = new Like($root);
|
|
|
|
break;
|
|
|
|
|
2018-12-03 10:09:19 +00:00
|
|
|
case Note::TYPE:
|
|
|
|
$item = new Note($root);
|
2018-11-12 22:57:32 +00:00
|
|
|
break;
|
|
|
|
|
2018-12-13 09:40:27 +00:00
|
|
|
case Person::TYPE:
|
|
|
|
$item = new Note($root);
|
|
|
|
break;
|
|
|
|
|
2018-12-03 10:09:19 +00:00
|
|
|
case Reject::TYPE:
|
|
|
|
$item = new Reject($root);
|
2018-11-12 22:57:32 +00:00
|
|
|
break;
|
|
|
|
|
2018-12-13 09:40:27 +00:00
|
|
|
case Remove::TYPE:
|
|
|
|
$item = new Remove($root);
|
|
|
|
break;
|
|
|
|
|
2018-12-03 10:09:19 +00:00
|
|
|
case Tombstone::TYPE:
|
|
|
|
$item = new Tombstone($root);
|
2018-11-19 10:34:54 +00:00
|
|
|
break;
|
|
|
|
|
2018-12-03 10:09:19 +00:00
|
|
|
case Undo::TYPE:
|
|
|
|
$item = new Undo($root);
|
2018-11-12 22:57:32 +00:00
|
|
|
break;
|
|
|
|
|
2018-12-13 09:40:27 +00:00
|
|
|
case Update::TYPE:
|
|
|
|
$item = new Update($root);
|
|
|
|
break;
|
|
|
|
|
2018-11-12 22:57:32 +00:00
|
|
|
default:
|
|
|
|
throw new UnknownItemException();
|
|
|
|
}
|
|
|
|
|
2018-11-23 20:36:35 +00:00
|
|
|
$item->setUrlCloud($this->configService->getCloudAddress());
|
2018-11-12 22:57:32 +00:00
|
|
|
$item->import($data);
|
2018-11-15 16:26:18 +00:00
|
|
|
$item->setSource(json_encode($data, JSON_UNESCAPED_SLASHES));
|
2018-11-12 22:57:32 +00:00
|
|
|
|
|
|
|
try {
|
2018-12-03 10:09:19 +00:00
|
|
|
$object = $this->importFromData($this->getArray('object', $data, []), $item);
|
2018-11-12 22:57:32 +00:00
|
|
|
$item->setObject($object);
|
|
|
|
} catch (UnknownItemException $e) {
|
|
|
|
}
|
|
|
|
|
2018-11-23 20:52:14 +00:00
|
|
|
try {
|
|
|
|
/** @var Document $icon */
|
2018-12-03 10:09:19 +00:00
|
|
|
$icon = $this->importFromData($this->getArray('icon', $data, []), $item);
|
2018-11-23 20:52:14 +00:00
|
|
|
$item->setIcon($icon);
|
|
|
|
} catch (UnknownItemException $e) {
|
|
|
|
}
|
|
|
|
|
2018-11-12 22:57:32 +00:00
|
|
|
return $item;
|
|
|
|
}
|
|
|
|
|
2018-11-19 10:34:54 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param ACore $activity
|
|
|
|
*
|
|
|
|
* @throws UnknownItemException
|
|
|
|
*/
|
2018-12-03 10:09:19 +00:00
|
|
|
public function parseIncomingRequest(ACore $activity) {
|
2018-11-19 10:34:54 +00:00
|
|
|
|
2018-12-13 09:40:27 +00:00
|
|
|
// not sure we need to recursive on activity/parsing.
|
|
|
|
// if ($activity->gotObject()) {
|
|
|
|
// try {
|
|
|
|
// $this->parseIncomingRequest($activity->getObject());
|
|
|
|
// } catch (UnknownItemException $e) {
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
$service = $this->getServiceForItem($activity);
|
|
|
|
|
|
|
|
try {
|
|
|
|
$service->processIncomingRequest($activity, $this);
|
|
|
|
} catch (Exception $e) {
|
|
|
|
$this->miscService->log(
|
|
|
|
'Cannot parse ' . $activity->getType() . ': ' . $e->getMessage()
|
|
|
|
);
|
2018-11-19 10:34:54 +00:00
|
|
|
}
|
2018-12-13 09:40:27 +00:00
|
|
|
}
|
|
|
|
|
2018-11-19 10:34:54 +00:00
|
|
|
|
2018-12-13 09:40:27 +00:00
|
|
|
/**
|
|
|
|
* @param ACore $activity
|
|
|
|
*
|
|
|
|
* @return ICoreService
|
|
|
|
* @throws UnknownItemException
|
|
|
|
*/
|
|
|
|
public function getServiceForItem(Acore $activity): ICoreService {
|
2018-11-19 10:34:54 +00:00
|
|
|
switch ($activity->getType()) {
|
|
|
|
|
2018-12-13 09:40:27 +00:00
|
|
|
case Accept::TYPE:
|
|
|
|
$service = $this->acceptService;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Add::TYPE:
|
|
|
|
$service = $this->addService;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Block::TYPE:
|
|
|
|
$service = $this->blockService;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Create::TYPE:
|
|
|
|
$service = $this->createService;
|
|
|
|
break;
|
|
|
|
|
2018-11-20 12:52:24 +00:00
|
|
|
case Delete::TYPE:
|
|
|
|
$service = $this->deleteService;
|
|
|
|
break;
|
|
|
|
|
2018-11-19 10:34:54 +00:00
|
|
|
case Follow::TYPE:
|
|
|
|
$service = $this->followService;
|
|
|
|
break;
|
|
|
|
|
2018-12-13 09:40:27 +00:00
|
|
|
case Like::TYPE:
|
|
|
|
$service = $this->likeService;
|
|
|
|
break;
|
|
|
|
|
2018-11-19 10:34:54 +00:00
|
|
|
case Note::TYPE:
|
|
|
|
$service = $this->noteService;
|
|
|
|
break;
|
|
|
|
|
2018-12-13 09:40:27 +00:00
|
|
|
case Person::TYPE:
|
|
|
|
$service = $this->personService;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Reject::TYPE:
|
|
|
|
$service = $this->rejectService;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Remove::TYPE:
|
|
|
|
$service = $this->removeService;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Undo::TYPE:
|
|
|
|
$service = $this->undoService;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Update::TYPE:
|
|
|
|
$service = $this->updateService;
|
|
|
|
break;
|
|
|
|
|
2018-11-19 10:34:54 +00:00
|
|
|
default:
|
|
|
|
throw new UnknownItemException();
|
|
|
|
}
|
|
|
|
|
2018-12-13 09:40:27 +00:00
|
|
|
return $service;
|
2018-11-19 10:34:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-11-12 22:57:32 +00:00
|
|
|
}
|
|
|
|
|