From a05dfc587909aef339241d6ab46e18fc90eaf116 Mon Sep 17 00:00:00 2001 From: Maxence Lange Date: Tue, 20 Nov 2018 21:38:36 -0100 Subject: [PATCH] local in ACore Signed-off-by: Maxence Lange --- appinfo/routes.php | 3 +- lib/Controller/LocalController.php | 48 +++++++++++++++++++++++++----- lib/Db/CacheActorsRequest.php | 7 ++--- lib/Db/NotesRequest.php | 3 +- lib/Model/ActivityPub/Person.php | 29 ++---------------- 5 files changed, 48 insertions(+), 42 deletions(-) diff --git a/appinfo/routes.php b/appinfo/routes.php index 1dd4f1c5..41da2069 100644 --- a/appinfo/routes.php +++ b/appinfo/routes.php @@ -36,7 +36,8 @@ return [ ['name' => 'SocialPub#displayPost', 'url' => '/@{username}/{postId}', 'verb' => 'GET'], - ['name' => 'Local#newPost', 'url' => '/api/v1/post', 'verb' => 'POST'], + ['name' => 'Local#postCreate', 'url' => '/api/v1/post', 'verb' => 'POST'], + ['name' => 'Local#postDelete', 'url' => '/api/v1/post', 'verb' => 'GET'], ['name' => 'Local#timeline', 'url' => '/api/v1/timeline', 'verb' => 'GET'], ['name' => 'Local#direct', 'url' => '/api/v1/direct', 'verb' => 'PUT'], ['name' => 'Local#accountsSearch', 'url' => '/api/v1/accounts/search', 'verb' => 'GET'], diff --git a/lib/Controller/LocalController.php b/lib/Controller/LocalController.php index 95f26f02..906c82aa 100644 --- a/lib/Controller/LocalController.php +++ b/lib/Controller/LocalController.php @@ -34,6 +34,7 @@ use daita\MySmallPhpTools\Traits\TArrayTools; use daita\MySmallPhpTools\Traits\Nextcloud\TNCDataResponse; use Exception; use OCA\Social\AppInfo\Application; +use OCA\Social\Exceptions\InvalidResourceException; use OCA\Social\Model\Post; use OCA\Social\Service\ActivityPub\FollowService; use OCA\Social\Service\ActivityPub\NoteService; @@ -121,7 +122,7 @@ class LocalController extends Controller { * * @return DataResponse */ - public function newPost(array $data): DataResponse { + public function postCreate(array $data): DataResponse { try { $post = new Post($this->userId); $post->setContent($this->get('content', $data, '')); @@ -134,7 +135,38 @@ class LocalController extends Controller { return $this->success($result); } catch (Exception $e) { - return $this->fail($e->getMessage()); + return $this->fail($e); + } + } + + + /** + * Create a new post. + * + * // TODO: Delete the NoCSRF check + * + * @NoCSRFRequired + * @NoAdminRequired + * @NoSubAdminRequired + * + * @param string $id + * + * @return DataResponse + */ + public function postDelete(string $id): DataResponse { + try { + $note = $this->noteService->getNoteById($id); + $actor = $this->actorService->getActorFromUserId($this->userId); + + if ($note->getAttributedTo() !== $actor->getId()) { + throw new InvalidResourceException('user have no rights'); + } + + $this->noteService->delete($note); + + return $this->success(); + } catch (Exception $e) { + return $this->fail($e); } } @@ -159,7 +191,7 @@ class LocalController extends Controller { return $this->success($posts); } catch (Exception $e) { - return $this->fail($e->getMessage()); + return $this->fail($e); } } @@ -178,7 +210,7 @@ class LocalController extends Controller { return $this->success($posts); } catch (Exception $e) { - return $this->fail($e->getMessage()); + return $this->fail($e); } } @@ -201,7 +233,7 @@ class LocalController extends Controller { return $this->success(['accounts' => $accounts]); } catch (Exception $e) { - return $this->fail($e->getMessage()); + return $this->fail($e); } } @@ -225,7 +257,7 @@ class LocalController extends Controller { return $this->success([]); } catch (Exception $e) { - return $this->fail($e->getMessage()); + return $this->fail($e); } } @@ -249,7 +281,7 @@ class LocalController extends Controller { return $this->success([]); } catch (Exception $e) { - return $this->fail($e->getMessage()); + return $this->fail($e); } } @@ -272,7 +304,7 @@ class LocalController extends Controller { return $this->success(['actor' => $actor]); } catch (Exception $e) { - return $this->fail($e->getMessage()); + return $this->fail($e); } } diff --git a/lib/Db/CacheActorsRequest.php b/lib/Db/CacheActorsRequest.php index a693096f..fe722dcc 100644 --- a/lib/Db/CacheActorsRequest.php +++ b/lib/Db/CacheActorsRequest.php @@ -31,7 +31,6 @@ namespace OCA\Social\Db; use OCA\Social\Exceptions\CacheActorDoesNotExistException; -use OCA\Social\Model\ActivityPub\Cache\CacheActor; use OCA\Social\Model\ActivityPub\Person; use OCA\Social\Service\ConfigService; use OCA\Social\Service\MiscService; @@ -58,16 +57,14 @@ class CacheActorsRequest extends CacheActorsRequestBuilder { * insert cache about an Actor in database. * * @param Person $actor - * @param bool $local * * @return int */ - public function save(Person $actor, bool $local = false): int { - + public function save(Person $actor): int { $qb = $this->getCacheActorsInsertSql(); $qb->setValue('id', $qb->createNamedParameter($actor->getId())) ->setValue('account', $qb->createNamedParameter($actor->getAccount())) - ->setValue('local', $qb->createNamedParameter(($local) ? '1' : '0')) + ->setValue('local', $qb->createNamedParameter(($actor->isLocal()) ? '1' : '0')) ->setValue('following', $qb->createNamedParameter($actor->getFollowing())) ->setValue('followers', $qb->createNamedParameter($actor->getFollowers())) ->setValue('inbox', $qb->createNamedParameter($actor->getInbox())) diff --git a/lib/Db/NotesRequest.php b/lib/Db/NotesRequest.php index c0651496..fa9788c9 100644 --- a/lib/Db/NotesRequest.php +++ b/lib/Db/NotesRequest.php @@ -94,6 +94,7 @@ class NotesRequest extends NotesRequestBuilder { ->setValue('attributed_to', $qb->createNamedParameter($note->getAttributedTo())) ->setValue('in_reply_to', $qb->createNamedParameter($note->getInReplyTo())) ->setValue('source', $qb->createNamedParameter($note->getSource())) + ->setValue('local', $qb->createNamedParameter(($note->isLocal()) ? '1' : '0')) ->setValue( 'creation', $qb->createNamedParameter(new DateTime('now'), IQueryBuilder::PARAM_DATE) @@ -111,7 +112,7 @@ class NotesRequest extends NotesRequestBuilder { * @return Note * @throws NoteNotFoundException */ - public function getFromId(string $id): Note { + public function getNoteById(string $id): Note { if ($id === '') { throw new NoteNotFoundException(); }; diff --git a/lib/Model/ActivityPub/Person.php b/lib/Model/ActivityPub/Person.php index ff8de11d..18c73be6 100644 --- a/lib/Model/ActivityPub/Person.php +++ b/lib/Model/ActivityPub/Person.php @@ -84,10 +84,6 @@ class Person extends ACore implements JsonSerializable { /** @var string */ private $featured = ''; - /** @var bool */ - private $local = false; - - /** * Person constructor. * @@ -342,25 +338,6 @@ class Person extends ACore implements JsonSerializable { } - /** - * @return bool - */ - public function isLocal(): bool { - return $this->local; - } - - /** - * @param bool $local - * - * @return Person - */ - public function setLocal(bool $local): Person { - $this->local = $local; - - return $this; - } - - /** * @param array $data */ @@ -377,8 +354,7 @@ class Person extends ACore implements JsonSerializable { ->setFollowing($this->get('following', $data, '')) ->setSharedInbox($this->get('shared_inbox', $data, '')) ->setFeatured($this->get('featured', $data, '')) - ->setCreation($this->getInt('creation', $data, 0)) - ->setLocal(($this->getInt('local', $data, 0) === 1)); + ->setCreation($this->getInt('creation', $data, 0)); // if ($this->getPreferredUsername() === '') { // $this->setType('Invalid'); @@ -410,8 +386,7 @@ class Person extends ACore implements JsonSerializable { 'id' => $this->getId() . '#main-key', 'owner' => $this->getId(), 'publicKeyPem' => $this->getPublicKey() - ], - 'local' => $this->isLocal() + ] ] ); }