[WIP] Comments: Allow photo uploads in comments

This commit is contained in:
Celestora 2021-10-12 14:17:21 +03:00
parent 2e15de74fc
commit 4ab9d67718
4 changed files with 35 additions and 10 deletions

View file

@ -41,4 +41,19 @@ class Photo extends Media
DB::i()->getContext()->table("album_relations")->where("media", $this->getRecord()->id)->delete(); DB::i()->getContext()->table("album_relations")->where("media", $this->getRecord()->id)->delete();
} }
static function fastMake(int $owner, string $description = "", array $file, ?Album $album = NULL): Photo
{
$photo = new static;
$photo->setOwner($owner);
$photo->setDescription(iconv_substr($description, 0, 36) . "...");
$photo->setCreated(time());
$photo->setFile($file);
$photo->save();
if(!is_null($album))
$album->addPhoto($photo);
return $photo;
}
} }

View file

@ -1,6 +1,6 @@
<?php declare(strict_types=1); <?php declare(strict_types=1);
namespace openvk\Web\Presenters; namespace openvk\Web\Presenters;
use openvk\Web\Models\Entities\{Comment, User}; use openvk\Web\Models\Entities\{Comment, Photo, User};
use openvk\Web\Models\Entities\Notifications\CommentNotification; use openvk\Web\Models\Entities\Notifications\CommentNotification;
use openvk\Web\Models\Repositories\Comments; use openvk\Web\Models\Repositories\Comments;
@ -46,6 +46,15 @@ final class CommentPresenter extends OpenVKPresenter
$comment->setContent($this->postParam("text")); $comment->setContent($this->postParam("text"));
$comment->setCreated(time()); $comment->setCreated(time());
$comment->save(); $comment->save();
if($_FILES["_pic_attachment"]["error"] === UPLOAD_ERR_OK) {
try {
$photo = Photo::fastMake($this->user->id, $this->postParam("text"), $_FILES["_pic_attachment"]);
$comment->attach($photo);
} catch(ISE $ex) {
$this->flashFail("err", "Не удалось опубликовать пост", "Файл изображения повреждён, слишком велик или одна сторона изображения в разы больше другой.");
}
}
} catch(\LogicException $ex) { } catch(\LogicException $ex) {
$this->flashFail("err", "Не удалось опубликовать комментарий", "Нельзя опубликовать пустой комментарий."); $this->flashFail("err", "Не удалось опубликовать комментарий", "Нельзя опубликовать пустой комментарий.");
} }

View file

@ -189,16 +189,11 @@ final class WallPresenter extends OpenVKPresenter
if($_FILES["_pic_attachment"]["error"] === UPLOAD_ERR_OK) { if($_FILES["_pic_attachment"]["error"] === UPLOAD_ERR_OK) {
try { try {
$photo = new Photo; $album = NULL;
$photo->setOwner($this->user->id); if($wall > 0 && $wall === $this->user->id)
$photo->setDescription(iconv_substr($this->postParam("text"), 0, 36) . "..."); $album = (new Albums)->getUserWallAlbum($wallOwner);
$photo->setCreated(time());
$photo->setFile($_FILES["_pic_attachment"]);
$photo->save();
if($wall > 0 && $wall === $this->user->id) { $photo = Photo::fastMake($this->user->id, $this->postParam("text"), $_FILES["_pic_attachment"], $album);
(new Albums)->getUserWallAlbum($wallOwner)->addPhoto($photo);
}
} catch(ISE $ex) { } catch(ISE $ex) {
$this->flashFail("err", "Не удалось опубликовать пост", "Файл изображения повреждён, слишком велик или одна сторона изображения в разы больше другой."); $this->flashFail("err", "Не удалось опубликовать пост", "Файл изображения повреждён, слишком велик или одна сторона изображения в разы больше другой.");
} }

View file

@ -19,6 +19,12 @@
<div class="post-content" id="{$comment->getId()}"> <div class="post-content" id="{$comment->getId()}">
<div class="text" id="text{$comment->getId()}"> <div class="text" id="text{$comment->getId()}">
{$comment->getText()|noescape} {$comment->getText()|noescape}
<div n:ifcontent class="attachments_b">
<div class="attachment" n:foreach="$comment->getChildren() as $attachment" data-localized-nsfw-text="{_nsfw_warning}">
{include "attachment.xml", attachment => $attachment}
</div>
</div>
</div> </div>
<div n:if="isset($thisUser) &&! ($compact ?? false)" class="post-menu"> <div n:if="isset($thisUser) &&! ($compact ?? false)" class="post-menu">
{var canDelete = $comment->getOwner()->getId() == $thisUser->getId()} {var canDelete = $comment->getOwner()->getId() == $thisUser->getId()}