mirror of
https://github.com/flarum/core.git
synced 2025-08-11 02:44:04 +02:00
@@ -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();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user