Skip to content

Commit

Permalink
feat(friends)!: reject request
Browse files Browse the repository at this point in the history
  • Loading branch information
veselcraft committed Sep 15, 2024
1 parent 89157a8 commit 4c78617
Show file tree
Hide file tree
Showing 14 changed files with 92 additions and 19 deletions.
17 changes: 13 additions & 4 deletions VKAPI/Handlers/Friends.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
Expand All @@ -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;
Expand Down
21 changes: 21 additions & 0 deletions Web/Models/Entities/Traits/TSubscribable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
10 changes: 10 additions & 0 deletions Web/Models/Entities/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion Web/Models/sql/get-followers.tsql
Original file line number Diff line number Diff line change
@@ -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
Expand Down
6 changes: 6 additions & 0 deletions Web/Models/sql/get-requests.tsql
Original file line number Diff line number Diff line change
@@ -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
7 changes: 5 additions & 2 deletions Web/Presenters/UserPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions Web/Presenters/templates/@layout.xml
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,9 @@
<a href="/edit" class="link edit-button">{_edit_button}</a>
<a href="{$thisUser->getURL()}" class="link" title="{_my_page} [Alt+Shift+.]" accesskey=".">{_my_page}</a>
<a href="/friends{$thisUser->getId()}" class="link">{_my_friends}
<object type="internal/link" n:if="$thisUser->getFollowersCount() > 0">
<object type="internal/link" n:if="$thisUser->getRequestsCount() > 0">
<a href="/friends{$thisUser->getId()}?act=incoming" class="linkunderline">
(<b>{$thisUser->getFollowersCount()}</b>)
(<b>{$thisUser->getRequestsCount()}</b>)
</a>
</object>
</a>
Expand Down
32 changes: 27 additions & 5 deletions Web/Presenters/templates/User/Friends.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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()}
Expand All @@ -22,6 +25,8 @@
{_incoming_req}
{elseif $act == "outcoming"}
{_outcoming_req}
{elseif $act == "followers"}
{_followers}
{elseif $act == "online"}
{_friends_online}
{else}
Expand All @@ -38,6 +43,8 @@
{_incoming_req}
{elseif $act == "outcoming"}
{_outcoming_req}
{elseif $act == "followers"}
{_followers}
{elseif $act == "online"}
{_friends_online}
{else}
Expand All @@ -53,18 +60,23 @@
<div n:attr="id => ($act === 'online' ? 'activetabs' : 'ki')" class="tab">
<a n:attr="id => ($act === 'online' ? 'act_tab_a' : 'ki')" href="?act=online">{_online}</a>
</div>
<div n:if="!is_null($thisUser) && $user->getId() === $thisUser->getId()" n:attr="id => ($act === 'incoming' || $act === 'outcoming' ? 'activetabs' : 'ki')" class="tab">
<a n:attr="id => ($act === 'incoming' || $act === 'outcoming' ? 'act_tab_a' : 'ki')" href="?act=incoming">{_req}</a>
<div n:if="!is_null($thisUser) && $user->getId() === $thisUser->getId()" n:attr="id => ($act === 'incoming' || $act === 'followers' || $act === 'outcoming' ? 'activetabs' : 'ki')" class="tab">
<a n:attr="id => ($act === 'incoming' || $act === 'followers' || $act === 'outcoming' ? 'act_tab_a' : 'ki')" href="?act=incoming">{_req}</a>
</div>
{/block}

{block size}
<div n:if="$act === 'incoming' || $act === 'outcoming'" class="mb_tabs">
<div n:if="$act === 'incoming' || $act === 'followers' || $act === 'outcoming'" class="mb_tabs">
<div n:attr="id => ($act === 'incoming' ? 'active' : 'ki')" class="mb_tab">
<div>
<a href="?act=incoming">{_incoming_req}</a>
</div>
</div>
<div n:attr="id => ($act === 'followers' ? 'active' : 'ki')" class="mb_tab">
<div>
<a href="?act=followers">{_followers}</a>
</div>
</div>
<div n:attr="id => ($act === 'outcoming' ? 'active' : 'ki')" class="mb_tab">
<div>
<a href="?act=outcoming">{_outcoming_req}</a>
Expand All @@ -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}
Expand Down Expand Up @@ -144,6 +158,14 @@
<input type="hidden" name="hash" value="{$csrfToken}" />
<input type="submit" class="profile_link" value="{_friends_accept}" />
</form>
{if $act !== 'followers'}
<form action="/setSub/user" method="post" class="profile_link_form">
<input type="hidden" name="act" value="rej" />
<input type="hidden" name="id" value="{$x->getId()}" />
<input type="hidden" name="hash" value="{$csrfToken}" />
<input type="submit" class="profile_link" value="{_friends_leave_in_flw}" />
</form>
{/if}
{elseif $subStatus === 2}
<form action="/setSub/user" method="post" class="profile_link_form">
<input type="hidden" name="act" value="rem" />
Expand Down
7 changes: 2 additions & 5 deletions Web/static/css/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -2068,20 +2068,17 @@ table td[width="120"] {
font-weight: bold;
margin-right: 10px;
display: inline-block;
border-radius: 2px;
}

.mb_tab div {
padding: 3px 7px;
padding: 5px 9px;
}

.mb_tab#active {
background-color: #898989;
}

.mb_tab#active div {
border: 2px solid #5f5f5f;
}

.mb_tab#active a {
color: white;
}
Expand Down
1 change: 1 addition & 0 deletions install/sqls/00046-deny-request.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE `subscriptions` ADD `flags` tinyint(3) unsigned NOT NULL;
1 change: 1 addition & 0 deletions locales/by.strings
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@
"friends_delete" = "Выдаліць з сяброў";
"friends_reject" = "Адмяніць заяўку";
"friends_accept" = "Прыняць заяўку";
"friends_leave_in_flw" = "Пакінуть у падпісчыках";
"send_message" = "Адправіць паведамленне";
"incoming_req" = "Падпісанты";
"outcoming_req" = "Заяўкі";
Expand Down
1 change: 1 addition & 0 deletions locales/en.strings
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@
"friends_delete" = "Remove from friends";
"friends_reject" = "Reject request";
"friends_accept" = "Accept request";
"friends_leave_in_flw" = "Leave in followers";
"send_message" = "Send a message";
"incoming_req" = "Pending";
"outcoming_req" = "Outgoing";
Expand Down
1 change: 1 addition & 0 deletions locales/ru.strings
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@
"friends_delete" = "Удалить из друзей";
"friends_reject" = "Отменить заявку";
"friends_accept" = "Принять заявку";
"friends_leave_in_flw" = "Оставить в подписчиках";
"send_message" = "Отправить сообщение";
"incoming_req" = "Входящие";
"outcoming_req" = "Исходящие";
Expand Down
1 change: 1 addition & 0 deletions locales/uk.strings
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@
"friends_delete" = "Видалити з друзів";
"friends_reject" = "Скасувати заявку";
"friends_accept" = "Прийняти заявку";
"friends_leave_in_flw" = "Залишити у підписниках";
"send_message" = "Відправити повідомлення";
"incoming_req" = "Підписники";
"outcoming_req" = "Вихідні";
Expand Down

0 comments on commit 4c78617

Please sign in to comment.