From a1d51b691fdd098084e8f93448fd5d1e45843571 Mon Sep 17 00:00:00 2001 From: liufei Date: Mon, 11 Dec 2023 18:07:21 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AE=B0=E5=BD=95=20=E9=87=91=E9=A2=9D?= =?UTF-8?q?=E6=B5=81=E8=BD=AC=E6=97=A5=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Listeners/DiscussionWasDeletedHistory.php | 24 +++++++++--- src/Listeners/DiscussionWasHiddenHistory.php | 26 +++++++++---- .../DiscussionWasRestoredHistory.php | 24 +++++++++--- src/Listeners/DiscussionWasStartedHistory.php | 21 +++++++--- src/Listeners/HistoryListeners.php | 27 +++++++++++++ src/Listeners/PostWasDeletedHistory.php | 26 ++++++++++--- src/Listeners/PostWasHiddenHistory.php | 23 +++++++++-- src/Listeners/PostWasLikedHistory.php | 22 +++++++---- src/Listeners/PostWasPostedHistory.php | 22 +++++++++-- src/Listeners/PostWasRestoredHistory.php | 23 +++++++++-- src/Listeners/PostWasUnlikedHistory.php | 21 +++++++--- src/Listeners/UserWillBeSavedHistory.php | 22 ++++++++--- src/Middleware/MoneyMiddleware.php | 38 ------------------- src/model/UserMoneyHistory.php | 38 +++++++++++++++++++ 14 files changed, 261 insertions(+), 96 deletions(-) create mode 100644 src/Listeners/HistoryListeners.php delete mode 100644 src/Middleware/MoneyMiddleware.php create mode 100644 src/model/UserMoneyHistory.php diff --git a/src/Listeners/DiscussionWasDeletedHistory.php b/src/Listeners/DiscussionWasDeletedHistory.php index c9cdd40..c630dbd 100644 --- a/src/Listeners/DiscussionWasDeletedHistory.php +++ b/src/Listeners/DiscussionWasDeletedHistory.php @@ -2,19 +2,31 @@ namespace Mattoid\MoneyHistory\Listeners; +use AntoineFr\Money\Listeners\AutoRemoveEnum; use Flarum\Notification\NotificationSyncer; -use Flarum\Post\Event\Restored as PostRestored; +use Flarum\Discussion\Event\Deleted as DiscussionDeleted; +use Flarum\Settings\SettingsRepositoryInterface; -class DiscussionWasDeletedHistory +class DiscussionWasDeletedHistory extends HistoryListeners { - private $sourceDesc = "帖子恢复"; + protected $source = "DISCUSSIONWASDELETED"; + protected $sourceDesc = ""; - public function __construct(NotificationSyncer $notifications) + private $settings; + private $autoremove; + + public function __construct(NotificationSyncer $notifications, SettingsRepositoryInterface $settings) { + $this->settings = $settings; $this->notifications = $notifications; - } - public function handle(PostRestored $event) { + $this->autoremove = (int)$this->settings->get('antoinefr-money.autoremove', 1); + } + public function handle(DiscussionDeleted $event) { + if ($this->autoremove == AutoRemoveEnum::DELETED) { + $money = (float)$this->settings->get('antoinefr-money.moneyfordiscussion', 0); + $this->exec($event->discussion->user, -$money); + } } } diff --git a/src/Listeners/DiscussionWasHiddenHistory.php b/src/Listeners/DiscussionWasHiddenHistory.php index b8fe95b..d482461 100644 --- a/src/Listeners/DiscussionWasHiddenHistory.php +++ b/src/Listeners/DiscussionWasHiddenHistory.php @@ -2,19 +2,31 @@ namespace Mattoid\MoneyHistory\Listeners; +use AntoineFr\Money\Listeners\AutoRemoveEnum; use Flarum\Notification\NotificationSyncer; -use Flarum\Post\Event\Restored as PostRestored; +use Flarum\Settings\SettingsRepositoryInterface; +use Flarum\Discussion\Event\Hidden as DiscussionHidden; -class DiscussionWasHiddenHistory +class DiscussionWasHiddenHistory extends HistoryListeners { - private $sourceDesc = "帖子恢复"; - public function __construct(NotificationSyncer $notifications) + protected $source = "DISCUSSIONWASHIDDEN"; + protected $sourceDesc = ""; + + private $settings; + private $autoremove; + + public function __construct(NotificationSyncer $notifications, SettingsRepositoryInterface $settings) { + $this->settings = $settings; $this->notifications = $notifications; - } - - public function handle(PostRestored $event) { + $this->autoremove = (int)$this->settings->get('antoinefr-money.autoremove', 1); + } + public function handle(DiscussionHidden $event) { + if ($this->autoremove == AutoRemoveEnum::HIDDEN) { + $money = (float)$this->settings->get('antoinefr-money.moneyfordiscussion', 0); + $this->exec($event->discussion->user, -$money); + } } } diff --git a/src/Listeners/DiscussionWasRestoredHistory.php b/src/Listeners/DiscussionWasRestoredHistory.php index 982e48f..3b2b273 100644 --- a/src/Listeners/DiscussionWasRestoredHistory.php +++ b/src/Listeners/DiscussionWasRestoredHistory.php @@ -2,19 +2,31 @@ namespace Mattoid\MoneyHistory\Listeners; +use AntoineFr\Money\Listeners\AutoRemoveEnum; use Flarum\Notification\NotificationSyncer; -use Flarum\Post\Event\Restored as PostRestored; +use Flarum\Settings\SettingsRepositoryInterface; +use Flarum\Discussion\Event\Restored as DiscussionRestored; -class DiscussionWasRestoredHistory +class DiscussionWasRestoredHistory extends HistoryListeners { - private $sourceDesc = "帖子恢复"; + protected $source = "DISCUSSIONWASRESTORED"; + protected $sourceDesc = ""; - public function __construct(NotificationSyncer $notifications) + private $settings; + private $autoremove; + + public function __construct(NotificationSyncer $notifications, SettingsRepositoryInterface $settings) { + $this->settings = $settings; $this->notifications = $notifications; - } - public function handle(PostRestored $event) { + $this->autoremove = (int)$this->settings->get('antoinefr-money.autoremove', 1); + } + public function handle(DiscussionRestored $event) { + if ($this->autoremove == AutoRemoveEnum::HIDDEN) { + $money = (float)$this->settings->get('antoinefr-money.moneyfordiscussion', 0); + $this->exec($event->discussion->user, $money); + } } } diff --git a/src/Listeners/DiscussionWasStartedHistory.php b/src/Listeners/DiscussionWasStartedHistory.php index 2964bbe..5ed0546 100644 --- a/src/Listeners/DiscussionWasStartedHistory.php +++ b/src/Listeners/DiscussionWasStartedHistory.php @@ -3,18 +3,27 @@ namespace Mattoid\MoneyHistory\Listeners; use Flarum\Notification\NotificationSyncer; -use Flarum\Post\Event\Restored as PostRestored; +use Flarum\Discussion\Event\Started; +use Flarum\Settings\SettingsRepositoryInterface; -class DiscussionWasStartedHistory +class DiscussionWasStartedHistory extends HistoryListeners { - private $sourceDesc = "帖子恢复"; + protected $source = "DISCUSSIONWASSTARTED"; + protected $sourceDesc = ""; - public function __construct(NotificationSyncer $notifications) + private $settings; + private $autoremove; + + public function __construct(NotificationSyncer $notifications, SettingsRepositoryInterface $settings) { + $this->settings = $settings; $this->notifications = $notifications; - } - public function handle(PostRestored $event) { + $this->autoremove = (int)$this->settings->get('antoinefr-money.autoremove', 1); + } + public function handle(Started $event) { + $money = (float)$this->settings->get('antoinefr-money.moneyfordiscussion', 0); + $this->exec($event->actor, $money); } } diff --git a/src/Listeners/HistoryListeners.php b/src/Listeners/HistoryListeners.php new file mode 100644 index 0000000..470df84 --- /dev/null +++ b/src/Listeners/HistoryListeners.php @@ -0,0 +1,27 @@ +user_id = $user->id; + $userMoneyHistory->type = $money > 0 ? "C" : "D"; + $userMoneyHistory->money = $money > 0 ? $money : -$money; + $userMoneyHistory->source = $this->source; + $userMoneyHistory->source_desc = $this->sourceDesc; + $userMoneyHistory->last_money = $user->money; + $userMoneyHistory->create_user_id = $user->id; + $userMoneyHistory->change_time = Date("Y-m-d H:i:s"); + $userMoneyHistory->save(); + } + +} diff --git a/src/Listeners/PostWasDeletedHistory.php b/src/Listeners/PostWasDeletedHistory.php index 2ff4322..8ac417a 100644 --- a/src/Listeners/PostWasDeletedHistory.php +++ b/src/Listeners/PostWasDeletedHistory.php @@ -2,19 +2,35 @@ namespace Mattoid\MoneyHistory\Listeners; +use AntoineFr\Money\Listeners\AutoRemoveEnum; use Flarum\Notification\NotificationSyncer; -use Flarum\Post\Event\Restored as PostRestored; +use Flarum\Post\Event\Deleted as PostDeleted; +use Flarum\Settings\SettingsRepositoryInterface; -class PostWasDeletedHistory +class PostWasDeletedHistory extends HistoryListeners { - private $sourceDesc = "帖子恢复"; + protected $source = "POSTWASDELETED"; + protected $sourceDesc = ""; - public function __construct(NotificationSyncer $notifications) + private $settings; + private $autoremove; + + public function __construct(NotificationSyncer $notifications, SettingsRepositoryInterface $settings) { + $this->settings = $settings; $this->notifications = $notifications; + + $this->autoremove = (int)$this->settings->get('antoinefr-money.autoremove', 1); } - public function handle(PostRestored $event) { + public function handle(PostDeleted $event) { + if ($this->autoremove == AutoRemoveEnum::DELETED && $event->post->type == 'comment') { + $minimumLength = (int)$this->settings->get('antoinefr-money.postminimumlength', 0); + if (strlen($event->post->content) >= $minimumLength) { + $money = (float)$this->settings->get('antoinefr-money.moneyforpost', 0); + $this->exec($event->post->user, -$money); + } + } } } diff --git a/src/Listeners/PostWasHiddenHistory.php b/src/Listeners/PostWasHiddenHistory.php index 4580436..fd4390b 100644 --- a/src/Listeners/PostWasHiddenHistory.php +++ b/src/Listeners/PostWasHiddenHistory.php @@ -2,18 +2,35 @@ namespace Mattoid\MoneyHistory\Listeners; +use AntoineFr\Money\Listeners\AutoRemoveEnum; use Flarum\Notification\NotificationSyncer; use Flarum\Post\Event\Hidden as PostHidden; -class PostWasHiddenHistory +use Flarum\Settings\SettingsRepositoryInterface; + +class PostWasHiddenHistory extends HistoryListeners { - private $sourceDesc = "隐藏帖子"; + protected $source = "POSTWASHIDDEN"; + protected $sourceDesc = ""; + + private $settings; + private $autoremove; - public function __construct(NotificationSyncer $notifications) + public function __construct(NotificationSyncer $notifications, SettingsRepositoryInterface $settings) { + $this->settings = $settings; $this->notifications = $notifications; + + $this->autoremove = (int)$this->settings->get('antoinefr-money.autoremove', 1); } public function handle(PostHidden $event) { + if ($this->autoremove == AutoRemoveEnum::HIDDEN) { + $minimumLength = (int)$this->settings->get('antoinefr-money.postminimumlength', 0); + if (strlen($event->post->content) >= $minimumLength) { + $money = (float)$this->settings->get('antoinefr-money.moneyforpost', 0); + $this->exec($event->post->user, -$money); + } + } } } diff --git a/src/Listeners/PostWasLikedHistory.php b/src/Listeners/PostWasLikedHistory.php index 27476cf..6716431 100644 --- a/src/Listeners/PostWasLikedHistory.php +++ b/src/Listeners/PostWasLikedHistory.php @@ -2,19 +2,27 @@ namespace Mattoid\MoneyHistory\Listeners; +use Flarum\Likes\Event\PostWasLiked; use Flarum\Notification\NotificationSyncer; -use Flarum\Post\Event\Restored as PostRestored; +use Flarum\Settings\SettingsRepositoryInterface; -class PostWasLikedHistory +class PostWasLikedHistory extends HistoryListeners { - private $sourceDesc = "帖子恢复"; + protected $source = "POSTWASLIKED"; + protected $sourceDesc = ""; - public function __construct(NotificationSyncer $notifications) + private $settings; + private $autoremove; + + public function __construct(NotificationSyncer $notifications, SettingsRepositoryInterface $settings) { + $this->settings = $settings; $this->notifications = $notifications; - } - - public function handle(PostRestored $event) { + $this->autoremove = (int)$this->settings->get('antoinefr-money.autoremove', 1); + } + public function handle(PostWasLiked $event) { + $money = (float)$this->settings->get('antoinefr-money.moneyforlike', 0); + $this->exec($event->post->user, $money); } } diff --git a/src/Listeners/PostWasPostedHistory.php b/src/Listeners/PostWasPostedHistory.php index 1af80da..92019af 100644 --- a/src/Listeners/PostWasPostedHistory.php +++ b/src/Listeners/PostWasPostedHistory.php @@ -2,19 +2,35 @@ namespace Mattoid\MoneyHistory\Listeners; +use Flarum\Settings\SettingsRepositoryInterface; use Flarum\Notification\NotificationSyncer; use Flarum\Post\Event\Posted; -class PostWasPostedHistory +class PostWasPostedHistory extends HistoryListeners { - private $sourceDesc = "发帖奖励"; + protected $source = "POSTWASPOSTED"; + protected $sourceDesc = ""; - public function __construct(NotificationSyncer $notifications) + private $settings; + private $autoremove; + + public function __construct(NotificationSyncer $notifications, SettingsRepositoryInterface $settings) { + $this->settings = $settings; $this->notifications = $notifications; + + $this->autoremove = (int)$this->settings->get('antoinefr-money.autoremove', 1); } public function handle(Posted $event) { + if ($event->post['number'] > 1) { + $minimumLength = (int)$this->settings->get('antoinefr-money.postminimumlength', 0); + + if (strlen($event->post->content) >= $minimumLength) { + $money = (float)$this->settings->get('antoinefr-money.moneyforpost', 0); + $this->exec($event->actor, $money); + } + } } } diff --git a/src/Listeners/PostWasRestoredHistory.php b/src/Listeners/PostWasRestoredHistory.php index ca15012..d0be443 100644 --- a/src/Listeners/PostWasRestoredHistory.php +++ b/src/Listeners/PostWasRestoredHistory.php @@ -2,19 +2,34 @@ namespace Mattoid\MoneyHistory\Listeners; +use AntoineFr\Money\Listeners\AutoRemoveEnum; use Flarum\Notification\NotificationSyncer; use Flarum\Post\Event\Restored as PostRestored; +use Flarum\Settings\SettingsRepositoryInterface; -class PostWasRestoredHistory +class PostWasRestoredHistory extends HistoryListeners { - private $sourceDesc = "帖子恢复"; + protected $source = "POSTWASRESTORED"; + protected $sourceDesc = ""; - public function __construct(NotificationSyncer $notifications) + private $settings; + private $autoremove; + + public function __construct(NotificationSyncer $notifications, SettingsRepositoryInterface $settings) { + $this->settings = $settings; $this->notifications = $notifications; - } + $this->autoremove = (int)$this->settings->get('antoinefr-money.autoremove', 1); + } public function handle(PostRestored $event) { + if ($this->autoremove == AutoRemoveEnum::HIDDEN) { + $minimumLength = (int)$this->settings->get('antoinefr-money.postminimumlength', 0); + if (strlen($event->post->content) >= $minimumLength) { + $money = (float)$this->settings->get('antoinefr-money.moneyforpost', 0); + $this->exec($event->post->user, $money); + } + } } } diff --git a/src/Listeners/PostWasUnlikedHistory.php b/src/Listeners/PostWasUnlikedHistory.php index af715ae..b7c491c 100644 --- a/src/Listeners/PostWasUnlikedHistory.php +++ b/src/Listeners/PostWasUnlikedHistory.php @@ -2,19 +2,28 @@ namespace Mattoid\MoneyHistory\Listeners; +use Flarum\Likes\Event\PostWasUnliked; use Flarum\Notification\NotificationSyncer; -use Flarum\Post\Event\Restored as PostRestored; +use Flarum\Settings\SettingsRepositoryInterface; -class PostWasUnlikedHistory +class PostWasUnlikedHistory extends HistoryListeners { - private $sourceDesc = "帖子恢复"; + protected $source = "POSTWASUNLIKED"; + protected $sourceDesc = ""; - public function __construct(NotificationSyncer $notifications) + private $settings; + private $autoremove; + + public function __construct(NotificationSyncer $notifications, SettingsRepositoryInterface $settings) { + $this->settings = $settings; $this->notifications = $notifications; - } - public function handle(PostRestored $event) { + $this->autoremove = (int)$this->settings->get('antoinefr-money.autoremove', 1); + } + public function handle(PostWasUnliked $event) { + $money = (float)$this->settings->get('antoinefr-money.moneyforlike', 0); + $this->exec($event->post->user, -$money); } } diff --git a/src/Listeners/UserWillBeSavedHistory.php b/src/Listeners/UserWillBeSavedHistory.php index 39d9bb5..6b9cc63 100644 --- a/src/Listeners/UserWillBeSavedHistory.php +++ b/src/Listeners/UserWillBeSavedHistory.php @@ -2,19 +2,31 @@ namespace Mattoid\MoneyHistory\Listeners; +use Flarum\User\Event\Saving; use Flarum\Notification\NotificationSyncer; -use Flarum\Post\Event\Restored as PostRestored; +use Flarum\Settings\SettingsRepositoryInterface; -class UserWillBeSavedHistory +class UserWillBeSavedHistory extends HistoryListeners { - private $sourceDesc = "帖子恢复"; + protected $source = "USERWILLBESAVED"; + protected $sourceDesc = ""; - public function __construct(NotificationSyncer $notifications) + private $settings; + private $autoremove; + + public function __construct(NotificationSyncer $notifications, SettingsRepositoryInterface $settings) { + $this->settings = $settings; $this->notifications = $notifications; + + $this->autoremove = (int)$this->settings->get('antoinefr-money.autoremove', 1); } - public function handle(PostRestored $event) { + public function handle(Saving $event) { + $attributes = Arr::get($event->data, 'attributes', []); + if (array_key_exists('money', $attributes)) { + $this->exec($event->user, (float)$attributes['money']); + } } } diff --git a/src/Middleware/MoneyMiddleware.php b/src/Middleware/MoneyMiddleware.php deleted file mode 100644 index a06c04e..0000000 --- a/src/Middleware/MoneyMiddleware.php +++ /dev/null @@ -1,38 +0,0 @@ -settings = $settings; - } - - public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface - { - - $typesAllowed = []; - $actor = RequestUtil::getActor($request); - $userId = Arr::get($actor, 'id'); - $money = Arr::get($actor, 'money'); - - $response = $handler->handle($request); - - if (!in_array($request->getMethod(), $typesAllowed)) { - return $response; - } - - - - return $response; - } -} diff --git a/src/model/UserMoneyHistory.php b/src/model/UserMoneyHistory.php new file mode 100644 index 0000000..fc51883 --- /dev/null +++ b/src/model/UserMoneyHistory.php @@ -0,0 +1,38 @@ +