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;
|
2019-01-24 11:26:44 +00:00
|
|
|
use daita\MySmallPhpTools\Traits\TStringTools;
|
2018-11-19 10:34:54 +00:00
|
|
|
use Exception;
|
2018-12-17 09:12:27 +00:00
|
|
|
use OCA\Social\AP;
|
2018-12-03 10:09:19 +00:00
|
|
|
use OCA\Social\Exceptions\ActivityPubFormatException;
|
2018-12-21 01:02:34 +00:00
|
|
|
use OCA\Social\Exceptions\InvalidOriginException;
|
2018-12-17 09:12:27 +00:00
|
|
|
use OCA\Social\Exceptions\RedundancyLimitException;
|
2018-11-23 20:53:01 +00:00
|
|
|
use OCA\Social\Exceptions\SocialAppConfigException;
|
2018-12-29 14:07:57 +00:00
|
|
|
use OCA\Social\Exceptions\ItemUnknownException;
|
2018-11-12 22:57:32 +00:00
|
|
|
use OCA\Social\Model\ActivityPub\ACore;
|
2018-11-19 10:34:54 +00:00
|
|
|
|
2018-11-12 22:57:32 +00:00
|
|
|
|
|
|
|
class ImportService {
|
|
|
|
|
|
|
|
|
|
|
|
use TArrayTools;
|
2019-01-24 11:26:44 +00:00
|
|
|
use TStringTools;
|
2018-11-12 22:57:32 +00:00
|
|
|
|
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-11-23 20:53:01 +00:00
|
|
|
* @param ConfigService $configService
|
2018-11-12 22:57:32 +00:00
|
|
|
* @param MiscService $miscService
|
|
|
|
*/
|
2018-12-17 09:12:27 +00:00
|
|
|
public function __construct(ConfigService $configService, MiscService $miscService) {
|
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
|
2018-12-19 01:13:33 +00:00
|
|
|
* @throws ActivityPubFormatException
|
2018-12-19 01:14:24 +00:00
|
|
|
* @throws RedundancyLimitException
|
2018-12-19 01:13:33 +00:00
|
|
|
* @throws SocialAppConfigException
|
2018-12-29 14:07:57 +00:00
|
|
|
* @throws ItemUnknownException
|
2018-11-12 22:57:32 +00:00
|
|
|
*/
|
2018-12-17 09:12:27 +00:00
|
|
|
public function importFromJson(string $json): ACore {
|
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();
|
|
|
|
}
|
2018-11-12 22:57:32 +00:00
|
|
|
|
2018-12-17 09:12:27 +00:00
|
|
|
return AP::$activityPub->getItemFromData($data);
|
2018-11-12 22:57:32 +00:00
|
|
|
}
|
|
|
|
|
2018-12-29 14:07:57 +00:00
|
|
|
|
2018-11-19 10:34:54 +00:00
|
|
|
/**
|
|
|
|
* @param ACore $activity
|
|
|
|
*
|
2018-12-29 14:07:57 +00:00
|
|
|
* @throws ItemUnknownException
|
2018-12-21 01:02:34 +00:00
|
|
|
* @throws InvalidOriginException
|
2018-11-19 10:34:54 +00:00
|
|
|
*/
|
2018-12-03 10:09:19 +00:00
|
|
|
public function parseIncomingRequest(ACore $activity) {
|
2018-12-21 01:02:34 +00:00
|
|
|
$activity->checkOrigin($activity->getId());
|
2019-01-24 11:26:44 +00:00
|
|
|
$activity->setRequestToken($this->uuid());
|
2018-12-13 09:40:27 +00:00
|
|
|
|
2018-12-21 01:02:34 +00:00
|
|
|
$interface = AP::$activityPub->getInterfaceForItem($activity);
|
2018-12-13 09:40:27 +00:00
|
|
|
try {
|
2018-12-17 09:12:27 +00:00
|
|
|
$interface->processIncomingRequest($activity);
|
2018-12-13 09:40:27 +00:00
|
|
|
} catch (Exception $e) {
|
|
|
|
$this->miscService->log(
|
2018-12-19 01:14:24 +00:00
|
|
|
'Cannot parse ' . $activity->getType() . ': ' . get_class($e) . ' '
|
|
|
|
. $e->getMessage()
|
2018-12-13 09:40:27 +00:00
|
|
|
);
|
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-11-12 22:57:32 +00:00
|
|
|
}
|
|
|
|
|