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

138 lines
4.6 KiB
PHP
Raw Normal View History

<?php
namespace App\Http\Controllers;
use App\Models\User;
use App\Models\Report;
use App\Models\Texture;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;
class ReportController extends Controller
{
public function submit(Request $request)
{
$data = $this->validate($request, [
'tid' => 'required|exists:textures',
2019-04-19 19:36:36 +08:00
'reason' => 'required',
]);
$reporter = auth()->user();
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();
$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();
return json(trans('skinlib.report.success'), 0);
}
public function track()
{
return Report::where('reporter', auth()->id())
->orderBy('report_at', 'desc')
->get();
}
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'])
->map(function ($report) {
$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,
];
}
public function review(Request $request)
{
$data = $this->validate($request, [
'id' => 'required|exists:reports',
2019-04-19 19:36:36 +08:00
'action' => ['required', Rule::in(['delete', 'ban', 'reject'])],
]);
$report = Report::find($data['id']);
if ($data['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
return json(trans('general.op-success'), 0, ['status' => Report::REJECTED]);
}
switch ($data['action']) {
case 'delete':
$report->texture->delete();
break;
case 'ban':
2019-05-19 14:04:32 +08:00
$uploader = User::find($report->uploader);
if (! $uploader) {
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();
break;
}
2019-05-08 12:51:48 +08:00
if ($report->status == Report::PENDING) {
if (($score = option('reporter_score_modification', 0)) < 0) {
$report->informer->score -= $score;
}
$report->informer->score += option('reporter_reward_score', 0);
$report->informer->save();
}
$report->status = Report::RESOLVED;
$report->save();
return json(trans('general.op-success'), 0, ['status' => Report::RESOLVED]);
}
}