From df0bb4127631f06fbfbe065fabca458338a42021 Mon Sep 17 00:00:00 2001 From: mrilyew <99399973+mrilyew@users.noreply.github.com> Date: Sun, 2 Mar 2025 20:05:34 +0300 Subject: [PATCH] add liked content --- Web/Models/Entities/User.php | 2 + Web/Models/Repositories/Faves.php | 48 +++++++++++ Web/Presenters/GroupPresenter.php | 1 + Web/Presenters/UserPresenter.php | 65 ++++++++++++++- Web/Presenters/templates/@layout.xml | 3 +- Web/Presenters/templates/User/Fave.xml | 80 +++++++++++++++++++ Web/Presenters/templates/User/Settings.xml | 11 +++ .../templates/components/comment.xml | 3 +- Web/di.yml | 1 + Web/routes.yml | 2 + locales/en.strings | 17 ++++ locales/ru.strings | 17 ++++ 12 files changed, 247 insertions(+), 3 deletions(-) create mode 100644 Web/Models/Repositories/Faves.php create mode 100644 Web/Presenters/templates/User/Fave.xml diff --git a/Web/Models/Entities/User.php b/Web/Models/Entities/User.php index 31d3d4fe..85731811 100644 --- a/Web/Models/Entities/User.php +++ b/Web/Models/Entities/User.php @@ -558,6 +558,7 @@ class User extends RowModel "poster", "apps", "docs", + "fave", ], ])->get($id); } @@ -1195,6 +1196,7 @@ class User extends RowModel "poster", "apps", "docs", + "fave", ], ])->set($id, (int) $status)->toInteger(); diff --git a/Web/Models/Repositories/Faves.php b/Web/Models/Repositories/Faves.php new file mode 100644 index 00000000..d73b588b --- /dev/null +++ b/Web/Models/Repositories/Faves.php @@ -0,0 +1,48 @@ +context = DatabaseConnection::i()->getContext(); + $this->likes = $this->context->table("likes"); + } + + private function fetchLikes(User $user, string $class = 'Post') + { + $fetch = $this->likes->where([ + "model" => "openvk\\Web\\Models\\Entities\\".$class, + "origin" => $user->getRealId(), + ]); + + return $fetch; + } + + function fetchLikesSection(User $user, string $class = 'Post', int $page = 1, ?int $perPage = NULL): \Traversable + { + $perPage ??= OPENVK_DEFAULT_PER_PAGE; + $fetch = $this->fetchLikes($user, $class)->page($page, $perPage); + foreach($fetch as $like) { + $className = "openvk\\Web\\Models\\Repositories\\".$class."s"; + $repo = new $className; + if(!$repo) { + continue; + } + + $entity = $repo->get($like->target); + yield $entity; + } + } + + function fetchLikesSectionCount(User $user, string $class = 'Post') + { + return $this->fetchLikes($user, $class)->count(); + } +} diff --git a/Web/Presenters/GroupPresenter.php b/Web/Presenters/GroupPresenter.php index 1aa14213..b3e27512 100644 --- a/Web/Presenters/GroupPresenter.php +++ b/Web/Presenters/GroupPresenter.php @@ -373,6 +373,7 @@ final class GroupPresenter extends OpenVKPresenter public function renderDeleteAvatar(int $id) { $this->assertUserLoggedIn(); + $this->assertNoCSRF(); $this->willExecuteWriteAction(); $club = $this->clubs->get($id); diff --git a/Web/Presenters/UserPresenter.php b/Web/Presenters/UserPresenter.php index 8770b4de..07d702cd 100644 --- a/Web/Presenters/UserPresenter.php +++ b/Web/Presenters/UserPresenter.php @@ -9,7 +9,7 @@ use openvk\Web\Util\Sms; use openvk\Web\Themes\Themepacks; use openvk\Web\Models\Entities\{Photo, Post, EmailChangeVerification}; use openvk\Web\Models\Entities\Notifications\{CoinsTransferNotification, RatingUpNotification}; -use openvk\Web\Models\Repositories\{Users, Clubs, Albums, Videos, Notes, Vouchers, EmailChangeVerifications, Audios}; +use openvk\Web\Models\Repositories\{Users, Clubs, Albums, Videos, Notes, Vouchers, EmailChangeVerifications, Audios, Faves}; use openvk\Web\Models\Exceptions\InvalidUserNameException; use openvk\Web\Util\Validator; use Chandler\Security\Authenticator; @@ -474,6 +474,7 @@ final class UserPresenter extends OpenVKPresenter public function renderDeleteAvatar() { $this->assertUserLoggedIn(); + $this->assertNoCSRF(); $this->willExecuteWriteAction(); $avatar = $this->user->identity->getAvatarPhoto(); @@ -666,6 +667,7 @@ final class UserPresenter extends OpenVKPresenter "menu_standardo" => "poster", "menu_aplikoj" => "apps", "menu_doxc" => "docs", + "menu_feva" => "fave", ]; foreach ($settings as $checkbox => $setting) { $user->setLeftMenuItemStatus($setting, $this->checkbox($checkbox)); @@ -942,4 +944,65 @@ final class UserPresenter extends OpenVKPresenter $this->redirect("/settings"); } } + + function renderFave(): void + { + $this->assertUserLoggedIn(); + + $page = (int)($this->queryParam("p") ?? 1); + $section = $this->queryParam("section") ?? "posts"; + $display_section = "posts"; + $data = NULL; + $count = 0; + + switch($section) { + default: + $this->notFound(); + break; + case 'wall': + case 'post': + case 'posts': + $data = (new Faves)->fetchLikesSection($this->user->identity, 'Post', $page); + $count = (new Faves)->fetchLikesSectionCount($this->user->identity, 'Post'); + $display_section = "posts"; + break; + case 'comment': + case 'comments': + $data = (new Faves)->fetchLikesSection($this->user->identity, 'Comment', $page); + $count = (new Faves)->fetchLikesSectionCount($this->user->identity, 'Comment'); + $display_section = "comments"; + break; + case 'photo': + case 'photos': + $data = (new Faves)->fetchLikesSection($this->user->identity, 'Photo', $page); + $count = (new Faves)->fetchLikesSectionCount($this->user->identity, 'Photo'); + $display_section = "photos"; + break; + case 'video': + case 'videos': + $data = (new Faves)->fetchLikesSection($this->user->identity, 'Video', $page); + $count = (new Faves)->fetchLikesSectionCount($this->user->identity, 'Video'); + $display_section = "videos"; + break; + } + + $this->template->data = iterator_to_array($data); + $this->template->count = $count; + $this->template->page = $page; + $this->template->perPage = OPENVK_DEFAULT_PER_PAGE; + $this->template->section = $display_section; + + $this->template->paginatorConf = (object) [ + "page" => $page, + "count" => $count, + "amount" => sizeof($this->template->data), + "perPage" => $this->template->perPage, + "atBottom" => false, + "tidy" => true, + 'pageCount' => ceil($count / $this->template->perPage), + ]; + $this->template->extendedPaginatorConf = clone $this->template->paginatorConf; + $this->template->extendedPaginatorConf->space = 11; + $this->template->paginatorConf->atTop = true; + } } diff --git a/Web/Presenters/templates/@layout.xml b/Web/Presenters/templates/@layout.xml index 2e8dc828..7a55a8b0 100644 --- a/Web/Presenters/templates/@layout.xml +++ b/Web/Presenters/templates/@layout.xml @@ -198,10 +198,11 @@ {_my_settings} - {if $thisUser->getLeftMenuItemStatus('docs') || $thisUser->getLeftMenuItemStatus('apps')} + {if $thisUser->getLeftMenuItemStatus('docs') || $thisUser->getLeftMenuItemStatus('apps') || $thisUser->getLeftMenuItemStatus('fave')}
{_apps} {_my_documents} + {_bookmarks_tab} {/if} {var $canAccessAdminPanel = $thisUser->getChandlerUser()->can("access")->model("admin")->whichBelongsTo(NULL)} diff --git a/Web/Presenters/templates/User/Fave.xml b/Web/Presenters/templates/User/Fave.xml new file mode 100644 index 00000000..50ead41f --- /dev/null +++ b/Web/Presenters/templates/User/Fave.xml @@ -0,0 +1,80 @@ +{extends "../@layout.xml"} + +{block title}{_bookmarks_tab}{/block} + +{block header} + {_bookmarks_tab} +{/block} + +{block wrap} +