Small imp

Пофиксил пагинацию у заметков и подарок
Перенёс структуру заметок
Добавил аттачи к комментариям
Добавил проверку на удалённость аттача
Ну и пофиксил сортировку заметок
This commit is contained in:
lalka2016 2023-05-26 09:16:09 +03:00
parent da0c363524
commit 31ec4499bd
6 changed files with 110 additions and 75 deletions

View file

@ -20,7 +20,7 @@ final class Gifts extends VKAPIRequestHandler
$gift_item = []; $gift_item = [];
$userGifts = $user->getGifts(1, $count, false); $userGifts = array_slice(iterator_to_array($user->getGifts(1, $count, false)), $offset);
if(sizeof($userGifts) < 0) { if(sizeof($userGifts) < 0) {
return NULL; return NULL;

View file

@ -4,7 +4,6 @@ use openvk\Web\Models\Repositories\Notes as NotesRepo;
use openvk\Web\Models\Repositories\Users as UsersRepo; use openvk\Web\Models\Repositories\Users as UsersRepo;
use openvk\Web\Models\Repositories\Comments as CommentsRepo; use openvk\Web\Models\Repositories\Comments as CommentsRepo;
use openvk\Web\Models\Entities\{Note, Comment}; use openvk\Web\Models\Entities\{Note, Comment};
use openvk\VKAPI\Structures\{Note as APINote};
use openvk\VKAPI\Structures\{Comment as APIComment}; use openvk\VKAPI\Structures\{Comment as APIComment};
final class Notes extends VKAPIRequestHandler final class Notes extends VKAPIRequestHandler
@ -72,7 +71,7 @@ final class Notes extends VKAPIRequestHandler
$comment = (new CommentsRepo)->get($comment_id); $comment = (new CommentsRepo)->get($comment_id);
if(!$comment) if(!$comment || !$comment->canBeDeletedBy($this->getUser()))
$this->fail(403, "Access to comment denied"); $this->fail(403, "Access to comment denied");
$comment->delete(); $comment->delete();
@ -128,29 +127,32 @@ final class Notes extends VKAPIRequestHandler
if(!$user) if(!$user)
$this->fail(15, "Invalid user"); $this->fail(15, "Invalid user");
$notes = iterator_to_array((new NotesRepo)->getUserNotes($user, 1, $count, $sort == 0 ? "ASC" : "DESC")); if(empty($note_ids)) {
$notes = array_slice(iterator_to_array((new NotesRepo)->getUserNotes($user, 1, $count, $sort == 0 ? "ASC" : "DESC")), $offset);
$nodez = (object) [ $nodez = (object) [
"count" => (new NotesRepo)->getUserNotesCount((new UsersRepo)->get($user_id)), "count" => (new NotesRepo)->getUserNotesCount((new UsersRepo)->get($user_id)),
"notes" => [] "notes" => []
]; ];
foreach($notes as $note) { foreach($notes as $note) {
if($note->isDeleted()) if($note->isDeleted()) continue;
continue;
$apiNote = new APINote; $nodez->notes[] = $note->toVkApiStruct();
$apiNote->id = $note->getId(); }
$apiNote->owner_id = $note->getOwner()->getId(); } else {
$apiNote->title = $note->getName(); $notes = explode(',', $note_ids);
$apiNote->text = $note->getText();
$apiNote->date = $note->getPublicationTime()->timestamp(); foreach($notes as $note)
$apiNote->comments = $note->getCommentsCount(); {
$apiNote->read_comments = $note->getCommentsCount(); $id = explode("_", $note);
$apiNote->view_url = "/note".$note->getOwner()->getId()."_".$note->getId();
$apiNote->privacy_view = 1; $items = [];
$apiNote->can_comment = 1;
$apiNote->text_wiki = "r"; $note = (new NotesRepo)->getNoteById((int)$id[0], (int)$id[1]);
$nodez->notes[] = $apiNote; if($note) {
$nodez->notes[] = $note->toVkApiStruct();
}
}
} }
return $nodez; return $nodez;
@ -168,26 +170,10 @@ final class Notes extends VKAPIRequestHandler
if($note->isDeleted()) if($note->isDeleted())
$this->fail(189, "Note is deleted"); $this->fail(189, "Note is deleted");
if(!$user || $note->getOwner()->isDeleted()) if(!$note->getOwner() || $note->getOwner()->isDeleted())
$this->fail(177, "Owner does not exists"); $this->fail(177, "Owner does not exists");
$apiNote = new APINote; return $note->toVkApiStruct();
$apiNote->id = $note->getId();
$apiNote->owner_id = $note->getOwner()->getId();
$apiNote->title = $note->getName();
$apiNote->text = $note->getText();
$apiNote->date = $note->getPublicationTime()->timestamp();
$apiNote->comments = $note->getCommentsCount();
$apiNote->read_comments = $note->getCommentsCount();
$apiNote->view_url = "/note".$note->getOwner()->getId()."_".$note->getId();
$apiNote->privacy_view = 1;
$apiNote->can_comment = 1;
$apiNote->text_wiki = "r";
$nodez->notes[] = $apiNote;
return $apiNote;
} }
function getComments(int $note_id, int $owner_id, int $sort = 1, int $offset = 0, int $count = 100) function getComments(int $note_id, int $owner_id, int $sort = 1, int $offset = 0, int $count = 100)
@ -200,13 +186,13 @@ final class Notes extends VKAPIRequestHandler
if($note->isDeleted()) if($note->isDeleted())
$this->fail(189, "Note is deleted"); $this->fail(189, "Note is deleted");
if($note->getOwner()->isDeleted()) if(!$note->getOwner())
$this->fail(177, "Owner does not exists"); $this->fail(177, "Owner does not exists");
$arr = (object) [ $arr = (object) [
"count" => $note->getCommentsCount(), "count" => $note->getCommentsCount(),
"comments" => []]; "comments" => []];
$comments = $note->getComments(1, $count); $comments = array_slice(iterator_to_array($note->getComments(1, $count)), $offset);
foreach($comments as $comment) { foreach($comments as $comment) {
$comm = new APIComment; $comm = new APIComment;
@ -223,13 +209,13 @@ final class Notes extends VKAPIRequestHandler
return $arr; return $arr;
} }
function getFriendsNotes(int $offset, int $count) function getFriendsNotes(int $offset = 0, int $count = 0)
{ {
return 1; $this->fail(4, "Not implemented");
} }
function restoreComment(int $comment_id, int $owner_id) function restoreComment(int $comment_id = 0, int $owner_id = 0)
{ {
$this->fail(4, " "); $this->fail(4, "Not implemented");
} }
} }

View file

@ -430,7 +430,7 @@ final class Wall extends VKAPIRequestHandler
# Аттачи такого вида: [тип][id владельца]_[id вложения] # Аттачи такого вида: [тип][id владельца]_[id вложения]
# Пример: photo1_1 # Пример: photo1_1
if(count($attachmentsArr) > 10) if(sizeof($attachmentsArr) > 10)
$this->fail(50, "Error: too many attachments"); $this->fail(50, "Error: too many attachments");
foreach($attachmentsArr as $attac) { foreach($attachmentsArr as $attac) {
@ -452,7 +452,7 @@ final class Wall extends VKAPIRequestHandler
if($attachmentType == "photo") { if($attachmentType == "photo") {
$attacc = (new PhotosRepo)->getByOwnerAndVID($attachmentOwner, $attachmentId); $attacc = (new PhotosRepo)->getByOwnerAndVID($attachmentOwner, $attachmentId);
if(is_null($attacc)) if(!$attacc || $attacc->isDeleted())
$this->fail(100, "Photo does not exists"); $this->fail(100, "Photo does not exists");
if($attacc->getOwner()->getId() != $this->getUser()->getId()) if($attacc->getOwner()->getId() != $this->getUser()->getId())
$this->fail(43, "You do not have access to this photo"); $this->fail(43, "You do not have access to this photo");
@ -460,7 +460,7 @@ final class Wall extends VKAPIRequestHandler
$post->attach($attacc); $post->attach($attacc);
} elseif($attachmentType == "video") { } elseif($attachmentType == "video") {
$attacc = (new VideosRepo)->getByOwnerAndVID($attachmentOwner, $attachmentId); $attacc = (new VideosRepo)->getByOwnerAndVID($attachmentOwner, $attachmentId);
if(!$attacc) if(!$attacc || $attacc->isDeleted())
$this->fail(100, "Video does not exists"); $this->fail(100, "Video does not exists");
if($attacc->getOwner()->getId() != $this->getUser()->getId()) if($attacc->getOwner()->getId() != $this->getUser()->getId())
$this->fail(43, "You do not have access to this video"); $this->fail(43, "You do not have access to this video");
@ -641,16 +641,20 @@ final class Wall extends VKAPIRequestHandler
return $response; return $response;
} }
function createComment(int $owner_id, int $post_id, string $message, int $from_group = 0) { function createComment(int $owner_id, int $post_id, string $message, int $from_group = 0, string $attachments = "") {
$this->requireUser(); $this->requireUser();
$this->willExecuteWriteAction(); $this->willExecuteWriteAction();
$post = (new PostsRepo)->getPostById($owner_id, $post_id); $post = (new PostsRepo)->getPostById($owner_id, $post_id);
if(!$post || $post->isDeleted()) $this->fail(100, "One of the parameters specified was missing or invalid"); if(!$post || $post->isDeleted()) $this->fail(100, "Invalid post");
if($post->getTargetWall() < 0) if($post->getTargetWall() < 0)
$club = (new ClubsRepo)->get(abs($post->getTargetWall())); $club = (new ClubsRepo)->get(abs($post->getTargetWall()));
if(empty($message) && empty($attachments)) {
$this->fail(100, "Required parameter 'message' missing.");
}
$flags = 0; $flags = 0;
if($from_group != 0 && !is_null($club) && $club->canBeModifiedBy($this->user)) if($from_group != 0 && !is_null($club) && $club->canBeModifiedBy($this->user))
$flags |= 0b10000000; $flags |= 0b10000000;
@ -668,6 +672,49 @@ final class Wall extends VKAPIRequestHandler
$this->fail(1, "ошибка про то что коммент большой слишком"); $this->fail(1, "ошибка про то что коммент большой слишком");
} }
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);
}
}
}
if($post->getOwner()->getId() !== $this->user->getId()) if($post->getOwner()->getId() !== $this->user->getId())
if(($owner = $post->getOwner()) instanceof User) if(($owner = $post->getOwner()) instanceof User)
(new CommentNotification($owner, $comment, $post, $this->user))->emit(); (new CommentNotification($owner, $comment, $post, $this->user))->emit();

View file

@ -1,17 +0,0 @@
<?php declare(strict_types=1);
namespace openvk\VKAPI\Structures;
final class Note
{
public $id;
public $owner_id;
public $title;
public $text;
public $date;
public $comments;
public $read_comments = 0;
public $view_url;
public $privacy_view;
public $can_comment;
public $text_wiki;
}

View file

@ -118,4 +118,23 @@ class Note extends Postable
{ {
return $this->getRecord()->source; return $this->getRecord()->source;
} }
function toVkApiStruct(): object
{
$res = (object) [];
$res->id = $this->getId();
$res->owner_id = $this->getOwner()->getId();
$res->title = $this->getName();
$res->text = $this->getText();
$res->date = $this->getPublicationTime()->timestamp();
$res->comments = $this->getCommentsCount();
$res->read_comments = $this->getCommentsCount();
$res->view_url = "/note".$this->getOwner()->getId()."_".$this->getId();
$res->privacy_view = 1;
$res->can_comment = 1;
$res->text_wiki = "r";
return $res;
}
} }

View file

@ -26,10 +26,10 @@ class Notes
return $this->toNote($this->notes->get($id)); return $this->toNote($this->notes->get($id));
} }
function getUserNotes(User $user, int $page = 1, ?int $perPage = NULL): \Traversable function getUserNotes(User $user, int $page = 1, ?int $perPage = NULL, string $sort = "DESC"): \Traversable
{ {
$perPage = $perPage ?? OPENVK_DEFAULT_PER_PAGE; $perPage = $perPage ?? OPENVK_DEFAULT_PER_PAGE;
foreach($this->notes->where("owner", $user->getId())->where("deleted", 0)->order("created DESC")->page($page, $perPage) as $album) foreach($this->notes->where("owner", $user->getId())->where("deleted", 0)->order("created $sort")->page($page, $perPage) as $album)
yield new Note($album); yield new Note($album);
} }