Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion VKAPI/Handlers/Users.php
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ public function get(string $user_ids = "0", string $fields = "", int $offset = 0
"notes" => (new Notes())->getUserNotesCount($usr),
"groups" => $usr->getClubCount(),
"online_friends" => $usr->getFriendsOnlineCount(),
"mutual_friends" => 0, // FIXME: not implemented
"mutual_friends" => $authuser ? $usr->getCommonFriendsCount($authuser) : 0,
"user_photos" => 0, // FIXME: not implemented
"albums" => (new Albums())->getUserAlbumsCount($usr),
"followers" => $usr->getFollowersCount(),
Expand Down
22 changes: 18 additions & 4 deletions Web/Models/Entities/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,16 @@ class User extends RowModel
private $_avatarAlbum = null;
private $_avatarPhoto = false; // false - not resolved, null - no avatar

protected function _abstractRelationGenerator(string $filename, int $page = 1, int $limit = 6): \Traversable
protected function _abstractRelationGenerator(string $filename, int $page = 1, int $limit = 6, $second_id = null): \Traversable
{
$id = $this->getId();
$second_id ??= $id;

$query = "SELECT id FROM\n" . file_get_contents(__DIR__ . "/../sql/$filename.tsql");
$query .= "\n LIMIT " . $limit . " OFFSET " . (($page - 1) * $limit);

$ids = [];
$rels = DatabaseConnection::i()->getConnection()->query($query, $id, $id);
$rels = DatabaseConnection::i()->getConnection()->query($query, $id, $second_id);
foreach ($rels as $rel) {
$rel = (new Users())->get($rel->id);
if (!$rel) {
Expand All @@ -68,12 +70,14 @@ protected function _abstractRelationGenerator(string $filename, int $page = 1, i
}
}

protected function _abstractRelationCount(string $filename): int
protected function _abstractRelationCount(string $filename, $second_id = null): int
{
$id = $this->getId();
$second_id ??= $id;

$query = "SELECT COUNT(*) AS cnt FROM\n" . file_get_contents(__DIR__ . "/../sql/$filename.tsql");

return (int) DatabaseConnection::i()->getConnection()->query($query, $id, $id)->fetch()->cnt;
return (int) DatabaseConnection::i()->getConnection()->query($query, $id, $second_id)->fetch()->cnt;
}

public function getId(): int
Expand Down Expand Up @@ -701,6 +705,16 @@ public function getFriendsOnlineCount(): int
return $this->_abstractRelationCount("get-online-friends");
}

public function getCommonFriends(User $me, int $page = 1, int $limit = 6): \Traversable
{
return $this->_abstractRelationGenerator("get-common-friends", $page, $limit, $me->getId());
}

public function getCommonFriendsCount(User $me): int
{
return $this->_abstractRelationCount("get-common-friends", $me->getId());
}

public function getFriendsBday(bool $today): array
{
$users = $this->_abstractRelationGenerator($today ? "get-bday-today" : "get-bday-tomorrow", 1, 3000);
Expand Down
1 change: 1 addition & 0 deletions Web/Models/sql/get-common-friends.tsql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(SELECT DISTINCT p.* FROM subscriptions af1 INNER JOIN subscriptions af2 ON af1.follower = af2.target AND af2.follower = af1.target AND af1.model="openvk\\Web\\Models\\Entities\\User" AND af2.model="openvk\\Web\\Models\\Entities\\User" INNER JOIN subscriptions bf1 ON bf1.follower = af1.follower INNER JOIN subscriptions bf2 ON bf1.follower = bf2.target AND bf2.follower = bf1.target AND bf1.model="openvk\\Web\\Models\\Entities\\User" AND bf2.model="openvk\\Web\\Models\\Entities\\User" INNER JOIN profiles p ON p.id = af1.follower WHERE af1.target = ? AND bf1.target = ?) as common
42 changes: 38 additions & 4 deletions Web/Presenters/UserPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,20 +95,54 @@ public function renderFriends(int $id): void
$this->template->user = $user;
}

$this->template->mode = in_array($this->queryParam("act"), [
"incoming", "outcoming", "friends",
$this->template->act = in_array($this->queryParam("act"), [
"incoming", "outcoming", "friends", "common", "online",
]) ? $this->queryParam("act")
: "friends";
: "friends";

$this->template->page = $page;

if (!is_null($this->user->identity)) {
if ($this->template->mode !== "friends" && $this->user->id !== $id) {
if (!in_array($this->template->act, ["friends", "common"]) && $this->user->id !== $id) {
$name = $user->getFullName();
$this->flash("err", tr("error_access_denied_short"), tr("error_viewing_subs", $name));

$this->redirect($user->getURL());
}
}

if ($this->template->act === "common" && is_null($this->user->identity)) {
$this->template->act = "friends";
}

switch ($this->template->act) {
case "incoming":
$iterator = $user->getRequests($page);
$count = $user->getRequestsCount();
break;
case "outcoming":
$iterator = $user->getSubscriptions($page);
$count = $user->getSubscriptionsCount();
break;
case "followers":
$iterator = $user->getFollowers($page);
$count = $user->getFollowersCount();
break;
case "online":
$iterator = $user->getFriendsOnline($page);
$count = $user->getFriendsOnlineCount();
break;
case "common":
$iterator = $user->getCommonFriends($this->user->identity, $page);
$count = $user->getCommonFriendsCount($this->user->identity);
break;
default:
$iterator = $user->getFriends($page);
$count = $user->getFriendsCount();
break;
}
$this->template->iterator = iterator_to_array($iterator);
$this->template->count = $count;
}

public function renderGroups(int $id): void
Expand Down
48 changes: 7 additions & 41 deletions Web/Presenters/templates/User/Friends.latte
Original file line number Diff line number Diff line change
@@ -1,55 +1,16 @@
{extends "../@listView.latte"}
{var $perPage = 6} {* Why 6? Check User::_abstractRelationGenerator *}

{var $act = $_GET["act"] ?? "friends"}

{if $act == "incoming"}
{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()}
{else}
{var $iterator = iterator_to_array($user->getFriends($page))}
{var $count = $user->getFriendsCount()}
{/if}

{block title}
{if $act == "incoming"}
{_incoming_req}
{elseif $act == "outcoming"}
{_outcoming_req}
{elseif $act == "followers"}
{_followers}
{elseif $act == "online"}
{_friends_online}
{else}
{_friends}
{/if}
{include "Friends/Title.latte", act => $act}
{/block}

{block header}
{if isset($thisUser) && $thisUser->getId() == $user->getId()}
{_my_friends}
{else}
<a href="{$user->getURL()}">{$user->getCanonicalName()}</a> »
{if $act == "incoming"}
{_incoming_req}
{elseif $act == "outcoming"}
{_outcoming_req}
{elseif $act == "followers"}
{_followers}
{elseif $act == "online"}
{_friends_online}
{else}
{_friends}
{/if}
{include "Friends/Title.latte", act => $act}
{/if}
{/block}

Expand All @@ -63,6 +24,9 @@
<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>
<div n:if="!is_null($thisUser) && $user->getId() !== $thisUser->getId()" n:attr="id => ($act === 'common' ? 'activetabs' : 'ki')" class="tab">
<a n:attr="id => ($act === 'common' ? 'act_tab_a' : 'ki')" href="?act=common">{_friends_common}</a>
</div>
{/block}

{block size}
Expand Down Expand Up @@ -97,6 +61,8 @@
{else}
{tr("friends_list", $count)}
{/if}
{elseif $act == "common"}
{tr("friends_list_common", $count)}
{else}
{tr("friends", $count)}
{/if}
Expand Down
13 changes: 13 additions & 0 deletions Web/Presenters/templates/User/Friends/Title.latte
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{if $act == "incoming"}
{_incoming_req}
{elseif $act == "outcoming"}
{_outcoming_req}
{elseif $act == "followers"}
{_followers}
{elseif $act == "online"}
{_friends_online}
{elseif $act == "common"}
{_friends_common}
{else}
{_friends}
{/if}
11 changes: 11 additions & 0 deletions Web/Presenters/templates/User/Thumb.latte
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<div class="cl_element" >
<div class="cl_avatar">
<a href="{$user->getURL()}">
<img class="ava" src="{$user->getAvatarUrl('miniscule')}" />
</a>
</div>
<a href="{$user->getURL()}" class="cl_name">
<text class="cl_fname">{$user->getFirstName()}</text>
<text class="cl_lname">{$user->getLastName()}</text>
</a>
</div>
46 changes: 24 additions & 22 deletions Web/Presenters/templates/User/View.latte
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,24 @@
{/if}
</div>
<br />
<div n:if="$thisUser != NULL && $thisUser->getId() != $user->getId() && $user->getPrivacyPermission('friends.read', $thisUser ?? NULL) && $commonFriendsCount = $user->getCommonFriendsCount($thisUser) > 0">
<div class="content_title_expanded" onclick="hidePanel(this, {$commonFriendsCount});">
{_friends_common}
</div>
<div>
<div class="content_subtitle">
{tr("friends_common", $commonFriendsCount)}
<div style="float:right;">
<a href="/friends{$user->getId()}?act=common">{_all_title}</a>
</div>
</div>
<div class="content_list">
{foreach $user->getCommonFriends($thisUser, 1) as $friend}
{include "Thumb.latte", user => $friend}
{/foreach}
</div>
</div>
</div>
<div class="fictional_profile_section" n:if="$user->getFriendsCount() > 0 && $user->getPrivacyPermission('friends.read', $thisUser ?? NULL)">
{var $friendCount = $user->getFriendsCount()}

Expand All @@ -229,17 +247,9 @@
</div>
</div>
<div class="content_list hide_on_mobiles">
<div class="cl_element" n:foreach="$user->getFriends(1) as $friend">
<div class="cl_avatar">
<a href="{$friend->getURL()}">
<img class="ava" src="{$friend->getAvatarUrl('miniscule')}" />
</a>
</div>
<a href="{$friend->getURL()}" class="cl_name">
<text class="cl_fname">{$friend->getFirstName()}</text>
<text class="cl_lname">{$friend->getLastName()}</text>
</a>
</div>
{foreach $user->getFriends(1) as $friend}
{include "Thumb.latte", user => $friend}
{/foreach}
</div>
</div>
</div>
Expand All @@ -257,17 +267,9 @@
</div>
</div>
<div class="hide_on_mobiles content_list">
<div class="cl_element" n:foreach="$user->getFriendsOnline(1) as $friend">
<div class="cl_avatar">
<a href="{$friend->getURL()}">
<img class="ava" src="{$friend->getAvatarUrl('miniscule')}" />
</a>
</div>
<a href="{$friend->getURL()}" class="cl_name">
<text class="cl_fname">{$friend->getFirstName()}</text>
<text class="cl_lname">{$friend->getLastName()}</text>
</a>
</div>
{foreach $user->getFriendsOnline(1) as $friend}
{include "Thumb.latte", user => $friend}
{/foreach}
</div>
</div>
</div>
Expand Down
11 changes: 11 additions & 0 deletions locales/ru.strings
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@
"outcoming_req" = "Исходящие";
"req" = "Заявки";
"friends_online" = "Друзья онлайн";
"friends_common" = "Общие друзья";
"all_friends" = "Все друзья";
"req_zero" = "Не найдено ни одной заявки...";
"req_one" = "Найдена $1 заявка";
Expand All @@ -346,6 +347,11 @@
"friends_online_few" = "$1 друга онлайн";
"friends_online_many" = "$1 друзей онлайн";
"friends_online_other" = "$1 друзей онлайн";
"friends_common_zero" = "Ни одного общего друга ";
"friends_common_one" = "$1 общий друг";
"friends_common_few" = "$1 общих друга";
"friends_common_many" = "$1 общих друзей";
"friends_common_other" = "$1 общих друзей";
"friends_list_zero" = "У Вас пока нет друзей";
"friends_list_one" = "У Вас $1 друг";
"friends_list_few" = "У Вас $1 друга";
Expand All @@ -366,6 +372,11 @@
"friends_list_online_few" = "У Вас $1 друга онлайн";
"friends_list_online_many" = "У Вас $1 друзей онлайн";
"friends_list_online_other" = "У Вас $1 друзей онлайн";
"friends_list_common_zero" = "У Вас пока нет общих друзей";
"friends_list_common_one" = "У Вас $1 общий друг";
"friends_list_common_few" = "У Вас $1 общих друга";
"friends_list_common_many" = "У Вас $1 общих друзей";
"friends_list_common_other" = "У Вас $1 общих друзей";

/* Group */

Expand Down