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