From bd72eead725611d5cf8a5e8b5d0e940d50af7a5d Mon Sep 17 00:00:00 2001 From: Murtaza Sultani Date: Thu, 2 Oct 2025 13:16:37 +0200 Subject: Add post log and extract post log from admin tag --- .../6.2.2_add_forum_posting_logs_table.php | 21 ++++++++++++--------- .../JsonApi/Routes/Forum/DiscussionPostings.php | 2 +- lib/classes/JsonApi/Routes/Forum/PostingShow.php | 2 +- lib/classes/JsonApi/Routes/Forum/PostingStore.php | 2 +- lib/classes/JsonApi/Routes/Forum/PostingUpdate.php | 6 ++++-- lib/classes/JsonApi/Schemas/Forum/Posting.php | 12 ++++++------ lib/models/Forum/Posting.php | 22 +++++++--------------- resources/vue/apps/forum/discussions/Show.vue | 2 +- resources/vue/components/forum/posts/Post.vue | 8 ++++---- 9 files changed, 37 insertions(+), 40 deletions(-) diff --git a/db/migrations/6.2.2_add_forum_posting_logs_table.php b/db/migrations/6.2.2_add_forum_posting_logs_table.php index 14112b0..055e72e 100644 --- a/db/migrations/6.2.2_add_forum_posting_logs_table.php +++ b/db/migrations/6.2.2_add_forum_posting_logs_table.php @@ -5,21 +5,24 @@ final class AddForumPostingLogsTable extends Migration public function up() { \DBManager::get()->exec(" - CREATE TABLE IF NOT EXISTS `forum_posting_logs` ( - `id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT, - `posting_id` CHAR(32) COLLATE latin1_bin NOT NULL, - `user_id` CHAR(32) COLLATE latin1_bin NOT NULL, - `action` ENUM('create', 'edit', 'delete') NOT NULL DEFAULT 'create', - `mkdate` INT(11) UNSIGNED NOT NULL, - PRIMARY KEY (`id`) - ) + ALTER TABLE `forum_postings` + ADD COLUMN `editor_id` CHAR(32) COLLATE latin1_bin NOT NULL AFTER `user_id` + "); + + \DBManager::get()->exec(" + UPDATE forum_postings + SET content = REGEXP_REPLACE( + content, + '', + '' + ) "); } public function down() { \DBManager::get()->exec(" - DROP TABLE IF EXISTS `forum_posting_logs` + ALTER TABLE `forum_postings` DROP COLUMN `editor_id` "); } } diff --git a/lib/classes/JsonApi/Routes/Forum/DiscussionPostings.php b/lib/classes/JsonApi/Routes/Forum/DiscussionPostings.php index 4cf39ae..acc3a5b 100644 --- a/lib/classes/JsonApi/Routes/Forum/DiscussionPostings.php +++ b/lib/classes/JsonApi/Routes/Forum/DiscussionPostings.php @@ -19,7 +19,7 @@ class DiscussionPostings extends JsonApiController \JsonApi\Schemas\Forum\Posting::REL_AUTHOR, \JsonApi\Schemas\Forum\Posting::REL_REACTIONS, \JsonApi\Schemas\Forum\Posting::REL_REACTIONS_USER, - \JsonApi\Schemas\Forum\Posting::REL_LOGS, + \JsonApi\Schemas\Forum\Posting::REL_EDITOR, \JsonApi\Schemas\Forum\Posting::REL_LOGS_USER ]; diff --git a/lib/classes/JsonApi/Routes/Forum/PostingShow.php b/lib/classes/JsonApi/Routes/Forum/PostingShow.php index 2e5e9e4..39f9f7c 100644 --- a/lib/classes/JsonApi/Routes/Forum/PostingShow.php +++ b/lib/classes/JsonApi/Routes/Forum/PostingShow.php @@ -17,7 +17,7 @@ class PostingShow extends JsonApiController \JsonApi\Schemas\Forum\Posting::REL_OPENGRAPH_URLS, \JsonApi\Schemas\Forum\Posting::REL_REACTIONS, \JsonApi\Schemas\Forum\Posting::REL_REACTIONS_USER, - \JsonApi\Schemas\Forum\Posting::REL_LOGS, + \JsonApi\Schemas\Forum\Posting::REL_EDITOR, \JsonApi\Schemas\Forum\Posting::REL_LOGS_USER ]; diff --git a/lib/classes/JsonApi/Routes/Forum/PostingStore.php b/lib/classes/JsonApi/Routes/Forum/PostingStore.php index 0e28bdf..cc3876f 100644 --- a/lib/classes/JsonApi/Routes/Forum/PostingStore.php +++ b/lib/classes/JsonApi/Routes/Forum/PostingStore.php @@ -25,7 +25,7 @@ class PostingStore extends JsonApiController \JsonApi\Schemas\Forum\Posting::REL_AUTHOR, \JsonApi\Schemas\Forum\Posting::REL_REACTIONS, \JsonApi\Schemas\Forum\Posting::REL_REACTIONS_USER, - \JsonApi\Schemas\Forum\Posting::REL_LOGS, + \JsonApi\Schemas\Forum\Posting::REL_EDITOR, \JsonApi\Schemas\Forum\Posting::REL_LOGS_USER ]; diff --git a/lib/classes/JsonApi/Routes/Forum/PostingUpdate.php b/lib/classes/JsonApi/Routes/Forum/PostingUpdate.php index 75864b2..068f840 100644 --- a/lib/classes/JsonApi/Routes/Forum/PostingUpdate.php +++ b/lib/classes/JsonApi/Routes/Forum/PostingUpdate.php @@ -21,7 +21,7 @@ class PostingUpdate extends JsonApiController \JsonApi\Schemas\Forum\Posting::REL_AUTHOR, \JsonApi\Schemas\Forum\Posting::REL_REACTIONS, \JsonApi\Schemas\Forum\Posting::REL_REACTIONS_USER, - \JsonApi\Schemas\Forum\Posting::REL_LOGS, + \JsonApi\Schemas\Forum\Posting::REL_EDITOR, \JsonApi\Schemas\Forum\Posting::REL_LOGS_USER ]; @@ -32,8 +32,9 @@ class PostingUpdate extends JsonApiController throw new RecordNotFoundException(); } + $auth = $this->getUser($request); if ( - !Authority::canEditPost($this->getUser($request), $posting, (bool) $posting->discussion->closed_at) + !Authority::canEditPost($auth, $posting, (bool) $posting->discussion->closed_at) ) { throw new AuthorizationFailedException(); } @@ -41,6 +42,7 @@ class PostingUpdate extends JsonApiController $json = $this->validate($request); $posting->content = Markup::purifyHtml(Markup::markAsHtml(self::arrayGet($json, 'data.attributes.content'))); $posting->anonymous = (self::arrayGet($json, 'data.attributes.anonymous') && \Config::get()->FORUM_ANONYMOUS_POSTINGS); + $posting->editor_id = $auth->user_id; $posting->store(); return $this->getCreatedResponse($posting); diff --git a/lib/classes/JsonApi/Schemas/Forum/Posting.php b/lib/classes/JsonApi/Schemas/Forum/Posting.php index 5dac675..97cdf7d 100644 --- a/lib/classes/JsonApi/Schemas/Forum/Posting.php +++ b/lib/classes/JsonApi/Schemas/Forum/Posting.php @@ -16,7 +16,7 @@ class Posting extends SchemaProvider const REL_REACTIONS = 'reactions'; const REL_REACTIONS_USER = 'reactions.user'; const REL_OPENGRAPH_URLS = 'opengraph-urls'; - const REL_LOGS = 'logs'; + const REL_EDITOR = 'editor'; const REL_LOGS_USER = 'logs.user'; /** @@ -76,7 +76,7 @@ class Posting extends SchemaProvider $relationships = $this->addDiscussionRelationship($relationships, $resource, $this->shouldInclude($context, self::REL_DISCUSSION)); $relationships = $this->addPostingRelationship($relationships, $resource, $this->shouldInclude($context, self::REL_POSTING)); $relationships = $this->addReactionsRelationship($relationships, $resource, $this->shouldInclude($context, self::REL_REACTIONS)); - $relationships = $this->addLogsRelationship($relationships, $resource, $this->shouldInclude($context, self::REL_LOGS)); + $relationships = $this->addEditorRelationship($relationships, $resource, $this->shouldInclude($context, self::REL_EDITOR)); return $relationships; } @@ -141,14 +141,14 @@ class Posting extends SchemaProvider return $relationships; } - private function addLogsRelationship(array $relationships, \Forum\Posting $posting, bool $withLogs = false) + private function addEditorRelationship(array $relationships, \Forum\Posting $posting, bool $withEditor = false) { - if ($withLogs) { - $relationships[self::REL_LOGS] = [ + if ($withEditor) { + $relationships[self::REL_EDITOR] = [ self::RELATIONSHIP_LINKS => [ Link::RELATED => $this->getRelationshipRelatedLink($posting, self::REL_REACTIONS) ], - self::RELATIONSHIP_DATA => $posting->logs + self::RELATIONSHIP_DATA => $posting->editor ]; } diff --git a/lib/models/Forum/Posting.php b/lib/models/Forum/Posting.php index f4a5d75..9d33a88 100644 --- a/lib/models/Forum/Posting.php +++ b/lib/models/Forum/Posting.php @@ -20,6 +20,7 @@ use Forum\DTO\Member as MemberDTO; * @property Posting $posting * @property PostingReaction[] $reactions * @property User $user + * @property User $editor * @property MemberDTO $author */ class Posting extends SimpleORMap @@ -46,6 +47,12 @@ class Posting extends SimpleORMap 'assoc_foreign_key' => 'user_id' ]; + $config['belongs_to']['editor'] = [ + 'class_name' => User::class, + 'foreign_key' => 'editor_id', + 'assoc_foreign_key' => 'user_id' + ]; + $config['has_many']['reactions'] = [ 'class_name' => PostingReaction::class, 'foreign_key' => 'posting_id', @@ -61,7 +68,6 @@ class Posting extends SimpleORMap $config['additional_fields']['author']['get'] = 'getAuthor'; $config['registered_callbacks']['after_create'][] = 'onCreate'; - $config['registered_callbacks']['after_update'][] = 'onUpdate'; $config['registered_callbacks']['after_delete'][] = 'onDelete'; parent::configure($config); @@ -135,22 +141,8 @@ class Posting extends SimpleORMap $postingNotification->notifySubscribers(); } - public function onUpdate(): void - { - PostingLog::create([ - 'posting_id' => $this->posting_id, - 'user_id' => User::findCurrent()->user_id, - 'action' => 'edit' - ]); - } - public function onDelete(): void { PostingReaction::deleteBySQL("posting_id = ?", [$this->posting_id]); - PostingLog::create([ - 'posting_id' => $this->posting_id, - 'user_id' => User::findCurrent()->user_id, - 'action' => 'delete' - ]); } } diff --git a/resources/vue/apps/forum/discussions/Show.vue b/resources/vue/apps/forum/discussions/Show.vue index 222e451..a563912 100644 --- a/resources/vue/apps/forum/discussions/Show.vue +++ b/resources/vue/apps/forum/discussions/Show.vue @@ -90,7 +90,7 @@ const fetchPostings = async () => { `forum-discussions/${props.discussion.discussion_id}/postings`, { data: { - include: 'author,opengraph-urls,posting,reactions,reactions.user,logs,logs.user&fields[users]=id,username,formatted-name', + include: 'author,opengraph-urls,posting,reactions,reactions.user,editor&fields[users]=id,username,formatted-name', page: { offset } } } diff --git a/resources/vue/components/forum/posts/Post.vue b/resources/vue/components/forum/posts/Post.vue index 6528326..0317bce 100644 --- a/resources/vue/components/forum/posts/Post.vue +++ b/resources/vue/components/forum/posts/Post.vue @@ -43,11 +43,11 @@ const showPostEditForm = ref(false); const showPostCreateForm = ref(false); const postRecentLog = computed(() => { - if (props.post.logs.length) { + if (props.post.editor) { return { - date: props.post.logs[0].mkdate, - author: props.post.logs[0].user.formatted_name, - username: props.post.logs[0].user.username, + date: props.post.chdate, + author: props.post.editor.formatted_name, + username: props.post.editor.username, } } else if (props.post.meta.log.chdate) { return { -- cgit v1.0