fix bug with friends method when you can add non-existing or deleted users and yourself

This commit is contained in:
veselcraft 2020-11-01 04:17:04 -05:00
parent 1e5e9d7080
commit 991e377e16

View file

@ -6,149 +6,155 @@ use openvk\Web\Models\Repositories\Users as UsersRepo;
final class Friends extends VKAPIRequestHandler final class Friends extends VKAPIRequestHandler
{ {
function get(int $user_id, string $fields = "", int $offset = 0, int $count = 100): object function get(int $user_id, string $fields = "", int $offset = 0, int $count = 100): object
{ {
$i = 0; $i = 0;
$offset++; $offset++;
$friends = []; $friends = [];
$users = new UsersRepo; $users = new UsersRepo;
$this->requireUser(); $this->requireUser();
foreach ($users->get($user_id)->getFriends($offset, $count) as $friend) { foreach ($users->get($user_id)->getFriends($offset, $count) as $friend) {
$friends[$i] = $friend->getId(); $friends[$i] = $friend->getId();
$i++; $i++;
} }
$response = $friends; $response = $friends;
$usersApi = new Users($this->getUser()); $usersApi = new Users($this->getUser());
if (!is_null($fields)) { if (!is_null($fields)) {
$response = $usersApi->get(implode(',', $friends), $fields, 0, $count, true); // FIXME $response = $usersApi->get(implode(',', $friends), $fields, 0, $count, true); // FIXME
} }
return (object) [ return (object) [
"count" => $users->get($user_id)->getFriendsCount(), "count" => $users->get($user_id)->getFriendsCount(),
"items" => $response "items" => $response
]; ];
} }
function getLists(): object function getLists(): object
{ {
$this->requireUser(); $this->requireUser();
return (object) [ return (object) [
"count" => 0, "count" => 0,
"items" => (array)[] "items" => (array)[]
]; ];
} }
function deleteList(): int function deleteList(): int
{ {
$this->requireUser(); $this->requireUser();
return 1; return 1;
} }
function edit(): int function edit(): int
{ {
$this->requireUser(); $this->requireUser();
return 1; return 1;
} }
function editList(): int function editList(): int
{ {
$this->requireUser(); $this->requireUser();
return 1; return 1;
} }
function add(string $user_id): int function add(string $user_id): int
{ {
$this->requireUser(); $this->requireUser();
$users = new UsersRepo; $users = new UsersRepo;
$user = $users->get(intval($user_id)); $user = $users->get(intval($user_id));
if(is_null($user)){
$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");
}
switch ($user->getSubscriptionStatus($this->getUser())) { switch ($user->getSubscriptionStatus($this->getUser())) {
case 0: case 0:
$user->toggleSubscription($this->getUser()); $user->toggleSubscription($this->getUser());
return 1; return 1;
break; break;
case 1: case 1:
$user->toggleSubscription($this->getUser()); $user->toggleSubscription($this->getUser());
return 2; return 2;
break; break;
case 3: case 3:
return 2; return 2;
break; break;
default: default:
return 1; return 1;
break; break;
} }
} }
function delete(string $user_id): int function delete(string $user_id): int
{ {
$this->requireUser(); $this->requireUser();
$users = new UsersRepo; $users = new UsersRepo;
$user = $users->get(intval($user_id)); $user = $users->get(intval($user_id));
switch ($user->getSubscriptionStatus($this->getUser())) { switch ($user->getSubscriptionStatus($this->getUser())) {
case 3: case 3:
$user->toggleSubscription($this->getUser()); $user->toggleSubscription($this->getUser());
return 1; return 1;
break; break;
default: default:
fail(15, "Access denied: No friend or friend request found."); fail(15, "Access denied: No friend or friend request found.");
break; break;
} }
} }
function areFriends(string $user_ids): array function areFriends(string $user_ids): array
{ {
$this->requireUser(); $this->requireUser();
$users = new UsersRepo; $users = new UsersRepo;
$friends = explode(',', $user_ids); $friends = explode(',', $user_ids);
$response = []; $response = [];
for ($i=0; $i < sizeof($friends); $i++) { for ($i=0; $i < sizeof($friends); $i++) {
$friend = $users->get(intval($friends[$i])); $friend = $users->get(intval($friends[$i]));
$status = 0; $status = 0;
switch ($friend->getSubscriptionStatus($this->getUser())) { switch ($friend->getSubscriptionStatus($this->getUser())) {
case 3: case 3:
case 0: case 0:
$status = $friend->getSubscriptionStatus($this->getUser()); $status = $friend->getSubscriptionStatus($this->getUser());
break; break;
case 1: case 1:
$status = 2; $status = 2;
break; break;
case 2: case 2:
$status = 1; $status = 1;
break; break;
} }
$response[] = (object)[ $response[] = (object)[
"friend_status" => $friend->getSubscriptionStatus($this->getUser()), "friend_status" => $friend->getSubscriptionStatus($this->getUser()),
"user_id" => $friend->getId() "user_id" => $friend->getId()
]; ];
} }
return $response; return $response;
} }
} }