openvk/VKAPI/Handlers/Friends.php

188 lines
4.5 KiB
PHP
Raw Normal View History

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
{
2023-07-05 18:06:15 +03:00
function get(int $user_id, string $fields = "", int $offset = 0, int $count = 100): object
{
$i = 0;
$offset++;
$friends = [];
$users = new UsersRepo;
$uzver = $users->get($user_id);
$this->requireUser();
if(!$uzver || $uzver->isDeleted()) {
$this->fail(10, "Invalid user");
}
if(!$uzver->getPrivacyPermission('friends.read', $this->getUser() ?? NULL)) {
$this->fail(7, "Access denied: this user chose to hide his friends");
}
if(!$uzver->canBeViewedBy($this->getUser())) {
$this->fail(8, "Access denied");
}
foreach($uzver->getFriends($offset, $count) as $friend) {
$friends[$i] = $friend->getId();
$i++;
}
$response = $friends;
$usersApi = new Users($this->getUser());
if(!is_null($fields))
$response = $usersApi->get(implode(',', $friends), $fields, 0, $count); # FIXME
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();
2023-02-08 14:20:50 +03:00
$this->willExecuteWriteAction();
2023-07-05 18:06:15 +03:00
$users = new UsersRepo;
$user = $users->get((int)$user_id);
if(!$user || $user->isDeleted()) {
$this->fail(177, "Invalid user");
}
if($user->getId() == $this->getUser()->getId()) {
$this->fail(174, "Cannot add user himself as friend");
}
switch($user->getSubscriptionStatus($this->getUser())) {
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();
2023-02-08 14:20:50 +03:00
$this->willExecuteWriteAction();
2023-07-05 18:06:15 +03:00
$users = new UsersRepo;
2023-07-05 18:06:15 +03:00
$user = $users->get((int)$user_id);
2023-07-05 18:06:15 +03:00
if(!$user || $user->isDeleted()) {
$this->fail(8, "Invalid user");
}
switch($user->getSubscriptionStatus($this->getUser())) {
case 3:
$user->toggleSubscription($this->getUser());
return 1;
default:
$this->fail(15, "Access denied: No friend or friend request found.");
}
}
2023-07-05 18:06:15 +03:00
function areFriends(string $user_ids): array
{
$this->requireUser();
2023-07-05 18:06:15 +03:00
$users = new UsersRepo;
2023-07-05 18:06:15 +03:00
$friends = explode(',', $user_ids);
2023-07-05 18:06:15 +03:00
$response = [];
2023-07-05 18:06:15 +03:00
for($i=0; $i < sizeof($friends); $i++) {
$friend = $users->get((int)$friends[$i]);
2023-07-05 18:06:15 +03:00
$response[] = (object)[
"friend_status" => $friend->getSubscriptionStatus($this->getUser()),
"user_id" => $friend->getId()
];
}
2023-07-05 18:06:15 +03:00
return $response;
}
2023-07-05 18:06:15 +03:00
function getRequests(string $fields = "", 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.");
2023-07-05 18:06:15 +03:00
$this->requireUser();
2023-07-05 18:06:15 +03:00
$i = 0;
$offset++;
$followers = [];
2023-07-05 18:06:15 +03:00
foreach($this->getUser()->getFollowers($offset, $count) as $follower) {
$followers[$i] = $follower->getId();
$i++;
}
2023-07-05 18:06:15 +03:00
$response = $followers;
$usersApi = new Users($this->getUser());
2023-07-05 18:06:15 +03:00
$response = $usersApi->get(implode(',', $followers), $fields, 0, $count);
2023-07-05 18:06:15 +03:00
foreach($response as $user)
$user->user_id = $user->id;
2023-07-05 18:06:15 +03:00
return (object) [
"count" => $this->getUser()->getFollowersCount(),
"items" => $response
];
}
}