Skip to content

Commit

Permalink
Improve API (#975)
Browse files Browse the repository at this point in the history
* Add API for notifications

* Add editing methods

* Add apis for likes and polls

* Add attaching polls

* can_edit

* Fix getting voters in anonymous posts and use c...

...anBeEditedBy instead of checking user's id
  • Loading branch information
mrilyew authored Nov 14, 2023
1 parent 6660cb8 commit 71c5902
Show file tree
Hide file tree
Showing 20 changed files with 645 additions and 22 deletions.
6 changes: 3 additions & 3 deletions VKAPI/Handlers/Groups.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@

final class Groups extends VKAPIRequestHandler
{
function get(int $user_id = 0, string $fields = "", int $offset = 0, int $count = 6, bool $online = false): object
function get(int $user_id = 0, string $fields = "", int $offset = 0, int $count = 6, bool $online = false, string $filter = "groups"): object
{
$this->requireUser();

if($user_id == 0) {
foreach($this->getUser()->getClubs($offset, false, $count, true) as $club)
foreach($this->getUser()->getClubs($offset, $filter == "admin", $count, true) as $club)
$clbs[] = $club;
$clbsCount = $this->getUser()->getClubCount();
} else {
Expand All @@ -21,7 +21,7 @@ function get(int $user_id = 0, string $fields = "", int $offset = 0, int $count
if(is_null($user))
$this->fail(15, "Access denied");

foreach($user->getClubs($offset, false, $count, true) as $club)
foreach($user->getClubs($offset, $filter == "admin", $count, true) as $club)
$clbs[] = $club;

$clbsCount = $user->getClubCount();
Expand Down
49 changes: 48 additions & 1 deletion VKAPI/Handlers/Likes.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php declare(strict_types=1);
namespace openvk\VKAPI\Handlers;
use openvk\Web\Models\Repositories\Users as UsersRepo;
use openvk\Web\Models\Repositories\Posts as PostsRepo;
use openvk\Web\Models\Repositories\{Posts as PostsRepo, Comments as CommentsRepo, Photos as PhotosRepo, Videos as VideosRepo};

final class Likes extends VKAPIRequestHandler
{
Expand Down Expand Up @@ -68,4 +68,51 @@ function isLiked(int $user_id, string $type, int $owner_id, int $item_id): objec
$this->fail(100, "One of the parameters specified was missing or invalid: incorrect type");
}
}

function getList(string $type, int $owner_id, int $item_id, bool $extended = false, int $offset = 0, int $count = 10, bool $skip_own = false)
{
$this->requireUser();

$object = NULL;

switch($type) {
case "post":
$object = (new PostsRepo)->getPostById($owner_id, $item_id);
break;
case "comment":
$object = (new CommentsRepo)->get($item_id);
break;
case "photo":
$object = (new PhotosRepo)->getByOwnerAndVID($owner_id, $item_id);
break;
case "video":
$object = (new VideosRepo)->getByOwnerAndVID($owner_id, $item_id);
break;
default:
$this->fail(58, "Invalid type");
break;
}

if(!$object || $object->isDeleted())
$this->fail(56, "Invalid postable");

$res = (object)[
"count" => $object->getLikesCount(),
"items" => []
];

$likers = array_slice(iterator_to_array($object->getLikers(1, $offset + $count)), $offset);

foreach($likers as $liker) {
if($skip_own && $liker->getId() == $this->getUser()->getId())
continue;

if(!$extended)
$res->items[] = $liker->getId();
else
$res->items[] = $liker->toVkApiStruct();
}

return $res;
}
}
66 changes: 64 additions & 2 deletions VKAPI/Handlers/Messages.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ function getById(string $message_ids, int $preview_length = 0, int $extended = 0
];
}

function send(int $user_id = -1, int $peer_id = -1, string $domain = "", int $chat_id = -1, string $user_ids = "", string $message = "", int $sticker_id = -1, int $forGodSakePleaseDoNotReportAboutMyOnlineActivity = 0)
function send(int $user_id = -1, int $peer_id = -1, string $domain = "", int $chat_id = -1, string $user_ids = "", string $message = "", int $sticker_id = -1, int $forGodSakePleaseDoNotReportAboutMyOnlineActivity = 0,
string $attachment = "") # интересно почему не attachments
{
$this->requireUser();
$this->willExecuteWriteAction();
Expand All @@ -79,7 +80,8 @@ function send(int $user_id = -1, int $peer_id = -1, string $domain = "", int $ch
$this->fail(946, "Chats are not implemented");
else if($sticker_id !== -1)
$this->fail(-151, "Stickers are not implemented");
else if(empty($message))

if(empty($message) && empty($attachment))
$this->fail(100, "Message text is empty or invalid");

# lol recursion
Expand Down Expand Up @@ -117,6 +119,21 @@ function send(int $user_id = -1, int $peer_id = -1, string $domain = "", int $ch
if(!$msg)
$this->fail(950, "Internal error");
else
if(!empty($attachment)) {
$attachs = parseAttachments($attachment);

# Работают только фотки, остальное просто не будет отображаться.
if(sizeof($attachs) >= 10)
$this->fail(15, "Too many attachments");

foreach($attachs as $attach) {
if($attach && !$attach->isDeleted() && $attach->getOwner()->getId() == $this->getUser()->getId())
$msg->attach($attach);
else
$this->fail(52, "One of the attachments is invalid");
}
}

return $msg->getId();
}

Expand Down Expand Up @@ -393,4 +410,49 @@ function getLongPollServer(int $need_pts = 1, int $lp_version = 3, ?int $group_i

return $res;
}

function edit(int $message_id, string $message = "", string $attachment = "", int $peer_id = 0)
{
$this->requireUser();
$this->willExecuteWriteAction();

$msg = (new MSGRepo)->get($message_id);

if(empty($message) && empty($attachment))
$this->fail(100, "Required parameter 'message' missing.");

if(!$msg || $msg->isDeleted())
$this->fail(102, "Invalid message");

if($msg->getSender()->getId() != $this->getUser()->getId())
$this->fail(15, "Access to message denied");

if(!empty($message))
$msg->setContent($message);

$msg->setEdited(time());
$msg->save(true);

if(!empty($attachment)) {
$attachs = parseAttachments($attachment);
$newAttachmentsCount = sizeof($attachs);

$postsAttachments = iterator_to_array($msg->getChildren());

if(sizeof($postsAttachments) >= 10)
$this->fail(15, "Message have too many attachments");

if(($newAttachmentsCount + sizeof($postsAttachments)) > 10)
$this->fail(158, "Message will have too many attachments");

foreach($attachs as $attach) {
if($attach && !$attach->isDeleted() && $attach->getOwner()->getId() == $this->getUser()->getId())
$msg->attach($attach);
else
$this->fail(52, "One of the attachments is invalid");
}
}

return 1;
}
}
6 changes: 2 additions & 4 deletions VKAPI/Handlers/Notes.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,19 +161,17 @@ function edit(string $note_id, string $title = "", string $text = "", int $priva

function editComment(int $comment_id, string $message, int $owner_id = NULL)
{
/*
$this->requireUser();
$this->willExecuteWriteAction();

$comment = (new CommentsRepo)->get($comment_id);

if($comment->getOwner() != $this->getUser()->getId())
if($comment->getOwner()->getId() != $this->getUser()->getId())
$this->fail(15, "Access to comment denied");

$comment->setContent($message);
$comment->setEdited(time());
$comment->save();
*/
$comment->save(true);

return 1;
}
Expand Down
83 changes: 83 additions & 0 deletions VKAPI/Handlers/Notifications.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php declare(strict_types=1);
namespace openvk\VKAPI\Handlers;
use openvk\Web\Models\Entities\Club;
use openvk\Web\Models\Repositories\{Notifications as Notifs, Clubs, Users};

final class Notifications extends VKAPIRequestHandler
{
function get(int $count = 10,
string $from = "",
int $offset = 0,
string $start_from = "",
string $filters = "",
int $start_time = 0,
int $end_time = 0,
int $archived = 0)
{
$this->requireUser();

$res = (object)[
"items" => [],
"profiles" => [],
"groups" => [],
"last_viewed" => $this->getUser()->getNotificationOffset()
];

if($count > 100)
$this->fail(125, "Count is too big");

if(!eventdb())
$this->fail(1289, "EventDB is disabled on this instance");

$notifs = array_slice(iterator_to_array((new Notifs)->getNotificationsByUser($this->getUser(), $this->getUser()->getNotificationOffset(), (bool)$archived, 1, $offset + $count)), $offset);
$tmpProfiles = [];
foreach($notifs as $notif) {
$sxModel = $notif->getModel(1);

if(!method_exists($sxModel, "getAvatarUrl"))
$sxModel = $notif->getModel(0);


$tmpProfiles[] = $sxModel instanceof Club ? $sxModel->getId() * -1 : $sxModel->getId();
$res->items[] = $notif->toVkApiStruct();
}

foreach(array_unique($tmpProfiles) as $id) {
if($id > 0) {
$sxModel = (new Users)->get($id);
$result = (object)[
"uid" => $sxModel->getId(),
"first_name" => $sxModel->getFirstName(),
"last_name" => $sxModel->getLastName(),
"photo" => $sxModel->getAvatarUrl(),
"photo_medium_rec" => $sxModel->getAvatarUrl("tiny"),
"screen_name" => $sxModel->getShortCode()
];

$res->profiles[] = $result;
} else {
$sxModel = (new Clubs)->get(abs($id));
$result = $sxModel->toVkApiStruct($this->getUser());

$res->groups[] = $result;
}
}

return $res;
}

function markAsViewed()
{
$this->requireUser();
$this->willExecuteWriteAction();

try {
$this->getUser()->updateNotificationOffset();
$this->getUser()->save();
} catch(\Throwable $e) {
return 0;
}

return 1;
}
}
63 changes: 63 additions & 0 deletions VKAPI/Handlers/Polls.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,67 @@ function deleteVote(int $poll_id)
$this->fail(8, "how.to. ook.bacon.in.microwova.");
}
}

function getVoters(int $poll_id, int $answer_ids, int $offset = 0, int $count = 6)
{
$this->requireUser();

$poll = (new PollsRepo)->get($poll_id);

if(!$poll)
$this->fail(251, "Invalid poll");

if($poll->isAnonymous())
$this->fail(251, "Access denied: poll is anonymous.");

$voters = array_slice($poll->getVoters($answer_ids, 1, $offset + $count), $offset);
$res = (object)[
"answer_id" => $answer_ids,
"users" => []
];

foreach($voters as $voter)
$res->users[] = $voter->toVkApiStruct();

return $res;
}

function create(string $question, string $add_answers, bool $disable_unvote = false, bool $is_anonymous = false, bool $is_multiple = false, int $end_date = 0)
{
$this->requireUser();
$this->willExecuteWriteAction();

$options = json_decode($add_answers);

if(!$options || empty($options))
$this->fail(62, "Invalid options");

if(sizeof($options) > ovkGetQuirk("polls.max-opts"))
$this->fail(51, "Too many options");

$poll = new Poll;
$poll->setOwner($this->getUser());
$poll->setTitle($question);
$poll->setMultipleChoice($is_multiple);
$poll->setAnonymity($is_anonymous);
$poll->setRevotability(!$disable_unvote);
$poll->setOptions($options);

if($end_date > time()) {
if($end_date > time() + (DAY * 365))
$this->fail(89, "End date is too big");

$poll->setEndDate($end_date);
}

$poll->save();

return $this->getById($poll->getId());
}

function edit()
{
#todo
return 1;
}
}
25 changes: 24 additions & 1 deletion VKAPI/Handlers/Users.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?php declare(strict_types=1);
namespace openvk\VKAPI\Handlers;
use openvk\Web\Models\Entities\User;
use openvk\Web\Models\Entities\{User, Report};
use openvk\Web\Models\Repositories\Users as UsersRepo;
use openvk\Web\Models\Repositories\Reports;

final class Users extends VKAPIRequestHandler
{
Expand Down Expand Up @@ -307,4 +308,26 @@ function search(string $q,
"items" => $this->get(implode(',', $array), $nfilds, $offset, $count)
];
}

function report(int $user_id, string $type = "spam", string $comment = "")
{
$this->requireUser();
$this->willExecuteWriteAction();

if($user_id == $this->getUser()->getId())
$this->fail(12, "Can't report yourself.");

if(sizeof(iterator_to_array((new Reports)->getDuplicates("user", $user_id, NULL, $this->getUser()->getId()))) > 0)
return 1;

$report = new Report;
$report->setUser_id($this->getUser()->getId());
$report->setTarget_id($user_id);
$report->setType("user");
$report->setReason($comment);
$report->setCreated(time());
$report->save();

return 1;
}
}
2 changes: 1 addition & 1 deletion VKAPI/Handlers/VKAPIRequestHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ protected function getUser(): ?User

protected function getPlatform(): ?string
{
return $this->platform;
return $this->platform ?? "";
}

protected function userAuthorized(): bool
Expand Down
Loading

0 comments on commit 71c5902

Please sign in to comment.