From 62f713f434f40e632883d9076390ea43921d244d Mon Sep 17 00:00:00 2001 From: Maxence Lange Date: Tue, 20 Nov 2018 11:47:14 -0100 Subject: [PATCH] managing notes Signed-off-by: Maxence Lange --- lib/Controller/ActivityPubController.php | 27 +++++- lib/Db/NotesRequest.php | 110 +++++++++++++++-------- lib/Exceptions/NoteNotFoundException.php | 8 ++ lib/Service/ActivityPub/NoteService.php | 20 ++++- lib/Service/ActivityService.php | 95 +++++++++----------- 5 files changed, 161 insertions(+), 99 deletions(-) create mode 100644 lib/Exceptions/NoteNotFoundException.php diff --git a/lib/Controller/ActivityPubController.php b/lib/Controller/ActivityPubController.php index fc7d2917..89ea905a 100644 --- a/lib/Controller/ActivityPubController.php +++ b/lib/Controller/ActivityPubController.php @@ -34,6 +34,7 @@ use daita\MySmallPhpTools\Traits\Nextcloud\TNCDataResponse; use Exception; use OCA\Social\AppInfo\Application; use OCA\Social\Db\NotesRequest; +use OCA\Social\Exceptions\UnknownItemException; use OCA\Social\Service\ActivityPub\FollowService; use OCA\Social\Service\ActivityService; use OCA\Social\Service\ActorService; @@ -125,6 +126,7 @@ class ActivityPubController extends Controller { try { $actor = $this->actorService->getActor($username); + // $actor->setTopLevel(true); return $this->directSuccess($actor); @@ -161,7 +163,23 @@ class ActivityPubController extends Controller { * @return Response */ public function sharedInbox(): Response { - return $this->success([]); + + try { + $this->activityService->checkRequest($this->request); + + $body = file_get_contents('php://input'); + $this->miscService->log('Shared Inbox: ' . $body); + + $activity = $this->importService->import($body); + try { + $this->importService->parse($activity); + } catch (UnknownItemException $e) { + } + + return $this->success([]); + } catch (Exception $e) { + return $this->fail($e->getMessage()); + } } @@ -186,12 +204,12 @@ class ActivityPubController extends Controller { $this->activityService->checkRequest($this->request); $body = file_get_contents('php://input'); - $this->miscService->log('Body: ' . $body); + $this->miscService->log('Inbox: ' . $body); $activity = $this->importService->import($body); try { - $this->importService->save($activity); - } catch (Exception $e) { + $this->importService->parse($activity); + } catch (UnknownItemException $e) { } return $this->success([]); @@ -248,6 +266,7 @@ class ActivityPubController extends Controller { try { $actor = $this->actorService->getActor($username); $followers = $this->followService->getFollowers($actor); + // $followers->setTopLevel(true); return $this->directSuccess($followers); diff --git a/lib/Db/NotesRequest.php b/lib/Db/NotesRequest.php index 83994763..c0651496 100644 --- a/lib/Db/NotesRequest.php +++ b/lib/Db/NotesRequest.php @@ -31,6 +31,7 @@ namespace OCA\Social\Db; use DateTime; +use OCA\Social\Exceptions\NoteNotFoundException; use OCA\Social\Model\ActivityPub\Note; use OCA\Social\Service\ActivityService; use OCA\Social\Service\ConfigService; @@ -61,52 +62,72 @@ class NotesRequest extends NotesRequestBuilder { * @param Note $note * * @return int - * @throws \Exception */ public function save(Note $note): int { - try { + $dTime = new DateTime(); + $dTime->setTimestamp($note->getPublishedTime()); - $dTime = new DateTime(); - $dTime->setTimestamp($note->getPublishedTime()); + $qb = $this->getNotesInsertSql(); + $qb->setValue('id', $qb->createNamedParameter($note->getId())) + ->setValue('to', $qb->createNamedParameter($note->getTo())) + ->setValue( + 'to_array', $qb->createNamedParameter( + json_encode($note->getToArray(), JSON_UNESCAPED_SLASHES) + ) + ) + ->setValue( + 'cc', $qb->createNamedParameter( + json_encode($note->getCcArray(), JSON_UNESCAPED_SLASHES) + ) + ) + ->setValue( + 'bcc', $qb->createNamedParameter( + json_encode($note->getBccArray()), JSON_UNESCAPED_SLASHES + ) + ) + ->setValue('content', $qb->createNamedParameter($note->getContent())) + ->setValue('summary', $qb->createNamedParameter($note->getSummary())) + ->setValue('published', $qb->createNamedParameter($note->getPublished())) + ->setValue( + 'published_time', $qb->createNamedParameter($dTime, IQueryBuilder::PARAM_DATE) + ) + ->setValue('attributed_to', $qb->createNamedParameter($note->getAttributedTo())) + ->setValue('in_reply_to', $qb->createNamedParameter($note->getInReplyTo())) + ->setValue('source', $qb->createNamedParameter($note->getSource())) + ->setValue( + 'creation', + $qb->createNamedParameter(new DateTime('now'), IQueryBuilder::PARAM_DATE) + ); - $qb = $this->getNotesInsertSql(); - $qb->setValue('id', $qb->createNamedParameter($note->getId())) - ->setValue('to', $qb->createNamedParameter($note->getTo())) - ->setValue( - 'to_array', $qb->createNamedParameter( - json_encode($note->getToArray(), JSON_UNESCAPED_SLASHES) - ) - ) - ->setValue( - 'cc', $qb->createNamedParameter( - json_encode($note->getCcArray(), JSON_UNESCAPED_SLASHES) - ) - ) - ->setValue( - 'bcc', $qb->createNamedParameter( - json_encode($note->getBccArray()), JSON_UNESCAPED_SLASHES - ) - ) - ->setValue('content', $qb->createNamedParameter($note->getContent())) - ->setValue('summary', $qb->createNamedParameter($note->getSummary())) - ->setValue('published', $qb->createNamedParameter($note->getPublished())) - ->setValue( - 'published_time', $qb->createNamedParameter($dTime, IQueryBuilder::PARAM_DATE) - ) - ->setValue('attributed_to', $qb->createNamedParameter($note->getAttributedTo())) - ->setValue('in_reply_to', $qb->createNamedParameter($note->getInReplyTo())) - ->setValue('source', $qb->createNamedParameter($note->getSource())) - ->setValue( - 'creation', - $qb->createNamedParameter(new DateTime('now'), IQueryBuilder::PARAM_DATE) - ); + $qb->execute(); - $qb->execute(); + return $qb->getLastInsertId(); + } - return $qb->getLastInsertId(); - } catch (\Exception $e) { - throw $e; + + /** + * @param string $id + * + * @return Note + * @throws NoteNotFoundException + */ + public function getFromId(string $id): Note { + if ($id === '') { + throw new NoteNotFoundException(); + }; + + $qb = $this->getNotesSelectSql(); + $this->limitToIdString($qb, $id); + + $cursor = $qb->execute(); + $data = $cursor->fetch(); + $cursor->closeCursor(); + + if ($data === false) { + throw new NoteNotFoundException(); } + + return $this->parseNotesSelectSql($data); } @@ -153,4 +174,15 @@ class NotesRequest extends NotesRequestBuilder { } + /** + * @param string $id + */ + public function deleteNoteById(string $id) { + $qb = $this->getNotesDeleteSql(); + $this->limitToIdString($qb, $id); + + $qb->execute(); + } + } + diff --git a/lib/Exceptions/NoteNotFoundException.php b/lib/Exceptions/NoteNotFoundException.php new file mode 100644 index 00000000..0bd86298 --- /dev/null +++ b/lib/Exceptions/NoteNotFoundException.php @@ -0,0 +1,8 @@ +isRoot()) { return; @@ -241,9 +243,21 @@ class NoteService implements ICoreService { if ($parent->getType() === Create::TYPE) { $parent->verify(($note->getAttributedTo())); - $this->notesRequest->save($note); + try { + $this->notesRequest->getFromId($note->getId()); + } catch (NoteNotFoundException $e) { + $this->notesRequest->save($note); + } } + } + + /** + * @param ACore $item + */ + public function delete(ACore $item) { + /** @var Note $item */ + $this->notesRequest->deleteNoteById($item->getId()); } diff --git a/lib/Service/ActivityService.php b/lib/Service/ActivityService.php index 8d23ecf7..13b9e404 100644 --- a/lib/Service/ActivityService.php +++ b/lib/Service/ActivityService.php @@ -35,6 +35,7 @@ use daita\MySmallPhpTools\Traits\TArrayTools; use DateTime; use Exception; use OCA\Social\Db\ActorsRequest; +use OCA\Social\Db\NotesRequest; use OCA\Social\Exceptions\ActorDoesNotExistException; use OCA\Social\Exceptions\InvalidResourceException; use OCA\Social\Exceptions\RequestException; @@ -65,6 +66,9 @@ class ActivityService { /** @var ActorsRequest */ private $actorsRequest; + /** @var NotesRequest */ + private $notesRequest; + /** @var ActorService */ private $actorService; @@ -88,6 +92,7 @@ class ActivityService { * ActivityService constructor. * * @param ActorsRequest $actorsRequest + * @param NotesRequest $notesRequest * @param CurlService $curlService * @param ActorService $actorService * @param PersonService $personService @@ -96,13 +101,15 @@ class ActivityService { * @param MiscService $miscService */ public function __construct( - ActorsRequest $actorsRequest, CurlService $curlService, ActorService $actorService, + ActorsRequest $actorsRequest, NotesRequest $notesRequest, CurlService $curlService, + ActorService $actorService, PersonService $personService, InstanceService $instanceService, ConfigService $configService, MiscService $miscService ) { $this->curlService = $curlService; $this->actorsRequest = $actorsRequest; + $this->notesRequest = $notesRequest; $this->actorService = $actorService; $this->personService = $personService; $this->instanceService = $instanceService; @@ -111,12 +118,6 @@ class ActivityService { } - public function test() { - - - } - - /** * @param Person $actor * @param ACore $item @@ -126,6 +127,7 @@ class ActivityService { * @return array * @throws RequestException * @throws SocialAppConfigException + * @throws ActorDoesNotExistException */ public function createActivity(Person $actor, ACore $item, int $type, ACore &$activity = null ): array { @@ -153,12 +155,41 @@ class ActivityService { } + /** + * @param string $id + * + * @return ACore + * @throws InvalidResourceException + */ + public function getItem(string $id): ACore { + if ($id === '') { + throw new InvalidResourceException(); + } + + $requests = [ + $this->notesRequest + ]; + + foreach ($requests as $request) { + try { + $toDelete = $request->getFromId($id); + + return $toDelete; + } catch (Exception $e) { + } + } + + throw new InvalidResourceException(); + } + + /** * @param ACore $activity * @param int $type * * @throws RequestException * @throws SocialAppConfigException + * @throws ActorDoesNotExistException */ public function manageRequest(ACore $activity, int $type) { $result = $this->request($activity, $type); @@ -175,6 +206,7 @@ class ActivityService { * @return array * @throws RequestException * @throws SocialAppConfigException + * @throws ActorDoesNotExistException */ public function request(ACore &$activity, int $type) { $this->setupCore($activity); @@ -206,7 +238,7 @@ class ActivityService { ): array { $document = json_encode($activity); $date = gmdate(self::DATE_FORMAT); - $localActor = $this->getActorFromActivity($activity); + $localActor = $this->getActorFromItem($activity); $localActorLink = $this->configService->getUrlRoot() . '@' . $localActor->getPreferredUsername(); @@ -255,49 +287,6 @@ class ActivityService { } -// /** -// * @param Core $activity -// * -// * @return array -// */ -// private function getHostsFromActivity(Core $activity) { -// -// $hosts = []; -// $hosts[] = $this->getHostFromUriId($activity->getTo()); -// foreach ($activity->getToArray() as $to) { -// $hosts[] = $this->getHostFromUriId($to); -// } -// -// if ($activity instanceof Note) { -// /** @var Note $activity */ -// $hosts[] = $this->getHostFromUriId($activity->getInReplyTo()); -// } -// -// $hosts = $this->cleaningHosts($hosts); -// -// return $hosts; -// } - - -// /** -// * @param array $hosts -// * -// * @return array -// */ -// private function cleaningHosts(array $hosts) { -// $ret = []; -// foreach ($hosts as $host) { -// if ($host === '') { -// continue; -// } -// -// $ret[] = $host; -// } -// -// return $ret; -// } - - /** * @param ACore $activity * @@ -305,7 +294,7 @@ class ActivityService { * @throws SocialAppConfigException * @throws ActorDoesNotExistException */ - private function getActorFromActivity(Acore $activity): Person { + private function getActorFromItem(Acore $activity): Person { if ($activity->gotActor()) { return $activity->getActor(); } @@ -413,7 +402,7 @@ class ActivityService { $coreService = $activity->savingAs(); if ($coreService !== null) { - $coreService->save($activity); + $coreService->parse($activity); } if ($activity->gotObject()) {