2020-08-12 14:36:18 +03:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
namespace openvk\VKAPI\Handlers;
|
|
|
|
use openvk\Web\Models\Repositories\Users as UsersRepo;
|
|
|
|
|
|
|
|
final class Friends extends VKAPIRequestHandler
|
|
|
|
{
|
|
|
|
function get(int $user_id, string $fields = "", int $offset = 0, int $count = 100): object
|
2020-11-01 12:17:04 +03:00
|
|
|
{
|
|
|
|
$i = 0;
|
|
|
|
$offset++;
|
|
|
|
$friends = [];
|
|
|
|
|
|
|
|
$users = new UsersRepo;
|
|
|
|
|
|
|
|
$this->requireUser();
|
|
|
|
|
2022-07-21 22:13:09 +03:00
|
|
|
foreach($users->get($user_id)->getFriends($offset, $count) as $friend) {
|
2020-11-01 12:17:04 +03:00
|
|
|
$friends[$i] = $friend->getId();
|
|
|
|
$i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
$response = $friends;
|
|
|
|
|
|
|
|
$usersApi = new Users($this->getUser());
|
|
|
|
|
2022-08-14 12:43:04 +03:00
|
|
|
if(!is_null($fields))
|
2022-05-08 13:06:26 +03:00
|
|
|
$response = $usersApi->get(implode(',', $friends), $fields, 0, $count); # FIXME
|
2020-11-01 12:17:04 +03:00
|
|
|
|
|
|
|
return (object) [
|
|
|
|
"count" => $users->get($user_id)->getFriendsCount(),
|
|
|
|
"items" => $response
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
function getLists(): object
|
|
|
|
{
|
|
|
|
$this->requireUser();
|
|
|
|
|
|
|
|
return (object) [
|
|
|
|
"count" => 0,
|
|
|
|
"items" => (array)[]
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
function deleteList(): int
|
|
|
|
{
|
|
|
|
$this->requireUser();
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
function edit(): int
|
|
|
|
{
|
|
|
|
$this->requireUser();
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
function editList(): int
|
|
|
|
{
|
|
|
|
$this->requireUser();
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
function add(string $user_id): int
|
|
|
|
{
|
|
|
|
$this->requireUser();
|
|
|
|
|
|
|
|
$users = new UsersRepo;
|
2022-07-21 22:13:09 +03:00
|
|
|
$user = $users->get(intval($user_id));
|
2020-11-01 12:17:04 +03:00
|
|
|
|
2022-07-21 22:13:09 +03:00
|
|
|
if(is_null($user)) {
|
2020-11-01 12:17:04 +03:00
|
|
|
$this->fail(177, "Cannot add this user to friends as user not found");
|
|
|
|
} else if($user->getId() == $this->getUser()->getId()) {
|
|
|
|
$this->fail(174, "Cannot add user himself as friend");
|
|
|
|
}
|
|
|
|
|
2022-07-21 22:13:09 +03:00
|
|
|
switch($user->getSubscriptionStatus($this->getUser())) {
|
2020-11-01 12:17:04 +03:00
|
|
|
case 0:
|
|
|
|
$user->toggleSubscription($this->getUser());
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
$user->toggleSubscription($this->getUser());
|
|
|
|
return 2;
|
|
|
|
|
|
|
|
case 3:
|
|
|
|
return 2;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function delete(string $user_id): int
|
|
|
|
{
|
|
|
|
$this->requireUser();
|
|
|
|
|
|
|
|
$users = new UsersRepo;
|
|
|
|
|
|
|
|
$user = $users->get(intval($user_id));
|
|
|
|
|
2022-07-21 22:13:09 +03:00
|
|
|
switch($user->getSubscriptionStatus($this->getUser())) {
|
2020-11-01 12:17:04 +03:00
|
|
|
case 3:
|
|
|
|
$user->toggleSubscription($this->getUser());
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
default:
|
|
|
|
fail(15, "Access denied: No friend or friend request found.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function areFriends(string $user_ids): array
|
|
|
|
{
|
|
|
|
$this->requireUser();
|
|
|
|
|
|
|
|
$users = new UsersRepo;
|
|
|
|
|
|
|
|
$friends = explode(',', $user_ids);
|
|
|
|
|
|
|
|
$response = [];
|
|
|
|
|
2022-07-21 22:13:09 +03:00
|
|
|
for($i=0; $i < sizeof($friends); $i++) {
|
2020-11-01 12:17:04 +03:00
|
|
|
$friend = $users->get(intval($friends[$i]));
|
|
|
|
|
|
|
|
$response[] = (object)[
|
|
|
|
"friend_status" => $friend->getSubscriptionStatus($this->getUser()),
|
2022-07-21 22:13:09 +03:00
|
|
|
"user_id" => $friend->getId()
|
2020-11-01 12:17:04 +03:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
}
|
2022-08-14 12:43:04 +03:00
|
|
|
|
|
|
|
function getRequests(string $fields = "", int $offset = 0, int $count = 100): object
|
|
|
|
{
|
|
|
|
$this->requireUser();
|
|
|
|
|
|
|
|
$i = 0;
|
|
|
|
$offset++;
|
|
|
|
$followers = [];
|
|
|
|
|
|
|
|
foreach($this->getUser()->getFollowers() as $follower) {
|
|
|
|
$followers[$i] = $follower->getId();
|
|
|
|
$i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
$response = $followers;
|
|
|
|
$usersApi = new Users($this->getUser());
|
|
|
|
|
|
|
|
if(!is_null($fields))
|
|
|
|
$response = $usersApi->get(implode(',', $followers), $fields, 0, $count); # FIXME
|
|
|
|
|
|
|
|
foreach($response as $user)
|
|
|
|
$user->user_id = $user->id;
|
|
|
|
|
|
|
|
return (object) [
|
|
|
|
"count" => $this->getUser()->getFollowersCount(),
|
|
|
|
"items" => $response
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|