Skip to content

Commit

Permalink
Merge branch 'master' into dev_discussion_count
Browse files Browse the repository at this point in the history
  • Loading branch information
zxy19 committed Oct 1, 2024
2 parents 275b233 + 2b33410 commit 3c95d2b
Show file tree
Hide file tree
Showing 7 changed files with 2,620 additions and 25 deletions.
31 changes: 14 additions & 17 deletions extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,38 @@
use Flarum\Api\Serializer\ForumSerializer;
use Flarum\Api\Serializer\UserSerializer;
use Flarum\Extend;
use Flarum\Extend\ApiController;
use Flarum\Post\Event\Posted;
use Flarum\Discussion\Event\Started;
use Flarum\Post\Event\Deleted as PostDeleted;
use Flarum\Discussion\Event\Deleted as DiscussionDeleted;
use Flarum\Api\Controller\ListUsersController;
use Flarum\User\Filter\UserFilterer;
use Illuminate\Database\Eloquent\Builder;

return [
(new Extend\Frontend('admin'))
->js(__DIR__ . '/js/dist/admin.js'),
->js(__DIR__.'/js/dist/admin.js'),

(new Extend\Frontend('forum'))
->js(__DIR__ . '/js/dist/forum.js')
->css(__DIR__ . '/resources/less/forum.less')
->js(__DIR__.'/js/dist/forum.js')
->css(__DIR__.'/resources/less/forum.less')
->route('/leaderboard', 'nodeloc_leaderboard', Content\LeaderBoard::class),

new Extend\Locales(__DIR__ . '/resources/locale'),
new Extend\Locales(__DIR__.'/resources/locale'),

(new Extend\ApiSerializer(ForumSerializer::class))
->attributes(PermissionBasedForumSettings::class),
// (new Extend\ApiSerializer(UserSerializer::class))
// ->attribute('monthlyDiscussionCount', "monthlyDiscussionCount"),
(new Extend\ApiSerializer(UserSerializer::class))
->attributes(AddMonthlyCountAttributes::class),
(new Extend\Policy())
->globalPolicy(Access\UserPolicy::class),
// (new Extend\Event())
// ->listen(Posted::class, [Listeners\UpdateCount::class, 'postWasPosted'])
// ->listen(Started::class, [Listeners\UpdateCount::class, 'discussionWasStarted']),
(new Extend\Event())
->listen(Posted::class, [Listeners\UpdateCount::class, 'postWasPosted'])
->listen(Started::class, [Listeners\UpdateCount::class, 'discussionWasStarted'])
->listen(PostDeleted::class, [Listeners\UpdateCount::class, 'postWasDeleted'])
->listen(DiscussionDeleted::class, [Listeners\UpdateCount::class, 'discussionWasDeleted']),
(new Extend\View())
->namespace('nodeloc.leaderboard', __DIR__ . '/resources/views'),
->namespace('nodeloc.leaderboard', __DIR__.'/resources/views'),
(new Extend\ApiController(ListUsersController::class))
->addSortField('money')
->addSortField('lotteryCount')
Expand All @@ -49,10 +52,4 @@
->add(Middleware\BeforeMiddleware::class),
(new Extend\Filter(UserFilterer::class))
->addFilterMutator(Filter\CheckDateFilter::class),
(new Extend\ApiSerializer(UserSerializer::class))
->attributes(function ($s, $model, $attrs) {
$attrs['monthlyCommentCount'] = $model->monthly_comment_count ?? 0;
$attrs['monthlyDiscussionCount'] = $model->monthly_discussion_count ?? 0;
return $attrs;
})
];
4 changes: 2 additions & 2 deletions js/dist/forum.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/dist/forum.js.map

Large diffs are not rendered by default.

2,566 changes: 2,566 additions & 0 deletions js/package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"prettier": "@flarum/prettier-config",
"dependencies": {
"@flarum/prettier-config": "^1.0.0",
"@nodeloc/flarum-ext-leaderboard": "file:",
"flarum-tsconfig": "^1.0.2",
"flarum-webpack-config": "^2.0.2",
"webpack": "^5.88.2",
Expand Down
17 changes: 17 additions & 0 deletions src/AddMonthlyCountAttributes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Nodeloc\LeaderBoard;

use Flarum\Api\Serializer\UserSerializer;
use Flarum\User\User;

class AddMonthlyCountAttributes
{
public function __invoke(UserSerializer $serializer, User $user)
{
$attributes = [];
$attributes['monthlyDiscussionCount'] = $user->monthly_discussion_count ;
$attributes['monthlyCommentCount'] = $user->monthly_comment_count ;
return $attributes;
}
}
24 changes: 19 additions & 5 deletions src/Listeners/UpdateCount.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,30 @@ public function __construct(SettingsRepositoryInterface $settings, Dispatcher $e

public function postWasPosted(Posted $event)
{
$user = $event->actor;
$user ->increment('monthly_comment_count', 1);
$user->save();
if ($event->post['number'] > 1) {
$user = $event->actor;
$user ->increment('monthly_comment_count', 1);
$user->save();
}
}
public function postWasDeleted(PostDeleted $event)
{
if ($event->post['number'] > 1) {
$user = $event->actor;
$user ->decrement('monthly_comment_count', 1);
$user->save();
}
}

public function discussionWasStarted(Started $event)
{
$user = $event->actor;
$user ->increment('monthly_discussion_count', 1);
$user->save();
}

public function discussionWasDeleted(DiscussionDeleted $event)
{
$user = $event->actor;
$user ->decrement('monthly_discussion_count', 1);
$user->save();
}
}

0 comments on commit 3c95d2b

Please sign in to comment.