1
0
mirror of https://github.com/flarum/core.git synced 2025-08-11 02:44:04 +02:00

Rename to Flags, tweak flag controls

- Display post "destructiveControls" in flag instead of custom buttons
- Make flags more versatile/extensible
- Delete associated flags when a post is deleted

Uninstall the Reports extension before installing.
This commit is contained in:
Toby Zerner
2015-09-22 18:14:33 +09:30
parent 8da417c5b5
commit a1e01938ef
36 changed files with 390 additions and 401 deletions

View File

@@ -1,4 +1,4 @@
<?php
<?php
/*
* This file is part of Flarum.
*
@@ -8,9 +8,9 @@
* file that was distributed with this source code.
*/
namespace Flarum\Reports\Api;
namespace Flarum\Flags\Api;
use Flarum\Reports\Commands\CreateReport;
use Flarum\Flags\Commands\CreateFlag;
use Flarum\Api\Actions\CreateAction as BaseCreateAction;
use Flarum\Api\JsonApiRequest;
use Illuminate\Contracts\Bus\Dispatcher;
@@ -25,14 +25,14 @@ class CreateAction extends BaseCreateAction
/**
* @inheritdoc
*/
public $serializer = 'Flarum\Reports\Api\ReportSerializer';
public $serializer = 'Flarum\Flags\Api\FlagSerializer';
/**
* @inheritdoc
*/
public $include = [
'post' => true,
'post.reports' => true
'post.flags' => true
];
/**
@@ -44,15 +44,15 @@ class CreateAction extends BaseCreateAction
}
/**
* Create a report according to input from the API request.
* Create a flag according to input from the API request.
*
* @param JsonApiRequest $request
* @return \Flarum\Reports\Report
* @return \Flarum\Flags\Flag
*/
protected function create(JsonApiRequest $request)
{
return $this->bus->dispatch(
new CreateReport($request->actor, $request->get('data'))
new CreateFlag($request->actor, $request->get('data'))
);
}
}

View File

@@ -1,4 +1,4 @@
<?php
<?php
/*
* This file is part of Flarum.
*
@@ -8,9 +8,9 @@
* file that was distributed with this source code.
*/
namespace Flarum\Reports\Api;
namespace Flarum\Flags\Api;
use Flarum\Reports\Commands\DeleteReports;
use Flarum\Flags\Commands\DeleteFlags;
use Flarum\Api\Actions\DeleteAction as BaseDeleteAction;
use Flarum\Api\Request;
use Illuminate\Contracts\Bus\Dispatcher;
@@ -31,14 +31,14 @@ class DeleteAction extends BaseDeleteAction
}
/**
* Delete reports for a post.
* Delete flags for a post.
*
* @param Request $request
*/
protected function delete(Request $request)
{
$this->bus->dispatch(
new DeleteReports($request->get('id'), $request->actor, $request->all())
new DeleteFlags($request->get('id'), $request->actor, $request->all())
);
}
}

View File

@@ -1,4 +1,4 @@
<?php
<?php
/*
* This file is part of Flarum.
*
@@ -8,20 +8,20 @@
* file that was distributed with this source code.
*/
namespace Flarum\Reports\Api;
namespace Flarum\Flags\Api;
use Flarum\Api\Serializers\Serializer;
class ReportSerializer extends Serializer
class FlagSerializer extends Serializer
{
protected $type = 'reports';
protected $type = 'flags';
protected function getDefaultAttributes($report)
protected function getDefaultAttributes($flag)
{
return [
'reporter' => $report->reporter,
'reason' => $report->reason,
'reasonDetail' => $report->reason_detail,
'type' => $flag->type,
'reason' => $flag->reason,
'reasonDetail' => $flag->reason_detail,
];
}

View File

@@ -1,4 +1,4 @@
<?php
<?php
/*
* This file is part of Flarum.
*
@@ -8,11 +8,11 @@
* file that was distributed with this source code.
*/
namespace Flarum\Reports\Api;
namespace Flarum\Flags\Api;
use Flarum\Api\Actions\SerializeCollectionAction;
use Flarum\Api\JsonApiRequest;
use Flarum\Reports\Report;
use Flarum\Flags\Flag;
use Tobscure\JsonApi\Document;
class IndexAction extends SerializeCollectionAction
@@ -20,7 +20,7 @@ class IndexAction extends SerializeCollectionAction
/**
* @inheritdoc
*/
public $serializer = 'Flarum\Reports\Api\ReportSerializer';
public $serializer = 'Flarum\Flags\Api\FlagSerializer';
/**
* @inheritdoc
@@ -41,12 +41,12 @@ class IndexAction extends SerializeCollectionAction
{
$actor = $request->actor;
$actor->reports_read_time = time();
$actor->flags_read_time = time();
$actor->save();
return Report::whereVisibleTo($actor)
return Flag::whereVisibleTo($actor)
->with($request->include)
->latest('reports.time')
->latest('flags.time')
->groupBy('post_id')
->get();
}

View File

@@ -1,4 +1,4 @@
<?php
<?php
/*
* This file is part of Flarum.
*
@@ -8,11 +8,11 @@
* file that was distributed with this source code.
*/
namespace Flarum\Reports\Commands;
namespace Flarum\Flags\Commands;
use Flarum\Core\Users\User;
class CreateReport
class CreateFlag
{
/**
* The user performing the action.
@@ -22,7 +22,7 @@ class CreateReport
public $actor;
/**
* The attributes of the new report.
* The attributes of the new flag.
*
* @var array
*/
@@ -30,7 +30,7 @@ class CreateReport
/**
* @param User $actor The user performing the action.
* @param array $data The attributes of the new report.
* @param array $data The attributes of the new flag.
*/
public function __construct(User $actor, array $data)
{

View File

@@ -1,4 +1,4 @@
<?php
<?php
/*
* This file is part of Flarum.
*
@@ -8,14 +8,14 @@
* file that was distributed with this source code.
*/
namespace Flarum\Reports\Commands;
namespace Flarum\Flags\Commands;
use Flarum\Reports\Report;
use Flarum\Flags\Flag;
use Flarum\Core\Posts\PostRepository;
use Flarum\Core\Posts\CommentPost;
use Exception;
class CreateReportHandler
class CreateFlagHandler
{
private $posts;
@@ -25,10 +25,10 @@ class CreateReportHandler
}
/**
* @param CreateReport $command
* @return Report
* @param CreateFlag $command
* @return Flag
*/
public function handle(CreateReport $command)
public function handle(CreateFlag $command)
{
$actor = $command->actor;
$data = $command->data;
@@ -41,23 +41,24 @@ class CreateReportHandler
throw new Exception;
}
$post->assertCan($actor, 'report');
$post->assertCan($actor, 'flag');
Report::unguard();
Flag::unguard();
$report = Report::firstOrNew([
$flag = Flag::firstOrNew([
'post_id' => $post->id,
'user_id' => $actor->id
]);
$report->post_id = $post->id;
$report->user_id = $actor->id;
$report->reason = array_get($data, 'attributes.reason');
$report->reason_detail = array_get($data, 'attributes.reasonDetail');
$report->time = time();
$flag->post_id = $post->id;
$flag->user_id = $actor->id;
$flag->type = 'user';
$flag->reason = array_get($data, 'attributes.reason');
$flag->reason_detail = array_get($data, 'attributes.reasonDetail');
$flag->time = time();
$report->save();
$flag->save();
return $report;
return $flag;
}
}

View File

@@ -1,4 +1,4 @@
<?php
<?php
/*
* This file is part of Flarum.
*
@@ -8,15 +8,15 @@
* file that was distributed with this source code.
*/
namespace Flarum\Reports\Commands;
namespace Flarum\Flags\Commands;
use Flarum\Reports\Report;
use Flarum\Flags\Flag;
use Flarum\Core\Users\User;
class DeleteReports
class DeleteFlags
{
/**
* The ID of the post to delete reports for.
* The ID of the post to delete flags for.
*
* @var int
*/
@@ -35,7 +35,7 @@ class DeleteReports
public $data;
/**
* @param int $postId The ID of the post to delete reports for.
* @param int $postId The ID of the post to delete flags for.
* @param User $actor The user performing the action.
* @param array $data
*/

View File

@@ -8,13 +8,13 @@
* file that was distributed with this source code.
*/
namespace Flarum\Reports\Commands;
namespace Flarum\Flags\Commands;
use Flarum\Reports\Report;
use Flarum\Flags\Flag;
use Flarum\Core\Posts\PostRepository;
use Flarum\Reports\Events\ReportsWillBeDeleted;
use Flarum\Flags\Events\FlagsWillBeDeleted;
class DeleteReportsHandler
class DeleteFlagsHandler
{
protected $posts;
@@ -24,21 +24,21 @@ class DeleteReportsHandler
}
/**
* @param DeleteReport $command
* @return Report
* @param DeleteFlag $command
* @return Flag
* @throws \Flarum\Core\Exceptions\PermissionDeniedException
*/
public function handle(DeleteReports $command)
public function handle(DeleteFlags $command)
{
$actor = $command->actor;
$post = $this->posts->findOrFail($command->postId, $actor);
$post->discussion->assertCan($actor, 'viewReports');
$post->discussion->assertCan($actor, 'viewFlags');
event(new ReportsWillBeDeleted($post, $actor, $command->data));
event(new FlagsWillBeDeleted($post, $actor, $command->data));
$post->reports()->delete();
$post->flags()->delete();
return $post;
}

View File

@@ -9,12 +9,12 @@
* file that was distributed with this source code.
*/
namespace Flarum\Reports\Events;
namespace Flarum\Flags\Events;
use Flarum\Core\Posts\Post;
use Flarum\Core\Users\User;
class ReportsWillBeDeleted
class FlagsWillBeDeleted
{
/**
* @var Post

View File

@@ -1,4 +1,4 @@
<?php
<?php
/*
* This file is part of Flarum.
*
@@ -8,17 +8,18 @@
* file that was distributed with this source code.
*/
namespace Flarum\Reports;
namespace Flarum\Flags;
use Flarum\Support\Extension as BaseExtension;
use Illuminate\Events\Dispatcher;
use Flarum\Core\Posts\Post;
class Extension extends BaseExtension
{
public function listen(Dispatcher $events)
{
$events->subscribe('Flarum\Reports\Listeners\AddClientAssets');
$events->subscribe('Flarum\Reports\Listeners\AddApiAttributes');
$events->subscribe('Flarum\Reports\Listeners\AddModelRelationship');
$events->subscribe('Flarum\Flags\Listeners\AddClientAssets');
$events->subscribe('Flarum\Flags\Listeners\AddApiAttributes');
$events->subscribe('Flarum\Flags\Listeners\AddModelRelationship');
}
}

View File

@@ -1,4 +1,4 @@
<?php
<?php
/*
* This file is part of Flarum.
*
@@ -8,16 +8,16 @@
* file that was distributed with this source code.
*/
namespace Flarum\Reports;
namespace Flarum\Flags;
use Flarum\Core\Model;
use Flarum\Core\Support\VisibleScope;
class Report extends Model
class Flag extends Model
{
use VisibleScope;
protected $table = 'reports';
protected $table = 'flags';
protected $dates = ['time'];

View File

@@ -8,7 +8,7 @@
* file that was distributed with this source code.
*/
namespace Flarum\Reports\Listeners;
namespace Flarum\Flags\Listeners;
use Flarum\Events\ApiRelationship;
use Flarum\Events\WillSerializeData;
@@ -19,26 +19,26 @@ use Flarum\Api\Serializers\PostSerializer;
use Flarum\Api\Serializers\ForumSerializer;
use Flarum\Api\Actions\Posts;
use Flarum\Api\Actions\Discussions;
use Flarum\Reports\Report;
use Flarum\Reports\Api\CreateAction as ReportsCreateAction;
use Flarum\Flags\Flag;
use Flarum\Flags\Api\CreateAction as FlagsCreateAction;
use Illuminate\Database\Eloquent\Collection;
class AddApiAttributes
{
public function subscribe($events)
{
$events->listen(ApiRelationship::class, [$this, 'addReportsRelationship']);
$events->listen(WillSerializeData::class, [$this, 'loadReportsRelationship']);
$events->listen(BuildApiAction::class, [$this, 'includeReportsRelationship']);
$events->listen(ApiRelationship::class, [$this, 'addFlagsRelationship']);
$events->listen(WillSerializeData::class, [$this, 'loadFlagsRelationship']);
$events->listen(BuildApiAction::class, [$this, 'includeFlagsRelationship']);
$events->listen(ApiAttributes::class, [$this, 'addAttributes']);
$events->listen(RegisterApiRoutes::class, [$this, 'addRoutes']);
}
public function loadReportsRelationship(WillSerializeData $event)
public function loadFlagsRelationship(WillSerializeData $event)
{
// For any API action that allows the 'reports' relationship to be
// For any API action that allows the 'flags' relationship to be
// included, we need to preload this relationship onto the data (Post
// models) so that we can selectively expose only the reports that the
// models) so that we can selectively expose only the flags that the
// user has permission to view.
if ($event->action instanceof Discussions\ShowAction) {
$discussion = $event->data;
@@ -53,9 +53,9 @@ class AddApiAttributes
$posts = [$event->data];
}
if ($event->action instanceof ReportsCreateAction) {
$report = $event->data;
$posts = [$report->post];
if ($event->action instanceof FlagsCreateAction) {
$flag = $event->data;
$posts = [$flag->post];
}
if (isset($posts)) {
@@ -63,67 +63,67 @@ class AddApiAttributes
$postsWithPermission = [];
foreach ($posts as $post) {
$post->setRelation('reports', null);
$post->setRelation('flags', null);
if ($post->discussion->can($actor, 'viewReports')) {
if ($post->discussion->can($actor, 'viewFlags')) {
$postsWithPermission[] = $post;
}
}
if (count($postsWithPermission)) {
(new Collection($postsWithPermission))
->load('reports', 'reports.user');
->load('flags', 'flags.user');
}
}
}
public function addReportsRelationship(ApiRelationship $event)
public function addFlagsRelationship(ApiRelationship $event)
{
if ($event->serializer instanceof PostSerializer &&
$event->relationship === 'reports') {
return $event->serializer->hasMany('Flarum\Reports\Api\ReportSerializer', 'reports');
$event->relationship === 'flags') {
return $event->serializer->hasMany('Flarum\Flags\Api\FlagSerializer', 'flags');
}
}
public function includeReportsRelationship(BuildApiAction $event)
public function includeFlagsRelationship(BuildApiAction $event)
{
if ($event->action instanceof Discussions\ShowAction) {
$event->addInclude('posts.reports');
$event->addInclude('posts.reports.user');
$event->addInclude('posts.flags');
$event->addInclude('posts.flags.user');
}
if ($event->action instanceof Posts\IndexAction ||
$event->action instanceof Posts\ShowAction) {
$event->addInclude('reports');
$event->addInclude('reports.user');
$event->addInclude('flags');
$event->addInclude('flags.user');
}
}
public function addAttributes(ApiAttributes $event)
{
if ($event->serializer instanceof ForumSerializer) {
$event->attributes['canViewReports'] = $event->actor->hasPermissionLike('discussion.viewReports');
$event->attributes['canViewFlags'] = $event->actor->hasPermissionLike('discussion.viewFlags');
if ($event->attributes['canViewReports']) {
$query = Report::whereVisibleTo($event->actor);
if ($event->attributes['canViewFlags']) {
$query = Flag::whereVisibleTo($event->actor);
if ($time = $event->actor->reports_read_time) {
$query->where('reports.time', '>', $time);
if ($time = $event->actor->flags_read_time) {
$query->where('flags.time', '>', $time);
}
$event->attributes['unreadReportsCount'] = $query->distinct('reports.post_id')->count();
$event->attributes['unreadFlagsCount'] = $query->distinct('flags.post_id')->count();
}
}
if ($event->serializer instanceof PostSerializer) {
$event->attributes['canReport'] = $event->model->can($event->actor, 'report');
$event->attributes['canFlag'] = $event->model->can($event->actor, 'flag');
}
}
public function addRoutes(RegisterApiRoutes $event)
{
$event->get('/reports', 'reports.index', 'Flarum\Reports\Api\IndexAction');
$event->post('/reports', 'reports.create', 'Flarum\Reports\Api\CreateAction');
$event->delete('/posts/{id}/reports', 'reports.delete', 'Flarum\Reports\Api\DeleteAction');
$event->get('/flags', 'flags.index', 'Flarum\Flags\Api\IndexAction');
$event->post('/flags', 'flags.create', 'Flarum\Flags\Api\CreateAction');
$event->delete('/posts/{id}/flags', 'flags.delete', 'Flarum\Flags\Api\DeleteAction');
}
}

View File

@@ -8,7 +8,7 @@
* file that was distributed with this source code.
*/
namespace Flarum\Reports\Listeners;
namespace Flarum\Flags\Listeners;
use Flarum\Events\RegisterLocales;
use Flarum\Events\BuildClientView;
@@ -34,16 +34,16 @@ class AddClientAssets
__DIR__.'/../../less/forum/extension.less'
]);
$event->forumBootstrapper('reports/main');
$event->forumBootstrapper('flags/main');
$event->forumTranslations([
'reports.reason_off_topic',
'reports.reason_spam',
'reports.reason_inappropriate',
'reports.reason_other',
'reports.reported_by',
'reports.reported_by_with_reason',
'reports.no_reports'
'flags.reason_off_topic',
'flags.reason_spam',
'flags.reason_inappropriate',
'flags.reason_other',
'flags.flagged_by',
'flags.flagged_by_with_reason',
'flags.no_flags'
]);
$event->adminAssets([
@@ -51,10 +51,10 @@ class AddClientAssets
__DIR__.'/../../less/admin/extension.less'
]);
$event->adminBootstrapper('reports/main');
$event->adminBootstrapper('flags/main');
$event->adminTranslations([
// 'report.hello_world'
// 'flag.hello_world'
]);
}
}

View File

@@ -1,4 +1,4 @@
<?php
<?php
/*
* This file is part of Flarum.
*
@@ -8,33 +8,40 @@
* file that was distributed with this source code.
*/
namespace Flarum\Reports\Listeners;
namespace Flarum\Flags\Listeners;
use Flarum\Events\ModelRelationship;
use Flarum\Events\ModelDates;
use Flarum\Events\PostWasDeleted;
use Flarum\Core\Posts\Post;
use Flarum\Core\Users\User;
use Flarum\Reports\Report;
use Flarum\Flags\Flag;
class AddModelRelationship
{
public function subscribe($events)
{
$events->listen(ModelRelationship::class, [$this, 'addReportsRelationship']);
$events->listen(ModelRelationship::class, [$this, 'addFlagsRelationship']);
$events->listen(ModelDates::class, [$this, 'modelDates']);
$events->listen(PostWasDeleted::class, [$this, 'deleteFlags']);
}
public function addReportsRelationship(ModelRelationship $event)
public function addFlagsRelationship(ModelRelationship $event)
{
if ($event->model instanceof Post && $event->relationship === 'reports') {
return $event->model->hasMany('Flarum\Reports\Report', 'post_id');
if ($event->model instanceof Post && $event->relationship === 'flags') {
return $event->model->hasMany('Flarum\Flags\Flag', 'post_id');
}
}
public function modelDates(ModelDates $event)
{
if ($event->model instanceof User) {
$event->dates[] = 'reports_read_time';
$event->dates[] = 'flags_read_time';
}
}
public function deleteFlags(PostWasDeleted $event)
{
$event->post->flags()->delete();
}
}