From 4c78617a9c1517210a3a0999505bfbc815f959ce Mon Sep 17 00:00:00 2001 From: veselcraft Date: Mon, 16 Sep 2024 02:11:00 +0300 Subject: [PATCH] feat(friends)!: reject request --- VKAPI/Handlers/Friends.php | 17 ++++++++--- Web/Models/Entities/Traits/TSubscribable.php | 21 +++++++++++++ Web/Models/Entities/User.php | 10 ++++++ Web/Models/sql/get-followers.tsql | 2 +- Web/Models/sql/get-requests.tsql | 6 ++++ Web/Presenters/UserPresenter.php | 7 +++-- Web/Presenters/templates/@layout.xml | 4 +-- Web/Presenters/templates/User/Friends.xml | 32 +++++++++++++++++--- Web/static/css/main.css | 7 ++--- install/sqls/00046-deny-request.sql | 1 + locales/by.strings | 1 + locales/en.strings | 1 + locales/ru.strings | 1 + locales/uk.strings | 1 + 14 files changed, 92 insertions(+), 19 deletions(-) create mode 100755 Web/Models/sql/get-requests.tsql create mode 100755 install/sqls/00046-deny-request.sql diff --git a/VKAPI/Handlers/Friends.php b/VKAPI/Handlers/Friends.php index 56af8d112..51046be44 100644 --- a/VKAPI/Handlers/Friends.php +++ b/VKAPI/Handlers/Friends.php @@ -147,7 +147,7 @@ function areFriends(string $user_ids): array return $response; } - function getRequests(string $fields = "", int $offset = 0, int $count = 100, int $extended = 0): object + function getRequests(string $fields = "", int $out = 0, int $offset = 0, int $count = 100, int $extended = 0): object { if ($count >= 1000) $this->fail(100, "One of the required parameters was not passed or is invalid."); @@ -158,9 +158,18 @@ function getRequests(string $fields = "", int $offset = 0, int $count = 100, int $offset++; $followers = []; - foreach($this->getUser()->getFollowers($offset, $count) as $follower) { - $followers[$i] = $follower->getId(); - $i++; + if ($out != 0) { + foreach($this->getUser()->getFollowers($offset, $count) as $follower) { + $followers[$i] = $follower->getId(); + $i++; + } + } + else + { + foreach($this->getUser()->getRequests($offset, $count) as $follower) { + $followers[$i] = $follower->getId(); + $i++; + } } $response = $followers; diff --git a/Web/Models/Entities/Traits/TSubscribable.php b/Web/Models/Entities/Traits/TSubscribable.php index 802bc4272..7d1c0d284 100644 --- a/Web/Models/Entities/Traits/TSubscribable.php +++ b/Web/Models/Entities/Traits/TSubscribable.php @@ -39,4 +39,25 @@ function toggleSubscription(User $user): bool $sub->delete(); return false; } + + function changeFlags(User $user, int $flags, bool $reverse): bool + { + $ctx = DatabaseConnection::i()->getContext(); + $data = [ + "follower" => $reverse ? $this->getId() : $user->getId(), + "model" => static::class, + "target" => $reverse ? $user->getId() : $this->getId(), + ]; + $sub = $ctx->table("subscriptions")->where($data); + + bdump($data); + + if (!$sub) + return false; + + $sub->update([ + 'flags' => $flags + ]); + return true; + } } diff --git a/Web/Models/Entities/User.php b/Web/Models/Entities/User.php index 4f1fbff18..933ab486f 100644 --- a/Web/Models/Entities/User.php +++ b/Web/Models/Entities/User.php @@ -592,6 +592,16 @@ function getFollowersCount(): int return $this->_abstractRelationCount("get-followers"); } + function getRequests(int $page = 1, int $limit = 6): \Traversable + { + return $this->_abstractRelationGenerator("get-requests", $page, $limit); + } + + function getRequestsCount(): int + { + return $this->_abstractRelationCount("get-requests"); + } + function getSubscriptions(int $page = 1, int $limit = 6): \Traversable { return $this->_abstractRelationGenerator("get-subscriptions-user", $page, $limit); diff --git a/Web/Models/sql/get-followers.tsql b/Web/Models/sql/get-followers.tsql index 552aafb8d..b07b1c690 100644 --- a/Web/Models/sql/get-followers.tsql +++ b/Web/Models/sql/get-followers.tsql @@ -1,5 +1,5 @@ (SELECT DISTINCT(follower) AS __id FROM - (SELECT follower FROM subscriptions WHERE target=? AND model="openvk\\Web\\Models\\Entities\\User") u0 + (SELECT follower, flags FROM subscriptions WHERE target=? AND model="openvk\\Web\\Models\\Entities\\User") u0 LEFT JOIN (SELECT target FROM subscriptions WHERE follower=? AND model="openvk\\Web\\Models\\Entities\\User") u1 ON u0.follower = u1.target WHERE u1.target IS NULL) u2 diff --git a/Web/Models/sql/get-requests.tsql b/Web/Models/sql/get-requests.tsql new file mode 100755 index 000000000..0220e3400 --- /dev/null +++ b/Web/Models/sql/get-requests.tsql @@ -0,0 +1,6 @@ + (SELECT DISTINCT(follower) AS __id FROM + (SELECT follower FROM subscriptions WHERE target=? AND flags=0 AND model="openvk\\Web\\Models\\Entities\\User") u0 + LEFT JOIN + (SELECT target FROM subscriptions WHERE follower=? AND flags=0 AND model="openvk\\Web\\Models\\Entities\\User") u1 + ON u0.follower = u1.target WHERE u1.target IS NULL) u2 +INNER JOIN profiles ON profiles.id = u2.__id \ No newline at end of file diff --git a/Web/Presenters/UserPresenter.php b/Web/Presenters/UserPresenter.php index 3785e47be..1c2fa02ea 100644 --- a/Web/Presenters/UserPresenter.php +++ b/Web/Presenters/UserPresenter.php @@ -329,9 +329,12 @@ function renderSub(): void $user = $this->users->get((int) $this->postParam("id")); if(!$user) exit("Invalid state"); - $user->toggleSubscription($this->user->identity); + if ($this->postParam("act") == "rej") + $user->changeFlags($this->user->identity, 0b10000000, true); + else + $user->toggleSubscription($this->user->identity); - $this->redirect($user->getURL()); + $this->redirect($_SERVER['HTTP_REFERER']); } function renderSetAvatar() diff --git a/Web/Presenters/templates/@layout.xml b/Web/Presenters/templates/@layout.xml index 59560a90e..aa2db799d 100644 --- a/Web/Presenters/templates/@layout.xml +++ b/Web/Presenters/templates/@layout.xml @@ -178,9 +178,9 @@ {_edit_button} {_my_page} {_my_friends} - + - ({$thisUser->getFollowersCount()}) + ({$thisUser->getRequestsCount()}) diff --git a/Web/Presenters/templates/User/Friends.xml b/Web/Presenters/templates/User/Friends.xml index cd546a131..40146f11f 100644 --- a/Web/Presenters/templates/User/Friends.xml +++ b/Web/Presenters/templates/User/Friends.xml @@ -4,11 +4,14 @@ {var $act = $_GET["act"] ?? "friends"} {if $act == "incoming"} - {var $iterator = iterator_to_array($user->getFollowers($page))} - {var $count = $user->getFollowersCount()} + {var $iterator = iterator_to_array($user->getRequests($page))} + {var $count = $user->getRequestsCount()} {elseif $act == "outcoming"} {var $iterator = iterator_to_array($user->getSubscriptions($page))} {var $count = $user->getSubscriptionsCount()} +{elseif $act == "followers"} + {var $iterator = iterator_to_array($user->getFollowers($page))} + {var $count = $user->getFollowersCount()} {elseif $act == "online"} {var $iterator = iterator_to_array($user->getFriendsOnline($page))} {var $count = $user->getFriendsOnlineCount()} @@ -22,6 +25,8 @@ {_incoming_req} {elseif $act == "outcoming"} {_outcoming_req} + {elseif $act == "followers"} + {_followers} {elseif $act == "online"} {_friends_online} {else} @@ -38,6 +43,8 @@ {_incoming_req} {elseif $act == "outcoming"} {_outcoming_req} + {elseif $act == "followers"} + {_followers} {elseif $act == "online"} {_friends_online} {else} @@ -53,18 +60,23 @@ -
- {_req} +
+ {_req}
{/block} {block size} -
+
+
+ +
{_outcoming_req} @@ -78,6 +90,8 @@ {tr("req", $count)} {elseif $act == "outcoming"} {tr("req", $count)} + {elseif $act == "followers"} + {tr("followers", $count)} {elseif $act == "online"} {tr("friends_list_online", $count)} {else} @@ -144,6 +158,14 @@ + {if $act !== 'followers'} + + {/if} {elseif $subStatus === 2}