From 1518e424ca5b73f6af9281086cafa2e509ed71eb Mon Sep 17 00:00:00 2001
From: lalka2016 <99399973+lalka2016@users.noreply.github.com>
Date: Wed, 6 Sep 2023 22:49:20 +0300
Subject: [PATCH] Add editing posts
---
Web/Models/Entities/Comment.php | 8 +++
Web/Models/Entities/Post.php | 11 +++
Web/Presenters/WallPresenter.php | 29 +++++++-
Web/Presenters/templates/Wall/Post.xml | 8 +++
.../templates/components/comment.xml | 12 ++--
.../components/post/microblogpost.xml | 16 +++--
.../templates/components/post/oldpost.xml | 16 +++--
Web/routes.yml | 2 +
Web/static/css/main.css | 16 ++++-
Web/static/css/microblog.css | 14 ++++
Web/static/img/edit.png | Bin 0 -> 571 bytes
Web/static/js/al_wall.js | 66 +++++++++++++++++-
locales/en.strings | 3 +
locales/ru.strings | 2 +
14 files changed, 184 insertions(+), 19 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..c368c40f 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,31 @@ 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"]);
+
+ $post->setEdited(time());
+ $post->setContent($this->postParam("newContent"));
+ $post->save(true);
+
+ $this->returnJson(["error" => "no", "new_content" => $post->getText(), "new_edited" => (string)$post->getEditTime()]);
+ }
}
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}