Skip to content

Commit

Permalink
记录 金额流转日志
Browse files Browse the repository at this point in the history
  • Loading branch information
liufei-ereach committed Dec 11, 2023
1 parent 9eba26f commit a1d51b6
Show file tree
Hide file tree
Showing 14 changed files with 261 additions and 96 deletions.
24 changes: 18 additions & 6 deletions src/Listeners/DiscussionWasDeletedHistory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
26 changes: 19 additions & 7 deletions src/Listeners/DiscussionWasHiddenHistory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
24 changes: 18 additions & 6 deletions src/Listeners/DiscussionWasRestoredHistory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
21 changes: 15 additions & 6 deletions src/Listeners/DiscussionWasStartedHistory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
27 changes: 27 additions & 0 deletions src/Listeners/HistoryListeners.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Mattoid\MoneyHistory\Listeners;

use Flarum\User\User;
use Mattoid\MoneyHistory\model\UserMoneyHistory;

abstract class HistoryListeners
{

protected $source;
protected $sourceDesc;

public function exec(?User $user, $money) {
$userMoneyHistory = new UserMoneyHistory();
$userMoneyHistory->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();
}

}
26 changes: 21 additions & 5 deletions src/Listeners/PostWasDeletedHistory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
}
23 changes: 20 additions & 3 deletions src/Listeners/PostWasHiddenHistory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
}
22 changes: 15 additions & 7 deletions src/Listeners/PostWasLikedHistory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
22 changes: 19 additions & 3 deletions src/Listeners/PostWasPostedHistory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
}
23 changes: 19 additions & 4 deletions src/Listeners/PostWasRestoredHistory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
}
Loading

0 comments on commit a1d51b6

Please sign in to comment.