From 97a176c261e0813abe9a3942ec71294f2d187eb6 Mon Sep 17 00:00:00 2001
From: lalka2018 <99399973+lalka2016@users.noreply.github.com>
Date: Thu, 14 Sep 2023 20:54:22 +0300
Subject: [PATCH] =?UTF-8?q?=D0=A0=D0=B5=D0=B4=D0=B0=D0=BA=D1=82=D0=B8?=
=?UTF-8?q?=D1=80=D0=BE=D0=B2=D0=B0=D0=BD=D0=B8=D0=B5=20=D0=BF=D0=BE=D1=81?=
=?UTF-8?q?=D1=82=D0=BE=D0=B2=20=D1=82=D0=BE=D0=BB=D1=8C=D0=BA=D0=BE=20?=
=?UTF-8?q?=D0=BF=D0=BE=D0=BA=D1=80=D1=83=D1=87=D0=B5=20(#979)?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
* Add editing posts
* Add checkboxes
* Add ctrl+enter + fix empty posts
* Fix funny bug
---
Web/Models/Entities/Comment.php | 8 ++
Web/Models/Entities/Post.php | 11 ++
Web/Presenters/WallPresenter.php | 61 +++++++++-
Web/Presenters/templates/Wall/Post.xml | 8 ++
.../templates/components/comment.xml | 12 +-
.../components/post/microblogpost.xml | 23 ++--
.../templates/components/post/oldpost.xml | 23 +++-
Web/routes.yml | 2 +
Web/static/css/main.css | 15 +++
Web/static/css/microblog.css | 14 +++
Web/static/img/edit.png | Bin 0 -> 571 bytes
Web/static/js/al_wall.js | 109 +++++++++++++++++-
locales/en.strings | 3 +
locales/ru.strings | 2 +
14 files changed, 270 insertions(+), 21 deletions(-)
create mode 100644 Web/static/img/edit.png
diff --git a/Web/Models/Entities/Comment.php b/Web/Models/Entities/Comment.php
index d813d6be..d64a2763 100644
--- a/Web/Models/Entities/Comment.php
+++ b/Web/Models/Entities/Comment.php
@@ -90,4 +90,12 @@ class Comment extends Post
{
return "/wall" . $this->getTarget()->getPrettyId() . "#_comment" . $this->getId();
}
+
+ function canBeEditedBy(?User $user = NULL): bool
+ {
+ if(!$user)
+ return false;
+
+ return $user->getId() == $this->getOwner(false)->getId();
+ }
}
diff --git a/Web/Models/Entities/Post.php b/Web/Models/Entities/Post.php
index 42941901..2d323a8e 100644
--- a/Web/Models/Entities/Post.php
+++ b/Web/Models/Entities/Post.php
@@ -245,6 +245,17 @@ class Post extends Postable
$this->unwire();
$this->save();
}
+
+ function canBeEditedBy(?User $user = NULL): bool
+ {
+ if(!$user)
+ return false;
+
+ if($this->isDeactivationMessage() || $this->isUpdateAvatarMessage())
+ return false;
+
+ return $user->getId() == $this->getOwner(false)->getId();
+ }
use Traits\TRichText;
}
diff --git a/Web/Presenters/WallPresenter.php b/Web/Presenters/WallPresenter.php
index 3e115ec7..09392bc3 100644
--- a/Web/Presenters/WallPresenter.php
+++ b/Web/Presenters/WallPresenter.php
@@ -3,7 +3,7 @@ namespace openvk\Web\Presenters;
use openvk\Web\Models\Exceptions\TooMuchOptionsException;
use openvk\Web\Models\Entities\{Poll, Post, Photo, Video, Club, User};
use openvk\Web\Models\Entities\Notifications\{MentionNotification, RepostNotification, WallPostNotification};
-use openvk\Web\Models\Repositories\{Posts, Users, Clubs, Albums, Notes};
+use openvk\Web\Models\Repositories\{Posts, Users, Clubs, Albums, Notes, Comments};
use Chandler\Database\DatabaseConnection;
use Nette\InvalidStateException as ISE;
use Bhaktaraz\RSSGenerator\Item;
@@ -498,4 +498,63 @@ final class WallPresenter extends OpenVKPresenter
# TODO localize message based on language and ?act=(un)pin
$this->flashFail("succ", tr("information_-1"), tr("changes_saved_comment"));
}
+
+ function renderEdit()
+ {
+ $this->assertUserLoggedIn();
+ $this->willExecuteWriteAction();
+
+ if($_SERVER["REQUEST_METHOD"] !== "POST")
+ $this->redirect("/id0");
+
+ if($this->postParam("type") == "post")
+ $post = $this->posts->get((int)$this->postParam("postid"));
+ else
+ $post = (new Comments)->get((int)$this->postParam("postid"));
+
+ if(!$post || $post->isDeleted())
+ $this->returnJson(["error" => "Invalid post"]);
+
+ if(!$post->canBeEditedBy($this->user->identity))
+ $this->returnJson(["error" => "Access denied"]);
+
+ $attachmentsCount = sizeof(iterator_to_array($post->getChildren()));
+
+ if(empty($this->postParam("newContent")) && $attachmentsCount < 1)
+ $this->returnJson(["error" => "Empty post"]);
+
+ $post->setEdited(time());
+
+ try {
+ $post->setContent($this->postParam("newContent"));
+ } catch(\LengthException $e) {
+ $this->returnJson(["error" => $e->getMessage()]);
+ }
+
+ if($this->postParam("type") === "post") {
+ $post->setNsfw($this->postParam("nsfw") == "true");
+ $flags = 0;
+
+ if($post->getTargetWall() < 0 && $post->getWallOwner()->canBeModifiedBy($this->user->identity)) {
+ if($this->postParam("fromgroup") == "true") {
+ $flags |= 0b10000000;
+ $post->setFlags($flags);
+ } else
+ $post->setFlags($flags);
+ }
+ }
+
+ $post->save(true);
+
+ $this->returnJson(["error" => "no",
+ "new_content" => $post->getText(),
+ "new_edited" => (string)$post->getEditTime(),
+ "nsfw" => $this->postParam("type") === "post" ? (int)$post->isExplicit() : 0,
+ "from_group" => $this->postParam("type") === "post" && $post->getTargetWall() < 0 ?
+ ((int)$post->isPostedOnBehalfOfGroup()) : "false",
+ "author" => [
+ "name" => $post->getOwner()->getCanonicalName(),
+ "avatar" => $post->getOwner()->getAvatarUrl()
+ ]]);
+ }
}
diff --git a/Web/Presenters/templates/Wall/Post.xml b/Web/Presenters/templates/Wall/Post.xml
index 575c7bba..8ac11bb7 100644
--- a/Web/Presenters/templates/Wall/Post.xml
+++ b/Web/Presenters/templates/Wall/Post.xml
@@ -34,6 +34,14 @@
{/if}
{_delete}
+
+ {_changes_history}
+
{_report}