Skip to content

Commit

Permalink
[WIP] Textarea: Upload multiple pictures (OpenVK#800)
Browse files Browse the repository at this point in the history
* VKAPI: Fix bug when DELETED user appear if there is no user_ids

* Textarea: Make multiple attachments

* постмодернистское искусство

* Use only attachPic for grabbing pic attachments

TODO throw flashFail on bruh moment with pic attachments

* draft masonry picture layout in posts xddd

где мои опиаты???

* fix funny typos in computeMasonryLayout

* Fix video bruh moment in textarea

* Posts: add multiple kakahi for microblog

* Photo: Add minimal implementation of миниатюра открывашка

Co-authored-by: Daniel <[email protected]>

* Photo: Add ability to slide trough photos in one post

This also gives ability to easily implement comments and actions

* Photo: The Fxck Is This implementation of comments under photo in viewer

* FloatingPhotoViewer: Better CSS

- Fix that details background issue
- Make slide buttons slightly shorter by height

* FloatingPhotoViewer: Refactor, and make it better

- Now you can actually check the comments under EVERY photo
- Fix for textarea. Now you can publish comments

* Fix funny typos xddd

* Kinda fix poll display in non-microblog posts

* Posts: Fix poll display in microblog posts

* Add photos picker (OpenVK#986)

* early implementation of photos pickir

Добавлен пикер фоточек и быстрая загрузка фото. Так же пофикшен просмотрщик фото в группах. Но, правда, я сломал копипейст, но это ладн.

* Fiks fotos viver four coments.

* Add picking photos from clubs albums

Копипейст и граффити так и не пофикшены

* Fix graffiti and copypaste

Какого-то хуя копипаста у постов срабатывает два раза.

* some fixesx

* dragon drop

* Fix PHP 8 compatibility

* 5 (OpenVK#988)

---------

Co-authored-by: celestora <[email protected]>
Co-authored-by: Daniel <[email protected]>
Co-authored-by: lalka2016 <[email protected]>
Co-authored-by: Alexander Minkin <[email protected]>
  • Loading branch information
5 people authored Oct 3, 2023
1 parent 6632d07 commit a859fa1
Show file tree
Hide file tree
Showing 31 changed files with 1,268 additions and 113 deletions.
92 changes: 92 additions & 0 deletions ServiceAPI/Photos.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php declare(strict_types=1);
namespace openvk\ServiceAPI;
use openvk\Web\Models\Entities\User;
use openvk\Web\Models\Repositories\{Photos as PhotosRepo, Albums, Clubs};

class Photos implements Handler
{
protected $user;
protected $photos;

function __construct(?User $user)
{
$this->user = $user;
$this->photos = new PhotosRepo;
}

function getPhotos(int $page = 1, int $album = 0, callable $resolve, callable $reject)
{
if($album == 0) {
$photos = $this->photos->getEveryUserPhoto($this->user, $page, 24);
$count = $this->photos->getUserPhotosCount($this->user);
} else {
$album = (new Albums)->get($album);

if(!$album || $album->isDeleted())
$reject(55, "Invalid .");

if($album->getOwner() instanceof User) {
if($album->getOwner()->getId() != $this->user->getId())
$reject(555, "Access to album denied");
} else {
if(!$album->getOwner()->canBeModifiedBy($this->user))
$reject(555, "Access to album denied");
}

$photos = $album->getPhotos($page, 24);
$count = $album->size();
}

$arr = [
"count" => $count,
"items" => [],
];

foreach($photos as $photo) {
$res = json_decode(json_encode($photo->toVkApiStruct()), true);

$arr["items"][] = $res;
}

$resolve($arr);
}

function getAlbums(int $club, callable $resolve, callable $reject)
{
$albumsRepo = (new Albums);

$count = $albumsRepo->getUserAlbumsCount($this->user);
$albums = $albumsRepo->getUserAlbums($this->user, 1, $count);

$arr = [
"count" => $count,
"items" => [],
];

foreach($albums as $album) {
$res = ["id" => $album->getId(), "name" => $album->getName()];

$arr["items"][] = $res;
}

if($club > 0) {
$cluber = (new Clubs)->get($club);

if(!$cluber || !$cluber->canBeModifiedBy($this->user))
$reject(1337, "Invalid (club), or you can't modify him");

$clubCount = (new Albums)->getClubAlbumsCount($cluber);
$clubAlbums = (new Albums)->getClubAlbums($cluber, 1, $clubCount);

foreach($clubAlbums as $albumr) {
$res = ["id" => $albumr->getId(), "name" => $albumr->getName()];

$arr["items"][] = $res;
}

$arr["count"] = $arr["count"] + $clubCount;
}

$resolve($arr);
}
}
2 changes: 1 addition & 1 deletion Web/Models/Entities/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Comment extends Post

function getPrettyId(): string
{
return $this->getRecord()->id;
return (string)$this->getRecord()->id;
}

function getVirtualId(): int
Expand Down
2 changes: 1 addition & 1 deletion Web/Models/Entities/IP.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ function setIp(string $ip): void
$this->stateChanges("ip", $ip);
}

function save($log): void
function save(?bool $log = false): void
{
if(is_null($this->getRecord()))
$this->stateChanges("first_seen", time());
Expand Down
4 changes: 2 additions & 2 deletions Web/Models/Entities/Media.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,14 @@ function setFile(array $file): void
$this->stateChanges("hash", $hash);
}

function save(): void
function save(?bool $log = false): void
{
if(!is_null($this->processingPlaceholder) && is_null($this->getRecord())) {
$this->stateChanges("processed", 0);
$this->stateChanges("last_checked", time());
}

parent::save();
parent::save($log);
}

function delete(bool $softly = true): void
Expand Down
4 changes: 2 additions & 2 deletions Web/Models/Entities/Postable.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ function setVirtual_Id(int $id): void
throw new ISE("Setting virtual id manually is forbidden");
}

function save(): void
function save(?bool $log = false): void
{
$vref = $this->upperNodeReferenceColumnName;

Expand All @@ -171,7 +171,7 @@ function save(): void
$this->stateChanges("edited", time());
}*/

parent::save();
parent::save($log);
}

use Traits\TAttachmentHost;
Expand Down
43 changes: 42 additions & 1 deletion Web/Models/Entities/Traits/TAttachmentHost.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php declare(strict_types=1);
namespace openvk\Web\Models\Entities\Traits;
use openvk\Web\Models\Entities\Attachable;
use openvk\Web\Models\Entities\{Attachable, Photo};
use openvk\Web\Util\Makima\Makima;
use Chandler\Database\DatabaseConnection;

trait TAttachmentHost
Expand Down Expand Up @@ -29,6 +30,46 @@ function getChildren(): \Traversable
yield $repo->get($rel->attachable_id);
}
}

function getChildrenWithLayout(int $w, int $h = -1): object
{
if($h < 0)
$h = $w;

$children = $this->getChildren();
$skipped = $photos = $result = [];
foreach($children as $child) {
if($child instanceof Photo) {
$photos[] = $child;
continue;
}

$skipped[] = $child;
}

$height = "unset";
$width = $w;
if(sizeof($photos) < 2) {
if(isset($photos[0]))
$result[] = ["100%", "unset", $photos[0], "unset"];
} else {
$mak = new Makima($photos);
$layout = $mak->computeMasonryLayout($w, $h);
$height = $layout->height;
$width = $layout->width;
for($i = 0; $i < sizeof($photos); $i++) {
$tile = $layout->tiles[$i];
$result[] = [$tile->width . "px", $tile->height . "px", $photos[$i], "left"];
}
}

return (object) [
"width" => $width . "px",
"height" => $height . "px",
"tiles" => $result,
"extras" => $skipped,
];
}

function attach(Attachable $attachment): void
{
Expand Down
20 changes: 16 additions & 4 deletions Web/Models/Repositories/Photos.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,26 @@ function getByOwnerAndVID(int $owner, int $vId): ?Photo
return new Photo($photo);
}

function getEveryUserPhoto(User $user): \Traversable
function getEveryUserPhoto(User $user, int $page = 1, ?int $perPage = NULL): \Traversable
{
$perPage = $perPage ?? OPENVK_DEFAULT_PER_PAGE;
$photos = $this->photos->where([
"owner" => $user->getId()
]);
"owner" => $user->getId(),
"deleted" => 0
])->order("id DESC");

foreach($photos as $photo) {
foreach($photos->page($page, $perPage) as $photo) {
yield new Photo($photo);
}
}

function getUserPhotosCount(User $user)
{
$photos = $this->photos->where([
"owner" => $user->getId(),
"deleted" => 0
]);

return sizeof($photos);
}
}
37 changes: 19 additions & 18 deletions Web/Presenters/CommentPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
namespace openvk\Web\Presenters;
use openvk\Web\Models\Entities\{Comment, Notifications\MentionNotification, Photo, Video, User, Topic, Post};
use openvk\Web\Models\Entities\Notifications\CommentNotification;
use openvk\Web\Models\Repositories\{Comments, Clubs, Videos};
use openvk\Web\Models\Repositories\{Comments, Clubs, Videos, Photos};

final class CommentPresenter extends OpenVKPresenter
{
Expand Down Expand Up @@ -54,9 +54,6 @@ function renderMakeComment(string $repo, int $eId): void
if ($entity instanceof Post && $entity->getWallOwner()->isBanned())
$this->flashFail("err", tr("error"), tr("forbidden"));

if($_FILES["_vid_attachment"] && OPENVK_ROOT_CONF['openvk']['preferences']['videos']['disableUploading'])
$this->flashFail("err", tr("error"), tr("video_uploads_disabled"));

$flags = 0;
if($this->postParam("as_group") === "on" && !is_null($club) && $club->canBeModifiedBy($this->user->identity))
$flags |= 0b10000000;
Expand All @@ -70,18 +67,22 @@ function renderMakeComment(string $repo, int $eId): void
}
}

# TODO move to trait
try {
$photo = NULL;
if($_FILES["_pic_attachment"]["error"] === UPLOAD_ERR_OK) {
$album = NULL;
if($wall > 0 && $wall === $this->user->id)
$album = (new Albums)->getUserWallAlbum($wallOwner);

$photo = Photo::fastMake($this->user->id, $this->postParam("text"), $_FILES["_pic_attachment"], $album);
$photos = [];
if(!empty($this->postParam("photos"))) {
$un = rtrim($this->postParam("photos"), ",");
$arr = explode(",", $un);

if(sizeof($arr) < 11) {
foreach($arr as $dat) {
$ids = explode("_", $dat);
$photo = (new Photos)->getByOwnerAndVID((int)$ids[0], (int)$ids[1]);

if(!$photo || $photo->isDeleted())
continue;

$photos[] = $photo;
}
}
} catch(ISE $ex) {
$this->flashFail("err", tr("error_when_publishing_comment"), tr("error_comment_file_too_big"));
}

$videos = [];
Expand All @@ -103,7 +104,7 @@ function renderMakeComment(string $repo, int $eId): void
}
}

if(empty($this->postParam("text")) && !$photo && !$video)
if(empty($this->postParam("text")) && sizeof($photos) < 1 && sizeof($videos) < 1)
$this->flashFail("err", tr("error_when_publishing_comment"), tr("error_comment_empty"));

try {
Expand All @@ -119,8 +120,8 @@ function renderMakeComment(string $repo, int $eId): void
$this->flashFail("err", tr("error_when_publishing_comment"), tr("error_comment_too_big"));
}

if(!is_null($photo))
$comment->attach($photo);
foreach($photos as $photo)
$comment->attach($photo);

if(sizeof($videos) > 0)
foreach($videos as $vid)
Expand Down
38 changes: 38 additions & 0 deletions Web/Presenters/InternalAPIPresenter.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php declare(strict_types=1);
namespace openvk\Web\Presenters;
use openvk\Web\Models\Repositories\{Posts, Comments};
use MessagePack\MessagePack;
use Chandler\Session\Session;

Expand Down Expand Up @@ -95,4 +96,41 @@ function renderTimezone() {
]);
}
}

function renderGetPhotosFromPost(int $owner_id, int $post_id) {
if($_SERVER["REQUEST_METHOD"] !== "POST") {
header("HTTP/1.1 405 Method Not Allowed");
exit("иди нахуй заебал");
}

if($this->postParam("parentType", false) == "post") {
$post = (new Posts)->getPostById($owner_id, $post_id);
} else {
$post = (new Comments)->get($post_id);
}


if(is_null($post)) {
$this->returnJson([
"success" => 0
]);
} else {
$response = [];
$attachments = $post->getChildren();
foreach($attachments as $attachment)
{
if($attachment instanceof \openvk\Web\Models\Entities\Photo)
{
$response[] = [
"url" => $attachment->getURLBySizeId('normal'),
"id" => $attachment->getPrettyId()
];
}
}
$this->returnJson([
"success" => 1,
"body" => $response
]);
}
}
}
22 changes: 15 additions & 7 deletions Web/Presenters/PhotosPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,15 +222,20 @@ function renderUploadPhoto(): void
{
$this->assertUserLoggedIn();
$this->willExecuteWriteAction(true);

if(is_null($this->queryParam("album")))
$this->flashFail("err", tr("error"), tr("error_adding_to_deleted"), 500, true);

[$owner, $id] = explode("_", $this->queryParam("album"));
$album = $this->albums->get((int) $id);

if(is_null($this->queryParam("album"))) {
$album = $this->albums->getUserWallAlbum($this->user->identity);
} else {
[$owner, $id] = explode("_", $this->queryParam("album"));
$album = $this->albums->get((int) $id);
}

if(!$album)
$this->flashFail("err", tr("error"), tr("error_adding_to_deleted"), 500, true);
if(is_null($this->user) || !$album->canBeModifiedBy($this->user->identity))

# Для быстрой загрузки фоток из пикера фотографий нужен альбом, но юзер не может загружать фото
# в системные альбомы, так что так.
if(is_null($this->user) || !is_null($this->queryParam("album")) && !$album->canBeModifiedBy($this->user->identity))
$this->flashFail("err", tr("error_access_denied_short"), tr("error_access_denied"), 500, true);

if($_SERVER["REQUEST_METHOD"] === "POST") {
Expand Down Expand Up @@ -261,6 +266,9 @@ function renderUploadPhoto(): void
$this->flashFail("err", tr("no_photo"), tr("select_file"), 500, true);

$photos = [];
if((int)$this->postParam("count") > 10)
$this->flashFail("err", tr("no_photo"), "ты еблан", 500, true);

for($i = 0; $i < $this->postParam("count"); $i++) {
try {
$photo = new Photo;
Expand Down
Loading

0 comments on commit a859fa1

Please sign in to comment.