From e44cfa5bc40ab7f361c35420eb8ceba072211154 Mon Sep 17 00:00:00 2001 From: Maxim Leshchenko Date: Thu, 11 Nov 2021 12:05:07 +0200 Subject: [PATCH] =?UTF-8?q?When=20you=20like=20a=20repost,=20put=20likes?= =?UTF-8?q?=20on=20the=20entire=20hierarchy=20of=20repost=20posts=20recurs?= =?UTF-8?q?ively=20If=20you=20like=20a=20repost,=20all=20posts=20from=20th?= =?UTF-8?q?e=20hierarchy=20will=20be=20liked.=20If=20you=20remove=20a=20li?= =?UTF-8?q?ke=20from=20a=20post,=20likes=20will=20be=20removed=20from=20al?= =?UTF-8?q?l=20posts=20from=20the=20hierarchy.=20I=20don=E2=80=99t=20know?= =?UTF-8?q?=20what=20the=20load=20will=20be=20in=20the=20case=20of=20large?= =?UTF-8?q?=20hierarchies=20(for=20example,=2020=20posts),=20but=20with=20?= =?UTF-8?q?small=20hierarchies=20(5=20posts)=20everything=20works=20fine.?= =?UTF-8?q?=20Fixes=20#159?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Web/Models/Entities/Post.php | 37 ++++++++++++++++++++++++++++++++ Web/Models/Entities/Postable.php | 24 ++++++++++++++++++--- Web/Presenters/WallPresenter.php | 5 +---- 3 files changed, 59 insertions(+), 7 deletions(-) diff --git a/Web/Models/Entities/Post.php b/Web/Models/Entities/Post.php index fe6a3078..03385736 100644 --- a/Web/Models/Entities/Post.php +++ b/Web/Models/Entities/Post.php @@ -3,6 +3,7 @@ namespace openvk\Web\Models\Entities; use Chandler\Database\DatabaseConnection as DB; use openvk\Web\Models\Repositories\Clubs; use openvk\Web\Models\RowModel; +use openvk\Web\Models\Entities\Notifications\LikeNotification; class Post extends Postable { @@ -121,6 +122,42 @@ class Post extends Postable $this->stateChanges("content", $content); } + + function toggleLike(User $user): bool + { + $liked = parent::toggleLike($user); + + if($this->getOwner(false)->getId() !== $user->getId() && !($this->getOwner() instanceof Club)) + (new LikeNotification($this->getOwner(false), $this, $user))->emit(); + + foreach($this->getChildren() as $attachment) { + if($attachment instanceof Post) + $attachment->setLike($liked, $user); + } + + return $liked; + } + + function setLike(bool $liked, User $user): void + { + $searchData = [ + "origin" => $user->getId(), + "model" => static::class, + "target" => $this->getRecord()->id, + ]; + + if((sizeof(DB::i()->getContext()->table("likes")->where($searchData)) > 0) !== $liked) { + if($this->getOwner(false)->getId() !== $user->getId() && !($this->getOwner() instanceof Club)) + (new LikeNotification($this->getOwner(false), $this, $user))->emit(); + + parent::setLike($liked, $user); + } + + foreach($this->getChildren() as $attachment) { + if($attachment instanceof Post) + $attachment->setLike($liked, $user); + } + } function deletePost(): void { diff --git a/Web/Models/Entities/Postable.php b/Web/Models/Entities/Postable.php index 05a39777..26670e94 100644 --- a/Web/Models/Entities/Postable.php +++ b/Web/Models/Entities/Postable.php @@ -96,17 +96,35 @@ abstract class Postable extends Attachable yield (new Users)->get($like->origin); } - function toggleLike(User $user): void + function toggleLike(User $user): bool { $searchData = [ "origin" => $user->getId(), "model" => static::class, "target" => $this->getRecord()->id, ]; - if(sizeof(DB::i()->getContext()->table("likes")->where($searchData)) > 0) + + if(sizeof(DB::i()->getContext()->table("likes")->where($searchData)) > 0) { DB::i()->getContext()->table("likes")->where($searchData)->delete(); - else + return false; + } + + DB::i()->getContext()->table("likes")->insert($searchData); + return true; + } + + function setLike(bool $liked, User $user): void + { + $searchData = [ + "origin" => $user->getId(), + "model" => static::class, + "target" => $this->getRecord()->id, + ]; + + if($liked) DB::i()->getContext()->table("likes")->insert($searchData); + else + DB::i()->getContext()->table("likes")->where($searchData)->delete(); } function hasLikeFrom(User $user): bool diff --git a/Web/Presenters/WallPresenter.php b/Web/Presenters/WallPresenter.php index d36d3db1..51734a3d 100644 --- a/Web/Presenters/WallPresenter.php +++ b/Web/Presenters/WallPresenter.php @@ -1,7 +1,7 @@ user)) { $post->toggleLike($this->user->identity); - - if($post->getOwner(false)->getId() !== $this->user->identity->getId() && !($post->getOwner() instanceof Club)) - (new LikeNotification($post->getOwner(false), $post, $this->user->identity))->emit(); } $this->redirect(