Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 52 additions & 50 deletions src/Listeners/GiveMoney.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -46,12 +45,12 @@ 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)) {
if ($user !== null) {
$user->money += $money;
$user->save();

Expand All @@ -63,126 +62,129 @@ public function giveMoney(?User $user, float $money): bool
return false;
}

public function postGiveMoney(?User $user, float $money, Post $post)
private function postGiveMoney(?Post $post, ?User $user, int $multiply): 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 ($post !== null && $user !== null) {
$content = $this->ignoreNotifyingUsers($post->content);

if (
$this->checkPermissions($user, $post->discussion)
&& mb_strlen($content) >= $this->postminimumlength
) {
$this->giveMoney($user, $multiply * $this->moneyforpost);
}
}
}

private function discussionGiveMoney(?Discussion $discussion, ?User $user, int $multiply): void
{
if ($discussion !== null && $user !== null) {
if ($this->checkPermissions($user, $discussion)) {
$this->giveMoney($user, $multiply * $this->moneyfordiscussion);

$this->discussionCascadePosts($discussion, $multiply);
}
}
}

if ($permissions) {
$this->giveMoney($user, $money);
private function checkPermissions(?User $user, ?Discussion $discussion): bool
{
if ($discussion !== null && $user !== null) {
foreach ($discussion->tags as $tag) {
if ($user->hasPermission("tag{$tag->id}.discussion.money.disable_money") && !$user->isAdmin()) {
return false;
}
}

return true;
}

return false;
}

public function ignoreNotifyingUsers(string $content): string
private function ignoreNotifyingUsers(string $content): string
{
if (!$this->ignoreNotifyingUsersSwitch) {
if (!$this->ignorenotifyingusers) {
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
{
$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->post, $event->actor, 1);
}
}

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, $event->post->user, 1);
}
}

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, $event->post->user, -1);
}
}

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, $event->post->user, -1);
}
}

public function discussionWasStarted(Started $event): void
{
$this->giveMoney($event->actor, $this->moneyfordiscussion);
$this->discussionGiveMoney($event->discussion, $event->actor, 1);
}

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, $event->discussion->user, 1);
}
}

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, $event->discussion->user, -1);
}
}

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, $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) {
$content = $this->ignoreNotifyingUsers($post->content);
if (
$post->type == 'comment'
&& mb_strlen($content) >= $this->postminimumlength
&& $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);
}
}
}
Expand Down
Loading