diff options
author | Maxence Lange <maxence@artificial-owl.com> | 2019-02-22 23:04:00 -0100 |
---|---|---|
committer | Maxence Lange <maxence@artificial-owl.com> | 2019-02-22 23:04:00 -0100 |
commit | 9ac8eb965b930110d0ec42d56988a10adf1e3f96 (patch) | |
tree | 85234dfae2e24b53a42e17e2abb087b53aa085e1 /lib/Service | |
parent | 8a92d5680abfa80302dafb421699ceb0d0aa74fc (diff) |
boost creation
Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
Diffstat (limited to 'lib/Service')
-rw-r--r-- | lib/Service/ActorService.php | 17 | ||||
-rw-r--r-- | lib/Service/CacheActorService.php | 17 | ||||
-rw-r--r-- | lib/Service/FollowService.php | 16 | ||||
-rw-r--r-- | lib/Service/NoteService.php | 128 | ||||
-rw-r--r-- | lib/Service/PostService.php | 21 |
5 files changed, 108 insertions, 91 deletions
diff --git a/lib/Service/ActorService.php b/lib/Service/ActorService.php index e15dbae5..dc6f5e1e 100644 --- a/lib/Service/ActorService.php +++ b/lib/Service/ActorService.php @@ -65,10 +65,6 @@ class ActorService { private $miscService; - /** @var string */ - private $viewerId = ''; - - /** * ActorService constructor. * @@ -93,19 +89,6 @@ class ActorService { /** - * @param string $viewerId - */ - public function setViewerId(string $viewerId) { - $this->viewerId = $viewerId; - $this->cacheActorsRequest->setViewerId($viewerId); - } - - public function getViewerId(): string { - return $this->viewerId; - } - - - /** * @param Person $actor */ public function cacheLocalActor(Person $actor) { diff --git a/lib/Service/CacheActorService.php b/lib/Service/CacheActorService.php index 6da7772a..5d16fc05 100644 --- a/lib/Service/CacheActorService.php +++ b/lib/Service/CacheActorService.php @@ -69,8 +69,8 @@ class CacheActorService { private $miscService; - /** @var string */ - private $viewerId; + /** @var Person */ + private $viewer = null; /** @@ -93,15 +93,11 @@ class CacheActorService { /** - * @param string $viewerId + * @param Person $viewer */ - public function setViewerId(string $viewerId) { - $this->viewerId = $viewerId; - $this->cacheActorsRequest->setViewerId($viewerId); - } - - public function getViewerId(): string { - return $this->viewerId; + public function setViewer(Person $viewer) { + $this->viewer = $viewer; + $this->cacheActorsRequest->setViewer($viewer); } @@ -171,6 +167,7 @@ class CacheActorService { if (strrpos($account, '@')) { $account = substr($account, 0, strrpos($account, '@')); } + return $this->cacheActorsRequest->getFromLocalAccount($account); } diff --git a/lib/Service/FollowService.php b/lib/Service/FollowService.php index b6ee5c54..e41cec6d 100644 --- a/lib/Service/FollowService.php +++ b/lib/Service/FollowService.php @@ -77,8 +77,8 @@ class FollowService { private $miscService; - /** @var string */ - private $viewerId = ''; + /** @var Person */ + private $viewer = null; /** @@ -104,15 +104,11 @@ class FollowService { /** - * @param string $viewerId + * @param Person $viewer */ - public function setViewerId(string $viewerId) { - $this->viewerId = $viewerId; - $this->followsRequest->setViewerId($viewerId); - } - - public function getViewerId(): string { - return $this->viewerId; + public function setViewer(Person $viewer) { + $this->viewer = $viewer; + $this->followsRequest->setViewer($viewer); } diff --git a/lib/Service/NoteService.php b/lib/Service/NoteService.php index a4d432a3..d0bf47ac 100644 --- a/lib/Service/NoteService.php +++ b/lib/Service/NoteService.php @@ -46,7 +46,9 @@ use OCA\Social\Exceptions\RequestServerException; use OCA\Social\Exceptions\SocialAppConfigException; use OCA\Social\Model\ActivityPub\ACore; use OCA\Social\Model\ActivityPub\Actor\Person; +use OCA\Social\Model\ActivityPub\Object\Announce; use OCA\Social\Model\ActivityPub\Object\Note; +use OCA\Social\Model\ActivityPub\Stream; use OCA\Social\Model\InstancePath; class NoteService { @@ -61,6 +63,9 @@ class NoteService { /** @var AccountService */ private $accountService; + /** @var SignatureService */ + private $signatureService; + /** @var CacheActorService */ private $cacheActorService; @@ -71,8 +76,8 @@ class NoteService { private $miscService; - /** @var string */ - private $viewerId = ''; + /** @var Person */ + private $viewer = null; /** @@ -81,18 +86,20 @@ class NoteService { * @param NotesRequest $notesRequest * @param ActivityService $activityService * @param AccountService $accountService + * @param SignatureService $signatureService * @param CacheActorService $cacheActorService * @param ConfigService $configService * @param MiscService $miscService */ public function __construct( NotesRequest $notesRequest, ActivityService $activityService, - AccountService $accountService, CacheActorService $cacheActorService, - ConfigService $configService, MiscService $miscService + AccountService $accountService, SignatureService $signatureService, + CacheActorService $cacheActorService, ConfigService $configService, MiscService $miscService ) { $this->notesRequest = $notesRequest; $this->activityService = $activityService; $this->accountService = $accountService; + $this->signatureService = $signatureService; $this->cacheActorService = $cacheActorService; $this->configService = $configService; $this->miscService = $miscService; @@ -100,65 +107,84 @@ class NoteService { /** - * @param string $viewerId + * @param Person $viewer */ - public function setViewerId(string $viewerId) { - $this->viewerId = $viewerId; - $this->notesRequest->setViewerId($viewerId); + public function setViewer(Person $viewer) { + $this->viewer = $viewer; + $this->notesRequest->setViewer($viewer); } - public function getViewerId(): string { - return $this->viewerId; + + /** + * @param Person $actor + * @param string $postId + * @param ACore|null $announce + * + * @return string + * @throws NoteNotFoundException + * @throws SocialAppConfigException + */ + public function createBoost(Person $actor, string $postId, ACore &$announce = null): string { + + $announce = new Announce(); + $this->assignStream($announce, $actor, Stream::TYPE_PUBLIC); + + $announce->setActor($actor); + $note = $this->getNoteById($postId, true); + + $announce->addCc($note->getAttributedTo()); + if ($note->isLocal()) { + $announce->setObject($note); + } else { + $announce->setObjectId($note->getId()); + } + + $this->signatureService->signObject($actor, $announce); + $this->notesRequest->save($announce); + $token = $this->activityService->request($announce); + + return $token; } /** + * @param Stream $stream * @param Person $actor - * @param string $content - * * @param string $type * - * @return Note * @throws SocialAppConfigException */ - public function generateNote(Person $actor, string $content, string $type) { - $note = new Note(); - $note->setId($this->configService->generateId('@' . $actor->getPreferredUsername())); - $note->setPublished(date("c")); - $note->setAttributedTo( - $this->configService->getUrlSocial() . '@' . $actor->getPreferredUsername() - ); - - $this->setRecipient($note, $actor, $type); - $note->setContent($content); - $note->convertPublished(); - $note->setLocal(true); + public function assignStream(Stream &$stream, Person $actor, string $type) { + $stream->setId($this->configService->generateId('@' . $actor->getPreferredUsername())); + $stream->setPublished(date("c")); - return $note; + $this->setRecipient($stream, $actor, $type); + $stream->convertPublished(); + $stream->setLocal(true); } /** - * @param Note $note + * @param Stream $stream * @param Person $actor * @param string $type */ - private function setRecipient(Note $note, Person $actor, string $type) { + private function setRecipient(Stream $stream, Person $actor, string $type) { switch ($type) { case Note::TYPE_UNLISTED: - $note->setTo($actor->getFollowers()); - $note->addInstancePath( + $stream->setTo($actor->getFollowers()); + $stream->addInstancePath( new InstancePath( $actor->getFollowers(), InstancePath::TYPE_FOLLOWERS, InstancePath::PRIORITY_LOW ) ); - $note->addCc(ACore::CONTEXT_PUBLIC); + $stream->addCc(ACore::CONTEXT_PUBLIC); break; case Note::TYPE_FOLLOWERS: - $note->setTo($actor->getFollowers()); - $note->addInstancePath( + $stream->setTo($actor->getFollowers()); + $stream->addInstancePath( new InstancePath( $actor->getFollowers(), InstancePath::TYPE_FOLLOWERS, InstancePath::PRIORITY_LOW @@ -170,9 +196,9 @@ class NoteService { break; default: - $note->setTo(ACore::CONTEXT_PUBLIC); - $note->addCc($actor->getFollowers()); - $note->addInstancePath( + $stream->setTo(ACore::CONTEXT_PUBLIC); + $stream->addCc($actor->getFollowers()); + $stream->addInstancePath( new InstancePath( $actor->getFollowers(), InstancePath::TYPE_FOLLOWERS, InstancePath::PRIORITY_LOW @@ -184,11 +210,11 @@ class NoteService { /** - * @param Note $note + * @param Stream $stream * @param string $type * @param string $account */ - public function addRecipient(Note $note, string $type, string $account) { + public function addRecipient(Stream $stream, string $type, string $account) { if ($account === '') { return; } @@ -204,12 +230,12 @@ class NoteService { ); if ($type === Note::TYPE_DIRECT) { $instancePath->setPriority(InstancePath::PRIORITY_HIGH); - $note->addToArray($actor->getId()); + $stream->addToArray($actor->getId()); } else { - $note->addCc($actor->getId()); + $stream->addCc($actor->getId()); } - $note->addTag( + $stream->addTag( [ 'type' => 'Mention', 'href' => $actor->getId(), @@ -217,7 +243,7 @@ class NoteService { ] ); - $note->addInstancePath($instancePath); + $stream->addInstancePath($instancePath); } @@ -242,13 +268,13 @@ class NoteService { /** - * @param Note $note + * @param Stream $stream * @param string $type * @param array $accounts */ - public function addRecipients(Note $note, string $type, array $accounts) { + public function addRecipients(Stream $stream, string $type, array $accounts) { foreach ($accounts as $account) { - $this->addRecipient($note, $type, $account); + $this->addRecipient($stream, $type, $account); } } @@ -316,12 +342,13 @@ class NoteService { /** * @param string $id + * @param bool $asViewer * * @return Note * @throws NoteNotFoundException */ - public function getNoteById(string $id): Note { - return $this->notesRequest->getNoteById($id); + public function getNoteById(string $id, bool $asViewer = false): Note { + return $this->notesRequest->getNoteById($id, $asViewer); } @@ -392,7 +419,8 @@ class NoteService { * * @return Note[] */ - public function getStreamLocalTag(Person $actor, string $hashtag, int $since = 0, int $limit = 5): array { + public function getStreamLocalTag(Person $actor, string $hashtag, int $since = 0, int $limit = 5 + ): array { return $this->notesRequest->getStreamTag($actor, $hashtag, $since, $limit); } @@ -409,7 +437,8 @@ class NoteService { } - /** + /**m + * * @param int $since * @param int $limit * @@ -443,5 +472,6 @@ class NoteService { return $this->cacheActorService->getFromId($note->getAttributedTo()); } + } diff --git a/lib/Service/PostService.php b/lib/Service/PostService.php index 817ddc40..b2fa6616 100644 --- a/lib/Service/PostService.php +++ b/lib/Service/PostService.php @@ -38,10 +38,12 @@ use OCA\Social\Exceptions\NoteNotFoundException; use OCA\Social\Exceptions\RedundancyLimitException; use OCA\Social\Exceptions\RequestContentException; use OCA\Social\Exceptions\RequestNetworkException; +use OCA\Social\Exceptions\RequestResultNotJsonException; use OCA\Social\Exceptions\RequestResultSizeException; use OCA\Social\Exceptions\RequestServerException; use OCA\Social\Exceptions\SocialAppConfigException; use OCA\Social\Model\ActivityPub\ACore; +use OCA\Social\Model\ActivityPub\Object\Note; use OCA\Social\Model\Post; class PostService { @@ -56,6 +58,9 @@ class PostService { /** @var ActivityService */ private $activityService; + /** @var ConfigService */ + private $configService; + /** @var MiscService */ private $miscService; @@ -66,15 +71,17 @@ class PostService { * @param NoteService $noteService * @param AccountService $accountService * @param ActivityService $activityService + * @param ConfigService $configService * @param MiscService $miscService */ public function __construct( NoteService $noteService, AccountService $accountService, ActivityService $activityService, - MiscService $miscService + ConfigService $configService, MiscService $miscService ) { $this->noteService = $noteService; $this->accountService = $accountService; $this->activityService = $activityService; + $this->configService = $configService; $this->miscService = $miscService; } @@ -95,12 +102,16 @@ class PostService { * @throws RequestResultSizeException * @throws RequestServerException * @throws MalformedArrayException + * @throws RequestResultNotJsonException */ public function createPost(Post $post, ACore &$activity = null): string { - $note = - $this->noteService->generateNote( - $post->getActor(), htmlentities($post->getContent(), ENT_QUOTES), $post->getType() - ); + $note = new Note(); + $this->noteService->assignStream($note, $post->getActor(), $post->getType()); + $note->setAttributedTo( + $this->configService->getUrlSocial() . '@' . $actor->getPreferredUsername() + ); + + $note->setContent(htmlentities($post->getContent(), ENT_QUOTES)); $this->noteService->replyTo($note, $post->getReplyTo()); $this->noteService->addRecipients($note, $post->getType(), $post->getTo()); |