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

Initial commit

This commit is contained in:
Toby Zerner
2015-09-04 13:26:51 +09:30
commit cb6347ef6a
44 changed files with 1633 additions and 0 deletions

View File

@@ -0,0 +1,58 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Flarum\Reports\Api;
use Flarum\Reports\Commands\CreateReport;
use Flarum\Api\Actions\CreateAction as BaseCreateAction;
use Flarum\Api\JsonApiRequest;
use Illuminate\Contracts\Bus\Dispatcher;
class CreateAction extends BaseCreateAction
{
/**
* @var Dispatcher
*/
protected $bus;
/**
* @inheritdoc
*/
public $serializer = 'Flarum\Reports\Api\ReportSerializer';
/**
* @inheritdoc
*/
public $include = [
'post' => true,
'post.reports' => true
];
/**
* @param Dispatcher $bus
*/
public function __construct(Dispatcher $bus)
{
$this->bus = $bus;
}
/**
* Create a report according to input from the API request.
*
* @param JsonApiRequest $request
* @return \Flarum\Reports\Report
*/
protected function create(JsonApiRequest $request)
{
return $this->bus->dispatch(
new CreateReport($request->actor, $request->get('data'))
);
}
}

View File

@@ -0,0 +1,44 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Flarum\Reports\Api;
use Flarum\Reports\Commands\DeleteReports;
use Flarum\Api\Actions\DeleteAction as BaseDeleteAction;
use Flarum\Api\Request;
use Illuminate\Contracts\Bus\Dispatcher;
class DeleteAction extends BaseDeleteAction
{
/**
* @var Dispatcher
*/
protected $bus;
/**
* @param Dispatcher $bus
*/
public function __construct(Dispatcher $bus)
{
$this->bus = $bus;
}
/**
* Delete reports for a post.
*
* @param Request $request
*/
protected function delete(Request $request)
{
$this->bus->dispatch(
new DeleteReports($request->get('id'), $request->actor, $request->all())
);
}
}

View File

@@ -0,0 +1,53 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Flarum\Reports\Api;
use Flarum\Api\Actions\SerializeCollectionAction;
use Flarum\Api\JsonApiRequest;
use Flarum\Reports\Report;
use Tobscure\JsonApi\Document;
class IndexAction extends SerializeCollectionAction
{
/**
* @inheritdoc
*/
public $serializer = 'Flarum\Reports\Api\ReportSerializer';
/**
* @inheritdoc
*/
public $include = [
'user' => true,
'post' => true,
'post.user' => true,
'post.discussion' => true
];
/**
* @inheritdoc
*/
public $link = [];
protected function data(JsonApiRequest $request, Document $document)
{
$actor = $request->actor;
$actor->reports_read_time = time();
$actor->save();
return Report::whereVisibleTo($actor)
->with($request->include)
->latest('reports.time')
->groupBy('post_id')
->get();
}
}

View File

@@ -0,0 +1,37 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Flarum\Reports\Api;
use Flarum\Api\Serializers\Serializer;
class ReportSerializer extends Serializer
{
protected $type = 'reports';
protected function getDefaultAttributes($report)
{
return [
'reporter' => $report->reporter,
'reason' => $report->reason,
'reasonDetail' => $report->reason_detail,
];
}
protected function post()
{
return $this->hasOne('Flarum\Api\Serializers\PostSerializer');
}
protected function user()
{
return $this->hasOne('Flarum\Api\Serializers\UserBasicSerializer');
}
}

View File

@@ -0,0 +1,40 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Flarum\Reports\Commands;
use Flarum\Core\Users\User;
class CreateReport
{
/**
* The user performing the action.
*
* @var User
*/
public $actor;
/**
* The attributes of the new report.
*
* @var array
*/
public $data;
/**
* @param User $actor The user performing the action.
* @param array $data The attributes of the new report.
*/
public function __construct(User $actor, array $data)
{
$this->actor = $actor;
$this->data = $data;
}
}

View File

@@ -0,0 +1,63 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Flarum\Reports\Commands;
use Flarum\Reports\Report;
use Flarum\Core\Posts\PostRepository;
use Flarum\Core\Posts\CommentPost;
use Exception;
class CreateReportHandler
{
private $posts;
public function __construct(PostRepository $posts)
{
$this->posts = $posts;
}
/**
* @param CreateReport $command
* @return Report
*/
public function handle(CreateReport $command)
{
$actor = $command->actor;
$data = $command->data;
$postId = array_get($data, 'relationships.post.data.id');
$post = $this->posts->findOrFail($postId, $actor);
if (! ($post instanceof CommentPost)) {
// TODO: throw 400(?) error
throw new Exception;
}
$post->assertCan($actor, 'report');
Report::unguard();
$report = Report::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();
$report->save();
return $report;
}
}

View File

@@ -0,0 +1,48 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Flarum\Reports\Commands;
use Flarum\Reports\Report;
use Flarum\Core\Users\User;
class DeleteReports
{
/**
* The ID of the post to delete reports for.
*
* @var int
*/
public $postId;
/**
* The user performing the action.
*
* @var User
*/
public $actor;
/**
* @var array
*/
public $data;
/**
* @param int $postId The ID of the post to delete reports for.
* @param User $actor The user performing the action.
* @param array $data
*/
public function __construct($postId, User $actor, array $data = [])
{
$this->postId = $postId;
$this->actor = $actor;
$this->data = $data;
}
}

View File

@@ -0,0 +1,45 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Flarum\Reports\Commands;
use Flarum\Reports\Report;
use Flarum\Core\Posts\PostRepository;
use Flarum\Reports\Events\ReportsWillBeDeleted;
class DeleteReportsHandler
{
protected $posts;
public function __construct(PostRepository $posts)
{
$this->posts = $posts;
}
/**
* @param DeleteReport $command
* @return Report
* @throws \Flarum\Core\Exceptions\PermissionDeniedException
*/
public function handle(DeleteReports $command)
{
$actor = $command->actor;
$post = $this->posts->findOrFail($command->postId, $actor);
$post->discussion->assertCan($actor, 'viewReports');
event(new ReportsWillBeDeleted($post, $actor, $command->data));
$post->reports()->delete();
return $post;
}
}

View File

@@ -0,0 +1,45 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Flarum\Reports\Events;
use Flarum\Core\Posts\Post;
use Flarum\Core\Users\User;
class ReportsWillBeDeleted
{
/**
* @var Post
*/
public $post;
/**
* @var User
*/
public $actor;
/**
* @var array
*/
public $data;
/**
* @param Post $post
* @param User $actor
* @param array $data
*/
public function __construct(Post $post, User $actor, array $data = [])
{
$this->post = $post;
$this->actor = $actor;
$this->data = $data;
}
}

View File

@@ -0,0 +1,24 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Flarum\Reports;
use Flarum\Support\Extension as BaseExtension;
use Illuminate\Events\Dispatcher;
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');
}
}

View File

@@ -0,0 +1,129 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Flarum\Reports\Listeners;
use Flarum\Events\ApiRelationship;
use Flarum\Events\WillSerializeData;
use Flarum\Events\BuildApiAction;
use Flarum\Events\ApiAttributes;
use Flarum\Events\RegisterApiRoutes;
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 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(ApiAttributes::class, [$this, 'addAttributes']);
$events->listen(RegisterApiRoutes::class, [$this, 'addRoutes']);
}
public function loadReportsRelationship(WillSerializeData $event)
{
// For any API action that allows the 'reports' 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
// user has permission to view.
if ($event->action instanceof Discussions\ShowAction) {
$discussion = $event->data;
$posts = $discussion->posts->all();
}
if ($event->action instanceof Posts\IndexAction) {
$posts = $event->data->all();
}
if ($event->action instanceof Posts\ShowAction) {
$posts = [$event->data];
}
if ($event->action instanceof ReportsCreateAction) {
$report = $event->data;
$posts = [$report->post];
}
if (isset($posts)) {
$actor = $event->request->actor;
$postsWithPermission = [];
foreach ($posts as $post) {
$post->setRelation('reports', null);
if ($post->discussion->can($actor, 'viewReports')) {
$postsWithPermission[] = $post;
}
}
if (count($postsWithPermission)) {
(new Collection($postsWithPermission))
->load('reports', 'reports.user');
}
}
}
public function addReportsRelationship(ApiRelationship $event)
{
if ($event->serializer instanceof PostSerializer &&
$event->relationship === 'reports') {
return $event->serializer->hasMany('Flarum\Reports\Api\ReportSerializer', 'reports');
}
}
public function includeReportsRelationship(BuildApiAction $event)
{
if ($event->action instanceof Discussions\ShowAction) {
$event->addInclude('posts.reports');
$event->addInclude('posts.reports.user');
}
if ($event->action instanceof Posts\IndexAction ||
$event->action instanceof Posts\ShowAction) {
$event->addInclude('reports');
$event->addInclude('reports.user');
}
}
public function addAttributes(ApiAttributes $event)
{
if ($event->serializer instanceof ForumSerializer) {
$event->attributes['canViewReports'] = $event->actor->hasPermissionLike('discussion.viewReports');
if ($event->attributes['canViewReports']) {
$query = Report::whereVisibleTo($event->actor);
if ($time = $event->actor->reports_read_time) {
$query->where('reports.time', '>', $time);
}
$event->attributes['unreadReportsCount'] = $query->distinct('reports.post_id')->count();
}
}
if ($event->serializer instanceof PostSerializer) {
$event->attributes['canReport'] = $event->model->can($event->actor, 'report');
}
}
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');
}
}

View File

@@ -0,0 +1,60 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Flarum\Reports\Listeners;
use Flarum\Events\RegisterLocales;
use Flarum\Events\BuildClientView;
use Illuminate\Contracts\Events\Dispatcher;
class AddClientAssets
{
public function subscribe(Dispatcher $events)
{
$events->listen(RegisterLocales::class, [$this, 'addLocale']);
$events->listen(BuildClientView::class, [$this, 'addAssets']);
}
public function addLocale(RegisterLocales $event)
{
$event->addTranslations('en', __DIR__.'/../../locale/en.yml');
}
public function addAssets(BuildClientView $event)
{
$event->forumAssets([
__DIR__.'/../../js/forum/dist/extension.js',
__DIR__.'/../../less/forum/extension.less'
]);
$event->forumBootstrapper('reports/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'
]);
$event->adminAssets([
__DIR__.'/../../js/admin/dist/extension.js',
__DIR__.'/../../less/admin/extension.less'
]);
$event->adminBootstrapper('reports/main');
$event->adminTranslations([
// 'report.hello_world'
]);
}
}

View File

@@ -0,0 +1,40 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Flarum\Reports\Listeners;
use Flarum\Events\ModelRelationship;
use Flarum\Events\ModelDates;
use Flarum\Core\Posts\Post;
use Flarum\Core\Users\User;
use Flarum\Reports\Report;
class AddModelRelationship
{
public function subscribe($events)
{
$events->listen(ModelRelationship::class, [$this, 'addReportsRelationship']);
$events->listen(ModelDates::class, [$this, 'modelDates']);
}
public function addReportsRelationship(ModelRelationship $event)
{
if ($event->model instanceof Post && $event->relationship === 'reports') {
return $event->model->hasMany('Flarum\Reports\Report', 'post_id');
}
}
public function modelDates(ModelDates $event)
{
if ($event->model instanceof User) {
$event->dates[] = 'reports_read_time';
}
}
}

View File

@@ -0,0 +1,33 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Flarum\Reports;
use Flarum\Core\Model;
use Flarum\Core\Support\VisibleScope;
class Report extends Model
{
use VisibleScope;
protected $table = 'reports';
protected $dates = ['time'];
public function post()
{
return $this->belongsTo('Flarum\Core\Posts\Post');
}
public function user()
{
return $this->belongsTo('Flarum\Core\Users\User');
}
}