openvk/VKAPI/Handlers/Users.php

182 lines
6.1 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\Entities\User;
use openvk\Web\Models\Repositories\Users as UsersRepo;
final class Users extends VKAPIRequestHandler
{
function get(string $user_ids = "0", string $fields = "", int $offset = 0, int $count = 100, User $authuser = null /* костыль(( */): array
2020-08-12 14:36:18 +03:00
{
if($authuser == NULL) $authuser = $this->getUser();
2020-08-12 14:36:18 +03:00
$users = new UsersRepo;
2021-10-22 23:49:25 +03:00
if($user_ids == "0")
$user_ids = (string) $authuser->getId();
2021-10-22 23:49:25 +03:00
2022-07-21 22:13:09 +03:00
$usrs = explode(',', $user_ids);','
2020-08-12 14:36:18 +03:00
$response;
$ic = sizeof($usrs);
2022-07-21 22:13:09 +03:00
if(sizeof($usrs) > $count)
$ic = $count;
2020-08-12 14:36:18 +03:00
$usrs = array_slice($usrs, $offset * $count);
2022-07-21 22:13:09 +03:00
for($i=0; $i < $ic; $i++) {
2020-08-12 14:36:18 +03:00
$usr = $users->get((int) $usrs[$i]);
2022-07-21 22:13:09 +03:00
if(is_null($usr)) {
2020-08-12 14:36:18 +03:00
$response[$i] = (object)[
2022-07-21 22:13:09 +03:00
"id" => $usrs[$i],
"first_name" => "DELETED",
"last_name" => "",
2020-08-12 14:36:18 +03:00
"deactivated" => "deleted"
];
2022-07-21 22:13:09 +03:00
} else if($usrs[$i] == NULL) {
2020-08-12 14:36:18 +03:00
2022-07-21 22:13:09 +03:00
} else {
2020-08-12 14:36:18 +03:00
$response[$i] = (object)[
2022-07-21 22:13:09 +03:00
"id" => $usr->getId(),
"first_name" => $usr->getFirstName(),
"last_name" => $usr->getLastName(),
"is_closed" => false,
2020-08-12 14:36:18 +03:00
"can_access_closed" => true,
];
$flds = explode(',', $fields);
foreach($flds as $field) {
2022-07-21 22:13:09 +03:00
switch($field) {
case "verified":
2021-10-22 23:49:25 +03:00
$response[$i]->verified = intval($usr->isVerified());
break;
2022-07-21 22:13:09 +03:00
case "sex":
2022-03-27 17:57:48 +03:00
$response[$i]->sex = $usr->isFemale() ? 1 : 2;
2021-10-22 23:49:25 +03:00
break;
2022-07-21 22:13:09 +03:00
case "has_photo":
2021-10-22 23:49:25 +03:00
$response[$i]->has_photo = is_null($usr->getAvatarPhoto()) ? 0 : 1;
break;
2022-07-21 22:13:09 +03:00
case "photo_max_orig":
2021-10-22 23:49:25 +03:00
$response[$i]->photo_max_orig = $usr->getAvatarURL();
break;
2022-07-21 22:13:09 +03:00
case "photo_max":
$response[$i]->photo_max = $usr->getAvatarURL("original");
2021-10-22 23:49:25 +03:00
break;
2022-07-21 22:13:09 +03:00
case "photo_50":
$response[$i]->photo_50 = $usr->getAvatarURL();
break;
2022-07-21 22:13:09 +03:00
case "photo_100":
$response[$i]->photo_100 = $usr->getAvatarURL("tiny");
break;
2022-07-21 22:13:09 +03:00
case "photo_200":
$response[$i]->photo_200 = $usr->getAvatarURL("normal");
break;
2022-07-21 22:13:09 +03:00
case "photo_200_orig": # вообще не ебу к чему эта строка ну пусть будет кек
$response[$i]->photo_200_orig = $usr->getAvatarURL("normal");
break;
2022-07-21 22:13:09 +03:00
case "photo_400_orig":
$response[$i]->photo_400_orig = $usr->getAvatarURL("normal");
break;
# Она хочет быть выебанной видя матан
# Покайфу когда ты Виет а вокруг лишь дискриминант
2022-07-21 22:13:09 +03:00
case "status":
if($usr->getStatus() != NULL)
2021-10-22 23:49:25 +03:00
$response[$i]->status = $usr->getStatus();
break;
2022-07-21 22:13:09 +03:00
case "screen_name":
if($usr->getShortCode() != NULL)
2021-10-22 23:49:25 +03:00
$response[$i]->screen_name = $usr->getShortCode();
break;
2022-07-21 22:13:09 +03:00
case "friend_status":
switch($usr->getSubscriptionStatus($authuser)) {
2021-10-22 23:49:25 +03:00
case 3:
2022-07-21 22:13:09 +03:00
# NOTICE falling through
2021-10-22 23:49:25 +03:00
case 0:
$response[$i]->friend_status = $usr->getSubscriptionStatus($authuser);
2021-10-22 23:49:25 +03:00
break;
case 1:
$response[$i]->friend_status = 2;
break;
case 2:
$response[$i]->friend_status = 1;
break;
}
break;
2022-07-21 22:13:09 +03:00
case "last_seen":
if ($usr->onlineStatus() == 0)
2021-10-22 23:49:25 +03:00
$response[$i]->last_seen = (object) [
"platform" => 1,
2022-07-21 22:13:09 +03:00
"time" => $usr->getOnline()->timestamp()
2021-10-22 23:49:25 +03:00
];
2022-07-21 22:13:09 +03:00
case "music":
2021-10-22 23:49:25 +03:00
$response[$i]->music = $usr->getFavoriteMusic();
break;
2022-07-21 22:13:09 +03:00
case "movies":
2021-10-22 23:49:25 +03:00
$response[$i]->movies = $usr->getFavoriteFilms();
break;
2022-07-21 22:13:09 +03:00
case "tv":
2021-10-22 23:49:25 +03:00
$response[$i]->tv = $usr->getFavoriteShows();
break;
2022-07-21 22:13:09 +03:00
case "books":
2021-10-22 23:49:25 +03:00
$response[$i]->books = $usr->getFavoriteBooks();
break;
2022-07-21 22:13:09 +03:00
case "city":
2021-10-22 23:49:25 +03:00
$response[$i]->city = $usr->getCity();
break;
2022-07-21 22:13:09 +03:00
case "interests":
2021-10-22 23:49:25 +03:00
$response[$i]->interests = $usr->getInterests();
break;
2020-08-12 14:36:18 +03:00
}
}
2022-07-21 22:13:09 +03:00
if($usr->getOnline()->timestamp() + 300 > time())
2021-10-22 23:49:25 +03:00
$response[$i]->online = 1;
2022-07-21 22:13:09 +03:00
else
2021-10-22 23:49:25 +03:00
$response[$i]->online = 0;
2020-08-12 14:36:18 +03:00
}
}
return $response;
}
function getFollowers(int $user_id, string $fields = "", int $offset = 0, int $count = 100): object
{
$offset++;
$followers = [];
$users = new UsersRepo;
$this->requireUser();
2022-07-21 22:13:09 +03:00
foreach($users->get($user_id)->getFollowers($offset, $count) as $follower)
2020-08-12 14:36:18 +03:00
$followers[] = $follower->getId();
$response = $followers;
2022-07-21 22:13:09 +03:00
if(!is_null($fields))
2020-08-12 14:36:18 +03:00
$response = $this->get(implode(',', $followers), $fields, 0, $count);
return (object) [
"count" => $users->get($user_id)->getFollowersCount(),
"items" => $response
];
}
2022-07-21 22:13:09 +03:00
function search(string $q, string $fields = "", int $offset = 0, int $count = 100)
2020-08-12 14:36:18 +03:00
{
$users = new UsersRepo;
$array = [];
2022-07-21 22:13:09 +03:00
$find = $users->find($q);
2020-08-12 14:36:18 +03:00
2022-07-21 22:13:09 +03:00
foreach ($find as $user)
2020-08-12 14:36:18 +03:00
$array[] = $user->getId();
2022-07-21 22:13:09 +03:00
return (object) [
"count" => $find->size(),
2020-08-12 14:36:18 +03:00
"items" => $this->get(implode(',', $array), $fields, $offset, $count)
];
}
}