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); <?php declare(strict_types=1);
namespace openvk\VKAPI\Handlers; namespace openvk\VKAPI\Handlers;
use openvk\Web\Models\Entities\User;
use openvk\Web\Models\Repositories\Users as UsersRepo; use openvk\Web\Models\Repositories\Users as UsersRepo;
final class Friends extends VKAPIRequestHandler final class Friends extends VKAPIRequestHandler
@ -15,7 +14,7 @@ final class Friends extends VKAPIRequestHandler
$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++;
} }
@ -24,7 +23,7 @@ final class Friends extends VKAPIRequestHandler
$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); # FIXME $response = $usersApi->get(implode(',', $friends), $fields, 0, $count); # FIXME
} }
@ -70,33 +69,28 @@ final class Friends extends VKAPIRequestHandler
$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)){ if(is_null($user)) {
$this->fail(177, "Cannot add this user to friends as user not found"); $this->fail(177, "Cannot add this user to friends as user not found");
} else if($user->getId() == $this->getUser()->getId()) { } else if($user->getId() == $this->getUser()->getId()) {
$this->fail(174, "Cannot add user himself as friend"); $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;
case 1: case 1:
$user->toggleSubscription($this->getUser()); $user->toggleSubscription($this->getUser());
return 2; return 2;
break;
case 3: case 3:
return 2; return 2;
break;
default: default:
return 1; return 1;
break;
} }
} }
@ -108,15 +102,13 @@ final class Friends extends VKAPIRequestHandler
$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;
default: default:
fail(15, "Access denied: No friend or friend request found."); fail(15, "Access denied: No friend or friend request found.");
break;
} }
} }
@ -130,25 +122,9 @@ final class Friends extends VKAPIRequestHandler
$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;
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)[ $response[] = (object)[
"friend_status" => $friend->getSubscriptionStatus($this->getUser()), "friend_status" => $friend->getSubscriptionStatus($this->getUser()),
"user_id" => $friend->getId() "user_id" => $friend->getId()

View file

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

View file

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

View file

@ -123,9 +123,8 @@ final class Messages extends VKAPIRequestHandler
$items = []; $items = [];
foreach($ids as $id) { foreach($ids as $id) {
$message = $msgs->get((int) $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; $items[$id] = 0;
}
$message->delete(); $message->delete();
$items[$id] = 1; $items[$id] = 1;

View file

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

View file

@ -2,7 +2,6 @@
namespace openvk\VKAPI\Handlers; namespace openvk\VKAPI\Handlers;
use openvk\Web\Models\Repositories\{Users as UsersRepo, Clubs as ClubsRepo, Posts as PostsRepo}; use openvk\Web\Models\Repositories\{Users as UsersRepo, Clubs as ClubsRepo, Posts as PostsRepo};
final class Ovk extends VKAPIRequestHandler final class Ovk extends VKAPIRequestHandler
{ {
function version(): string 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 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(); if($authuser == NULL) $authuser = $this->getUser();
$users = new UsersRepo; $users = new UsersRepo;
if($user_ids == "0") if($user_ids == "0")
$user_ids = (string) $authuser->getId(); $user_ids = (string) $authuser->getId();
$usrs = explode(',', $user_ids); $usrs = explode(',', $user_ids);','
$response; $response;
$ic = sizeof($usrs); $ic = sizeof($usrs);
if(sizeof($usrs) > $count) $ic = $count; if(sizeof($usrs) > $count)
$ic = $count;
$usrs = array_slice($usrs, $offset * $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]); $usr = $users->get((int) $usrs[$i]);
if(is_null($usr)) if(is_null($usr)) {
{
$response[$i] = (object)[ $response[$i] = (object)[
"id" => $usrs[$i], "id" => $usrs[$i],
"first_name" => "DELETED", "first_name" => "DELETED",
"last_name" => "", "last_name" => "",
"deactivated" => "deleted" "deactivated" => "deleted"
]; ];
}else if($usrs[$i] == NULL){ } else if($usrs[$i] == NULL) {
}else{ } else {
$response[$i] = (object)[ $response[$i] = (object)[
"id" => $usr->getId(), "id" => $usr->getId(),
"first_name" => $usr->getFirstName(), "first_name" => $usr->getFirstName(),
@ -48,51 +46,52 @@ final class Users extends VKAPIRequestHandler
$flds = explode(',', $fields); $flds = explode(',', $fields);
foreach($flds as $field) { foreach($flds as $field) {
switch ($field) { switch($field) {
case 'verified': case "verified":
$response[$i]->verified = intval($usr->isVerified()); $response[$i]->verified = intval($usr->isVerified());
break; break;
case 'sex': case "sex":
$response[$i]->sex = $usr->isFemale() ? 1 : 2; $response[$i]->sex = $usr->isFemale() ? 1 : 2;
break; break;
case 'has_photo': case "has_photo":
$response[$i]->has_photo = is_null($usr->getAvatarPhoto()) ? 0 : 1; $response[$i]->has_photo = is_null($usr->getAvatarPhoto()) ? 0 : 1;
break; break;
case 'photo_max_orig': case "photo_max_orig":
$response[$i]->photo_max_orig = $usr->getAvatarURL(); $response[$i]->photo_max_orig = $usr->getAvatarURL();
break; break;
case 'photo_max': case "photo_max":
$response[$i]->photo_max = $usr->getAvatarURL("original"); $response[$i]->photo_max = $usr->getAvatarURL("original");
break; break;
case 'photo_50': case "photo_50":
$response[$i]->photo_50 = $usr->getAvatarURL(); $response[$i]->photo_50 = $usr->getAvatarURL();
break; break;
case 'photo_100': case "photo_100":
$response[$i]->photo_100 = $usr->getAvatarURL("tiny"); $response[$i]->photo_100 = $usr->getAvatarURL("tiny");
break; break;
case 'photo_200': case "photo_200":
$response[$i]->photo_200 = $usr->getAvatarURL("normal"); $response[$i]->photo_200 = $usr->getAvatarURL("normal");
break; break;
case 'photo_200_orig': # вообще не ебу к чему эта строка ну пусть будет кек case "photo_200_orig": # вообще не ебу к чему эта строка ну пусть будет кек
$response[$i]->photo_200_orig = $usr->getAvatarURL("normal"); $response[$i]->photo_200_orig = $usr->getAvatarURL("normal");
break; break;
case 'photo_400_orig': case "photo_400_orig":
$response[$i]->photo_400_orig = $usr->getAvatarURL("normal"); $response[$i]->photo_400_orig = $usr->getAvatarURL("normal");
break; break;
# Она хочет быть выебанной видя матан # Она хочет быть выебанной видя матан
# Покайфу когда ты Виет а вокруг лишь дискриминант # Покайфу когда ты Виет а вокруг лишь дискриминант
case 'status': case "status":
if($usr->getStatus() != NULL) if($usr->getStatus() != NULL)
$response[$i]->status = $usr->getStatus(); $response[$i]->status = $usr->getStatus();
break; break;
case 'screen_name': case "screen_name":
if($usr->getShortCode() != NULL) if($usr->getShortCode() != NULL)
$response[$i]->screen_name = $usr->getShortCode(); $response[$i]->screen_name = $usr->getShortCode();
break; break;
case 'friend_status': case "friend_status":
switch($usr->getSubscriptionStatus($authuser)) { switch($usr->getSubscriptionStatus($authuser)) {
case 3: case 3:
# NOTICE falling through
case 0: case 0:
$response[$i]->friend_status = $usr->getSubscriptionStatus($authuser); $response[$i]->friend_status = $usr->getSubscriptionStatus($authuser);
break; break;
@ -104,50 +103,43 @@ final class Users extends VKAPIRequestHandler
break; break;
} }
break; break;
case 'last_seen': case "last_seen":
if ($usr->onlineStatus() == 0) { if ($usr->onlineStatus() == 0)
$response[$i]->last_seen = (object) [ $response[$i]->last_seen = (object) [
"platform" => 1, "platform" => 1,
"time" => $usr->getOnline()->timestamp() "time" => $usr->getOnline()->timestamp()
]; ];
} case "music":
case 'music':
$response[$i]->music = $usr->getFavoriteMusic(); $response[$i]->music = $usr->getFavoriteMusic();
break; break;
case 'movies': case "movies":
$response[$i]->movies = $usr->getFavoriteFilms(); $response[$i]->movies = $usr->getFavoriteFilms();
break; break;
case 'tv': case "tv":
$response[$i]->tv = $usr->getFavoriteShows(); $response[$i]->tv = $usr->getFavoriteShows();
break; break;
case 'books': case "books":
$response[$i]->books = $usr->getFavoriteBooks(); $response[$i]->books = $usr->getFavoriteBooks();
break; break;
case 'city': case "city":
$response[$i]->city = $usr->getCity(); $response[$i]->city = $usr->getCity();
break; break;
case 'interests': case "interests":
$response[$i]->interests = $usr->getInterests(); $response[$i]->interests = $usr->getInterests();
break; break;
} }
} }
if($usr->getOnline()->timestamp() + 300 > time()) { if($usr->getOnline()->timestamp() + 300 > time())
$response[$i]->online = 1; $response[$i]->online = 1;
}else{ else
$response[$i]->online = 0; $response[$i]->online = 0;
} }
}
} }
return $response; 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 function getFollowers(int $user_id, string $fields = "", int $offset = 0, int $count = 100): object
{ {
$offset++; $offset++;
@ -157,15 +149,13 @@ final class Users extends VKAPIRequestHandler
$this->requireUser(); $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(); $followers[] = $follower->getId();
}
$response = $followers; $response = $followers;
if (!is_null($fields)) { if(!is_null($fields))
$response = $this->get(implode(',', $followers), $fields, 0, $count); $response = $this->get(implode(',', $followers), $fields, 0, $count);
}
return (object) [ return (object) [
"count" => $users->get($user_id)->getFollowersCount(), "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; $users = new UsersRepo;
$array = []; $array = [];
$find = $users->find($q); $find = $users->find($q);
foreach ($find as $user) { foreach ($find as $user)
$array[] = $user->getId(); $array[] = $user->getId();
}
return (object)[ return (object) [
"count" => $find->size(), "count" => $find->size(),
"items" => $this->get(implode(',', $array), $fields, $offset, $count) "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\Entities\Club;
use openvk\Web\Models\Repositories\Clubs as ClubsRepo; use openvk\Web\Models\Repositories\Clubs as ClubsRepo;
use openvk\Web\Models\Entities\Post; use openvk\Web\Models\Entities\Post;
use openvk\Web\Models\Entities\Postable;
use openvk\Web\Models\Repositories\Posts as PostsRepo; use openvk\Web\Models\Repositories\Posts as PostsRepo;
final class Wall extends VKAPIRequestHandler final class Wall extends VKAPIRequestHandler
@ -20,7 +19,7 @@ final class Wall extends VKAPIRequestHandler
$groups = []; $groups = [];
$count = $posts->getPostCountOnUserWall((int) $owner_id); $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(); $from_id = get_class($post->getOwner()) == "openvk\Web\Models\Entities\Club" ? $post->getOwner()->getId() * (-1) : $post->getOwner()->getId();
$attachments = []; $attachments = [];
@ -83,15 +82,14 @@ final class Wall extends VKAPIRequestHandler
$attachments = NULL; # free attachments so it will not clone everythingg $attachments = NULL; # free attachments so it will not clone everythingg
} }
if($extended == 1) if($extended == 1) {
{
$profiles = array_unique($profiles); $profiles = array_unique($profiles);
$groups = array_unique($groups); $groups = array_unique($groups);
$profilesFormatted = []; $profilesFormatted = [];
$groupsFormatted = []; $groupsFormatted = [];
foreach ($profiles as $prof) { foreach($profiles as $prof) {
$user = (new UsersRepo)->get($prof); $user = (new UsersRepo)->get($prof);
$profilesFormatted[] = (object)[ $profilesFormatted[] = (object)[
"first_name" => $user->getFirstName(), "first_name" => $user->getFirstName(),
@ -121,15 +119,14 @@ final class Wall extends VKAPIRequestHandler
]; ];
} }
return (object)[ return (object) [
"count" => $count, "count" => $count,
"items" => (array)$items, "items" => (array)$items,
"profiles" => (array)$profilesFormatted, "profiles" => (array)$profilesFormatted,
"groups" => (array)$groupsFormatted "groups" => (array)$groupsFormatted
]; ];
} } else
else return (object) [
return (object)[
"count" => $count, "count" => $count,
"items" => (array)$items "items" => (array)$items
]; ];
@ -137,26 +134,23 @@ final class Wall extends VKAPIRequestHandler
function getById(string $posts, int $extended = 0, string $fields = "", User $user = NULL) 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 = []; $items = [];
$profiles = []; $profiles = [];
$groups = []; $groups = [];
# $count = $posts->getPostCountOnUserWall((int) $owner_id);
$psts = explode(",", $posts); $psts = explode(',', $posts);
foreach($psts as $pst) foreach($psts as $pst) {
{
$id = explode("_", $pst); $id = explode("_", $pst);
$post = (new PostsRepo)->getPostById(intval($id[0]), intval($id[1])); $post = (new PostsRepo)->getPostById(intval($id[0]), intval($id[1]));
if($post) { if($post) {
$from_id = get_class($post->getOwner()) == "openvk\Web\Models\Entities\Club" ? $post->getOwner()->getId() * (-1) : $post->getOwner()->getId(); $from_id = get_class($post->getOwner()) == "openvk\Web\Models\Entities\Club" ? $post->getOwner()->getId() * (-1) : $post->getOwner()->getId();
$attachments; $attachments;
foreach($post->getChildren() as $attachment) foreach($post->getChildren() as $attachment) {
{ if($attachment instanceof \openvk\Web\Models\Entities\Photo) {
if($attachment instanceof \openvk\Web\Models\Entities\Photo)
{
$attachments[] = [ $attachments[] = [
"type" => "photo", "type" => "photo",
"photo" => [ "photo" => [
@ -248,15 +242,14 @@ final class Wall extends VKAPIRequestHandler
} }
} }
if($extended == 1) if($extended == 1) {
{
$profiles = array_unique($profiles); $profiles = array_unique($profiles);
$groups = array_unique($groups); $groups = array_unique($groups);
$profilesFormatted = []; $profilesFormatted = [];
$groupsFormatted = []; $groupsFormatted = [];
foreach ($profiles as $prof) { foreach($profiles as $prof) {
$user = (new UsersRepo)->get($prof); $user = (new UsersRepo)->get($prof);
$profilesFormatted[] = (object)[ $profilesFormatted[] = (object)[
"first_name" => $user->getFirstName(), "first_name" => $user->getFirstName(),
@ -286,14 +279,13 @@ final class Wall extends VKAPIRequestHandler
]; ];
} }
return (object)[ return (object) [
"items" => (array)$items, "items" => (array)$items,
"profiles" => (array)$profilesFormatted, "profiles" => (array)$profilesFormatted,
"groups" => (array)$groupsFormatted "groups" => (array)$groupsFormatted
]; ];
} } else
else return (object) [
return (object)[
"items" => (array)$items "items" => (array)$items
]; ];
} }