mirror of
https://github.com/flarum/framework.git
synced 2025-02-21 02:56:10 +08:00
parent
8a1c5cf24c
commit
e357d2b535
@ -63,7 +63,7 @@ export default class FlagList extends Component {
|
||||
* been loaded.
|
||||
*/
|
||||
load() {
|
||||
if (app.cache.flags && !app.forum.attribute('unreadFlagsCount')) {
|
||||
if (app.cache.flags && !app.session.user.attribute('newFlagsCount')) {
|
||||
return;
|
||||
}
|
||||
|
||||
@ -71,7 +71,7 @@ export default class FlagList extends Component {
|
||||
m.redraw();
|
||||
|
||||
app.store.find('flags').then(flags => {
|
||||
app.forum.pushAttributes({unreadFlagsCount: 0});
|
||||
app.session.user.pushAttributes({newFlagsCount: 0});
|
||||
app.cache.flags = flags.sort((a, b) => b.time() - a.time());
|
||||
|
||||
this.loading = false;
|
||||
|
@ -21,10 +21,10 @@ export default class FlagsDropdown extends NotificationsDropdown {
|
||||
}
|
||||
|
||||
getUnreadCount() {
|
||||
return app.forum.attribute('unreadFlagsCount');
|
||||
return app.cache.flags ? app.cache.flags.length : app.forum.attribute('flagsCount');
|
||||
}
|
||||
|
||||
getNewCount() {
|
||||
return app.forum.attribute('newFlagsCount');
|
||||
return app.session.user.attribute('newFlagsCount');
|
||||
}
|
||||
}
|
||||
|
@ -10,6 +10,7 @@
|
||||
|
||||
namespace Flarum\Flags\Listener;
|
||||
|
||||
use Flarum\Api\Serializer\CurrentUserSerializer;
|
||||
use Flarum\Api\Serializer\ForumSerializer;
|
||||
use Flarum\Api\Serializer\PostSerializer;
|
||||
use Flarum\Core\User;
|
||||
@ -51,16 +52,14 @@ class AddFlagsApi
|
||||
$event->attributes['canViewFlags'] = $event->actor->hasPermissionLike('discussion.viewFlags');
|
||||
|
||||
if ($event->attributes['canViewFlags']) {
|
||||
$query = Flag::whereVisibleTo($event->actor);
|
||||
|
||||
if ($time = $event->actor->flags_read_time) {
|
||||
$query->where('flags.time', '>', $time);
|
||||
}
|
||||
|
||||
$event->attributes['unreadFlagsCount'] = $query->distinct('flags.post_id')->count();
|
||||
$event->attributes['flagsCount'] = (int) $this->getFlagsCount($event->actor);
|
||||
}
|
||||
}
|
||||
|
||||
if ($event->isSerializer(CurrentUserSerializer::class)) {
|
||||
$event->attributes['newFlagsCount'] = (int) $this->getNewFlagsCount($event->model);
|
||||
}
|
||||
|
||||
if ($event->isSerializer(PostSerializer::class)) {
|
||||
$event->attributes['canFlag'] = $event->actor->can('flag', $event->model);
|
||||
}
|
||||
@ -75,4 +74,28 @@ class AddFlagsApi
|
||||
$event->post('/flags', 'flags.create', Controller\CreateFlagController::class);
|
||||
$event->delete('/posts/{id}/flags', 'flags.delete', Controller\DeleteFlagsController::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param User $actor
|
||||
* @return int
|
||||
*/
|
||||
protected function getFlagsCount(User $actor)
|
||||
{
|
||||
return Flag::whereVisibleTo($actor)->distinct('flags.post_id')->count();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param User $actor
|
||||
* @return int
|
||||
*/
|
||||
protected function getNewFlagsCount(User $actor)
|
||||
{
|
||||
$query = Flag::whereVisibleTo($actor);
|
||||
|
||||
if ($time = $actor->flags_read_time) {
|
||||
$query->where('flags.time', '>', $time);
|
||||
}
|
||||
|
||||
return $query->distinct('flags.post_id')->count();
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user