diff --git a/composer.json b/composer.json index b6edb31..9f18ae0 100644 --- a/composer.json +++ b/composer.json @@ -7,7 +7,8 @@ "type": "flarum-extension", "license": "LPL-1.02", "require": { - "flarum/core": "^1.2.0" + "flarum/core": "^1.2.0", + "antoinefr/flarum-ext-money": "^1.3" }, "authors": [ { diff --git a/extend.php b/extend.php index fa17b8b..c2dfb67 100644 --- a/extend.php +++ b/extend.php @@ -10,9 +10,31 @@ */ use Flarum\Extend; -use Mattoid\MoneyHistory\Middleware\MoneyMiddleware; +use Flarum\Post\Event\Posted; +use Flarum\Post\Event\Restored as PostRestored; +use Flarum\Post\Event\Hidden as PostHidden; +use Flarum\Post\Event\Deleted as PostDeleted; +use Flarum\Discussion\Event\Started; +use Flarum\Discussion\Event\Restored as DiscussionRestored; +use Flarum\Discussion\Event\Hidden as DiscussionHidden; +use Flarum\Discussion\Event\Deleted as DiscussionDeleted; +use Flarum\User\Event\Saving; +use Flarum\Likes\Event\PostWasLiked; +use Flarum\Likes\Event\PostWasUnliked; +use Mattoid\MoneyHistory\Listeners\DiscussionWasDeletedHistory; +use Mattoid\MoneyHistory\Listeners\DiscussionWasHiddenHistory; +use Mattoid\MoneyHistory\Listeners\DiscussionWasRestoredHistory; +use Mattoid\MoneyHistory\Listeners\DiscussionWasStartedHistory; +use Mattoid\MoneyHistory\Listeners\PostWasDeletedHistory; +use Mattoid\MoneyHistory\Listeners\PostWasHiddenHistory; +use Mattoid\MoneyHistory\Listeners\PostWasLikedHistory; +use Mattoid\MoneyHistory\Listeners\PostWasPostedHistory; +use Mattoid\MoneyHistory\Listeners\PostWasRestoredHistory; +use Mattoid\MoneyHistory\Listeners\PostWasUnlikedHistory; +use Mattoid\MoneyHistory\Listeners\UserWillBeSavedHistory; +use Mattoid\MoneyHistory\Middleware\HistoryMiddleware; -return [ +$extend = [ (new Extend\Frontend('forum')) ->js(__DIR__.'/js/dist/forum.js') ->css(__DIR__.'/less/forum.less'), @@ -21,5 +43,26 @@ ->css(__DIR__.'/less/admin.less'), new Extend\Locales(__DIR__.'/locale'), - (new Extend\Middleware("api"))->add(MoneyMiddleware::class), + (new Extend\Middleware("api"))->add(HistoryMiddleware::class), + + (new Extend\Event()) + ->listen(Posted::class, PostWasPostedHistory::class) + ->listen(PostRestored::class, PostWasRestoredHistory::class) + ->listen(PostHidden::class, PostWasHiddenHistory::class) + ->listen(PostDeleted::class, PostWasDeletedHistory::class) + ->listen(Started::class, DiscussionWasStartedHistory::class) + ->listen(DiscussionRestored::class, DiscussionWasRestoredHistory::class) + ->listen(DiscussionHidden::class, DiscussionWasHiddenHistory::class) + ->listen(DiscussionDeleted::class, DiscussionWasDeletedHistory::class) + ->listen(Saving::class, UserWillBeSavedHistory::class), ]; + +if (class_exists('Flarum\Likes\Event\PostWasLiked')) { + $extend[] = + (new Extend\Event()) + ->listen(PostWasLiked::class, PostWasLikedHistory::class) + ->listen(PostWasUnliked::class, PostWasUnlikedHistory::class) + ; +} + +return $extend; diff --git a/src/Listeners/DiscussionWasDeletedHistory.php b/src/Listeners/DiscussionWasDeletedHistory.php new file mode 100644 index 0000000..c9cdd40 --- /dev/null +++ b/src/Listeners/DiscussionWasDeletedHistory.php @@ -0,0 +1,20 @@ +notifications = $notifications; + } + + public function handle(PostRestored $event) { + + } +} diff --git a/src/Listeners/DiscussionWasHiddenHistory.php b/src/Listeners/DiscussionWasHiddenHistory.php new file mode 100644 index 0000000..b8fe95b --- /dev/null +++ b/src/Listeners/DiscussionWasHiddenHistory.php @@ -0,0 +1,20 @@ +notifications = $notifications; + } + + public function handle(PostRestored $event) { + + } +} diff --git a/src/Listeners/DiscussionWasRestoredHistory.php b/src/Listeners/DiscussionWasRestoredHistory.php new file mode 100644 index 0000000..982e48f --- /dev/null +++ b/src/Listeners/DiscussionWasRestoredHistory.php @@ -0,0 +1,20 @@ +notifications = $notifications; + } + + public function handle(PostRestored $event) { + + } +} diff --git a/src/Listeners/DiscussionWasStartedHistory.php b/src/Listeners/DiscussionWasStartedHistory.php new file mode 100644 index 0000000..2964bbe --- /dev/null +++ b/src/Listeners/DiscussionWasStartedHistory.php @@ -0,0 +1,20 @@ +notifications = $notifications; + } + + public function handle(PostRestored $event) { + + } +} diff --git a/src/Listeners/PostWasDeletedHistory.php b/src/Listeners/PostWasDeletedHistory.php new file mode 100644 index 0000000..2ff4322 --- /dev/null +++ b/src/Listeners/PostWasDeletedHistory.php @@ -0,0 +1,20 @@ +notifications = $notifications; + } + + public function handle(PostRestored $event) { + + } +} diff --git a/src/Listeners/PostWasHiddenHistory.php b/src/Listeners/PostWasHiddenHistory.php new file mode 100644 index 0000000..4580436 --- /dev/null +++ b/src/Listeners/PostWasHiddenHistory.php @@ -0,0 +1,19 @@ +notifications = $notifications; + } + + public function handle(PostHidden $event) { + + } +} diff --git a/src/Listeners/PostWasLikedHistory.php b/src/Listeners/PostWasLikedHistory.php new file mode 100644 index 0000000..27476cf --- /dev/null +++ b/src/Listeners/PostWasLikedHistory.php @@ -0,0 +1,20 @@ +notifications = $notifications; + } + + public function handle(PostRestored $event) { + + } +} diff --git a/src/Listeners/PostWasPostedHistory.php b/src/Listeners/PostWasPostedHistory.php new file mode 100644 index 0000000..1af80da --- /dev/null +++ b/src/Listeners/PostWasPostedHistory.php @@ -0,0 +1,20 @@ +notifications = $notifications; + } + + public function handle(Posted $event) { + + } +} diff --git a/src/Listeners/PostWasRestoredHistory.php b/src/Listeners/PostWasRestoredHistory.php new file mode 100644 index 0000000..ca15012 --- /dev/null +++ b/src/Listeners/PostWasRestoredHistory.php @@ -0,0 +1,20 @@ +notifications = $notifications; + } + + public function handle(PostRestored $event) { + + } +} diff --git a/src/Listeners/PostWasUnlikedHistory.php b/src/Listeners/PostWasUnlikedHistory.php new file mode 100644 index 0000000..af715ae --- /dev/null +++ b/src/Listeners/PostWasUnlikedHistory.php @@ -0,0 +1,20 @@ +notifications = $notifications; + } + + public function handle(PostRestored $event) { + + } +} diff --git a/src/Listeners/UserWillBeSavedHistory.php b/src/Listeners/UserWillBeSavedHistory.php new file mode 100644 index 0000000..39d9bb5 --- /dev/null +++ b/src/Listeners/UserWillBeSavedHistory.php @@ -0,0 +1,20 @@ +notifications = $notifications; + } + + public function handle(PostRestored $event) { + + } +} diff --git a/src/Middleware/HistoryMiddleware.php b/src/Middleware/HistoryMiddleware.php new file mode 100644 index 0000000..9917ea0 --- /dev/null +++ b/src/Middleware/HistoryMiddleware.php @@ -0,0 +1,57 @@ +settings = $settings; + } + + public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface + { + $typesAllowed = []; + $actor = RequestUtil::getActor($request); + $userId = Arr::get($actor, 'id'); + + if ($this->settings->get('mattoid-money-history.request-type-get')) { + $typesAllowed[] = "GET"; + } + if ($this->settings->get('mattoid-money-history.request-type-post')) { + $typesAllowed[] = "POST"; + } + if ($this->settings->get('mattoid-money-history.request-type-put')) { + $typesAllowed[] = "PUT"; + $typesAllowed[] = "PATCH"; + } + if ($this->settings->get('mattoid-money-history.request-type-delete')) { + $typesAllowed[] = "DELETE"; + } + + $response = $handler->handle($request); + + if (!in_array($request->getMethod(), $typesAllowed)) { + return $response; + } + + $operateLog = new UserOperateLog(); + $operateLog->user_id = $userId; + $operateLog->method = $request->getMethod(); + $operateLog->uri = $request->getUri(); + $operateLog->request = json_encode($request->getParsedBody()); + $operateLog->response = $response->getBody(); + $operateLog->save(); + + return $response; + } +} diff --git a/src/Middleware/MoneyMiddleware.php b/src/Middleware/MoneyMiddleware.php index e0727e3..a06c04e 100644 --- a/src/Middleware/MoneyMiddleware.php +++ b/src/Middleware/MoneyMiddleware.php @@ -5,7 +5,6 @@ use Flarum\Http\RequestUtil; use Flarum\Settings\SettingsRepositoryInterface; use Illuminate\Support\Arr; -use Mattoid\MoneyHistory\model\UserOperateLog; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\MiddlewareInterface; @@ -24,20 +23,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface $typesAllowed = []; $actor = RequestUtil::getActor($request); $userId = Arr::get($actor, 'id'); - - if ($this->settings->get('mattoid-money-history.request-type-get')) { - $typesAllowed[] = "GET"; - } - if ($this->settings->get('mattoid-money-history.request-type-post')) { - $typesAllowed[] = "POST"; - } - if ($this->settings->get('mattoid-money-history.request-type-put')) { - $typesAllowed[] = "PUT"; - $typesAllowed[] = "PATCH"; - } - if ($this->settings->get('mattoid-money-history.request-type-delete')) { - $typesAllowed[] = "DELETE"; - } + $money = Arr::get($actor, 'money'); $response = $handler->handle($request); @@ -45,13 +31,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface return $response; } - $operateLog = new UserOperateLog(); - $operateLog->user_id = $userId; - $operateLog->method = $request->getMethod(); - $operateLog->uri = $request->getUri(); - $operateLog->request = json_encode($request->getParsedBody()); - $operateLog->response = $response->getBody(); - $operateLog->save(); + return $response; }