Skip to content

Commit 6d18d9d

Browse files
committed
Umstellung auf RPC API und Button in Beitrag bearbeiten geschoben
1 parent dd2d109 commit 6d18d9d

File tree

13 files changed

+244
-170
lines changed

13 files changed

+244
-170
lines changed

fileDelete.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<data xmlns="http://www.woltlab.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.woltlab.com http://www.woltlab.com/XSD/6.0/fileDelete.xsd">
3+
<delete>
4+
<file application="wbb">lib/action/ChangeAuthorAction.class.php</file>
5+
</delete>
6+
</data>

files_wbb/lib/action/ChangeAuthorAction.class.php

Lines changed: 0 additions & 138 deletions
This file was deleted.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace wbb\system\endpoint\controller\hanashi\forum\posts;
4+
5+
use Override;
6+
use Psr\Http\Message\ResponseInterface;
7+
use Psr\Http\Message\ServerRequestInterface;
8+
use wbb\data\post\Post;
9+
use wbb\system\post\ChangeAuthorHandler;
10+
use wcf\http\Helper;
11+
use wcf\system\endpoint\GetRequest;
12+
use wcf\system\endpoint\IController;
13+
use wcf\system\exception\PermissionDeniedException;
14+
use wcf\system\WCF;
15+
16+
#[GetRequest('/hanashi/forum/posts/{id:\d+}/change-author')]
17+
final class GetChangeAuthor implements IController
18+
{
19+
#[Override]
20+
public function __invoke(ServerRequestInterface $request, array $variables): ResponseInterface
21+
{
22+
$post = Helper::fetchObjectFromRequestParameter($variables['id'], Post::class);
23+
$thread = $post->getThread();
24+
if (!$thread->canEditPost($post) || !WCF::getSession()->getPermission('mod.board.canChangeAuthor')) {
25+
throw new PermissionDeniedException();
26+
}
27+
28+
$form = ChangeAuthorHandler::getForm($post);
29+
30+
return $form->toResponse();
31+
}
32+
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
3+
namespace wbb\system\endpoint\controller\hanashi\forum\posts;
4+
5+
use Laminas\Diactoros\Response\JsonResponse;
6+
use Override;
7+
use Psr\Http\Message\ResponseInterface;
8+
use Psr\Http\Message\ServerRequestInterface;
9+
use wbb\data\post\Post;
10+
use wbb\data\post\PostAction;
11+
use wbb\data\post\PostEditor;
12+
use wbb\data\thread\Thread;
13+
use wbb\data\thread\ThreadAction;
14+
use wbb\system\log\modification\PostAuthorModificationLogHandler;
15+
use wbb\system\post\ChangeAuthorHandler;
16+
use wcf\data\user\User;
17+
use wcf\http\Helper;
18+
use wcf\system\endpoint\IController;
19+
use wcf\system\endpoint\PostRequest;
20+
use wcf\system\exception\IllegalLinkException;
21+
use wcf\system\exception\PermissionDeniedException;
22+
use wcf\system\WCF;
23+
24+
#[PostRequest('/hanashi/forum/posts/{id:\d+}/change-author')]
25+
final class PostChangeAuthor implements IController
26+
{
27+
#[Override]
28+
public function __invoke(ServerRequestInterface $request, array $variables): ResponseInterface
29+
{
30+
$post = Helper::fetchObjectFromRequestParameter($variables['id'], Post::class);
31+
$thread = $post->getThread();
32+
if (!$thread->canEditPost($post) || !WCF::getSession()->getPermission('mod.board.canChangeAuthor')) {
33+
throw new PermissionDeniedException();
34+
}
35+
36+
$form = ChangeAuthorHandler::getForm($post);
37+
38+
$response = $form->validateRequest($request);
39+
if ($response !== null) {
40+
return $response;
41+
}
42+
43+
$data = $form->getData();
44+
$userID = $data['data']['userID'];
45+
$oldUser = new User($post->userID);
46+
$user = new User($userID);
47+
if (!$user->userID) {
48+
throw new IllegalLinkException();
49+
}
50+
51+
$this->changePostOwner($post, $user);
52+
$this->changeThreadOwner($post, $thread, $user, $oldUser);
53+
54+
if ($user->userID != $oldUser->userID) {
55+
PostAuthorModificationLogHandler::getInstance()->changeAuthor($post);
56+
}
57+
58+
return new JsonResponse([]);
59+
}
60+
61+
private function changePostOwner(Post $post, User $user): void
62+
{
63+
$action = new PostAction([$post], 'update', [
64+
'data' => [
65+
'userID' => $user->userID,
66+
'username' => $user->username,
67+
],
68+
]);
69+
$action->executeAction();
70+
}
71+
72+
private function changeThreadOwner(Post $post, Thread $thread, User $user, User $oldUser): void
73+
{
74+
$threadData = [];
75+
if ($post->isFirstPost()) {
76+
$threadData = [
77+
'userID' => $user->userID,
78+
'username' => $user->username,
79+
];
80+
}
81+
if ($thread->lastPostID == $post->postID) {
82+
$threadData['lastPosterID'] = $user->userID;
83+
$threadData['lastPoster'] = $user->username;
84+
}
85+
if ($threadData !== []) {
86+
$action = new ThreadAction([$thread], 'update', [
87+
'data' => $threadData,
88+
]);
89+
$action->executeAction();
90+
}
91+
92+
if ($oldUser->userID) {
93+
PostEditor::updatePostCounter([$oldUser->userID => -1]);
94+
}
95+
PostEditor::updatePostCounter([$user->userID => 1]);
96+
}
97+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace wbb\system\post;
4+
5+
use wbb\data\post\Post;
6+
use wcf\system\form\builder\field\user\UserFormField;
7+
use wcf\system\form\builder\Psr15DialogForm;
8+
use wcf\system\WCF;
9+
10+
final class ChangeAuthorHandler
11+
{
12+
public static function getForm(Post $post): Psr15DialogForm
13+
{
14+
$form = new Psr15DialogForm(
15+
static::class,
16+
WCF::getLanguage()->get('wcf.message.changeAuthor')
17+
);
18+
19+
$form->appendChildren([
20+
UserFormField::create('userID')
21+
->label('wbb.thread.username')
22+
->required(),
23+
]);
24+
25+
if ($post->postID) {
26+
$form->updatedObject($post, empty($_POST));
27+
}
28+
29+
$form->build();
30+
31+
return $form;
32+
}
33+
}

files_wcf/js/Hanashi/Change/Author.js

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,31 @@
1-
define(["require", "exports", "tslib", "WoltLabSuite/Core/Component/Dialog", "WoltLabSuite/Core/Ui/Notification", "WoltLabSuite/Core/Language"], function (require, exports, tslib_1, Dialog_1, UiNotification, Language) {
1+
define(["require", "exports", "tslib", "WoltLabSuite/Core/Component/Dialog", "WoltLabSuite/Core/Event/Handler", "WoltLabSuite/Core/Ui/Notification", "WoltLabSuite/Core/Language"], function (require, exports, tslib_1, Dialog_1, EventHandler, UiNotification, Language) {
22
"use strict";
33
Object.defineProperty(exports, "__esModule", { value: true });
44
exports.ChangeAuthor = void 0;
5+
EventHandler = tslib_1.__importStar(EventHandler);
56
UiNotification = tslib_1.__importStar(UiNotification);
67
Language = tslib_1.__importStar(Language);
78
class ChangeAuthor {
89
constructor() {
9-
for (const button of document.querySelectorAll(".jsChangeAuthor")) {
10-
button.addEventListener("click", (event) => {
11-
void this.#changeAuthorClicked(event.target);
10+
EventHandler.add("com.woltlab.wcf.inlineEditor", "dropdownInit_wbbPost", (data) => {
11+
data.items.push({
12+
item: "changeAuthor",
13+
label: "wcf.message.changeAuthor",
1214
});
13-
}
15+
});
16+
EventHandler.add("com.woltlab.wcf.inlineEditor", "dropdownItemClick_wbbPost", (data) => {
17+
if (data.item != "changeAuthor") {
18+
return;
19+
}
20+
data.cancel = true;
21+
const postID = parseInt(data.element.dataset.objectId ?? "0");
22+
void this.#changeAuthor(postID);
23+
});
1424
}
15-
async #changeAuthorClicked(button) {
16-
const { ok } = await (0, Dialog_1.dialogFactory)().usingFormBuilder().fromEndpoint(button.dataset.endpoint);
25+
async #changeAuthor(postID) {
26+
const { ok } = await (0, Dialog_1.dialogFactory)()
27+
.usingFormBuilder()
28+
.fromEndpoint(`${window.WSC_RPC_API_URL}hanashi/forum/posts/${postID}/change-author`);
1729
if (ok) {
1830
UiNotification.show(Language.getPhrase("wcf.message.changeAuthor.success"), function () {
1931
window.location.reload();
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
use wbb\system\endpoint\controller\hanashi\forum\posts\GetChangeAuthor;
4+
use wbb\system\endpoint\controller\hanashi\forum\posts\PostChangeAuthor;
5+
use wcf\event\endpoint\ControllerCollecting;
6+
use wcf\system\event\EventHandler;
7+
8+
return static function (): void {
9+
EventHandler::getInstance()->register(
10+
ControllerCollecting::class,
11+
static function (ControllerCollecting $event) {
12+
$event->register(new GetChangeAuthor());
13+
$event->register(new PostChangeAuthor());
14+
}
15+
);
16+
};

package.xml

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
<packagename language="de">Beitrags-Autor bearbeiten</packagename>
77
<packagedescription>This plugin allows you to change the author of a post in the forum.</packagedescription>
88
<packagedescription language="de">Mit diesem Plugin lässt sich der Autor eines Beitrages im Forum ändern.</packagedescription>
9-
<version>1.0.1</version>
10-
<date>2025-07-08</date>
9+
<version>1.0.2</version>
10+
<date>2025-07-12</date>
1111
</packageinformation>
1212
<authorinformation>
1313
<author>Hanashi Development</author>
@@ -30,10 +30,12 @@
3030
<instruction type="userGroupOption"/>
3131
<instruction type="objectType"/>
3232
</instructions>
33-
<instructions type="update" fromversion="1.0.0">
33+
<instructions type="update" fromversion="1.0.1">
34+
<instruction type="file" application="wcf">files_wcf.tar</instruction>
3435
<instruction type="file" application="wbb">files_wbb.tar</instruction>
3536
<instruction type="template" application="wbb">templates_wbb.tar</instruction>
36-
<instruction type="language"/>
37-
<instruction type="objectType"/>
37+
<instruction type="templateListener"/>
38+
<instruction type="fileDelete"/>
39+
<instruction type="templateDelete"/>
3840
</instructions>
3941
</package>

0 commit comments

Comments
 (0)