From 280600ae13e6dac2c596a4bbfc84d138fe555903 Mon Sep 17 00:00:00 2001 From: AntoineFr Date: Tue, 30 Dec 2025 18:58:21 +0100 Subject: [PATCH 1/2] feat: Refactor --- src/Listeners/GiveMoney.php | 92 +++++++++++++++++++------------------ 1 file changed, 47 insertions(+), 45 deletions(-) diff --git a/src/Listeners/GiveMoney.php b/src/Listeners/GiveMoney.php index f454a57..00c9b0d 100644 --- a/src/Listeners/GiveMoney.php +++ b/src/Listeners/GiveMoney.php @@ -32,8 +32,7 @@ class GiveMoney protected float $moneyforlike; protected int $autoremove; protected bool $cascaderemove; - - protected bool $ignoreNotifyingUsersSwitch; + protected bool $ignorenotifyingusers; public function __construct(SettingsRepositoryInterface $settings, Dispatcher $events) { @@ -46,10 +45,10 @@ public function __construct(SettingsRepositoryInterface $settings, Dispatcher $e $this->moneyforlike = (float) $this->settings->get('antoinefr-money.moneyforlike', 0); $this->autoremove = (int) $this->settings->get('antoinefr-money.autoremove', 1); $this->cascaderemove = (bool) $this->settings->get('antoinefr-money.cascaderemove', false); - $this->ignoreNotifyingUsersSwitch = (bool) $this->settings->get('antoinefr-money.ignorenotifyingusers', false); + $this->ignorenotifyingusers = (bool) $this->settings->get('antoinefr-money.ignorenotifyingusers', false); } - public function giveMoney(?User $user, float $money): bool + private function giveMoney(?User $user, float $money): bool { if (!is_null($user)) { $user->money += $money; @@ -63,28 +62,49 @@ public function giveMoney(?User $user, float $money): bool return false; } - public function postGiveMoney(?User $user, float $money, Post $post) + private function postGiveMoney(?User $user, int $multiply, Post $post): void { - if (!is_null($user)) { - $permissions = true; - if ($post) { - $discussionTags = $post->discussion->tags; - foreach ($discussionTags as $tag) { - if ($user->hasPermission("tag{$tag->id}.discussion.money.disable_money") && !$user->isAdmin()) { - $permissions = false; - } - } + if (!is_null($user) && !is_null($post)) { + $content = $this->ignoreNotifyingUsers($post->content); + + if ( + $this->checkPermissions($user, $post->discussion) + && mb_strlen($content) >= $this->postminimumlength + ) { + $this->giveMoney($user, $multiply * $this->moneyforpost); } + } + } - if ($permissions) { - $this->giveMoney($user, $money); + private function discussionGiveMoney(?User $user, int $multiply, Discussion $discussion): void + { + if (!is_null($user) && !is_null($discussion)) { + if ($this->checkPermissions($user, $discussion)) { + $this->giveMoney($user, $multiply * $this->moneyfordiscussion); + + $this->discussionCascadePosts($discussion, $multiply); } } } - public function ignoreNotifyingUsers(string $content): string + private function checkPermissions(?User $user, ?Discussion $discussion): bool { - if (!$this->ignoreNotifyingUsersSwitch) { + if (!is_null($user) && !is_null($discussion)) { + foreach ($discussion->tags as $tag) { + if ($user->hasPermission("tag{$tag->id}.discussion.money.disable_money") && !$user->isAdmin()) { + return false; + } + } + + return true; + } + + return false; + } + + private function ignoreNotifyingUsers(string $content): string + { + if (!$this->ignorenotifyingusers) { return $content; } @@ -94,80 +114,64 @@ public function ignoreNotifyingUsers(string $content): string public function postWasPosted(Posted $event): void { - $content = $this->ignoreNotifyingUsers($event->post->content); - if ( - $event->post->number > 1 // If it's not the first post of a discussion - && mb_strlen($content) >= $this->postminimumlength - ) { - $this->postGiveMoney($event->actor, $this->moneyforpost, $event->post); + if ($event->post->number > 1) { // If it's not the first post of a discussion + $this->postGiveMoney($event->actor, 1, $event->post); } } public function postWasRestored(PostRestored $event): void { - $content = $this->ignoreNotifyingUsers($event->post->content); if ( $this->autoremove == AutoRemoveEnum::HIDDEN && $event->post->type == 'comment' - && mb_strlen($content) >= $this->postminimumlength ) { - $this->postGiveMoney($event->post->user, $this->moneyforpost, $event->post); + $this->postGiveMoney($event->post->user, 1, $event->post); } } public function postWasHidden(PostHidden $event): void { - $content = $this->ignoreNotifyingUsers($event->post->content); if ( $this->autoremove == AutoRemoveEnum::HIDDEN && $event->post->type == 'comment' - && mb_strlen($content) >= $this->postminimumlength ) { - $this->postGiveMoney($event->post->user, -1 * $this->moneyforpost, $event->post); + $this->postGiveMoney($event->post->user, -1, $event->post); } } public function postWasDeleted(PostDeleted $event): void { - $content = $this->ignoreNotifyingUsers($event->post->content); if ( $this->autoremove == AutoRemoveEnum::DELETED && $event->post->type == 'comment' - && mb_strlen($content) >= $this->postminimumlength ) { - $this->postGiveMoney($event->post->user, -1 * $this->moneyforpost, $event->post); + $this->postGiveMoney($event->post->user, -1, $event->post); } } public function discussionWasStarted(Started $event): void { - $this->giveMoney($event->actor, $this->moneyfordiscussion); + $this->discussionGiveMoney($event->actor, 1, $event->discussion); } public function discussionWasRestored(DiscussionRestored $event): void { if ($this->autoremove == AutoRemoveEnum::HIDDEN) { - $this->giveMoney($event->discussion->user, $this->moneyfordiscussion); - - $this->discussionCascadePosts($event->discussion, 1); + $this->discussionGiveMoney($event->discussion->user, 1, $event->discussion); } } public function discussionWasHidden(DiscussionHidden $event): void { if ($this->autoremove == AutoRemoveEnum::HIDDEN) { - $this->giveMoney($event->discussion->user, -$this->moneyfordiscussion); - - $this->discussionCascadePosts($event->discussion, -1); + $this->discussionGiveMoney($event->discussion->user, -1, $event->discussion); } } public function discussionWasDeleted(DiscussionDeleted $event): void { if ($this->autoremove == AutoRemoveEnum::DELETED) { - $this->giveMoney($event->discussion->user, -$this->moneyfordiscussion); - - $this->discussionCascadePosts($event->discussion, -1); + $this->discussionGiveMoney($event->discussion->user, -1, $event->discussion); } } @@ -175,10 +179,8 @@ protected function discussionCascadePosts(Discussion $discussion, int $multiply) { if ($this->cascaderemove) { foreach ($discussion->posts as $post) { - $content = $this->ignoreNotifyingUsers($post->content); if ( $post->type == 'comment' - && mb_strlen($content) >= $this->postminimumlength && $post->number > 1 && is_null($post->hidden_at) ) { From 854fdddb0ea9fca6dcf1b68c51f539045a50618b Mon Sep 17 00:00:00 2001 From: AntoineFr Date: Tue, 30 Dec 2025 19:23:56 +0100 Subject: [PATCH 2/2] fixes --- src/Listeners/GiveMoney.php | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/src/Listeners/GiveMoney.php b/src/Listeners/GiveMoney.php index 00c9b0d..ff799e1 100644 --- a/src/Listeners/GiveMoney.php +++ b/src/Listeners/GiveMoney.php @@ -50,7 +50,7 @@ public function __construct(SettingsRepositoryInterface $settings, Dispatcher $e private function giveMoney(?User $user, float $money): bool { - if (!is_null($user)) { + if ($user !== null) { $user->money += $money; $user->save(); @@ -62,9 +62,9 @@ private function giveMoney(?User $user, float $money): bool return false; } - private function postGiveMoney(?User $user, int $multiply, Post $post): void + private function postGiveMoney(?Post $post, ?User $user, int $multiply): void { - if (!is_null($user) && !is_null($post)) { + if ($post !== null && $user !== null) { $content = $this->ignoreNotifyingUsers($post->content); if ( @@ -76,9 +76,9 @@ private function postGiveMoney(?User $user, int $multiply, Post $post): void } } - private function discussionGiveMoney(?User $user, int $multiply, Discussion $discussion): void + private function discussionGiveMoney(?Discussion $discussion, ?User $user, int $multiply): void { - if (!is_null($user) && !is_null($discussion)) { + if ($discussion !== null && $user !== null) { if ($this->checkPermissions($user, $discussion)) { $this->giveMoney($user, $multiply * $this->moneyfordiscussion); @@ -89,7 +89,7 @@ private function discussionGiveMoney(?User $user, int $multiply, Discussion $dis private function checkPermissions(?User $user, ?Discussion $discussion): bool { - if (!is_null($user) && !is_null($discussion)) { + if ($discussion !== null && $user !== null) { foreach ($discussion->tags as $tag) { if ($user->hasPermission("tag{$tag->id}.discussion.money.disable_money") && !$user->isAdmin()) { return false; @@ -108,14 +108,14 @@ private function ignoreNotifyingUsers(string $content): string return $content; } - $pattern = '/@.*(#\d+|#p\d+)/'; + $pattern = '/@.*?(#\d+|#p\d+)/'; return trim(str_replace(["\r", "\n"], '', preg_replace($pattern, '', $content))); } public function postWasPosted(Posted $event): void { if ($event->post->number > 1) { // If it's not the first post of a discussion - $this->postGiveMoney($event->actor, 1, $event->post); + $this->postGiveMoney($event->post, $event->actor, 1); } } @@ -125,7 +125,7 @@ public function postWasRestored(PostRestored $event): void $this->autoremove == AutoRemoveEnum::HIDDEN && $event->post->type == 'comment' ) { - $this->postGiveMoney($event->post->user, 1, $event->post); + $this->postGiveMoney($event->post, $event->post->user, 1); } } @@ -135,7 +135,7 @@ public function postWasHidden(PostHidden $event): void $this->autoremove == AutoRemoveEnum::HIDDEN && $event->post->type == 'comment' ) { - $this->postGiveMoney($event->post->user, -1, $event->post); + $this->postGiveMoney($event->post, $event->post->user, -1); } } @@ -145,46 +145,46 @@ public function postWasDeleted(PostDeleted $event): void $this->autoremove == AutoRemoveEnum::DELETED && $event->post->type == 'comment' ) { - $this->postGiveMoney($event->post->user, -1, $event->post); + $this->postGiveMoney($event->post, $event->post->user, -1); } } public function discussionWasStarted(Started $event): void { - $this->discussionGiveMoney($event->actor, 1, $event->discussion); + $this->discussionGiveMoney($event->discussion, $event->actor, 1); } public function discussionWasRestored(DiscussionRestored $event): void { if ($this->autoremove == AutoRemoveEnum::HIDDEN) { - $this->discussionGiveMoney($event->discussion->user, 1, $event->discussion); + $this->discussionGiveMoney($event->discussion, $event->discussion->user, 1); } } public function discussionWasHidden(DiscussionHidden $event): void { if ($this->autoremove == AutoRemoveEnum::HIDDEN) { - $this->discussionGiveMoney($event->discussion->user, -1, $event->discussion); + $this->discussionGiveMoney($event->discussion, $event->discussion->user, -1); } } public function discussionWasDeleted(DiscussionDeleted $event): void { if ($this->autoremove == AutoRemoveEnum::DELETED) { - $this->discussionGiveMoney($event->discussion->user, -1, $event->discussion); + $this->discussionGiveMoney($event->discussion, $event->discussion->user, -1); } } - protected function discussionCascadePosts(Discussion $discussion, int $multiply): void + protected function discussionCascadePosts(?Discussion $discussion, int $multiply): void { if ($this->cascaderemove) { foreach ($discussion->posts as $post) { if ( $post->type == 'comment' && $post->number > 1 - && is_null($post->hidden_at) + && $post->hidden_at === null ) { - $this->postGiveMoney($post->user, $multiply * $this->moneyforpost, $post); + $this->postGiveMoney($post, $post->user, $multiply); } } }