From 0b08b1ed763f5f2371750f3b11810f8415e32689 Mon Sep 17 00:00:00 2001 From: Maxence Lange Date: Tue, 13 Nov 2018 17:05:58 -0100 Subject: [PATCH] type can be public', 'direct', 'followers', 'unlisted' Signed-off-by: Maxence Lange --- lib/Command/NoteCreate.php | 11 +++-- lib/Controller/ActivityPubController.php | 2 + lib/Controller/LocalController.php | 1 + lib/Model/ActivityPub/ACore.php | 6 +++ lib/Model/Post.php | 60 ++++++++++++++++++++--- lib/Service/ActivityPub/NoteService.php | 57 ++++++++++++++++++--- lib/Service/ActivityPub/PersonService.php | 1 - lib/Service/PostService.php | 6 ++- 8 files changed, 124 insertions(+), 20 deletions(-) diff --git a/lib/Command/NoteCreate.php b/lib/Command/NoteCreate.php index 5cea74fb..0d4f6f72 100644 --- a/lib/Command/NoteCreate.php +++ b/lib/Command/NoteCreate.php @@ -32,7 +32,6 @@ namespace OCA\Social\Command; use Exception; use OC\Core\Command\Base; -use OCA\Social\Model\InstancePath; use OCA\Social\Model\Post; use OCA\Social\Service\ActivityPub\NoteService; use OCA\Social\Service\ActivityService; @@ -104,7 +103,11 @@ class NoteCreate extends Base { 'replyTo', 'r', InputOption::VALUE_OPTIONAL, 'in reply to an existing thread' ) ->addOption( - 'to', 't', InputOption::VALUE_OPTIONAL, 'to (default Public)' + 'to', 't', InputOption::VALUE_OPTIONAL, 'mentioning people' + ) + ->addOption( + 'type', 'y', InputOption::VALUE_OPTIONAL, + 'type: public (default), followers, unlisted, direct' ) ->addArgument('userid', InputArgument::REQUIRED, 'userId of the author') ->addArgument('content', InputArgument::REQUIRED, 'content of the post') @@ -124,11 +127,13 @@ class NoteCreate extends Base { $content = $input->getArgument('content'); $to = $input->getOption('to'); $replyTo = $input->getOption('replyTo'); + $type = $input->getOption('type'); $post = new Post($userId); $post->setContent($content); + $post->setType(($type === null) ? '' : $type); $post->setReplyTo(($replyTo === null) ? '' : $replyTo); - $post->addTo($to); + $post->addTo(($to === null) ? '' : $to); $result = $this->postService->createPost($post, $activity); diff --git a/lib/Controller/ActivityPubController.php b/lib/Controller/ActivityPubController.php index 0c4bc52b..75dc25cb 100644 --- a/lib/Controller/ActivityPubController.php +++ b/lib/Controller/ActivityPubController.php @@ -186,6 +186,8 @@ class ActivityPubController extends Controller { $this->activityService->checkRequest($this->request); $body = file_get_contents('php://input'); +// $this->miscService->log('Body: ' . $body); + $activity = $this->importService->import($body); try { diff --git a/lib/Controller/LocalController.php b/lib/Controller/LocalController.php index 4291e047..39193c2d 100644 --- a/lib/Controller/LocalController.php +++ b/lib/Controller/LocalController.php @@ -115,6 +115,7 @@ class LocalController extends Controller { $post->setReplyTo($this->get('replyTo', $data, '')); $post->setTo($this->getArray('to', $data, [])); $post->addTo($this->get('to', $data, '')); + $post->setType($this->get('type', $data, NoteService::TYPE_PUBLIC)); $result = $this->postService->createPost($post); diff --git a/lib/Model/ActivityPub/ACore.php b/lib/Model/ActivityPub/ACore.php index ab47f4f9..6189b43b 100644 --- a/lib/Model/ActivityPub/ACore.php +++ b/lib/Model/ActivityPub/ACore.php @@ -435,6 +435,12 @@ abstract class ACore implements JsonSerializable { } + public function addCc(string $cc): Acore { + $this->cc[] = $cc; + + return $this; + } + /** * @return array */ diff --git a/lib/Model/Post.php b/lib/Model/Post.php index d2a34322..2c2d5353 100644 --- a/lib/Model/Post.php +++ b/lib/Model/Post.php @@ -33,6 +33,12 @@ namespace OCA\Social\Model; use daita\MySmallPhpTools\Traits\TArrayTools; use JsonSerializable; + +/** + * Class Post + * + * @package OCA\Social\Model + */ class Post implements JsonSerializable { @@ -51,7 +57,16 @@ class Post implements JsonSerializable { /** @var string */ private $content; - public function __construct($userId = '') { + /** @var string */ + private $type; + + + /** + * Post constructor. + * + * @param string $userId + */ + public function __construct(string $userId = '') { $this->userId = $userId; } @@ -65,13 +80,15 @@ class Post implements JsonSerializable { /** * @param string $to + * + * @return Post */ - public function addTo(string $to) { - if ($to === '') { - return; + public function addTo(string $to): Post { + if ($to !== '') { + $this->to[] = $to; } - $this->to[] = $to; + return $this; } /** @@ -83,9 +100,13 @@ class Post implements JsonSerializable { /** * @param array $to + * + * @return Post */ - public function setTo(array $to) { + public function setTo(array $to): Post { $this->to = $to; + + return $this; } @@ -98,9 +119,31 @@ class Post implements JsonSerializable { /** * @param string $replyTo + * + * @return Post */ - public function setReplyTo(string $replyTo) { + public function setReplyTo(string $replyTo): Post { $this->replyTo = $replyTo; + + return $this; + } + + /** + * @return string + */ + public function getType(): string { + return $this->type; + } + + /** + * @param string $type + * + * @return Post + */ + public function setType(string $type): Post { + $this->type = $type; + + return $this; } @@ -127,7 +170,8 @@ class Post implements JsonSerializable { 'userId' => $this->getUserId(), 'to' => $this->getTo(), 'replyTo' => $this->getReplyTo(), - 'content' => $this->getContent() + 'content' => $this->getContent(), + 'type' => $this->getType() ]; } diff --git a/lib/Service/ActivityPub/NoteService.php b/lib/Service/ActivityPub/NoteService.php index d6f96558..554a8191 100644 --- a/lib/Service/ActivityPub/NoteService.php +++ b/lib/Service/ActivityPub/NoteService.php @@ -49,6 +49,12 @@ use OCA\Social\Service\MiscService; class NoteService implements ICoreService { + const TYPE_PUBLIC = 'public'; + const TYPE_UNLISTED = 'unlisted'; + const TYPE_FOLLOWERS = 'followers'; + const TYPE_DIRECT = 'direct'; + + /** @var NotesRequest */ private $notesRequest; @@ -95,11 +101,13 @@ class NoteService implements ICoreService { * @param string $userId * @param string $content * + * @param string $type + * * @return Note * @throws ActorDoesNotExistException * @throws NoUserException */ - public function generateNote(string $userId, string $content) { + public function generateNote(string $userId, string $content, string $type) { $note = new Note(); $actor = $this->actorService->getActorFromUserId($userId); @@ -108,7 +116,8 @@ class NoteService implements ICoreService { $note->setAttributedTo( $this->configService->getRoot() . '@' . $actor->getPreferredUsername() ); - $note->setTo(ActivityService::TO_PUBLIC); + + $this->setRecipient($note, $actor, $type); $note->setContent($content); $note->saveAs($this); @@ -119,18 +128,53 @@ class NoteService implements ICoreService { /** * @param Note $note + * @param Person $actor + * @param string $type + */ + private function setRecipient(Note $note, Person $actor, string $type) { + switch ($type) { + case self::TYPE_UNLISTED: + $note->setTo($actor->getFollowers()); + $note->addCc(ActivityService::TO_PUBLIC); + break; + + case self::TYPE_FOLLOWERS: + $note->setTo($actor->getFollowers()); + break; + + case self::TYPE_DIRECT: + break; + + default: + $note->setTo(ActivityService::TO_PUBLIC); + $note->addCc($actor->getFollowers()); + break; + + + } + } + + + /** + * @param Note $note + * @param string $type * @param string $account * * @throws RequestException */ - public function assignTo(Note $note, string $account) { + public function addRecipient(Note $note, string $type, string $account) { if ($account === '') { return; } $actor = $this->personService->getFromAccount($account); - $note->addToArray($actor->getId()); + if ($type === self::TYPE_DIRECT) { + $note->addToArray($actor->getId()); + } else { + $note->addCc($actor->getId()); + } + $note->addTag( [ 'type' => 'Mention', @@ -144,17 +188,18 @@ class NoteService implements ICoreService { /** * @param Note $note + * @param string $type * @param array $accounts * * @throws RequestException */ - public function assignToArray(Note $note, array $accounts) { + public function addRecipients(Note $note, string $type, array $accounts) { if ($accounts === []) { return; } foreach ($accounts as $account) { - $this->assignTo($note, $account); + $this->addRecipient($note, $type, $account); } } diff --git a/lib/Service/ActivityPub/PersonService.php b/lib/Service/ActivityPub/PersonService.php index 93e4ee82..55bf3d6e 100644 --- a/lib/Service/ActivityPub/PersonService.php +++ b/lib/Service/ActivityPub/PersonService.php @@ -94,7 +94,6 @@ class PersonService implements ICoreService { try { $actor = $this->cacheActorsRequest->getFromAccount($account); } catch (CacheActorDoesNotExistException $e) { - $object = $this->instanceService->retrieveAccount($account); $actor = new Person(); $actor->import($object); diff --git a/lib/Service/PostService.php b/lib/Service/PostService.php index fba1a675..458f8b25 100644 --- a/lib/Service/PostService.php +++ b/lib/Service/PostService.php @@ -78,9 +78,11 @@ class PostService { */ public function createPost(Post $post, ACore &$activity = null) { $note = - $this->noteService->generateNote($post->getUserId(), $post->getContent()); + $this->noteService->generateNote( + $post->getUserId(), $post->getContent(), $post->getType() + ); - $this->noteService->assignToArray($note, $post->getTo()); + $this->noteService->addRecipients($note, $post->getType(), $post->getTo()); $this->noteService->replyTo($note, $post->getReplyTo()); return $this->activityService->createActivity(