diff options
author | Maxence Lange <maxence@artificial-owl.com> | 2018-11-19 09:34:54 -0100 |
---|---|---|
committer | Maxence Lange <maxence@artificial-owl.com> | 2018-11-19 09:34:54 -0100 |
commit | 94d653640c460a328cb5d53738760050fedbabea (patch) | |
tree | d7e13ca47aeb2b6296c5671b490713ffed3a1df7 | |
parent | 3cec05e7f8f20480ad8f1251edbdb8e0c32e9688 (diff) |
moving thing around
Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
-rw-r--r-- | lib/Controller/ActivityPubController.php | 9 | ||||
-rw-r--r-- | lib/Db/ActorsRequest.php | 2 | ||||
-rw-r--r-- | lib/Db/ActorsRequestBuilder.php | 14 | ||||
-rw-r--r-- | lib/Db/CoreRequestBuilder.php | 2 | ||||
-rw-r--r-- | lib/Model/ActivityPub/ACore.php | 135 | ||||
-rw-r--r-- | lib/Model/ActivityPub/Follow.php | 13 | ||||
-rw-r--r-- | lib/Model/ActivityPub/Note.php | 26 | ||||
-rw-r--r-- | lib/Model/ActivityPub/OrderedCollection.php | 11 | ||||
-rw-r--r-- | lib/Model/ActivityPub/Person.php | 20 | ||||
-rw-r--r-- | lib/Service/ActivityPub/NoteService.php | 2 | ||||
-rw-r--r-- | lib/Service/ActivityPub/PersonService.php | 4 | ||||
-rw-r--r-- | lib/Service/ActivityService.php | 139 | ||||
-rw-r--r-- | lib/Service/ConfigService.php | 4 | ||||
-rw-r--r-- | lib/Service/ImportService.php | 116 | ||||
-rw-r--r-- | lib/Service/PostService.php | 15 |
15 files changed, 288 insertions, 224 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); - } - - /** - * @param array $meta - * - * @return ACore - */ - public function setMetaAll(array $meta): ACore { - $this->meta = $meta; + public function verify(string $url): bool { + if (parse_url($this->getId(), PHP_URL_HOST) !== + parse_url($url, PHP_URL_HOST)) + return false; - return $this; - } + \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 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); } @@ -650,6 +638,22 @@ abstract class ACore implements JsonSerializable { /** * @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 * * @return ACore @@ -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 { + const TYPE = 'Follow'; + + /** - * Activity constructor. + * Follow 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('Follow'); + $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,10 +145,8 @@ class ActivityService { // $activity->setTo($item->getTo()); // } - $actor = $this->actorService->getActorFromUserId($userId); $activity->setActor($actor); - $this->setupCore($activity); $result = $this->request($activity, $type); return $result; @@ -179,6 +155,20 @@ class ActivityService { /** * @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 * * @param int $type * @@ -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 // * @@ -320,6 +299,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 * * @throws Exception @@ -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 { |