2018-12-16 22:53:00 +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;
|
|
|
|
|
|
|
|
|
|
|
|
use daita\MySmallPhpTools\Traits\TArrayTools;
|
2019-01-03 11:36:46 +00:00
|
|
|
use OCA\Social\Exceptions\ItemUnknownException;
|
2018-12-16 22:53:00 +00:00
|
|
|
use OCA\Social\Exceptions\RedundancyLimitException;
|
|
|
|
use OCA\Social\Exceptions\SocialAppConfigException;
|
|
|
|
use OCA\Social\Interfaces\Activity\AcceptInterface;
|
|
|
|
use OCA\Social\Interfaces\Activity\AddInterface;
|
|
|
|
use OCA\Social\Interfaces\Activity\BlockInterface;
|
|
|
|
use OCA\Social\Interfaces\Activity\CreateInterface;
|
|
|
|
use OCA\Social\Interfaces\Activity\DeleteInterface;
|
|
|
|
use OCA\Social\Interfaces\Activity\FollowInterface;
|
|
|
|
use OCA\Social\Interfaces\Activity\LikeInterface;
|
|
|
|
use OCA\Social\Interfaces\Activity\RejectInterface;
|
|
|
|
use OCA\Social\Interfaces\Activity\RemoveInterface;
|
|
|
|
use OCA\Social\Interfaces\Activity\UndoInterface;
|
|
|
|
use OCA\Social\Interfaces\Activity\UpdateInterface;
|
|
|
|
use OCA\Social\Interfaces\Actor\PersonInterface;
|
|
|
|
use OCA\Social\Interfaces\IActivityPubInterface;
|
|
|
|
use OCA\Social\Interfaces\Object\NoteInterface;
|
|
|
|
use OCA\Social\Model\ActivityPub\ACore;
|
|
|
|
use OCA\Social\Model\ActivityPub\Activity\Accept;
|
|
|
|
use OCA\Social\Model\ActivityPub\Activity\Add;
|
|
|
|
use OCA\Social\Model\ActivityPub\Activity\Block;
|
|
|
|
use OCA\Social\Model\ActivityPub\Activity\Create;
|
|
|
|
use OCA\Social\Model\ActivityPub\Activity\Delete;
|
|
|
|
use OCA\Social\Model\ActivityPub\Activity\Follow;
|
|
|
|
use OCA\Social\Model\ActivityPub\Activity\Like;
|
|
|
|
use OCA\Social\Model\ActivityPub\Activity\Reject;
|
|
|
|
use OCA\Social\Model\ActivityPub\Activity\Remove;
|
|
|
|
use OCA\Social\Model\ActivityPub\Activity\Undo;
|
|
|
|
use OCA\Social\Model\ActivityPub\Activity\Update;
|
|
|
|
use OCA\Social\Model\ActivityPub\Actor\Person;
|
|
|
|
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\Object\Tombstone;
|
|
|
|
use OCA\Social\Service\ConfigService;
|
|
|
|
use OCP\AppFramework\QueryException;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class AP
|
|
|
|
*
|
|
|
|
* @package OCA\Social
|
|
|
|
*/
|
|
|
|
class AP {
|
|
|
|
|
|
|
|
|
|
|
|
use TArrayTools;
|
|
|
|
|
|
|
|
|
|
|
|
const REDUNDANCY_LIMIT = 10;
|
|
|
|
|
|
|
|
|
|
|
|
/** @var AcceptInterface */
|
|
|
|
public $acceptInterface;
|
|
|
|
|
|
|
|
/** @var AddInterface */
|
|
|
|
public $addInterface;
|
|
|
|
|
|
|
|
/** @var BlockInterface */
|
|
|
|
public $blockInterface;
|
|
|
|
|
|
|
|
/** @var CreateInterface */
|
|
|
|
public $createInterface;
|
|
|
|
|
|
|
|
/** @var DeleteInterface */
|
|
|
|
public $deleteInterface;
|
|
|
|
|
|
|
|
/** @var FollowInterface */
|
|
|
|
public $followInterface;
|
|
|
|
|
|
|
|
/** @var LikeInterface */
|
|
|
|
public $likeInterface;
|
|
|
|
|
|
|
|
/** @var PersonInterface */
|
|
|
|
public $personInterface;
|
|
|
|
|
|
|
|
/** @var NoteInterface */
|
|
|
|
public $noteInterface;
|
|
|
|
|
|
|
|
/** @var RejectInterface */
|
|
|
|
public $rejectInterface;
|
|
|
|
|
|
|
|
/** @var RemoveInterface */
|
|
|
|
public $removeInterface;
|
|
|
|
|
|
|
|
/** @var UndoInterface */
|
|
|
|
public $undoInterface;
|
|
|
|
|
|
|
|
/** @var UpdateInterface */
|
|
|
|
public $updateInterface;
|
|
|
|
|
|
|
|
/** @var ConfigService */
|
|
|
|
public $configService;
|
|
|
|
|
|
|
|
|
|
|
|
/** @var AP */
|
|
|
|
public static $activityPub = null;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* AP constructor.
|
|
|
|
*/
|
|
|
|
public function __construct() {
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public static function init() {
|
|
|
|
$ap = new AP();
|
|
|
|
try {
|
|
|
|
$ap->acceptInterface = \OC::$server->query(AcceptInterface::class);
|
|
|
|
$ap->addInterface = \OC::$server->query(AddInterface::class);
|
|
|
|
$ap->blockInterface = \OC::$server->query(BlockInterface::class);
|
|
|
|
$ap->createInterface = \OC::$server->query(CreateInterface::class);
|
|
|
|
$ap->deleteInterface = \OC::$server->query(DeleteInterface::class);
|
|
|
|
$ap->followInterface = \OC::$server->query(FollowInterface::class);
|
|
|
|
$ap->likeInterface = \OC::$server->query(LikeInterface::class);
|
|
|
|
$ap->rejectInterface = \OC::$server->query(RejectInterface::class);
|
|
|
|
$ap->removeInterface = \OC::$server->query(RemoveInterface::class);
|
|
|
|
$ap->personInterface = \OC::$server->query(PersonInterface::class);
|
|
|
|
$ap->noteInterface = \OC::$server->query(NoteInterface::class);
|
|
|
|
$ap->undoInterface = \OC::$server->query(UndoInterface::class);
|
|
|
|
$ap->updateInterface = \OC::$server->query(UpdateInterface::class);
|
|
|
|
|
|
|
|
$ap->configService = \OC::$server->query(ConfigService::class);
|
|
|
|
|
|
|
|
AP::$activityPub = $ap;
|
|
|
|
} catch (QueryException $e) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $data
|
|
|
|
* @param ACore $parent
|
|
|
|
* @param int $level
|
|
|
|
*
|
|
|
|
* @return ACore
|
|
|
|
* @throws RedundancyLimitException
|
|
|
|
* @throws SocialAppConfigException
|
2018-12-29 14:07:57 +00:00
|
|
|
* @throws ItemUnknownException
|
2018-12-16 22:53:00 +00:00
|
|
|
*/
|
|
|
|
public function getItemFromData(array $data, $parent = null, int $level = 0): ACore {
|
|
|
|
if (++$level > self::REDUNDANCY_LIMIT) {
|
|
|
|
throw new RedundancyLimitException($level);
|
|
|
|
}
|
|
|
|
|
|
|
|
$item = $this->getSimpleItemFromData($data);
|
|
|
|
if ($parent !== null) {
|
|
|
|
$item->setParent($parent);
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
$object = $this->getItemFromData($this->getArray('object', $data, []), $item, $level);
|
|
|
|
$item->setObject($object);
|
2018-12-29 14:07:57 +00:00
|
|
|
} catch (ItemUnknownException $e) {
|
2018-12-16 22:53:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
/** @var Document $icon */
|
|
|
|
$icon = $this->getItemFromData($this->getArray('icon', $data, []), $item, $level);
|
|
|
|
$item->setIcon($icon);
|
2018-12-29 14:07:57 +00:00
|
|
|
} catch (ItemUnknownException $e) {
|
2018-12-16 22:53:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $item;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $data
|
|
|
|
*
|
|
|
|
* @return ACore
|
|
|
|
* @throws SocialAppConfigException
|
2018-12-29 14:07:57 +00:00
|
|
|
* @throws ItemUnknownException
|
2018-12-16 22:53:00 +00:00
|
|
|
*/
|
|
|
|
public function getSimpleItemFromData(array $data): Acore {
|
|
|
|
$item = $this->getItemFromType($this->get('type', $data, ''));
|
|
|
|
$item->import($data);
|
|
|
|
$item->setSource(json_encode($data, JSON_UNESCAPED_SLASHES));
|
|
|
|
|
|
|
|
return $item;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $type
|
|
|
|
*
|
|
|
|
* @return ACore
|
2018-12-29 14:07:57 +00:00
|
|
|
* @throws ItemUnknownException
|
2019-01-03 11:36:46 +00:00
|
|
|
* @throws SocialAppConfigException
|
2018-12-16 22:53:00 +00:00
|
|
|
*/
|
2019-01-03 11:36:46 +00:00
|
|
|
public function getItemFromType(string $type): ACore {
|
|
|
|
|
2018-12-16 22:53:00 +00:00
|
|
|
switch ($type) {
|
|
|
|
case Accept::TYPE:
|
2019-01-03 11:36:46 +00:00
|
|
|
$item = new Accept();
|
|
|
|
break;
|
2018-12-16 22:53:00 +00:00
|
|
|
|
|
|
|
case Add::TYPE:
|
2019-01-03 11:36:46 +00:00
|
|
|
$item = new Add();
|
|
|
|
break;
|
2018-12-16 22:53:00 +00:00
|
|
|
|
|
|
|
case Block::TYPE:
|
2019-01-03 11:36:46 +00:00
|
|
|
$item = new Block();
|
|
|
|
break;
|
2018-12-16 22:53:00 +00:00
|
|
|
|
|
|
|
case Create::TYPE:
|
2019-01-03 11:36:46 +00:00
|
|
|
$item = new Create();
|
|
|
|
break;
|
2018-12-16 22:53:00 +00:00
|
|
|
|
|
|
|
case Delete::TYPE:
|
2019-01-03 11:36:46 +00:00
|
|
|
$item = new Delete();
|
|
|
|
break;
|
2018-12-16 22:53:00 +00:00
|
|
|
|
|
|
|
case Follow::TYPE:
|
2019-01-03 11:36:46 +00:00
|
|
|
$item = new Follow();
|
|
|
|
break;
|
2018-12-16 22:53:00 +00:00
|
|
|
|
|
|
|
case Image::TYPE:
|
2019-01-03 11:36:46 +00:00
|
|
|
$item = new Image();
|
|
|
|
break;
|
2018-12-16 22:53:00 +00:00
|
|
|
|
|
|
|
case Like::TYPE:
|
2019-01-03 11:36:46 +00:00
|
|
|
$item = new Like();
|
|
|
|
break;
|
2018-12-16 22:53:00 +00:00
|
|
|
|
|
|
|
case Note::TYPE:
|
2019-01-03 11:36:46 +00:00
|
|
|
$item = new Note();
|
|
|
|
break;
|
2018-12-16 22:53:00 +00:00
|
|
|
|
|
|
|
case Person::TYPE:
|
2019-01-03 11:36:46 +00:00
|
|
|
$item = new Person();
|
|
|
|
break;
|
2018-12-16 22:53:00 +00:00
|
|
|
|
|
|
|
case Reject::TYPE:
|
2019-01-03 11:36:46 +00:00
|
|
|
$item = new Reject();
|
|
|
|
break;
|
2018-12-16 22:53:00 +00:00
|
|
|
|
|
|
|
case Remove::TYPE:
|
2019-01-03 11:36:46 +00:00
|
|
|
$item = new Remove();
|
|
|
|
break;
|
2018-12-16 22:53:00 +00:00
|
|
|
|
|
|
|
case Tombstone::TYPE:
|
2019-01-03 11:36:46 +00:00
|
|
|
$item = new Tombstone();
|
|
|
|
break;
|
2018-12-16 22:53:00 +00:00
|
|
|
|
|
|
|
case Undo::TYPE:
|
2019-01-03 11:36:46 +00:00
|
|
|
$item = new Undo();
|
|
|
|
break;
|
2018-12-16 22:53:00 +00:00
|
|
|
|
|
|
|
case Update::TYPE:
|
2019-01-03 11:36:46 +00:00
|
|
|
$item = new Update();
|
|
|
|
break;
|
2018-12-16 22:53:00 +00:00
|
|
|
|
|
|
|
default:
|
2018-12-29 14:07:57 +00:00
|
|
|
throw new ItemUnknownException();
|
2018-12-16 22:53:00 +00:00
|
|
|
}
|
2019-01-03 11:36:46 +00:00
|
|
|
|
|
|
|
$item->setUrlCloud($this->configService->getCloudAddress());
|
|
|
|
|
|
|
|
return $item;
|
2018-12-16 22:53:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param ACore $activity
|
|
|
|
*
|
|
|
|
* @return IActivityPubInterface
|
2018-12-29 14:07:57 +00:00
|
|
|
* @throws ItemUnknownException
|
2018-12-16 22:53:00 +00:00
|
|
|
*/
|
|
|
|
public function getInterfaceForItem(Acore $activity): IActivityPubInterface {
|
|
|
|
return $this->getInterfaceFromType($activity->getType());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $type
|
|
|
|
*
|
|
|
|
* @return IActivityPubInterface
|
2018-12-29 14:07:57 +00:00
|
|
|
* @throws ItemUnknownException
|
2018-12-16 22:53:00 +00:00
|
|
|
*/
|
|
|
|
public function getInterfaceFromType(string $type): IActivityPubInterface {
|
|
|
|
switch ($type) {
|
|
|
|
case Accept::TYPE:
|
|
|
|
$service = $this->acceptInterface;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Add::TYPE:
|
|
|
|
$service = $this->addInterface;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Block::TYPE:
|
|
|
|
$service = $this->blockInterface;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Create::TYPE:
|
|
|
|
$service = $this->createInterface;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Delete::TYPE:
|
|
|
|
$service = $this->deleteInterface;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Follow::TYPE:
|
|
|
|
$service = $this->followInterface;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Like::TYPE:
|
|
|
|
$service = $this->likeInterface;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Note::TYPE:
|
|
|
|
$service = $this->noteInterface;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Person::TYPE:
|
|
|
|
$service = $this->personInterface;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Reject::TYPE:
|
|
|
|
$service = $this->rejectInterface;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Remove::TYPE:
|
|
|
|
$service = $this->removeInterface;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Undo::TYPE:
|
|
|
|
$service = $this->undoInterface;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Update::TYPE:
|
|
|
|
$service = $this->updateInterface;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2018-12-29 14:07:57 +00:00
|
|
|
throw new ItemUnknownException();
|
2018-12-16 22:53:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $service;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
AP::init();
|
|
|
|
|