VKAPI: Make code match the code style

This commit is contained in:
Maxim Leshchenko 2022-07-21 20:13:09 +01:00
parent a578dca41f
commit 771595ef9c
No known key found for this signature in database
GPG key ID: BB9C44A8733FBEEE
10 changed files with 312 additions and 366 deletions

View file

@ -1,6 +1,5 @@
<?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 Friends extends VKAPIRequestHandler
@ -15,7 +14,7 @@ final class Friends extends VKAPIRequestHandler
$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();
$i++;
}
@ -24,7 +23,7 @@ final class Friends extends VKAPIRequestHandler
$usersApi = new Users($this->getUser());
if (!is_null($fields)) {
if(!is_null($fields)) {
$response = $usersApi->get(implode(',', $friends), $fields, 0, $count); # FIXME
}
@ -70,33 +69,28 @@ final class Friends extends VKAPIRequestHandler
$this->requireUser();
$users = new UsersRepo;
$user = $users->get(intval($user_id));
if(is_null($user)){
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:
$user->toggleSubscription($this->getUser());
return 1;
break;
case 1:
$user->toggleSubscription($this->getUser());
return 2;
break;
case 3:
return 2;
break;
default:
return 1;
break;
}
}
@ -108,15 +102,13 @@ final class Friends extends VKAPIRequestHandler
$user = $users->get(intval($user_id));
switch ($user->getSubscriptionStatus($this->getUser())) {
switch($user->getSubscriptionStatus($this->getUser())) {
case 3:
$user->toggleSubscription($this->getUser());
return 1;
break;
default:
fail(15, "Access denied: No friend or friend request found.");
break;
}
}
@ -130,25 +122,9 @@ final class Friends extends VKAPIRequestHandler
$response = [];
for ($i=0; $i < sizeof($friends); $i++) {
for($i=0; $i < sizeof($friends); $i++) {
$friend = $users->get(intval($friends[$i]));
$status = 0;
switch ($friend->getSubscriptionStatus($this->getUser())) {
case 3:
case 0:
$status = $friend->getSubscriptionStatus($this->getUser());
break;
case 1:
$status = 2;
break;
case 2:
$status = 1;
break;
}
$response[] = (object)[
"friend_status" => $friend->getSubscriptionStatus($this->getUser()),
"user_id" => $friend->getId()

View file

@ -1,12 +1,7 @@
<?php declare(strict_types=1);
namespace openvk\VKAPI\Handlers;
use openvk\Web\Models\Entities\User;
use openvk\Web\Models\Entities\Clubs;
use openvk\Web\Models\Repositories\Clubs as ClubsRepo;
use openvk\Web\Models\Repositories\Users as UsersRepo;
use openvk\Web\Models\Entities\Post;
use openvk\Web\Models\Entities\Postable;
use openvk\Web\Models\Repositories\Posts as PostsRepo;
final class Groups extends VKAPIRequestHandler
{
@ -14,45 +9,44 @@ final class Groups extends VKAPIRequestHandler
{
$this->requireUser();
if ($user_id == 0) {
foreach($this->getUser()->getClubs($offset+1) as $club) {
if($user_id == 0) {
foreach($this->getUser()->getClubs($offset+1) as $club)
$clbs[] = $club;
}
$clbsCount = $this->getUser()->getClubCount();
} else {
$users = new UsersRepo;
$user = $users->get($user_id);
if (is_null($user)) {
if(is_null($user))
$this->fail(15, "Access denied");
}
foreach($user->getClubs($offset+1) as $club) {
foreach($user->getClubs($offset+1) as $club)
$clbs[] = $club;
}
$clbsCount = $user->getClubCount();
}
$rClubs;
$ic = sizeof($clbs);
if(sizeof($clbs) > $count)
$ic = $count;
if(sizeof($clbs) > $count) $ic = $count;
if (!empty($clbs)) {
if(!empty($clbs)) {
$clbs = array_slice($clbs, $offset * $count);
for ($i=0; $i < $ic; $i++) {
for($i=0; $i < $ic; $i++) {
$usr = $clbs[$i];
if(is_null($usr))
{
if(is_null($usr)) {
$rClubs[$i] = (object)[
"id" => $clbs[$i],
"name" => "DELETED",
"deactivated" => "deleted"
];
}else if($clbs[$i] == NULL){
} else if($clbs[$i] == NULL) {
}else{
$rClubs[$i] = (object)[
} else {
$rClubs[$i] = (object) [
"id" => $usr->getId(),
"name" => $usr->getName(),
"screen_name" => $usr->getShortCode(),
@ -63,35 +57,35 @@ final class Groups extends VKAPIRequestHandler
$flds = explode(',', $fields);
foreach($flds as $field) {
switch ($field) {
case 'verified':
switch($field) {
case "verified":
$rClubs[$i]->verified = intval($usr->isVerified());
break;
case 'has_photo':
case "has_photo":
$rClubs[$i]->has_photo = is_null($usr->getAvatarPhoto()) ? 0 : 1;
break;
case 'photo_max_orig':
case "photo_max_orig":
$rClubs[$i]->photo_max_orig = $usr->getAvatarURL();
break;
case 'photo_max':
case "photo_max":
$rClubs[$i]->photo_max = $usr->getAvatarURL("original"); // ORIGINAL ANDREI CHINITEL 🥵🥵🥵🥵
break;
case 'photo_50':
case "photo_50":
$rClubs[$i]->photo_50 = $usr->getAvatarURL();
break;
case 'photo_100':
case "photo_100":
$rClubs[$i]->photo_100 = $usr->getAvatarURL("tiny");
break;
case 'photo_200':
case "photo_200":
$rClubs[$i]->photo_200 = $usr->getAvatarURL("normal");
break;
case 'photo_200_orig':
case "photo_200_orig":
$rClubs[$i]->photo_200_orig = $usr->getAvatarURL("normal");
break;
case 'photo_400_orig':
case "photo_400_orig":
$rClubs[$i]->photo_400_orig = $usr->getAvatarURL("normal");
break;
case 'members_count':
case "members_count":
$rClubs[$i]->members_count = $usr->getFollowersCount();
break;
}
@ -112,10 +106,10 @@ final class Groups extends VKAPIRequestHandler
{
$clubs = new ClubsRepo;
if ($group_ids == NULL && $group_id != NULL)
if($group_ids == NULL && $group_id != NULL)
$group_ids = $group_id;
if ($group_ids == NULL && $group_id == NULL)
if($group_ids == NULL && $group_id == NULL)
$this->fail(100, "One of the parameters specified was missing or invalid: group_ids is undefined");
$clbs = explode(',', $group_ids);
@ -123,7 +117,7 @@ final class Groups extends VKAPIRequestHandler
$ic = sizeof($clbs);
for ($i=0; $i < $ic; $i++) {
for($i=0; $i < $ic; $i++) {
if($i > 500)
break;
@ -131,8 +125,7 @@ final class Groups extends VKAPIRequestHandler
$this->fail(100, "ты ошибся чутка, у айди группы убери минус");
$clb = $clubs->get((int) $clbs[$i]);
if(is_null($clb))
{
if(is_null($clb)) {
$response[$i] = (object)[
"id" => intval($clbs[$i]),
"name" => "DELETED",
@ -140,9 +133,9 @@ final class Groups extends VKAPIRequestHandler
"type" => "group",
"description" => "This group was deleted or it doesn't exist"
];
}else if($clbs[$i] == NULL){
} else if($clbs[$i] == NULL) {
}else{
} else {
$response[$i] = (object)[
"id" => $clb->getId(),
"name" => $clb->getName(),
@ -155,40 +148,41 @@ final class Groups extends VKAPIRequestHandler
$flds = explode(',', $fields);
foreach($flds as $field) {
switch ($field) {
case 'verified':
switch($field) {
case "verified":
$response[$i]->verified = intval($clb->isVerified());
break;
case 'has_photo':
case "has_photo":
$response[$i]->has_photo = is_null($clb->getAvatarPhoto()) ? 0 : 1;
break;
case 'photo_max_orig':
case "photo_max_orig":
$response[$i]->photo_max_orig = $clb->getAvatarURL();
break;
case 'photo_max':
case "photo_max":
$response[$i]->photo_max = $clb->getAvatarURL();
break;
case 'members_count':
case "members_count":
$response[$i]->members_count = $clb->getFollowersCount();
break;
case 'site':
case "site":
$response[$i]->site = $clb->getWebsite();
break;
case 'description':
case "description":
$response[$i]->desctiption = $clb->getDescription();
break;
case 'contacts':
case "contacts":
$contacts;
$contactTmp = $clb->getManagers(1, true);
foreach($contactTmp as $contact) {
foreach($contactTmp as $contact)
$contacts[] = array(
'user_id' => $contact->getUser()->getId(),
'desc' => $contact->getComment()
"user_id" => $contact->getUser()->getId(),
"desc" => $contact->getComment()
);
}
$response[$i]->contacts = $contacts;
break;
case 'can_post':
case "can_post":
if(!is_null($this->getUser()))
if($clb->canBeModifiedBy($this->getUser()))
$response[$i]->can_post = true;

View file

@ -9,19 +9,19 @@ final class Likes extends VKAPIRequestHandler
{
$this->requireUser();
switch ($type) {
case 'post':
switch($type) {
case "post":
$post = (new PostsRepo)->getPostById($owner_id, $item_id);
if (is_null($post)) $this->fail(100, 'One of the parameters specified was missing or invalid: object not found');
if(is_null($post))
$this->fail(100, "One of the parameters specified was missing or invalid: object not found");
$post->setLike(true, $this->getUser());
return (object)[
return (object) [
"likes" => $post->getLikesCount()
];
break;
default:
$this->fail(100, 'One of the parameters specified was missing or invalid: incorrect type');
break;
$this->fail(100, "One of the parameters specified was missing or invalid: incorrect type");
}
}
@ -29,19 +29,18 @@ final class Likes extends VKAPIRequestHandler
{
$this->requireUser();
switch ($type) {
case 'post':
switch($type) {
case "post":
$post = (new PostsRepo)->getPostById($owner_id, $item_id);
if (is_null($post)) $this->fail(100, 'One of the parameters specified was missing or invalid: object not found');
if (is_null($post))
$this->fail(100, "One of the parameters specified was missing or invalid: object not found");
$post->setLike(false, $this->getUser());
return (object)[
return (object) [
"likes" => $post->getLikesCount()
];
break;
default:
$this->fail(100, 'One of the parameters specified was missing or invalid: incorrect type');
break;
$this->fail(100, "One of the parameters specified was missing or invalid: incorrect type");
}
}
@ -49,26 +48,26 @@ final class Likes extends VKAPIRequestHandler
{
$this->requireUser();
switch ($type) {
case 'post':
switch($type) {
case "post":
$user = (new UsersRepo)->get($user_id);
if (is_null($user)) return (object)[
if (is_null($user))
return (object) [
"liked" => 0,
"copied" => 0,
"sex" => 0
];
$post = (new PostsRepo)->getPostById($owner_id, $item_id);
if (is_null($post)) $this->fail(100, 'One of the parameters specified was missing or invalid: object not found');
if (is_null($post))
$this->fail(100, "One of the parameters specified was missing or invalid: object not found");
return (object)[
return (object) [
"liked" => (int) $post->hasLikeFrom($user),
"copied" => 0 # TODO: handle this
];
break;
default:
$this->fail(100, 'One of the parameters specified was missing or invalid: incorrect type');
break;
$this->fail(100, "One of the parameters specified was missing or invalid: incorrect type");
}
}
}

View file

@ -123,9 +123,8 @@ final class Messages extends VKAPIRequestHandler
$items = [];
foreach($ids as $id) {
$message = $msgs->get((int) $id);
if(!$message || $message->getSender()->getId() !== $this->getUser()->getId() && $message->getRecipient()->getId() !== $this->getUser()->getId()) {
if(!$message || $message->getSender()->getId() !== $this->getUser()->getId() && $message->getRecipient()->getId() !== $this->getUser()->getId())
$items[$id] = 0;
}
$message->delete();
$items[$id] = 1;

View file

@ -1,8 +1,5 @@
<?php declare(strict_types=1);
namespace openvk\VKAPI\Handlers;
use openvk\Web\Models\Entities\User;
use openvk\Web\Models\Entities\Post;
use openvk\Web\Models\Entities\Postable;
use Chandler\Database\DatabaseConnection;
use openvk\Web\Models\Repositories\Posts as PostsRepo;
use openvk\VKAPI\Handlers\Wall;
@ -13,7 +10,8 @@ final class Newsfeed extends VKAPIRequestHandler
{
$this->requireUser();
if($offset != 0) $start_from = $offset;
if($offset != 0)
$start_from = $offset;
$id = $this->getUser()->getId();
$subs = DatabaseConnection::i()

View file

@ -2,7 +2,6 @@
namespace openvk\VKAPI\Handlers;
use openvk\Web\Models\Repositories\{Users as UsersRepo, Clubs as ClubsRepo, Posts as PostsRepo};
final class Ovk extends VKAPIRequestHandler
{
function version(): string

View file

@ -7,36 +7,34 @@ final class Users extends VKAPIRequestHandler
{
function get(string $user_ids = "0", string $fields = "", int $offset = 0, int $count = 100, User $authuser = null /* костыль(( */): array
{
# $this->requireUser();
if($authuser == NULL) $authuser = $this->getUser();
$users = new UsersRepo;
if($user_ids == "0")
$user_ids = (string) $authuser->getId();
$usrs = explode(',', $user_ids);
$usrs = explode(',', $user_ids);','
$response;
$ic = sizeof($usrs);
if(sizeof($usrs) > $count) $ic = $count;
if(sizeof($usrs) > $count)
$ic = $count;
$usrs = array_slice($usrs, $offset * $count);
for ($i=0; $i < $ic; $i++) {
for($i=0; $i < $ic; $i++) {
$usr = $users->get((int) $usrs[$i]);
if(is_null($usr))
{
if(is_null($usr)) {
$response[$i] = (object)[
"id" => $usrs[$i],
"first_name" => "DELETED",
"last_name" => "",
"deactivated" => "deleted"
];
}else if($usrs[$i] == NULL){
} else if($usrs[$i] == NULL) {
}else{
} else {
$response[$i] = (object)[
"id" => $usr->getId(),
"first_name" => $usr->getFirstName(),
@ -48,51 +46,52 @@ final class Users extends VKAPIRequestHandler
$flds = explode(',', $fields);
foreach($flds as $field) {
switch ($field) {
case 'verified':
switch($field) {
case "verified":
$response[$i]->verified = intval($usr->isVerified());
break;
case 'sex':
case "sex":
$response[$i]->sex = $usr->isFemale() ? 1 : 2;
break;
case 'has_photo':
case "has_photo":
$response[$i]->has_photo = is_null($usr->getAvatarPhoto()) ? 0 : 1;
break;
case 'photo_max_orig':
case "photo_max_orig":
$response[$i]->photo_max_orig = $usr->getAvatarURL();
break;
case 'photo_max':
case "photo_max":
$response[$i]->photo_max = $usr->getAvatarURL("original");
break;
case 'photo_50':
case "photo_50":
$response[$i]->photo_50 = $usr->getAvatarURL();
break;
case 'photo_100':
case "photo_100":
$response[$i]->photo_100 = $usr->getAvatarURL("tiny");
break;
case 'photo_200':
case "photo_200":
$response[$i]->photo_200 = $usr->getAvatarURL("normal");
break;
case 'photo_200_orig': # вообще не ебу к чему эта строка ну пусть будет кек
case "photo_200_orig": # вообще не ебу к чему эта строка ну пусть будет кек
$response[$i]->photo_200_orig = $usr->getAvatarURL("normal");
break;
case 'photo_400_orig':
case "photo_400_orig":
$response[$i]->photo_400_orig = $usr->getAvatarURL("normal");
break;
# Она хочет быть выебанной видя матан
# Покайфу когда ты Виет а вокруг лишь дискриминант
case 'status':
case "status":
if($usr->getStatus() != NULL)
$response[$i]->status = $usr->getStatus();
break;
case 'screen_name':
case "screen_name":
if($usr->getShortCode() != NULL)
$response[$i]->screen_name = $usr->getShortCode();
break;
case 'friend_status':
case "friend_status":
switch($usr->getSubscriptionStatus($authuser)) {
case 3:
# NOTICE falling through
case 0:
$response[$i]->friend_status = $usr->getSubscriptionStatus($authuser);
break;
@ -104,50 +103,43 @@ final class Users extends VKAPIRequestHandler
break;
}
break;
case 'last_seen':
if ($usr->onlineStatus() == 0) {
case "last_seen":
if ($usr->onlineStatus() == 0)
$response[$i]->last_seen = (object) [
"platform" => 1,
"time" => $usr->getOnline()->timestamp()
];
}
case 'music':
case "music":
$response[$i]->music = $usr->getFavoriteMusic();
break;
case 'movies':
case "movies":
$response[$i]->movies = $usr->getFavoriteFilms();
break;
case 'tv':
case "tv":
$response[$i]->tv = $usr->getFavoriteShows();
break;
case 'books':
case "books":
$response[$i]->books = $usr->getFavoriteBooks();
break;
case 'city':
case "city":
$response[$i]->city = $usr->getCity();
break;
case 'interests':
case "interests":
$response[$i]->interests = $usr->getInterests();
break;
}
}
if($usr->getOnline()->timestamp() + 300 > time()) {
if($usr->getOnline()->timestamp() + 300 > time())
$response[$i]->online = 1;
}else{
else
$response[$i]->online = 0;
}
}
}
return $response;
}
/* private function getUsersById(string $user_ids, string $fields = "", int $offset = 0, int $count = PHP_INT_MAX){
} */
function getFollowers(int $user_id, string $fields = "", int $offset = 0, int $count = 100): object
{
$offset++;
@ -157,15 +149,13 @@ final class Users extends VKAPIRequestHandler
$this->requireUser();
foreach ($users->get($user_id)->getFollowers($offset, $count) as $follower) {
foreach($users->get($user_id)->getFollowers($offset, $count) as $follower)
$followers[] = $follower->getId();
}
$response = $followers;
if (!is_null($fields)) {
if(!is_null($fields))
$response = $this->get(implode(',', $followers), $fields, 0, $count);
}
return (object) [
"count" => $users->get($user_id)->getFollowersCount(),
@ -173,18 +163,17 @@ final class Users extends VKAPIRequestHandler
];
}
function search(string $q, string $fields = '', int $offset = 0, int $count = 100)
function search(string $q, string $fields = "", int $offset = 0, int $count = 100)
{
$users = new UsersRepo;
$array = [];
$find = $users->find($q);
foreach ($find as $user) {
foreach ($find as $user)
$array[] = $user->getId();
}
return (object)[
return (object) [
"count" => $find->size(),
"items" => $this->get(implode(',', $array), $fields, $offset, $count)
];

View file

@ -6,7 +6,6 @@ use openvk\Web\Models\Repositories\Users as UsersRepo;
use openvk\Web\Models\Entities\Club;
use openvk\Web\Models\Repositories\Clubs as ClubsRepo;
use openvk\Web\Models\Entities\Post;
use openvk\Web\Models\Entities\Postable;
use openvk\Web\Models\Repositories\Posts as PostsRepo;
final class Wall extends VKAPIRequestHandler
@ -20,7 +19,7 @@ final class Wall extends VKAPIRequestHandler
$groups = [];
$count = $posts->getPostCountOnUserWall((int) $owner_id);
foreach ($posts->getPostsFromUsersWall((int)$owner_id, 1, $count, $offset) as $post) {
foreach($posts->getPostsFromUsersWall((int)$owner_id, 1, $count, $offset) as $post) {
$from_id = get_class($post->getOwner()) == "openvk\Web\Models\Entities\Club" ? $post->getOwner()->getId() * (-1) : $post->getOwner()->getId();
$attachments = [];
@ -83,15 +82,14 @@ final class Wall extends VKAPIRequestHandler
$attachments = NULL; # free attachments so it will not clone everythingg
}
if($extended == 1)
{
if($extended == 1) {
$profiles = array_unique($profiles);
$groups = array_unique($groups);
$profilesFormatted = [];
$groupsFormatted = [];
foreach ($profiles as $prof) {
foreach($profiles as $prof) {
$user = (new UsersRepo)->get($prof);
$profilesFormatted[] = (object)[
"first_name" => $user->getFirstName(),
@ -121,15 +119,14 @@ final class Wall extends VKAPIRequestHandler
];
}
return (object)[
return (object) [
"count" => $count,
"items" => (array)$items,
"profiles" => (array)$profilesFormatted,
"groups" => (array)$groupsFormatted
];
}
else
return (object)[
} else
return (object) [
"count" => $count,
"items" => (array)$items
];
@ -137,26 +134,23 @@ final class Wall extends VKAPIRequestHandler
function getById(string $posts, int $extended = 0, string $fields = "", User $user = NULL)
{
if($user == NULL) $user = $this->getUser(); # костыли костыли крылышки
if($user == NULL)
$user = $this->getUser(); # костыли костыли крылышки
$items = [];
$profiles = [];
$groups = [];
# $count = $posts->getPostCountOnUserWall((int) $owner_id);
$psts = explode(",", $posts);
$psts = explode(',', $posts);
foreach($psts as $pst)
{
foreach($psts as $pst) {
$id = explode("_", $pst);
$post = (new PostsRepo)->getPostById(intval($id[0]), intval($id[1]));
if($post) {
$from_id = get_class($post->getOwner()) == "openvk\Web\Models\Entities\Club" ? $post->getOwner()->getId() * (-1) : $post->getOwner()->getId();
$attachments;
foreach($post->getChildren() as $attachment)
{
if($attachment instanceof \openvk\Web\Models\Entities\Photo)
{
foreach($post->getChildren() as $attachment) {
if($attachment instanceof \openvk\Web\Models\Entities\Photo) {
$attachments[] = [
"type" => "photo",
"photo" => [
@ -248,15 +242,14 @@ final class Wall extends VKAPIRequestHandler
}
}
if($extended == 1)
{
if($extended == 1) {
$profiles = array_unique($profiles);
$groups = array_unique($groups);
$profilesFormatted = [];
$groupsFormatted = [];
foreach ($profiles as $prof) {
foreach($profiles as $prof) {
$user = (new UsersRepo)->get($prof);
$profilesFormatted[] = (object)[
"first_name" => $user->getFirstName(),
@ -286,14 +279,13 @@ final class Wall extends VKAPIRequestHandler
];
}
return (object)[
return (object) [
"items" => (array)$items,
"profiles" => (array)$profilesFormatted,
"groups" => (array)$groupsFormatted
];
}
else
return (object)[
} else
return (object) [
"items" => (array)$items
];
}