Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Events and APIs #1

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use App\Models\Texture;
use App\Services\Hook;
use Blessing\Filter;
use Blessing\Rejection;
Expand All @@ -12,7 +13,9 @@
use LittleSkin\TextureModeration\RecordSource;

return function (Filter $filter, Dispatcher $events) {
$events->listen('texture.uploaded', OnTextureUploaded::class);
$events->listen('texture.uploaded', function (Texture $texture) use ($events) {
return OnTextureUploaded::handle($texture, $events);
});
$events->listen('texture.deleted', OnTextureDeleted::class);

$filter->add('can_update_texture_privacy', function ($can, $texture) {
Expand Down Expand Up @@ -41,7 +44,7 @@
->prefix('admin/texture-moderation')
->group(function () {
Route::get('', 'TextureModerationController@show');
Route::post('review', 'TextureModerationController@review');
Route::put('review/{record}', 'TextureModerationController@review');
Route::get('list', 'TextureModerationController@manage');
});

Expand All @@ -53,6 +56,14 @@
Route::post('', 'WhitelistController@add');
Route::delete('', 'WhitelistController@delete');
});

Route::namespace('LittleSkin\TextureModeration\Controllers')
->middleware(['api', 'auth:oauth', 'role:admin'])
->prefix('api/admin/texture-moderation')
->group(function () {
Route::get('', 'TextureModerationController@manage')->middleware(['scope:TextureModeration.Read,TextureModeration.ReadWrite']);
Route::put('{record}', 'TextureModerationController@review')->middleware(['scope:TextureModeration.ReadWrite']);
});
});

Hook::addMenuItem('admin', 4001, [
Expand Down
16 changes: 16 additions & 0 deletions callbacks.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

use App\Models\Scope;

return [
App\Events\PluginWasEnabled::class => function () {
if (!Schema::hasTable('moderation_whitelist')) {
Expand All @@ -25,5 +27,19 @@
$table->timestamps();
});
}

if (!Scope::where('name', 'TextureModeration.Read')->exists()) {
Scope::create([
'name' => 'TextureModeration.Read',
'description' => 'Ability to read texture moderation records'
]);
}

if (!Scope::where('name', 'TextureModeration.ReadWrite')->exists()) {
Scope::create([
'name' => 'TextureModeration.ReadWrite',
'description' => 'Ability to read and write texture moderation records'
]);
}
},
];
16 changes: 15 additions & 1 deletion src/Controllers/ModerationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Models\Texture;
use Blessing\Rejection;
use Exception;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Storage;
use LittleSkin\TextureModeration\Models\ModerationRecord;
Expand All @@ -16,12 +17,14 @@

class ModerationController extends Controller
{
public static function start(Texture $texture, $source)
public static function start(Texture $texture, $source, Dispatcher $dispatcher)
{
$disk = Storage::disk('textures');
$hash = $texture->hash;
$file = $disk->get($hash);

$dispatcher->dispatch('texture-moderation.starting', [$texture]);

$record = new ModerationRecord();
$record->tid = $texture->tid;
$record->source = $source;
Expand All @@ -30,6 +33,8 @@ public static function start(Texture $texture, $source)
$record->review_state = ReviewState::MISS;
$record->save();

$dispatcher->dispatch('texture-moderation.finished', [$record]);

return;
}

Expand All @@ -38,6 +43,8 @@ public static function start(Texture $texture, $source)
$record->review_state = ReviewState::USER;
$record->save();

$dispatcher->dispatch('texture-moderation.finished', [$record]);

return;
}

Expand All @@ -50,6 +57,8 @@ public static function start(Texture $texture, $source)
$record = $itsRecord->review_state;
$record->save();

$dispatcher->dispatch('texture-moderation.finished', [$record]);

return;
}
}
Expand Down Expand Up @@ -96,14 +105,19 @@ public static function start(Texture $texture, $source)
$record->review_state = ReviewState::APPROVED;
$record->save();

$dispatcher->dispatch('texture-moderation.finished', [$record]);

return;
}

$texture->public = false;
$texture->save();

$record->review_state = ReviewState::MANUAL;
$record->save();

$dispatcher->dispatch('texture-moderation.onegai', [$record]);

return new Rejection(trans('LittleSkin\TextureModeration::skinlib.manual_tip'));
}
}
150 changes: 69 additions & 81 deletions src/Controllers/TextureModerationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Models\Texture;
use App\Services\Hook;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Auth;
Expand Down Expand Up @@ -34,129 +35,116 @@ public function manage(Request $request)
$q = $request->input('q');

return ModerationRecord::usingSearchString($q)
->leftJoin('users as operator', 'operator.uid', '=', 'moderation_records.operator')
->leftJoin('textures', 'textures.tid', '=', 'moderation_records.tid')
->leftJoin('users', 'users.uid', '=', 'textures.uploader')
->select(['textures.uploader', 'users.uid', 'users.nickname', 'moderation_records.*', 'operator.nickname as operator_nickname'])
->with(['texture:tid,name,type,uploader', 'texture.owner:uid,nickname', 'operator:uid,nickname'])
->paginate(9);
}

public function review(Request $request)
public function review(ModerationRecord $record, Request $request, Dispatcher $dispatcher)
{
$data = $request->validate([
'id' => ['required', 'integer'],
'action' => ['required', Rule::in(['approve', 'reject', 'private'])],
]);

$tid = $data['id'];
$tid = $record->tid;
$action = $data['action'];

$record->operator = Auth::user()->uid;

switch ($action) {
case 'approve':
$record = ModerationRecord::where('tid', $tid)->first();
$record->review_state = ReviewState::APPROVED;

if ($record) {
$record->operator = Auth::user()->uid;
$record->review_state = ReviewState::APPROVED;
$record->save();

$record->save();
$texture = Texture::where('tid', $tid)->first();
$texture->public = true;

$texture = Texture::where('tid', $tid)->first();
$texture->public = true;
$texture->save();

$texture->save();
$dispatcher->dispatch('texture-moderation.finished', [$record]);

$uploader = $texture->owner;
if ($uploader) {
Hook::sendNotification([$uploader], trans('LittleSkin\TextureModeration::skinlib.notification.title'), trans('LittleSkin\TextureModeration::skinlib.notification.approved', [
'name' => $texture->name,
]));
}

return json(trans('general.op-success'), 0);
} else {
return json(trans('LittleSkin\TextureModeration::manage.message.texture-not-exist'), 1);
$uploader = $texture->owner;
if ($uploader) {
Hook::sendNotification([$uploader], trans('LittleSkin\TextureModeration::skinlib.notification.title'), trans('LittleSkin\TextureModeration::skinlib.notification.approved', [
'name' => $texture->name,
]));
}

return json(trans('general.op-success'), 0);

break;
case 'reject':
$record = ModerationRecord::where('tid', $tid)->first();
$record->review_state = ReviewState::REJECTED;

if ($record) {
$record->operator = Auth::user()->uid;
$record->review_state = ReviewState::REJECTED;
$record->save();

$record->save();

$texture = Texture::where('tid', $tid)->first();

if($record->source === RecordSource::ON_PRIVACY_UPDATED){
$texture->public = false;
$texture->save();

return json(trans('LittleSkin\TextureModeration::manage.message.keep-privacy'), 0);
}
$texture = Texture::where('tid', $tid)->first();

$texture->delete();
if ($record->source === RecordSource::ON_PRIVACY_UPDATED) {
$texture->public = false;
$texture->save();

$uploader = $texture->owner;
if ($uploader) {
$uploader->score += $texture->size * option('score_per_storage');
$uploader->save();
$dispatcher->dispatch('texture-moderation.finished', [$record]);

Hook::sendNotification([$uploader], trans('LittleSkin\TextureModeration::skinlib.notification.title'), trans('LittleSkin\TextureModeration::skinlib.notification.deleted', [
'name' => $texture->name,
]));
}
return json(trans('LittleSkin\TextureModeration::manage.message.keep-privacy'), 0);
}

$texture->delete();

$uploader = $texture->owner;
if ($uploader) {
$uploader->score += $texture->size * option('score_per_storage');
$uploader->save();

return json(trans('LittleSkin\TextureModeration::manage.message.deleted'), 0);
} else {
return json(trans('LittleSkin\TextureModeration::manage.message.texture-not-exist'), 1);
Hook::sendNotification([$uploader], trans('LittleSkin\TextureModeration::skinlib.notification.title'), trans('LittleSkin\TextureModeration::skinlib.notification.deleted', [
'name' => $texture->name,
]));
}

break;
$dispatcher->dispatch('texture-moderation.finished', [$record]);

return json(trans('LittleSkin\TextureModeration::manage.message.deleted'), 0);

break;
case 'private':
$record = ModerationRecord::where('tid', $tid)->first();
$record->review_state = ReviewState::REJECTED;

if ($record) {
$record->operator = Auth::user()->uid;
$record->review_state = ReviewState::REJECTED;
$record->save();

$record->save();
$texture = Texture::where('tid', $tid)->first();

$texture = Texture::where('tid', $tid)->first();
$uploader = $texture->owner;
if ($uploader) {
$diff = $texture->size * (option('private_score_per_storage') - option('score_per_storage'));

$uploader = $texture->owner;
if ($uploader) {
$diff = $texture->size * (option('private_score_per_storage') - option('score_per_storage'));
if ($uploader->score >= $diff) {
$uploader->score -= $diff;
$uploader->save();

if ($uploader->score >= $diff) {
$uploader->score -= $diff;
$uploader->save();
$texture->public = false;
$texture->save();

$texture->public = false;
$texture->save();
Hook::sendNotification([$uploader], trans('LittleSkin\TextureModeration::skinlib.notification.title'), trans('LittleSkin\TextureModeration::skinlib.notification.private', [
'name' => $texture->name,
]));

Hook::sendNotification([$uploader], trans('LittleSkin\TextureModeration::skinlib.notification.title'), trans('LittleSkin\TextureModeration::skinlib.notification.private', [
'name' => $texture->name,
]));
$dispatcher->dispatch('texture-moderation.finished', [$record]);

return json(trans('LittleSkin\TextureModeration::manage.message.privacy'), 0);
} else {
$uploader->score += $texture->size * option('score_per_storage');
$uploader->save();

return json(trans('LittleSkin\TextureModeration::manage.message.privacy'), 0);
} else {
$uploader->score += $texture->size * option('score_per_storage');
$uploader->save();
$texture->delete();

$texture->delete();
Hook::sendNotification([$uploader], trans('LittleSkin\TextureModeration::skinlib.notification.title'), trans('LittleSkin\TextureModeration::skinlib.notification.deleted', [
'name' => $texture->name,
]));

Hook::sendNotification([$uploader], trans('LittleSkin\TextureModeration::skinlib.notification.title'), trans('LittleSkin\TextureModeration::skinlib.notification.deleted', [
'name' => $texture->name,
]));
$dispatcher->dispatch('texture-moderation.finished', [$record]);

return json(trans('LittleSkin\TextureModeration::manage.message.privacy-failed'), 1);
}
return json(trans('LittleSkin\TextureModeration::manage.message.privacy-failed'), 1);
}
} else {
return json(trans('LittleSkin\TextureModeration::manage.message.texture-not-exist'), 1);
}

break;
Expand Down
5 changes: 3 additions & 2 deletions src/Listeners/OnTextureUploaded.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
namespace LittleSkin\TextureModeration\Listeners;

use App\Models\Texture;
use Illuminate\Contracts\Events\Dispatcher;
use LittleSkin\TextureModeration\Controllers\ModerationController;
use LittleSkin\TextureModeration\RecordSource;

class OnTextureUploaded
{
public function handle(Texture $texture)
public static function handle(Texture $texture, Dispatcher $dispatcher)
{
if ($texture->public) {
return ModerationController::start($texture, RecordSource::ON_PUBLIC_UPLOAD);
return ModerationController::start($texture, RecordSource::ON_PUBLIC_UPLOAD, $dispatcher);
}
}
}
7 changes: 6 additions & 1 deletion src/Models/ModerationRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ class ModerationRecord extends Model

public function texture()
{
return $this->belongsTo('App\Models\Texture');
return $this->belongsTo('App\Models\Texture', 'tid', 'tid');
}

public function operator()
{
return $this->belongsTo('App\Models\User', 'operator', 'uid');
}
}
Loading