2020-06-07 19:04:43 +03:00
|
|
|
|
<?php declare(strict_types=1);
|
|
|
|
|
namespace openvk\Web\Presenters;
|
2022-12-10 22:33:13 +03:00
|
|
|
|
use openvk\Web\Models\Entities\{Comment, Notifications\MentionNotification, Photo, Video, User, Topic, Post};
|
2020-06-07 19:04:43 +03:00
|
|
|
|
use openvk\Web\Models\Entities\Notifications\CommentNotification;
|
2021-12-17 18:13:40 +03:00
|
|
|
|
use openvk\Web\Models\Repositories\{Comments, Clubs};
|
2020-06-07 19:04:43 +03:00
|
|
|
|
|
|
|
|
|
final class CommentPresenter extends OpenVKPresenter
|
|
|
|
|
{
|
2022-09-17 00:19:46 +03:00
|
|
|
|
protected $presenterName = "comment";
|
2020-06-07 19:04:43 +03:00
|
|
|
|
private $models = [
|
2021-12-15 01:27:17 +03:00
|
|
|
|
"posts" => "openvk\\Web\\Models\\Repositories\\Posts",
|
|
|
|
|
"photos" => "openvk\\Web\\Models\\Repositories\\Photos",
|
|
|
|
|
"videos" => "openvk\\Web\\Models\\Repositories\\Videos",
|
|
|
|
|
"notes" => "openvk\\Web\\Models\\Repositories\\Notes",
|
|
|
|
|
"topics" => "openvk\\Web\\Models\\Repositories\\Topics",
|
2020-06-07 19:04:43 +03:00
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
function renderLike(int $id): void
|
|
|
|
|
{
|
|
|
|
|
$this->assertUserLoggedIn();
|
2021-01-01 00:18:53 +03:00
|
|
|
|
$this->willExecuteWriteAction();
|
2020-06-07 19:04:43 +03:00
|
|
|
|
|
|
|
|
|
$comment = (new Comments)->get($id);
|
|
|
|
|
if(!$comment || $comment->isDeleted()) $this->notFound();
|
2023-08-07 21:24:48 +03:00
|
|
|
|
|
2023-08-10 00:34:53 +03:00
|
|
|
|
if (!($comment->getTarget() instanceof Post && $comment->getTarget()->getOwner() instanceof User && ($comment->getTarget()->getOwner()->isServiceAccount()))
|
2023-08-07 21:24:48 +03:00
|
|
|
|
if(!is_null($this->user)) $comment->toggleLike($this->user->identity);
|
2023-08-10 00:34:53 +03:00
|
|
|
|
|
2023-08-09 16:05:01 +03:00
|
|
|
|
if ($comment->getTarget() instanceof Post && $comment->getTarget()->getWallOwner()->isBanned())
|
|
|
|
|
$this->flashFail("err", tr("error"), tr("forbidden"));
|
2020-06-07 19:04:43 +03:00
|
|
|
|
|
|
|
|
|
if(!is_null($this->user)) $comment->toggleLike($this->user->identity);
|
|
|
|
|
|
2022-08-09 08:52:36 +03:00
|
|
|
|
$this->redirect($_SERVER["HTTP_REFERER"]);
|
2020-06-07 19:04:43 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function renderMakeComment(string $repo, int $eId): void
|
|
|
|
|
{
|
|
|
|
|
$this->assertUserLoggedIn();
|
2021-01-01 00:18:53 +03:00
|
|
|
|
$this->willExecuteWriteAction();
|
2020-06-07 19:04:43 +03:00
|
|
|
|
|
|
|
|
|
$repoClass = $this->models[$repo] ?? NULL;
|
|
|
|
|
if(!$repoClass) chandler_http_panic(400, "Bad Request", "Unexpected $repo.");
|
|
|
|
|
|
|
|
|
|
$repo = new $repoClass;
|
|
|
|
|
$entity = $repo->get($eId);
|
|
|
|
|
if(!$entity) $this->notFound();
|
2021-12-15 01:27:17 +03:00
|
|
|
|
|
|
|
|
|
if($entity instanceof Topic && $entity->isClosed())
|
|
|
|
|
$this->notFound();
|
2021-12-15 20:19:13 +03:00
|
|
|
|
|
2021-12-17 18:13:40 +03:00
|
|
|
|
if($entity instanceof Post && $entity->getTargetWall() < 0)
|
2021-12-15 20:19:13 +03:00
|
|
|
|
$club = (new Clubs)->get(abs($entity->getTargetWall()));
|
|
|
|
|
else if($entity instanceof Topic)
|
|
|
|
|
$club = $entity->getClub();
|
2023-05-27 09:31:20 +03:00
|
|
|
|
|
2023-08-10 00:34:53 +03:00
|
|
|
|
if ($entity instanceof Post && ($entity->getOwner()->isServiceAccount() || $entity->getWallOwner()->isBanned()))
|
2023-08-07 21:24:48 +03:00
|
|
|
|
$this->flashFail("err", tr("error"), tr("forbidden"));
|
|
|
|
|
|
2023-05-27 09:31:20 +03:00
|
|
|
|
if($_FILES["_vid_attachment"] && OPENVK_ROOT_CONF['openvk']['preferences']['videos']['disableUploading'])
|
|
|
|
|
$this->flashFail("err", tr("error"), "Video uploads are disabled by the system administrator.");
|
2020-06-07 19:04:43 +03:00
|
|
|
|
|
2021-12-04 22:46:26 +03:00
|
|
|
|
$flags = 0;
|
2021-12-15 20:19:13 +03:00
|
|
|
|
if($this->postParam("as_group") === "on" && !is_null($club) && $club->canBeModifiedBy($this->user->identity))
|
2021-12-04 22:46:26 +03:00
|
|
|
|
$flags |= 0b10000000;
|
|
|
|
|
|
2021-10-12 14:18:07 +03:00
|
|
|
|
$photo = NULL;
|
|
|
|
|
if($_FILES["_pic_attachment"]["error"] === UPLOAD_ERR_OK) {
|
|
|
|
|
try {
|
|
|
|
|
$photo = Photo::fastMake($this->user->id, $this->postParam("text"), $_FILES["_pic_attachment"]);
|
|
|
|
|
} catch(ISE $ex) {
|
|
|
|
|
$this->flashFail("err", "Не удалось опубликовать пост", "Файл изображения повреждён, слишком велик или одна сторона изображения в разы больше другой.");
|
2021-10-12 14:17:21 +03:00
|
|
|
|
}
|
2020-06-07 19:04:43 +03:00
|
|
|
|
}
|
|
|
|
|
|
2022-05-08 13:06:26 +03:00
|
|
|
|
# TODO move to trait
|
2021-10-13 23:23:01 +03:00
|
|
|
|
try {
|
|
|
|
|
$photo = NULL;
|
|
|
|
|
$video = 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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if($_FILES["_vid_attachment"]["error"] === UPLOAD_ERR_OK) {
|
2023-05-23 15:28:21 +03:00
|
|
|
|
$video = Video::fastMake($this->user->id, $_FILES["_vid_attachment"]["name"], $this->postParam("text"), $_FILES["_vid_attachment"]);
|
2021-10-13 23:23:01 +03:00
|
|
|
|
}
|
|
|
|
|
} catch(ISE $ex) {
|
|
|
|
|
$this->flashFail("err", "Не удалось опубликовать комментарий", "Файл медиаконтента повреждён или слишком велик.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(empty($this->postParam("text")) && !$photo && !$video)
|
|
|
|
|
$this->flashFail("err", "Не удалось опубликовать комментарий", "Комментарий пустой или слишком большой.");
|
2021-10-12 14:18:07 +03:00
|
|
|
|
|
2021-12-04 22:59:47 +03:00
|
|
|
|
try {
|
|
|
|
|
$comment = new Comment;
|
|
|
|
|
$comment->setOwner($this->user->id);
|
|
|
|
|
$comment->setModel(get_class($entity));
|
|
|
|
|
$comment->setTarget($entity->getId());
|
|
|
|
|
$comment->setContent($this->postParam("text"));
|
|
|
|
|
$comment->setCreated(time());
|
|
|
|
|
$comment->setFlags($flags);
|
|
|
|
|
$comment->save();
|
|
|
|
|
} catch (\LengthException $ex) {
|
|
|
|
|
$this->flashFail("err", "Не удалось опубликовать комментарий", "Комментарий слишком большой.");
|
|
|
|
|
}
|
2021-10-12 14:18:07 +03:00
|
|
|
|
|
|
|
|
|
if(!is_null($photo))
|
|
|
|
|
$comment->attach($photo);
|
|
|
|
|
|
2021-10-13 23:23:01 +03:00
|
|
|
|
if(!is_null($video))
|
2021-10-13 23:26:27 +03:00
|
|
|
|
$comment->attach($video);
|
2021-10-13 23:23:01 +03:00
|
|
|
|
|
2020-06-07 19:04:43 +03:00
|
|
|
|
if($entity->getOwner()->getId() !== $this->user->identity->getId())
|
|
|
|
|
if(($owner = $entity->getOwner()) instanceof User)
|
|
|
|
|
(new CommentNotification($owner, $comment, $entity, $this->user->identity))->emit();
|
2022-12-10 22:33:13 +03:00
|
|
|
|
|
2022-12-11 17:26:12 +03:00
|
|
|
|
$excludeMentions = [$this->user->identity->getId()];
|
|
|
|
|
if(($owner = $entity->getOwner()) instanceof User)
|
|
|
|
|
$excludeMentions[] = $owner->getId();
|
|
|
|
|
|
|
|
|
|
$mentions = iterator_to_array($comment->resolveMentions($excludeMentions));
|
2022-12-10 22:33:13 +03:00
|
|
|
|
foreach($mentions as $mentionee)
|
|
|
|
|
if($mentionee instanceof User)
|
|
|
|
|
(new MentionNotification($mentionee, $entity, $comment->getOwner(), strip_tags($comment->getText())))->emit();
|
2020-06-07 19:04:43 +03:00
|
|
|
|
|
|
|
|
|
$this->flashFail("succ", "Комментарий добавлен", "Ваш комментарий появится на странице.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function renderDeleteComment(int $id): void
|
|
|
|
|
{
|
|
|
|
|
$this->assertUserLoggedIn();
|
2021-01-01 00:18:53 +03:00
|
|
|
|
$this->willExecuteWriteAction();
|
2020-06-07 19:04:43 +03:00
|
|
|
|
|
|
|
|
|
$comment = (new Comments)->get($id);
|
|
|
|
|
if(!$comment) $this->notFound();
|
2023-08-07 21:24:48 +03:00
|
|
|
|
if(!$comment->canBeDeletedBy($this->user->identity) || ($comment->getTarget() instanceof Post && $comment->getTarget()->getOwner() instanceof User && $comment->getTarget()->getOwner()->isServiceAccount()))
|
2021-12-12 22:47:39 +03:00
|
|
|
|
$this->throwError(403, "Forbidden", "У вас недостаточно прав чтобы редактировать этот ресурс.");
|
2023-08-09 16:05:01 +03:00
|
|
|
|
if ($comment->getTarget() instanceof Post && $comment->getTarget()->getWallOwner()->isBanned())
|
|
|
|
|
$this->flashFail("err", tr("error"), tr("forbidden"));
|
|
|
|
|
|
2020-06-07 19:04:43 +03:00
|
|
|
|
$comment->delete();
|
|
|
|
|
$this->flashFail(
|
|
|
|
|
"succ",
|
|
|
|
|
"Успешно",
|
2021-12-31 07:28:57 +03:00
|
|
|
|
"Этот комментарий больше не будет показыватся.<br/><a href='/al_comments/spam?$id'>Отметить как спам</a>?"
|
2020-06-07 19:04:43 +03:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|