Skip to content

Commit

Permalink
feat(documents) (#1205)
Browse files Browse the repository at this point in the history
* create document entity

* add upload, previews, most of api methods

* ui start

* better ui, search, uploading (icons by myslivets)

Co-Authored-By: Daniel <[email protected]>

* add editing functions

* add viewer and gallery

* preparations for picker

* things

* add counter on tab

* add tags

* fix gif processing

* fix png processing

* picker

* addd search

* add fast uploader

* openvk midn. support, change midn.photomodal color

* fix low register format chekc

* add gif play on click

* unauthorized limitations

---------

Co-authored-by: Daniel <[email protected]>
  • Loading branch information
mrilyew and myslivets authored Jan 22, 2025
1 parent 5048015 commit 2d83003
Show file tree
Hide file tree
Showing 45 changed files with 2,550 additions and 37 deletions.
215 changes: 215 additions & 0 deletions VKAPI/Handlers/Docs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
<?php declare(strict_types=1);
namespace openvk\VKAPI\Handlers;
use Chandler\Database\DatabaseConnection;
use openvk\Web\Models\Entities\Document;
use openvk\Web\Models\Repositories\Documents;

final class Docs extends VKAPIRequestHandler
{
function add(int $owner_id, int $doc_id, ?string $access_key): string
{
$this->requireUser();
$this->willExecuteWriteAction();

$doc = (new Documents)->getDocumentById($owner_id, $doc_id);
if(!$doc || $doc->isDeleted())
$this->fail(1150, "Invalid document id");

if(!$doc->checkAccessKey($access_key))
$this->fail(15, "Access denied");

if($doc->isCopiedBy($this->getUser()))
$this->fail(100, "One of the parameters specified was missing or invalid: this document already added");

$new_doc = $doc->copy($this->getUser());

return $new_doc->getPrettyId();
}

function delete(int $owner_id, int $doc_id): int
{
$this->requireUser();
$this->willExecuteWriteAction();
$doc = (new Documents)->getDocumentByIdUnsafe($owner_id, $doc_id);
if(!$doc || $doc->isDeleted())
$this->fail(1150, "Invalid document id");

if(!$doc->canBeModifiedBy($this->getUser()))
$this->fail(1153, "Access to document is denied");

$doc->delete();

return 1;
}

function restore(int $owner_id, int $doc_id): int
{
$this->requireUser();
$this->willExecuteWriteAction();

return $this->add($owner_id, $doc_id, "");
}

function edit(int $owner_id, int $doc_id, ?string $title = "", ?string $tags = "", ?int $folder_id = 0, int $owner_hidden = -1): int
{
$this->requireUser();
$this->willExecuteWriteAction();

$doc = (new Documents)->getDocumentByIdUnsafe($owner_id, $doc_id);
if(!$doc || $doc->isDeleted())
$this->fail(1150, "Invalid document id");
if(!$doc->canBeModifiedBy($this->getUser()))
$this->fail(1153, "Access to document is denied");
if(iconv_strlen($title ?? "") > 128 || iconv_strlen($title ?? "") < 0)
$this->fail(1152, "Invalid document title");
if(iconv_strlen($tags ?? "") > 256)
$this->fail(1154, "Invalid tags");

if($title)
$doc->setName($title);

$doc->setTags($tags);
if(in_array($folder_id, [0, 3]))
$doc->setFolder_id($folder_id);
if(in_array($owner_hidden, [0, 1]))
$doc->setOwner_hidden($owner_hidden);

try {
$doc->setEdited(time());
$doc->save();
} catch(\Throwable $e) {
return 0;
}

return 1;
}

function get(int $count = 30, int $offset = 0, int $type = -1, int $owner_id = NULL, int $return_tags = 0, int $order = 0): object
{
$this->requireUser();
if(!$owner_id)
$owner_id = $this->getUser()->getId();

if($owner_id > 0 && $owner_id != $this->getUser()->getId())
$this->fail(15, "Access denied");

$documents = (new Documents)->getDocumentsByOwner($owner_id, $order, $type);
$res = (object)[
"count" => $documents->size(),
"items" => [],
];

foreach($documents->offsetLimit($offset, $count) as $doc) {
$res->items[] = $doc->toVkApiStruct($this->getUser(), $return_tags == 1);
}

return $res;
}

function getById(string $docs, int $return_tags = 0): array
{
$this->requireUser();

$item_ids = explode(",", $docs);
$response = [];
if(sizeof($item_ids) < 1) {
$this->fail(100, "One of the parameters specified was missing or invalid: docs is undefined");
}

foreach($item_ids as $id) {
$splitted_id = explode("_", $id);
$doc = (new Documents)->getDocumentById((int)$splitted_id[0], (int)$splitted_id[1], $splitted_id[2]);
if(!$doc || $doc->isDeleted())
continue;

$response[] = $doc->toVkApiStruct($this->getUser(), $return_tags === 1);
}

return $response;
}

function getTypes(?int $owner_id)
{
$this->requireUser();
if(!$owner_id)
$owner_id = $this->getUser()->getId();

if($owner_id > 0 && $owner_id != $this->getUser()->getId())
$this->fail(15, "Access denied");

$types = (new Documents)->getTypes($owner_id);
return [
"count" => sizeof($types),
"items" => $types,
];
}

function getTags(?int $owner_id, ?int $type = 0)
{
$this->requireUser();
if(!$owner_id)
$owner_id = $this->getUser()->getId();

if($owner_id > 0 && $owner_id != $this->getUser()->getId())
$this->fail(15, "Access denied");

$tags = (new Documents)->getTags($owner_id, $type);
return $tags;
}

function search(string $q = "", int $search_own = -1, int $order = -1, int $count = 30, int $offset = 0, int $return_tags = 0, int $type = 0, ?string $tags = NULL): object
{
$this->requireUser();

$params = [];
$o_order = ["type" => "id", "invert" => false];

if(iconv_strlen($q) > 512)
$this->fail(100, "One of the parameters specified was missing or invalid: q should be not more 512 letters length");

if(in_array($type, [1,2,3,4,5,6,7,8]))
$params["type"] = $type;

if(iconv_strlen($tags ?? "") < 512)
$params["tags"] = $tags;

if($search_own === 1)
$params["from_me"] = $this->getUser()->getId();

$documents = (new Documents)->find($q, $params, $o_order);
$res = (object)[
"count" => $documents->size(),
"items" => [],
];

foreach($documents->offsetLimit($offset, $count) as $doc) {
$res->items[] = $doc->toVkApiStruct($this->getUser(), $return_tags == 1);
}

return $res;
}

function getUploadServer(?int $group_id = NULL)
{
$this->requireUser();
$this->willExecuteWriteAction();

return 0;
}

function getWallUploadServer(?int $group_id = NULL)
{
$this->requireUser();
$this->willExecuteWriteAction();

return 0;
}

function save(string $file, string $title, string $tags, ?int $return_tags = 0)
{
$this->requireUser();
$this->willExecuteWriteAction();

return 0;
}
}
30 changes: 25 additions & 5 deletions VKAPI/Handlers/Wall.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@ function get(int $owner_id, string $domain = "", int $offset = 0, int $count = 3
"type" => "audio",
"audio" => $attachment->toVkApiStruct($this->getUser()),
];
} else if ($attachment instanceof \openvk\Web\Models\Entities\Document) {
$attachments[] = [
"type" => "doc",
"doc" => $attachment->toVkApiStruct($this->getUser()),
];
} else if ($attachment instanceof \openvk\Web\Models\Entities\Post) {
$repostAttachments = [];

Expand Down Expand Up @@ -333,6 +338,11 @@ function getById(string $posts, int $extended = 0, string $fields = "", User $us
"type" => "audio",
"audio" => $attachment->toVkApiStruct($this->getUser())
];
} else if ($attachment instanceof \openvk\Web\Models\Entities\Document) {
$attachments[] = [
"type" => "doc",
"doc" => $attachment->toVkApiStruct($this->getUser()),
];
} else if ($attachment instanceof \openvk\Web\Models\Entities\Post) {
$repostAttachments = [];

Expand Down Expand Up @@ -577,7 +587,7 @@ function post(string $owner_id, string $message = "", string $copyright = "", in
if($signed == 1)
$flags |= 0b01000000;

$parsed_attachments = parseAttachments($attachments, ['photo', 'video', 'note', 'poll', 'audio']);
$parsed_attachments = parseAttachments($attachments, ['photo', 'video', 'note', 'poll', 'audio', 'doc']);
$final_attachments = [];
$should_be_suggested = $owner_id < 0 && !$wallOwner->canBeModifiedBy($this->getUser()) && $wallOwner->getWallType() == 2;
foreach($parsed_attachments as $attachment) {
Expand Down Expand Up @@ -670,7 +680,7 @@ function repost(string $object, string $message = "", string $attachments = "",
if(preg_match('/(wall|video|photo)((?:-?)[0-9]+)_([0-9]+)/', $object, $postArray) == 0)
$this->fail(100, "One of the parameters specified was missing or invalid: object is incorrect");

$parsed_attachments = parseAttachments($attachments, ['photo', 'video', 'note', 'audio']);
$parsed_attachments = parseAttachments($attachments, ['photo', 'video', 'note', 'audio', 'doc']);
$final_attachments = [];
foreach($parsed_attachments as $attachment) {
if($attachment && !$attachment->isDeleted() && $attachment->canBeViewedBy($this->getUser()) &&
Expand Down Expand Up @@ -780,6 +790,11 @@ function getComments(int $owner_id, int $post_id, bool $need_likes = true, int $
"type" => "audio",
"audio" => $attachment->toVkApiStruct($this->getUser()),
];
} else if ($attachment instanceof \openvk\Web\Models\Entities\Document) {
$attachments[] = [
"type" => "doc",
"doc" => $attachment->toVkApiStruct($this->getUser()),
];
}
}

Expand Down Expand Up @@ -867,6 +882,11 @@ function getComment(int $owner_id, int $comment_id, bool $extended = false, stri
"type" => "audio",
"audio" => $attachment->toVkApiStruct($this->getUser()),
];
} else if ($attachment instanceof \openvk\Web\Models\Entities\Document) {
$attachments[] = [
"type" => "doc",
"doc" => $attachment->toVkApiStruct($this->getUser()),
];
}
}

Expand Down Expand Up @@ -928,7 +948,7 @@ function createComment(int $owner_id, int $post_id, string $message = "", int $f
if($post->getTargetWall() < 0)
$club = (new ClubsRepo)->get(abs($post->getTargetWall()));

$parsed_attachments = parseAttachments($attachments, ['photo', 'video', 'note', 'audio']);
$parsed_attachments = parseAttachments($attachments, ['photo', 'video', 'note', 'audio', 'doc']);
$final_attachments = [];
foreach($parsed_attachments as $attachment) {
if($attachment && !$attachment->isDeleted() && $attachment->canBeViewedBy($this->getUser()) &&
Expand Down Expand Up @@ -1014,7 +1034,7 @@ function edit(int $owner_id, int $post_id, string $message = "", string $attachm
$this->requireUser();
$this->willExecuteWriteAction();

$parsed_attachments = parseAttachments($attachments, ['photo', 'video', 'note', 'audio', 'poll']);
$parsed_attachments = parseAttachments($attachments, ['photo', 'video', 'note', 'audio', 'poll', 'doc']);
$final_attachments = [];
foreach($parsed_attachments as $attachment) {
if($attachment && !$attachment->isDeleted() && $attachment->canBeViewedBy($this->getUser()) &&
Expand Down Expand Up @@ -1083,7 +1103,7 @@ function editComment(int $comment_id, int $owner_id = 0, string $message = "", s
$this->willExecuteWriteAction();

$comment = (new CommentsRepo)->get($comment_id);
$parsed_attachments = parseAttachments($attachments, ['photo', 'video', 'note', 'audio']);
$parsed_attachments = parseAttachments($attachments, ['photo', 'video', 'note', 'audio', 'doc']);
$final_attachments = [];
foreach($parsed_attachments as $attachment) {
if($attachment && !$attachment->isDeleted() && $attachment->canBeViewedBy($this->getUser()) &&
Expand Down
8 changes: 8 additions & 0 deletions Web/Models/Entities/Club.php
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,14 @@ function canUploadAudio(?User $user): bool
return $this->isEveryoneCanUploadAudios() || $this->canBeModifiedBy($user);
}

function canUploadDocs(?User $user): bool
{
if(!$user)
return false;

return $this->canBeModifiedBy($user);
}

function getAudiosCollectionSize()
{
return (new \openvk\Web\Models\Repositories\Audios)->getClubCollectionSize($this);
Expand Down
Loading

0 comments on commit 2d83003

Please sign in to comment.