From 626b343baaa548384b9b6a3129dbb2eb47fccb04 Mon Sep 17 00:00:00 2001 From: Celestora Date: Thu, 7 Oct 2021 09:28:41 +0000 Subject: [PATCH] Add service api handler for Wall.getPost --- ServiceAPI/Wall.php | 58 ++++++++++++++++++++++++++++++++ Web/Models/Entities/Postable.php | 12 +++++++ 2 files changed, 70 insertions(+) create mode 100644 ServiceAPI/Wall.php diff --git a/ServiceAPI/Wall.php b/ServiceAPI/Wall.php new file mode 100644 index 00000000..af74464f --- /dev/null +++ b/ServiceAPI/Wall.php @@ -0,0 +1,58 @@ +user = $user; + $this->posts = new Posts; + } + + function getPost(int $id, callable $resolve, callable $reject): void + { + $post = $this->posts->get($id); + if(!$post || $post->isDeleted()) + $reject("No post with id=$id"); + + $res = (object) []; + $res->id = $post->getId(); + $res->wall = $post->getTargetWall(); + $res->author = (($owner = $post->getOwner())) instanceof User + ? ($owner->getId()) + : ($owner->getId() * -1); + + if($post->isSigned()) + $res->signedOffBy = $post->getOwnerPost(); + + $res->pinned = $post->isPinned(); + $res->sponsored = $post->isAd(); + $res->nsfw = $post->isExplicit(); + $res->text = $post->getText(); + + $res->likes = [ + "count" => $post->getLikesCount(), + "hasLike" => $post->hasLikeFrom($this->user), + "likedBy" => [], + ]; + foreach($post->getLikers() as $liker) { + $res->likes["likedBy"][] = [ + "id" => $liker->getId(), + "url" => $liker->getURL(), + "name" => $liker->getCanonicalName(), + "avatar" => $liker->getAvatarURL(), + ]; + } + + $res->created = (string) $post->getPublicationTime(); + $res->canPin = $post->canBePinnedBy($this->user); + $res->canEdit = $res->canDelete = $post->canBeDeletedBy($this->user); + + $resolve((array) $res); + } +} diff --git a/Web/Models/Entities/Postable.php b/Web/Models/Entities/Postable.php index 27c406fc..05a39777 100644 --- a/Web/Models/Entities/Postable.php +++ b/Web/Models/Entities/Postable.php @@ -84,6 +84,18 @@ abstract class Postable extends Attachable ])); } + // TODO add pagination + function getLikers(): \Traversable + { + $sel = DB::i()->getContext()->table("likes")->where([ + "model" => static::class, + "target" => $this->getRecord()->id, + ]); + + foreach($sel as $like) + yield (new Users)->get($like->origin); + } + function toggleLike(User $user): void { $searchData = [