This commit is contained in:
lalka2016 2023-06-12 17:56:42 +03:00
parent 686a4d0471
commit 800c6ca995
10 changed files with 164 additions and 67 deletions

View file

@ -22,7 +22,7 @@ final class Board extends VKAPIRequestHandler
$this->fail(403, "Invalid club");
}
if(!$club->canBeModifiedBy($this->getUser())) {
if(!$club->canBeModifiedBy($this->getUser()) && !$club->isEveryoneCanCreateTopics()) {
$this->fail(403, "Access to club denied");
}
@ -111,9 +111,10 @@ final class Board extends VKAPIRequestHandler
return 0;
}
if(!$topic->isClosed()) {
$topic->setClosed(1);
$topic->save();
}
return 1;
}
@ -219,7 +220,7 @@ final class Board extends VKAPIRequestHandler
$topic = (new TopicsRepo)->getTopicById($group_id, $topic_id);
if(!$topic || !$topic->getClub() || !$topic->getClub()->canBeModifiedBy($this->getUser())) {
if(!$topic || !$topic->getClub() || $topic->isDeleted() || !$topic->getClub()->canBeModifiedBy($this->getUser())) {
return 0;
}
@ -253,7 +254,7 @@ final class Board extends VKAPIRequestHandler
$topic = (new TopicsRepo)->getTopicById($group_id, $topic_id);
if(!$topic || !$topic->getClub() || !$topic->getClub()->canBeModifiedBy($this->getUser())) {
if(!$topic || !$topic->getClub() || $topic->isDeleted() || !$topic->getClub()->canBeModifiedBy($this->getUser())) {
return 0;
}
@ -290,13 +291,14 @@ final class Board extends VKAPIRequestHandler
$topic = (new TopicsRepo)->getTopicById($group_id, $topic_id);
if(!$topic || !$topic->getClub() || !$topic->getClub()->canBeModifiedBy($this->getUser())) {
if(!$topic || !$topic->getClub() || $topic->isDeleted() || !$topic->getClub()->canBeModifiedBy($this->getUser())) {
$this->fail(5, "Invalid topic");
}
$arr = [
"items" => []
];
$comms = array_slice(iterator_to_array($topic->getComments(1, $count + $offset)), $offset);
foreach($comms as $comm) {
$arr["items"][] = $this->getApiBoardComment($comm, $need_likes);
@ -305,6 +307,10 @@ final class Board extends VKAPIRequestHandler
if($comm->getOwner() instanceof \openvk\Web\Models\Entities\User) {
$arr["profiles"][] = $comm->getOwner()->toVkApiStruct();
}
if($comm->getOwner() instanceof \openvk\Web\Models\Entities\Club) {
$arr["groups"][] = $comm->getOwner()->toVkApiStruct();
}
}
}
@ -334,8 +340,7 @@ final class Board extends VKAPIRequestHandler
} else {
$topics = explode(',', $topic_ids);
foreach($topics as $topic)
{
foreach($topics as $topic) {
$id = explode("_", $topic);
$topicy = (new TopicsRepo)->getTopicById((int)$id[0], (int)$id[1]);
if($topicy) {
@ -354,20 +359,21 @@ final class Board extends VKAPIRequestHandler
$topic = (new TopicsRepo)->getTopicById($group_id, $topic_id);
if(!$topic || !$topic->getClub() || !$topic->getClub()->canBeModifiedBy($this->getUser())) {
if(!$topic || !$topic->getClub() || !$topic->isDeleted() || !$topic->getClub()->canBeModifiedBy($this->getUser())) {
return 0;
}
if($topic->isClosed()) {
$topic->setClosed(0);
$topic->save();
}
return 1;
}
function restoreComment(int $group_id, int $topic_id, int $comment_id)
{
$this->fail(4, "Not implemented");
$this->fail(501, "Not implemented");
}
function unfixTopic(int $group_id, int $topic_id)
@ -381,6 +387,11 @@ final class Board extends VKAPIRequestHandler
return 0;
}
if($topic->isPinned()) {
$topic->setClosed(0);
$topic->save();
}
$topic->setPinned(0);
$topic->save();
@ -388,7 +399,7 @@ final class Board extends VKAPIRequestHandler
return 1;
}
private function getApiBoardComment($comment, bool $need_likes = false)
private function getApiBoardComment(?Comment $comment, bool $need_likes = false)
{
$res = (object) [];
@ -407,14 +418,10 @@ final class Board extends VKAPIRequestHandler
}
foreach($comment->getChildren() as $attachment) {
if($attachment instanceof \openvk\Web\Models\Entities\Photo) {
if($attachment->isDeleted())
continue;
$res->attachments[] = $attachment->toVkApiStruct();
} else if($$attachment instanceof \openvk\Web\Models\Entities\Video) {
$res->attachments[] = $attachment->getApiStructure();
}
}
return $res;

View file

@ -12,9 +12,10 @@ final class Gifts extends VKAPIRequestHandler
$i = 0;
$i+=$offset;
$i += $offset;
$user = (new UsersRepo)->get($user_id);
if(!$user || $user->isDeleted())
$this->fail(177, "Invalid user");
@ -54,7 +55,8 @@ final class Gifts extends VKAPIRequestHandler
$this->willExecuteWriteAction();
$user = (new UsersRepo)->get((int) $user_ids);
if(OPENVK_ROOT_CONF['openvk']['preferences']['commerce'] == false)
if(!OPENVK_ROOT_CONF['openvk']['preferences']['commerce'])
$this->fail(105, "Commerce is disabled on this instance");
if(!$user || $user->isDeleted())
@ -105,8 +107,8 @@ final class Gifts extends VKAPIRequestHandler
{
$this->requireUser();
$this->willExecuteWriteAction();
# тожэ заглушка
return 0;
$this->fail(501, "Not implemented");
}
# этих методов не было в ВК, но я их добавил чтобы можно было отобразить список подарков
@ -116,6 +118,9 @@ final class Gifts extends VKAPIRequestHandler
$categ = [];
$i = 0;
if(!OPENVK_ROOT_CONF['openvk']['preferences']['commerce'])
$this->fail(105, "Commerce is disabled on this instance");
foreach($cats as $cat) {
$categ[$i] = [
"name" => $cat->getName(),
@ -123,6 +128,7 @@ final class Gifts extends VKAPIRequestHandler
"id" => $cat->getId(),
"thumbnail" => $cat->getThumbnailURL(),
];
if($extended == true) {
$categ[$i]["localizations"] = [];
foreach(getLanguages() as $lang) {
@ -143,6 +149,10 @@ final class Gifts extends VKAPIRequestHandler
function getGiftsInCategory(int $id, int $page = 1)
{
$this->requireUser();
if(!OPENVK_ROOT_CONF['openvk']['preferences']['commerce'])
$this->fail(105, "Commerce is disabled on this instance");
if(!(new GiftsRepo)->getCat($id))
$this->fail(177, "Category not found");

View file

@ -291,7 +291,7 @@ final class Groups extends VKAPIRequestHandler
int $wall = NULL,
int $topics = NULL,
int $adminlist = NULL,
int $topicsAboveVall = NULL,
int $topicsAboveWall = NULL,
int $hideFromGlobalFeed = NULL)
{
$this->requireUser();
@ -310,7 +310,7 @@ final class Groups extends VKAPIRequestHandler
!is_null($wall) ? $club->setWall($wall) : NULL;
!is_null($topics) ? $club->setEveryone_Can_Create_Topics($topics) : NULL;
!is_null($adminlist) ? $club->setAdministrators_List_Display($adminlist) : NULL;
!is_null($topicsAboveVall) ? $club->setDisplay_Topics_Above_Wall($topicsAboveVall) : NULL;
!is_null($topicsAboveWall) ? $club->setDisplay_Topics_Above_Wall($topicsAboveWall) : NULL;
!is_null($hideFromGlobalFeed) ? $club->setHide_From_Global_Feed($hideFromGlobalFeed) : NULL;
$club->save();
@ -508,7 +508,7 @@ final class Groups extends VKAPIRequestHandler
if(!$club || $group_id == 0)
$this->fail(203, "Invalid club");
if(!$usver || $user_id == 0)
if(!$usver || $usver->isDeleted() || $user_id == 0)
$this->fail(30, "Invalid user");
if($extended == false) {
@ -528,7 +528,7 @@ final class Groups extends VKAPIRequestHandler
function remove(int $group_id, int $user_id)
{
$this->requireUser();
# зоглущка
return 0;
$this->fail(501, "Not implemented");
}
}

View file

@ -3,8 +3,9 @@ namespace openvk\VKAPI\Handlers;
use openvk\Web\Models\Repositories\Notes as NotesRepo;
use openvk\Web\Models\Repositories\Users as UsersRepo;
use openvk\Web\Models\Repositories\Comments as CommentsRepo;
use openvk\Web\Models\Repositories\Photos as PhotosRepo;
use openvk\Web\Models\Repositories\Videos as VideosRepo;
use openvk\Web\Models\Entities\{Note, Comment};
use openvk\VKAPI\Structures\{Comment as APIComment};
final class Notes extends VKAPIRequestHandler
{
@ -24,7 +25,7 @@ final class Notes extends VKAPIRequestHandler
return $note->getVirtualId();
}
function createComment(string $note_id, int $owner_id, string $message, int $reply_to = 0)
function createComment(string $note_id, int $owner_id, string $message, int $reply_to = 0, string $attachments = "")
{
$this->requireUser();
$this->willExecuteWriteAction();
@ -32,9 +33,16 @@ final class Notes extends VKAPIRequestHandler
if(!$note)
$this->fail(180, "Note not found");
if($note->isDeleted())
$this->fail(189, "Note is deleted");
if($note->getOwner()->isDeleted())
$this->fail(403, "Owner is deleted");
if(empty($message) && empty($attachments))
$this->fail(100, "Required parameter 'message' missing.");
$comment = new Comment;
$comment->setOwner($this->getUser()->getId());
$comment->setModel(get_class($note));
@ -43,6 +51,49 @@ final class Notes extends VKAPIRequestHandler
$comment->setCreated(time());
$comment->save();
if(!empty($attachments)) {
$attachmentsArr = explode(",", $attachments);
if(sizeof($attachmentsArr) > 10)
$this->fail(50, "Error: too many attachments");
foreach($attachmentsArr as $attac) {
$attachmentType = NULL;
if(str_contains($attac, "photo"))
$attachmentType = "photo";
elseif(str_contains($attac, "video"))
$attachmentType = "video";
else
$this->fail(205, "Unknown attachment type");
$attachment = str_replace($attachmentType, "", $attac);
$attachmentOwner = (int)explode("_", $attachment)[0];
$attachmentId = (int)end(explode("_", $attachment));
$attacc = NULL;
if($attachmentType == "photo") {
$attacc = (new PhotosRepo)->getByOwnerAndVID($attachmentOwner, $attachmentId);
if(!$attacc || $attacc->isDeleted())
$this->fail(100, "Photo does not exists");
if($attacc->getOwner()->getId() != $this->getUser()->getId())
$this->fail(43, "You do not have access to this photo");
$comment->attach($attacc);
} elseif($attachmentType == "video") {
$attacc = (new VideosRepo)->getByOwnerAndVID($attachmentOwner, $attachmentId);
if(!$attacc || $attacc->isDeleted())
$this->fail(100, "Video does not exists");
if($attacc->getOwner()->getId() != $this->getUser()->getId())
$this->fail(43, "You do not have access to this video");
$comment->attach($attacc);
}
}
}
return $comment->getId();
}
@ -92,6 +143,9 @@ final class Notes extends VKAPIRequestHandler
if($note->isDeleted())
$this->fail(189, "Note is deleted");
if(!$note->canBeModifiedBy($this->getUser()))
$this->fail(403, "No access");
!empty($title) ? $note->setName($title) : NULL;
!empty($text) ? $note->setSource($text) : NULL;
@ -126,7 +180,7 @@ final class Notes extends VKAPIRequestHandler
$this->requireUser();
$user = (new UsersRepo)->get($user_id);
if(!$user)
if(!$user || $user->isDeleted())
$this->fail(15, "Invalid user");
if(empty($note_ids)) {
@ -180,6 +234,8 @@ final class Notes extends VKAPIRequestHandler
function getComments(int $note_id, int $owner_id, int $sort = 1, int $offset = 0, int $count = 100)
{
$this->requireUser();
$note = (new NotesRepo)->getNoteById($owner_id, $note_id);
if(!$note)
@ -194,18 +250,10 @@ final class Notes extends VKAPIRequestHandler
$arr = (object) [
"count" => $note->getCommentsCount(),
"comments" => []];
$comments = array_slice(iterator_to_array($note->getComments(1, $count)), $offset);
$comments = array_slice(iterator_to_array($note->getComments(1, $count + $offset)), $offset);
foreach($comments as $comment) {
$comm = new APIComment;
$comm->id = $comment->getId();
$comm->uid = $comment->getOwner()->getId();
$comm->nid = $note->getId();
$comm->oid = $note->getOwner()->getId();
$comm->date = $comment->getPublicationTime()->timestamp();
$comm->message = $comment->getText();
$comm->reply_to = 0;
$arr->comments[] = $comm;
$arr->comments[] = $comment->toVkApiStruct($this->getUser(), false, false, $note);
}
return $arr;
@ -213,11 +261,11 @@ final class Notes extends VKAPIRequestHandler
function getFriendsNotes(int $offset = 0, int $count = 0)
{
$this->fail(4, "Not implemented");
$this->fail(501, "Not implemented");
}
function restoreComment(int $comment_id = 0, int $owner_id = 0)
{
$this->fail(4, "Not implemented");
$this->fail(501, "Not implemented");
}
}

View file

@ -408,6 +408,10 @@ final class Photos extends VKAPIRequestHandler
$this->fail(21, "Invalid photo");
}
if($photo->getOwner()->isDeleted()) {
$this->fail(21, "Owner of this photo is deleted");
}
if(!$photo->getOwner()->getPrivacyPermission('photos.read', $this->getUser())) {
$this->fail(21, "This user chose to hide his photos.");
}
@ -560,7 +564,7 @@ final class Photos extends VKAPIRequestHandler
function getAllComments(int $owner_id, int $album_id, bool $need_likes = false, int $offset = 0, int $count = 100)
{
$this->fail(10, "Not implemented :D");
$this->fail(501, "Not implemented");
}
function deleteComment(int $comment_id, int $owner_id = 0)

View file

@ -8,11 +8,11 @@ final class Status extends VKAPIRequestHandler
function get(int $user_id = 0, int $group_id = 0)
{
$this->requireUser();
if ($user_id == 0 && $group_id == 0) {
if($user_id == 0 && $group_id == 0) {
return $this->getUser()->getStatus();
} else {
if ($group_id > 0)
$this->fail(1, "OpenVK has no group statuses");
if($group_id > 0)
$this->fail(501, "Group statuses are not implemented");
else
return (new UsersRepo)->get($user_id)->getStatus();
}
@ -23,8 +23,8 @@ final class Status extends VKAPIRequestHandler
$this->requireUser();
$this->willExecuteWriteAction();
if ($group_id > 0) {
$this->fail(1, "OpenVK has no group statuses");
if($group_id > 0) {
$this->fail(501, "Group statuses are not implemented");
} else {
$this->getUser()->setStatus($text);
$this->getUser()->save();

View file

@ -1,13 +0,0 @@
<?php declare(strict_types=1);
namespace openvk\VKAPI\Structures;
final class Comment
{
public $id;
public $uid;
public $nid;
public $oid;
public $date;
public $message;
public $reply_to;
}

View file

@ -360,6 +360,35 @@ class Club extends RowModel
return $this->getRecord()->alert;
}
function toVkApiStruct(?User $user = NULL): object
{
$res = [];
$res->id = $this->getId();
$res->name = $this->getName();
$res->screen_name = $this->getShortCode();
$res->is_closed = 0;
$res->deactivated = NULL;
$res->is_admin = $this->canBeModifiedBy($user);
if($this->canBeModifiedBy($user)) {
$res->admin_level = 3;
}
$res->is_member = $this->getSubscriptionStatus($user) ? 1 : 0;
$res->type = "group";
$res->photo_50 = $this->getAvatarUrl("miniscule");
$res->photo_100 = $this->getAvatarUrl("tiny");
$res->photo_200 = $this->getAvatarUrl("normal");
$res->can_create_topic = $this->canBeModifiedBy($user) ? 1 : $this->isEveryoneCanCreateTopics() ? 1 : 0;
$res->can_post = $this->canBeModifiedBy($user) ? 1 : $this->canPost() ? 1 : 0;
return (object) $res;
}
use Traits\TBackDrops;
use Traits\TSubscribable;
}

View file

@ -2,6 +2,7 @@
namespace openvk\Web\Models\Entities;
use openvk\Web\Models\Repositories\Clubs;
use openvk\Web\Models\RowModel;
use openvk\Web\Models\Entities\{Note};
class Comment extends Post
{
@ -53,7 +54,7 @@ class Comment extends Post
$this->getTarget() instanceof Topic && $this->getTarget()->canBeModifiedBy($user);
}
function toVkApiStruct(?User $user = NULL, bool $need_likes = false, bool $extended = false): object
function toVkApiStruct(?User $user = NULL, bool $need_likes = false, bool $extended = false, ?Note $note = NULL): object
{
$res = (object) [];
@ -64,6 +65,12 @@ class Comment extends Post
$res->attachments = [];
$res->parents_stack = [];
if(!is_null($note)) {
$res->uid = $this->getOwner()->getId();
$res->nid = $note->getId();
$res->oid = $note->getOwner()->getId();
}
foreach($this->getChildren() as $attachment) {
if($attachment->isDeleted())
continue;

View file

@ -165,6 +165,11 @@ class Video extends Media
];
}
function toVkApiStruct(): object
{
return $this->getApiStructure();
}
function setLink(string $link): string
{
if(preg_match(file_get_contents(__DIR__ . "/../VideoDrivers/regex/youtube.txt"), $link, $matches)) {