From 94d653640c460a328cb5d53738760050fedbabea Mon Sep 17 00:00:00 2001 From: Maxence Lange Date: Mon, 19 Nov 2018 09:34:54 -0100 Subject: [PATCH] moving thing around Signed-off-by: Maxence Lange --- lib/Controller/ActivityPubController.php | 9 +- lib/Db/ActorsRequest.php | 2 +- lib/Db/ActorsRequestBuilder.php | 14 +- lib/Db/CoreRequestBuilder.php | 2 +- lib/Model/ActivityPub/ACore.php | 135 +++++++++---------- lib/Model/ActivityPub/Follow.php | 19 +-- lib/Model/ActivityPub/Note.php | 26 ++-- lib/Model/ActivityPub/OrderedCollection.php | 11 +- lib/Model/ActivityPub/Person.php | 20 +-- lib/Service/ActivityPub/NoteService.php | 2 +- lib/Service/ActivityPub/PersonService.php | 4 +- lib/Service/ActivityService.php | 139 +++++++------------- lib/Service/ConfigService.php | 4 +- lib/Service/ImportService.php | 116 +++++++++++++--- lib/Service/PostService.php | 15 ++- 15 files changed, 291 insertions(+), 227 deletions(-) diff --git a/lib/Controller/ActivityPubController.php b/lib/Controller/ActivityPubController.php index f31df274..fc7d2917 100644 --- a/lib/Controller/ActivityPubController.php +++ b/lib/Controller/ActivityPubController.php @@ -125,7 +125,7 @@ class ActivityPubController extends Controller { try { $actor = $this->actorService->getActor($username); - $actor->setTopLevel(true); +// $actor->setTopLevel(true); return $this->directSuccess($actor); } catch (Exception $e) { @@ -186,12 +186,11 @@ class ActivityPubController extends Controller { $this->activityService->checkRequest($this->request); $body = file_get_contents('php://input'); -// $this->miscService->log('Body: ' . $body); + $this->miscService->log('Body: ' . $body); $activity = $this->importService->import($body); - try { - $this->activityService->save($activity); + $this->importService->save($activity); } catch (Exception $e) { } @@ -249,7 +248,7 @@ class ActivityPubController extends Controller { try { $actor = $this->actorService->getActor($username); $followers = $this->followService->getFollowers($actor); - $followers->setTopLevel(true); +// $followers->setTopLevel(true); return $this->directSuccess($followers); } catch (Exception $e) { diff --git a/lib/Db/ActorsRequest.php b/lib/Db/ActorsRequest.php index eca61466..208f9723 100644 --- a/lib/Db/ActorsRequest.php +++ b/lib/Db/ActorsRequest.php @@ -64,7 +64,7 @@ class ActorsRequest extends ActorsRequestBuilder { */ public function create(Person $actor): string { - $id = $this->configService->getRoot() . '@' . $actor->getPreferredUsername(); + $id = $this->configService->getUrlRoot() . '@' . $actor->getPreferredUsername(); try { $qb = $this->getActorsInsertSql(); diff --git a/lib/Db/ActorsRequestBuilder.php b/lib/Db/ActorsRequestBuilder.php index 613eb074..3d11b9ca 100644 --- a/lib/Db/ActorsRequestBuilder.php +++ b/lib/Db/ActorsRequestBuilder.php @@ -109,7 +109,7 @@ class ActorsRequestBuilder extends CoreRequestBuilder { * @throws SocialAppConfigException */ protected function parseActorsSelectSql($data): Person { - $root = $this->configService->getRoot(); + $root = $this->configService->getUrlRoot(); $actor = new Person(); $actor->import($data); @@ -117,11 +117,13 @@ class ActorsRequestBuilder extends CoreRequestBuilder { ->setOutbox($actor->getId() . '/outbox') ->setFollowers($actor->getId() . '/followers') ->setFollowing($actor->getId() . '/following') - ->setSharedInbox($root . 'inbox'); - $actor->setAccount( - $actor->getPreferredUsername() . '@' . $this->configService->getCloudAddress(true) - ); - $actor->setUrl($actor->getId()); + ->setSharedInbox($root . 'inbox') + ->setLocal(true) + ->setAccount( + $actor->getPreferredUsername() . '@' . $this->configService->getCloudAddress(true) + ); + $actor->setUrlRoot($root) + ->setUrl($actor->getId()); return $actor; } diff --git a/lib/Db/CoreRequestBuilder.php b/lib/Db/CoreRequestBuilder.php index 203e6939..47461ef4 100644 --- a/lib/Db/CoreRequestBuilder.php +++ b/lib/Db/CoreRequestBuilder.php @@ -359,7 +359,7 @@ class CoreRequestBuilder { $actor = new Person(); $actor->import($new); - if ($actor->getType() !== 'Person') { + if ($actor->getType() !== Person::TYPE) { throw new InvalidResourceException(); } diff --git a/lib/Model/ActivityPub/ACore.php b/lib/Model/ActivityPub/ACore.php index 22bd9c4a..9fc4ee80 100644 --- a/lib/Model/ActivityPub/ACore.php +++ b/lib/Model/ActivityPub/ACore.php @@ -49,8 +49,8 @@ abstract class ACore implements JsonSerializable { /** @var string */ private $root = ''; - /** @var bool */ - private $isTopLevel = false; +// /** @var bool */ +// private $isTopLevel = false; /** @var array */ private $meta = []; @@ -115,14 +115,20 @@ abstract class ACore implements JsonSerializable { /** @var string */ private $source = ''; + /** @var null ACore */ + private $parent = null; /** * Core constructor. * - * @param bool $isTopLevel + * @param ACore $parent */ - public function __construct(bool $isTopLevel = false) { - $this->isTopLevel = $isTopLevel; + public function __construct($parent = null) { +// $this->isTopLevel = $isTopLevel; + + if ($parent instanceof ACore) { + $this->setParent($parent); + } } @@ -176,63 +182,22 @@ abstract class ACore implements JsonSerializable { /** - * @param string $meta - * @param string $value - * - * @return ACore - */ - public function addMeta(string $meta, string $value): ACore { - $this->meta[$meta] = $value; - - return $this; - } - - /** - * @param string $meta - * @param bool $value - * - * @return ACore - */ - public function addMetaBool(string $meta, bool $value): ACore { - $this->meta[$meta] = $value; - - return $this; - } - - /** - * @param string $meta - * - * @return string - */ - public function getMeta(string $meta): string { - return $this->get($meta, $this->meta, ''); - } - - /** - * @param string $meta + * @param string $url * * @return bool */ - public function getMetaBool(string $meta): bool { - return $this->getBool($meta, $this->meta, false); - } + public function verify(string $url): bool { + if (parse_url($this->getId(), PHP_URL_HOST) !== + parse_url($url, PHP_URL_HOST)) + return false; - /** - * @param array $meta - * - * @return ACore - */ - public function setMetaAll(array $meta): ACore { - $this->meta = $meta; + \OC::$server->getLogger()->log(2, '#### ' . json_encode(parse_url($this->getId(), PHP_URL_PORT))); +// if (parse_url($this->getId(), PHP_URL_PORT) !== +// parse_url($url, PHP_URL_HOST)) +// return false; +// - return $this; - } - - /** - * @return array - */ - public function getMetaAll(): array { - return $this->meta; + return true; } @@ -368,7 +333,7 @@ abstract class ACore implements JsonSerializable { /** * @return string */ - public function getRoot(): string { + public function getUrlRoot(): string { return $this->root; } @@ -377,7 +342,7 @@ abstract class ACore implements JsonSerializable { * * @return ACore */ - public function setRoot(string $path): ACore { + public function setUrlRoot(string $path): ACore { $this->root = $path; return $this; @@ -594,21 +559,44 @@ abstract class ACore implements JsonSerializable { /** - * @param bool $topLevel + * @param ACore $parent * * @return ACore */ - public function setTopLevel(bool $topLevel): ACore { - $this->isTopLevel = $topLevel; + public function setParent(ACore $parent): ACore { + $this->parent = $parent; return $this; } + /** + * @return ACore + */ + public function getParent(): ACore { + return $this->parent; + } + /** * @return bool */ - public function isTopLevel(): bool { - return $this->isTopLevel; + public function isRoot(): bool { + return ($this->parent === null); + } + + /** + * @param array $chain + * + * @return ACore + */ + public function getRoot(array &$chain = []): ACore { + $chain[] = $this; + if ($this->isRoot()) { + return $this; + } + + + return $this->getParent() + ->getRoot($chain); } @@ -648,6 +636,22 @@ abstract class ACore implements JsonSerializable { return $this; } + /** + * @param string $k + * @param int $v + * + * @return ACore + */ + public function addEntryInt(string $k, int $v): ACore { + if ($v === 0) { + return $this; + } + + $this->entries[$k] = $v; + + return $this; + } + /** * @param string $k * @param array $v @@ -760,8 +764,7 @@ abstract class ACore implements JsonSerializable { * @return array */ public function jsonSerialize(): array { - - if ($this->isTopLevel()) { + if ($this->isRoot()) { $this->addEntryArray( '@context', [ self::CONTEXT_ACTIVITYSTREAMS, diff --git a/lib/Model/ActivityPub/Follow.php b/lib/Model/ActivityPub/Follow.php index fa9170f0..06c48dfd 100644 --- a/lib/Model/ActivityPub/Follow.php +++ b/lib/Model/ActivityPub/Follow.php @@ -42,15 +42,18 @@ use JsonSerializable; class Follow extends ACore implements JsonSerializable { - /** - * Activity constructor. - * - * @param bool $isTopLevel - */ - public function __construct(bool $isTopLevel = false) { - parent::__construct($isTopLevel); + const TYPE = 'Follow'; - $this->setType('Follow'); + + /** + * Follow constructor. + * + * @param ACore $parent + */ + public function __construct($parent = null) { + parent::__construct($parent); + + $this->setType(self::TYPE); } diff --git a/lib/Model/ActivityPub/Note.php b/lib/Model/ActivityPub/Note.php index df2f77e9..704b4a81 100644 --- a/lib/Model/ActivityPub/Note.php +++ b/lib/Model/ActivityPub/Note.php @@ -37,6 +37,9 @@ use OCA\Social\Service\ActivityService; class Note extends ACore implements JsonSerializable { + const TYPE = 'Note'; + + /** @var string */ private $content; @@ -53,18 +56,18 @@ class Note extends ACore implements JsonSerializable { private $conversation = ''; /** @var int */ - private $publishedTime; + private $publishedTime = 0; /** * Note constructor. * - * @param bool $isTopLevel + * @param ACore $parent */ - public function __construct(bool $isTopLevel = false) { - parent::__construct($isTopLevel); + public function __construct($parent = null) { + parent::__construct($parent); - $this->setType('Note'); + $this->setType(self::TYPE); } @@ -210,15 +213,16 @@ class Note extends ACore implements JsonSerializable { * @return array */ public function jsonSerialize(): array { + $this->addEntryInt('publishedTime', $this->getPublishedTime()); + return array_merge( parent::jsonSerialize(), [ - 'content' => $this->getContent(), - 'publishedTime' => $this->getPublishedTime(), - 'attributedTo' => $this->getRoot() . $this->getAttributedTo(), - 'inReplyTo' => $this->getInReplyTo(), - 'sensitive' => $this->isSensitive(), - 'conversation' => $this->getConversation() + 'content' => $this->getContent(), + 'attributedTo' => $this->getUrlRoot() . $this->getAttributedTo(), + 'inReplyTo' => $this->getInReplyTo(), + 'sensitive' => $this->isSensitive(), + 'conversation' => $this->getConversation() ] ); } diff --git a/lib/Model/ActivityPub/OrderedCollection.php b/lib/Model/ActivityPub/OrderedCollection.php index e3ea848e..0a2a0382 100644 --- a/lib/Model/ActivityPub/OrderedCollection.php +++ b/lib/Model/ActivityPub/OrderedCollection.php @@ -42,6 +42,9 @@ use JsonSerializable; class OrderedCollection extends ACore implements JsonSerializable { + const TYPE = 'OrderedCollection'; + + /** @var int */ private $totalItems = 0; @@ -51,12 +54,12 @@ class OrderedCollection extends ACore implements JsonSerializable { /** * Activity constructor. * - * @param bool $isTopLevel + * @param ACore $parent */ - public function __construct(bool $isTopLevel = false) { - parent::__construct($isTopLevel); + public function __construct($parent = null) { + parent::__construct($parent); - $this->setType('OrderedCollection'); + $this->setType(self::TYPE); } diff --git a/lib/Model/ActivityPub/Person.php b/lib/Model/ActivityPub/Person.php index 843a1177..ff8de11d 100644 --- a/lib/Model/ActivityPub/Person.php +++ b/lib/Model/ActivityPub/Person.php @@ -42,6 +42,9 @@ use JsonSerializable; class Person extends ACore implements JsonSerializable { + const TYPE = 'Person'; + + /** @var string */ private $userId = ''; @@ -84,15 +87,16 @@ class Person extends ACore implements JsonSerializable { /** @var bool */ private $local = false; + /** * Person constructor. * - * @param bool $isTopLevel + * @param ACore $parent */ - public function __construct(bool $isTopLevel = false) { - parent::__construct($isTopLevel); + public function __construct($parent = null) { + parent::__construct($parent); - $this->setType('Person'); + $this->setType(self::TYPE); } @@ -373,8 +377,8 @@ class Person extends ACore implements JsonSerializable { ->setFollowing($this->get('following', $data, '')) ->setSharedInbox($this->get('shared_inbox', $data, '')) ->setFeatured($this->get('featured', $data, '')) - ->setLocal(($this->getInt('local', $data, 0) === 1)) - ->setCreation($this->getInt('creation', $data, 0)); + ->setCreation($this->getInt('creation', $data, 0)) + ->setLocal(($this->getInt('local', $data, 0) === 1)); // if ($this->getPreferredUsername() === '') { // $this->setType('Invalid'); @@ -390,8 +394,8 @@ class Person extends ACore implements JsonSerializable { parent::jsonSerialize(), [ 'aliases' => [ - $this->getRoot() . '@' . $this->getPreferredUsername(), - $this->getRoot() . 'users/' . $this->getPreferredUsername() + $this->getUrlRoot() . '@' . $this->getPreferredUsername(), + $this->getUrlRoot() . 'users/' . $this->getPreferredUsername() ], 'preferredUsername' => $this->getPreferredUsername(), 'name' => $this->getName(), diff --git a/lib/Service/ActivityPub/NoteService.php b/lib/Service/ActivityPub/NoteService.php index 30d9e9d7..e143ef01 100644 --- a/lib/Service/ActivityPub/NoteService.php +++ b/lib/Service/ActivityPub/NoteService.php @@ -116,7 +116,7 @@ class NoteService implements ICoreService { $note->setId($this->configService->generateId('@' . $actor->getPreferredUsername())); $note->setPublished(date("c")); $note->setAttributedTo( - $this->configService->getRoot() . '@' . $actor->getPreferredUsername() + $this->configService->getUrlRoot() . '@' . $actor->getPreferredUsername() ); $this->setRecipient($note, $actor, $type); diff --git a/lib/Service/ActivityPub/PersonService.php b/lib/Service/ActivityPub/PersonService.php index 4fb7d6df..796f7ea7 100644 --- a/lib/Service/ActivityPub/PersonService.php +++ b/lib/Service/ActivityPub/PersonService.php @@ -133,7 +133,7 @@ class PersonService implements ICoreService { $actor->setSharedInbox($this->get('endpoints.sharedInbox', $object)); $actor->setAccount($actor->getPreferredUsername() . '@' . $this->get('_host', $object)); - if ($actor->getType() !== 'Person') { + if ($actor->getType() !== Person::TYPE) { throw new InvalidResourceException(); } @@ -169,7 +169,7 @@ class PersonService implements ICoreService { $actor->setPublicKey($this->get('publicKey.publicKeyPem', $object)); $actor->setSharedInbox($this->get('endpoints.sharedInbox', $object)); - if ($actor->getType() !== 'Person') { + if ($actor->getType() !== Person::TYPE) { throw new InvalidResourceException(); } diff --git a/lib/Service/ActivityService.php b/lib/Service/ActivityService.php index 4ca380e5..8d23ecf7 100644 --- a/lib/Service/ActivityService.php +++ b/lib/Service/ActivityService.php @@ -34,21 +34,16 @@ use daita\MySmallPhpTools\Model\Request; use daita\MySmallPhpTools\Traits\TArrayTools; use DateTime; use Exception; -use OC\User\NoUserException; use OCA\Social\Db\ActorsRequest; use OCA\Social\Exceptions\ActorDoesNotExistException; use OCA\Social\Exceptions\InvalidResourceException; use OCA\Social\Exceptions\RequestException; use OCA\Social\Exceptions\SocialAppConfigException; -use OCA\Social\Exceptions\UnknownItemException; use OCA\Social\Model\ActivityPub\ACore; -use OCA\Social\Model\ActivityPub\Activity; +use OCA\Social\Model\ActivityPub\Activity\Create; use OCA\Social\Model\ActivityPub\Person; use OCA\Social\Model\InstancePath; -use OCA\Social\Service\ActivityPub\FollowService; -use OCA\Social\Service\ActivityPub\NoteService; use OCA\Social\Service\ActivityPub\PersonService; -use OCA\Social\Service\ActivityPub\UndoService; use OCP\IRequest; class ActivityService { @@ -76,15 +71,6 @@ class ActivityService { /** @var PersonService */ private $personService; - /** @var NoteService */ - private $noteService; - - /** @var UndoService */ - private $undoService; - - /** @var FollowService */ - private $followService; - /** @var InstanceService */ private $instanceService; @@ -105,27 +91,20 @@ class ActivityService { * @param CurlService $curlService * @param ActorService $actorService * @param PersonService $personService - * @param NoteService $noteService - * @param UndoService $undoService - * @param FollowService $followService * @param InstanceService $instanceService * @param ConfigService $configService * @param MiscService $miscService */ public function __construct( ActorsRequest $actorsRequest, CurlService $curlService, ActorService $actorService, - PersonService $personService, NoteService $noteService, UndoService $undoService, - FollowService $followService, - InstanceService $instanceService, ConfigService $configService, + PersonService $personService, InstanceService $instanceService, + ConfigService $configService, MiscService $miscService ) { $this->curlService = $curlService; $this->actorsRequest = $actorsRequest; $this->actorService = $actorService; $this->personService = $personService; - $this->noteService = $noteService; - $this->undoService = $undoService; - $this->followService = $followService; $this->instanceService = $instanceService; $this->configService = $configService; $this->miscService = $miscService; @@ -139,21 +118,20 @@ class ActivityService { /** - * @param string $userId - * + * @param Person $actor * @param ACore $item * @param int $type * @param ACore $activity * * @return array - * @throws ActorDoesNotExistException - * @throws NoUserException * @throws RequestException + * @throws SocialAppConfigException */ - public function createActivity($userId, ACore $item, int $type, ACore &$activity = null + public function createActivity(Person $actor, ACore $item, int $type, ACore &$activity = null ): array { - $activity = new Activity(true); + $activity = new Create(); + $item->setParent($activity); // $this->activityStreamsService->initCore($activity); @@ -167,16 +145,28 @@ class ActivityService { // $activity->setTo($item->getTo()); // } - $actor = $this->actorService->getActorFromUserId($userId); $activity->setActor($actor); - $this->setupCore($activity); $result = $this->request($activity, $type); return $result; } + /** + * @param ACore $activity + * @param int $type + * + * @throws RequestException + * @throws SocialAppConfigException + */ + public function manageRequest(ACore $activity, int $type) { + $result = $this->request($activity, $type); + $this->miscService->log('Activity: ' . json_encode($activity)); + $this->miscService->log('Result: ' . json_encode($result)); + } + + /** * @param ACore $activity * @@ -186,8 +176,8 @@ class ActivityService { * @throws RequestException * @throws SocialAppConfigException */ - public function request(ACore $activity, int $type) { - + public function request(ACore &$activity, int $type) { + $this->setupCore($activity); $hosts = $this->instanceService->getInstancesFromActivity($activity); $result = []; @@ -210,15 +200,16 @@ class ActivityService { * @return Request[] * @throws RequestException * @throws SocialAppConfigException + * @throws ActorDoesNotExistException */ public function generateRequest(string $address, InstancePath $path, int $type, ACore $activity ): array { $document = json_encode($activity); $date = gmdate(self::DATE_FORMAT); - $localActor = $activity->getActor(); + $localActor = $this->getActorFromActivity($activity); $localActorLink = - $this->configService->getRoot() . '@' . $localActor->getPreferredUsername(); + $this->configService->getUrlRoot() . '@' . $localActor->getPreferredUsername(); $signature = "(request-target): post " . $path->getPath() . "\nhost: " . $address . "\ndate: " . $date; @@ -229,10 +220,12 @@ class ActivityService { 'keyId="' . $localActorLink . '",headers="(request-target) host date",signature="' . $signed . '"'; + $requestType = Request::TYPE_GET; if ($type === self::REQUEST_INBOX) { $requestType = Request::TYPE_POST; } + $request = new Request($path->getPath(), $requestType); $request->addHeader('Host: ' . $address); $request->addHeader('Date: ' . $date); @@ -286,20 +279,6 @@ class ActivityService { // } - /** - * @param string $uriId - * - * @return Person - * @throws RequestException - */ - private function getRemoteActor(string $uriId) { - $actor = $this->personService->getFromAccount($uriId); - - return $actor; - } - - - // /** // * @param array $hosts // * @@ -319,6 +298,24 @@ class ActivityService { // } + /** + * @param ACore $activity + * + * @return Person + * @throws SocialAppConfigException + * @throws ActorDoesNotExistException + */ + private function getActorFromActivity(Acore $activity): Person { + if ($activity->gotActor()) { + return $activity->getActor(); + } + + $actorId = $activity->getActorId(); + + return $this->actorService->getActorById($actorId); + } + + /** * @param IRequest $request * @@ -410,7 +407,7 @@ class ActivityService { private function setupCore(ACore $activity) { // $this->initCore($activity); - if ($activity->isTopLevel()) { + if ($activity->isRoot()) { $activity->addEntry('@context', self::CONTEXT_ACTIVITYSTREAMS); } @@ -425,45 +422,5 @@ class ActivityService { } - /** - * @param ACore $activity - * - * @throws UnknownItemException - */ - public function save(Acore $activity) { - - if ($activity->gotObject()) { - $this->save($activity->getObject()); - } - - switch ($activity->getType()) { -// case 'Activity': -// $service = $this; -// break; - - case 'Undo': - $service = $this->undoService; - break; - - case 'Follow': - $service = $this->followService; - break; - - case 'Note': - $service = $this->noteService; - break; - - default: - throw new UnknownItemException(); - } - - try { - $service->save($activity); - } catch (Exception $e) { - $this->miscService->log( - 2, 'Cannot save ' . $activity->getType() . ': ' . $e->getMessage() - ); - } - } } diff --git a/lib/Service/ConfigService.php b/lib/Service/ConfigService.php index 2ac6efe8..66e88c68 100644 --- a/lib/Service/ConfigService.php +++ b/lib/Service/ConfigService.php @@ -242,7 +242,7 @@ class ConfigService { * @return string * @throws SocialAppConfigException */ - public function getRoot(): string { + public function getUrlRoot(): string { return $this->getCloudAddress() . '/apps/social/'; } @@ -257,7 +257,7 @@ class ConfigService { public function generateId(string $path = '', $generateId = true): string { $path = $this->withoutBeginSlash($this->withEndSlash($path)); - $id = $this->getRoot() . $path; + $id = $this->getUrlRoot() . $path; if ($generateId === true) { $id .= time() . crc32(uniqid()); } diff --git a/lib/Service/ImportService.php b/lib/Service/ImportService.php index 7460f543..c7b9c977 100644 --- a/lib/Service/ImportService.php +++ b/lib/Service/ImportService.php @@ -27,16 +27,24 @@ declare(strict_types=1); * */ + namespace OCA\Social\Service; use daita\MySmallPhpTools\Traits\TArrayTools; +use Exception; use OCA\Social\Exceptions\UnknownItemException; use OCA\Social\Model\ActivityPub\ACore; -use OCA\Social\Model\ActivityPub\Activity; +use OCA\Social\Model\ActivityPub\Activity\Accept; +use OCA\Social\Model\ActivityPub\Activity\Create; +use OCA\Social\Model\ActivityPub\Activity\Reject; use OCA\Social\Model\ActivityPub\Follow; use OCA\Social\Model\ActivityPub\Note; -use OCA\Social\Model\ActivityPub\Undo; +use OCA\Social\Model\ActivityPub\Activity\Undo; +use OCA\Social\Service\ActivityPub\FollowService; +use OCA\Social\Service\ActivityPub\NoteService; +use OCA\Social\Service\ActivityPub\UndoService; + class ImportService { @@ -44,16 +52,34 @@ class ImportService { use TArrayTools; + /** @var NoteService */ + private $noteService; + + /** @var UndoService */ + private $undoService; + + /** @var FollowService */ + private $followService; + /** @var MiscService */ private $miscService; /** - * ActorService constructor. + * ImportService constructor. * + * @param NoteService $noteService + * @param UndoService $undoService + * @param FollowService $followService * @param MiscService $miscService */ - public function __construct(MiscService $miscService) { + public function __construct( + NoteService $noteService, UndoService $undoService, FollowService $followService, + MiscService $miscService + ) { + $this->noteService = $noteService; + $this->undoService = $undoService; + $this->followService = $followService; $this->miscService = $miscService; } @@ -66,7 +92,6 @@ class ImportService { */ public function import(string $json) { $data = json_decode($json, true); - $activity = $this->createItem($data, null); return $activity; @@ -82,32 +107,36 @@ class ImportService { */ private function createItem(array $data, $root = null): ACore { - $isTopLevel = ($root === null); +// $isTopLevel = ($root === null); switch ($this->get('type', $data)) { - case 'Create': - $item = new Activity($isTopLevel); + case Create::TYPE: + $item = new Create($root); break; - case 'Note': - $item = new Note($isTopLevel); + case Note::TYPE: + $item = new Note($root); break; - case 'Follow': - $item = new Follow($isTopLevel); + case Follow::TYPE: + $item = new Follow($root); break; - case 'Undo': - $item = new Undo($isTopLevel); + case Undo::TYPE: + $item = new Undo($root); + break; + + case Accept::TYPE: + $item = new Accept($root); + break; + + case Reject::TYPE: + $item = new Reject($root); break; default: throw new UnknownItemException(); } - if ($root instanceof ACore) { - $item->setMetaAll($root->getMetaAll()); - } - $item->import($data); $item->setSource(json_encode($data, JSON_UNESCAPED_SLASHES)); @@ -120,5 +149,56 @@ class ImportService { return $item; } + + /** + * @param ACore $activity + * + * @throws UnknownItemException + */ + public function save(Acore $activity) { + + if ($activity->gotObject()) { + $this->save($activity->getObject()); + } + + switch ($activity->getType()) { +// case 'Activity': +// $service = $this; +// break; + +// case Undo::TYPE: +// $service = $this->undoService; +// break; +// +// case Accept::TYPE: +// $service = $this->acceptService; +// break; +// +// case Reject::TYPE: +// $service = $this->rejectService; +// break; + + case Follow::TYPE: + $service = $this->followService; + break; + + case Note::TYPE: + $service = $this->noteService; + break; + + default: + throw new UnknownItemException(); + } + + try { + $service->save($activity); + } catch (Exception $e) { + $this->miscService->log( + 2, 'Cannot save ' . $activity->getType() . ': ' . $e->getMessage() + ); + } + } + + } diff --git a/lib/Service/PostService.php b/lib/Service/PostService.php index 458f8b25..b5100eee 100644 --- a/lib/Service/PostService.php +++ b/lib/Service/PostService.php @@ -33,8 +33,8 @@ namespace OCA\Social\Service; use OC\User\NoUserException; use OCA\Social\Exceptions\ActorDoesNotExistException; use OCA\Social\Exceptions\RequestException; +use OCA\Social\Exceptions\SocialAppConfigException; use OCA\Social\Model\ActivityPub\ACore; -use OCA\Social\Model\InstancePath; use OCA\Social\Model\Post; use OCA\Social\Service\ActivityPub\NoteService; @@ -44,6 +44,9 @@ class PostService { /** @var NoteService */ private $noteService; + /** @var ActorService */ + private $actorService; + /** @var ActivityService */ private $activityService; @@ -55,13 +58,16 @@ class PostService { * PostService constructor. * * @param NoteService $noteService + * @param ActorService $actorService * @param ActivityService $activityService * @param MiscService $miscService */ public function __construct( - NoteService $noteService, ActivityService $activityService, MiscService $miscService + NoteService $noteService, ActorService $actorService, ActivityService $activityService, + MiscService $miscService ) { $this->noteService = $noteService; + $this->actorService = $actorService; $this->activityService = $activityService; $this->miscService = $miscService; } @@ -75,6 +81,7 @@ class PostService { * @throws ActorDoesNotExistException * @throws NoUserException * @throws RequestException + * @throws SocialAppConfigException */ public function createPost(Post $post, ACore &$activity = null) { $note = @@ -85,8 +92,10 @@ class PostService { $this->noteService->addRecipients($note, $post->getType(), $post->getTo()); $this->noteService->replyTo($note, $post->getReplyTo()); + $actor = $this->actorService->getActorFromUserId($post->getUserId()); + return $this->activityService->createActivity( - $post->getUserId(), $note, ActivityService::REQUEST_INBOX, $activity + $actor, $note, ActivityService::REQUEST_INBOX, $activity ); }