openvk/VKAPI/Handlers/Account.php

194 lines
6.5 KiB
PHP
Raw Permalink Normal View History

2020-08-12 14:36:18 +03:00
<?php declare(strict_types=1);
namespace openvk\VKAPI\Handlers;
use openvk\Web\Models\Exceptions\InvalidUserNameException;
2020-08-12 14:36:18 +03:00
final class Account extends VKAPIRequestHandler
{
function getProfileInfo(): object
{
$this->requireUser();
$user = $this->getUser();
$return_object = (object) [
"first_name" => $user->getFirstName(),
"photo_200" => $user->getAvatarURL("normal"),
"nickname" => $user->getPseudo(),
"is_service_account" => false,
"id" => $user->getId(),
"is_verified" => $user->isVerified(),
"verification_status" => $user->isVerified() ? 'verified' : 'unverified',
"last_name" => $user->getLastName(),
"home_town" => $user->getHometown(),
"status" => $user->getStatus(),
"bdate" => is_null($user->getBirthday()) ? '01.01.1970' : $user->getBirthday()->format('%e.%m.%Y'),
"bdate_visibility" => $user->getBirthdayPrivacy(),
2022-07-21 22:13:09 +03:00
"phone" => "+420 ** *** 228", # TODO
"relation" => $user->getMaritalStatus(),
"screen_name" => $user->getShortCode(),
"sex" => $user->isFemale() ? 1 : 2,
#"email" => $user->getEmail(),
2020-08-12 14:36:18 +03:00
];
$audio_status = $user->getCurrentAudioStatus();
if(!is_null($audio_status))
$return_object->audio_status = $audio_status->toVkApiStruct($user);
return $return_object;
2020-08-12 14:36:18 +03:00
}
function getInfo(): object
{
$this->requireUser();
return (object) [
2022-08-11 23:20:36 +03:00
"2fa_required" => $this->getUser()->is2faEnabled() ? 1 : 0,
2022-07-21 22:13:09 +03:00
"country" => "CZ", # TODO
"eu_user" => false, # TODO
"https_required" => 1,
"intro" => 0,
"community_comments" => false,
"is_live_streaming_enabled" => false,
2020-08-12 14:36:18 +03:00
"is_new_live_streaming_enabled" => false,
2022-07-21 22:13:09 +03:00
"lang" => 1,
"no_wall_replies" => 0,
"own_posts_default" => 0
2020-08-12 14:36:18 +03:00
];
}
function setOnline(): int
2020-08-12 14:36:18 +03:00
{
$this->requireUser();
$this->getUser()->updOnline($this->getPlatform());
2022-08-13 05:41:59 +03:00
2020-08-12 14:36:18 +03:00
return 1;
}
function setOffline(): int
2020-08-12 14:36:18 +03:00
{
$this->requireUser();
# Цiй метод є заглушка
2020-08-12 14:36:18 +03:00
return 1;
}
function getAppPermissions(): int
2020-08-12 14:36:18 +03:00
{
return 9355263;
}
function getCounters(string $filter = ""): object
{
$this->requireUser();
2020-08-12 14:36:18 +03:00
return (object) [
2022-07-21 22:13:09 +03:00
"friends" => $this->getUser()->getFollowersCount(),
"notifications" => $this->getUser()->getNotificationsCount(),
2022-07-21 22:13:09 +03:00
"messages" => $this->getUser()->getUnreadMessagesCount()
2020-08-12 14:36:18 +03:00
];
# TODO: Filter
2020-08-12 14:36:18 +03:00
}
function saveProfileInfo(string $first_name = "", string $last_name = "", string $screen_name = "", int $sex = -1, int $relation = -1, string $bdate = "", int $bdate_visibility = -1, string $home_town = "", string $status = ""): object
{
$this->requireUser();
2023-02-08 14:20:50 +03:00
$this->willExecuteWriteAction();
$user = $this->getUser();
$output = [
"changed" => 0,
];
if(!empty($first_name) || !empty($last_name)) {
$output["name_request"] = [
"id" => random_int(1, 2048), # For compatibility with original VK API
"status" => "success",
"first_name" => !empty($first_name) ? $first_name : $user->getFirstName(),
"last_name" => !empty($last_name) ? $last_name : $user->getLastName(),
];
try {
if(!empty($first_name))
$user->setFirst_name($first_name);
if(!empty($last_name))
$user->setLast_Name($last_name);
} catch (InvalidUserNameException $e) {
$output["name_request"]["status"] = "declined";
return (object) $output;
}
}
if(!empty($screen_name))
if (!$user->setShortCode($screen_name))
$this->fail(1260, "Invalid screen name");
# For compatibility with original VK API
if($sex > 0)
$user->setSex($sex == 1 ? 1 : 0);
if($relation > -1)
$user->setMarital_Status($relation);
if(!empty($bdate)) {
$birthday = strtotime($bdate);
if (!is_int($birthday))
$this->fail(100, "invalid value of bdate.");
$user->setBirthday($birthday);
}
# For compatibility with original VK API
switch($bdate_visibility) {
case 0:
$this->fail(946, "Hiding date of birth is not implemented.");
break;
case 1:
$user->setBirthday_privacy(0);
break;
case 2:
$user->setBirthday_privacy(1);
}
if(!empty($home_town))
$user->setHometown($home_town);
if(!empty($status))
$user->setStatus($status);
if($sex > 0 || $relation > -1 || $bdate_visibility > 1 || !empty("$first_name$last_name$screen_name$bdate$home_town$status")) {
$output["changed"] = 1;
$user->save();
}
return (object) $output;
}
function getBalance(): object
{
$this->requireUser();
if(!OPENVK_ROOT_CONF['openvk']['preferences']['commerce'])
$this->fail(105, "Commerce is disabled on this instance");
return (object) ['votes' => $this->getUser()->getCoins()];
}
function getOvkSettings(): object
{
$this->requireUser();
$user = $this->getUser();
$settings_list = (object)[
'avatar_style' => $user->getStyleAvatar(),
'style' => $user->getStyle(),
'show_rating' => !$user->prefersNotToSeeRating(),
'nsfw_tolerance' => $user->getNsfwTolerance(),
'post_view' => $user->hasMicroblogEnabled() ? 'microblog' : 'old',
'main_page' => $user->getMainPage() == 0 ? 'my_page' : 'news',
];
return $settings_list;
}
2020-08-12 14:36:18 +03:00
}