blessing-skin-server/app/Http/Controllers/ReportController.php

182 lines
6.3 KiB
PHP
Raw Normal View History

<?php
namespace App\Http\Controllers;
use App\Models\Report;
use App\Models\Texture;
2019-12-14 11:10:37 +08:00
use App\Models\User;
2019-11-20 15:01:09 +08:00
use App\Services\Filter;
use App\Services\Rejection;
2019-12-14 11:10:37 +08:00
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;
class ReportController extends Controller
{
2019-11-20 15:01:09 +08:00
public function submit(Request $request, Dispatcher $dispatcher, Filter $filter)
{
$data = $this->validate($request, [
'tid' => 'required|exists:textures',
2019-04-19 19:36:36 +08:00
'reason' => 'required',
]);
$reporter = auth()->user();
2019-11-20 15:01:09 +08:00
$can = $filter->apply('user_can_report', true, [$data['tid'], $data['reason'], $reporter]);
if ($can instanceof Rejection) {
return json($can->getReason(), 1);
}
$dispatcher->dispatch('report.submitting', [$data['tid'], $data['reason'], $reporter]);
if (Report::where('reporter', $reporter->uid)->where('tid', $data['tid'])->count() > 0) {
return json(trans('skinlib.report.duplicate'), 1);
}
$score = option('reporter_score_modification', 0);
if ($score < 0 && $reporter->score < -$score) {
return json(trans('skinlib.upload.lack-score'), 1);
}
$reporter->score += $score;
$reporter->save();
2019-12-14 11:10:37 +08:00
$report = new Report();
$report->tid = $data['tid'];
$report->uploader = Texture::find($data['tid'])->uploader;
$report->reporter = $reporter->uid;
$report->reason = $data['reason'];
$report->status = Report::PENDING;
$report->save();
2019-11-20 15:01:09 +08:00
$dispatcher->dispatch('report.submitted', [$report]);
return json(trans('skinlib.report.success'), 0);
}
public function track()
{
return Report::where('reporter', auth()->id())
->orderBy('report_at', 'desc')
->get();
}
2019-09-07 11:15:23 +08:00
public function manage(Request $request)
{
$search = $request->input('search', '');
$sortField = $request->input('sortField', 'report_at');
$sortType = $request->input('sortType', 'desc');
$page = $request->input('page', 1);
$perPage = $request->input('perPage', 10);
$reports = Report::where('tid', 'like', '%'.$search.'%')
->orWhere('reporter', 'like', '%'.$search.'%')
->orWhere('reason', 'like', '%'.$search.'%')
->orderBy($sortField, $sortType)
->offset(($page - 1) * $perPage)
->limit($perPage)
->get()
->makeHidden(['informer'])
2019-09-07 11:15:23 +08:00
->map(function ($report) {
2019-08-24 10:22:26 +08:00
$uploader = User::find($report->uploader);
if ($uploader) {
$report->uploaderName = $uploader->nickname;
}
if ($report->informer) {
$report->reporterName = $report->informer->nickname;
}
2019-04-19 19:36:36 +08:00
return $report;
});
return [
'totalRecords' => Report::count(),
'data' => $reports,
];
}
2019-11-20 15:01:09 +08:00
public function review(Request $request, Dispatcher $dispatcher)
{
$data = $this->validate($request, [
'id' => 'required|exists:reports',
2019-04-19 19:36:36 +08:00
'action' => ['required', Rule::in(['delete', 'ban', 'reject'])],
]);
2019-11-20 15:01:09 +08:00
$action = $data['action'];
$report = Report::find($data['id']);
2019-11-20 15:01:09 +08:00
$dispatcher->dispatch('report.reviewing', [$report, $action]);
if ($action == 'reject') {
if (
$report->informer &&
2019-05-08 12:51:48 +08:00
($score = option('reporter_score_modification', 0)) > 0 &&
$report->status == Report::PENDING
) {
$report->informer->score -= $score;
$report->informer->save();
}
$report->status = Report::REJECTED;
$report->save();
2019-04-19 19:36:36 +08:00
2019-11-20 15:01:09 +08:00
$dispatcher->dispatch('report.rejected', [$report]);
return json(trans('general.op-success'), 0, ['status' => Report::REJECTED]);
}
2019-11-20 15:01:09 +08:00
switch ($action) {
case 'delete':
2019-11-20 15:01:09 +08:00
$texture = $report->texture;
if ($texture) {
$texture->delete();
$dispatcher->dispatch('texture.deleted', [$texture]);
2019-05-19 17:04:17 +08:00
} else {
// The texture has been deleted by its uploader
// We will return the score, but will not give the informer any reward
self::returnScore($report);
$report->status = Report::RESOLVED;
$report->save();
2019-11-20 15:01:09 +08:00
$dispatcher->dispatch('report.resolved', [$report, $action]);
2019-05-19 17:04:17 +08:00
return json(trans('general.texture-deleted'), 0, ['status' => Report::RESOLVED]);
}
break;
case 'ban':
2019-08-24 10:22:26 +08:00
$uploader = User::find($report->uploader);
2019-12-14 11:10:37 +08:00
if (!$uploader) {
2019-05-19 14:04:32 +08:00
return json(trans('admin.users.operations.non-existent'), 1);
}
if (auth()->user()->permission <= $uploader->permission) {
return json(trans('admin.users.operations.no-permission'), 1);
}
2019-05-19 14:04:32 +08:00
$uploader->permission = User::BANNED;
$uploader->save();
2019-11-20 15:01:09 +08:00
$dispatcher->dispatch('user.banned', [$uploader]);
break;
}
2019-05-19 17:04:17 +08:00
self::returnScore($report);
self::giveAward($report);
$report->status = Report::RESOLVED;
$report->save();
2019-11-20 15:01:09 +08:00
$dispatcher->dispatch('report.resolved', [$report, $action]);
return json(trans('general.op-success'), 0, ['status' => Report::RESOLVED]);
}
2019-05-19 17:04:17 +08:00
public static function returnScore($report)
{
2019-05-19 17:04:17 +08:00
if ($report->status == Report::PENDING && ($score = option('reporter_score_modification', 0)) < 0) {
$report->informer->score -= $score;
$report->informer->save();
}
}
public static function giveAward($report)
{
2019-05-19 17:04:17 +08:00
if ($report->status == Report::PENDING) {
$report->informer->score += option('reporter_reward_score', 0);
$report->informer->save();
}
}
}