mirror of
https://github.com/bs-community/blessing-skin-server.git
synced 2025-01-09 04:27:47 +08:00
refactor
This commit is contained in:
parent
63556cabae
commit
5e051eadfe
@ -5,7 +5,6 @@ namespace App\Http\Controllers;
|
||||
use App\Models\Player;
|
||||
use App\Models\Texture;
|
||||
use App\Models\User;
|
||||
use App\Notifications;
|
||||
use App\Services\OptionForm;
|
||||
use App\Services\PluginManager;
|
||||
use Auth;
|
||||
@ -17,7 +16,6 @@ use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Str;
|
||||
use Notification;
|
||||
use Option;
|
||||
|
||||
class AdminController extends Controller
|
||||
@ -98,39 +96,6 @@ class AdminController extends Controller
|
||||
];
|
||||
}
|
||||
|
||||
public function sendNotification(Request $request)
|
||||
{
|
||||
$data = $this->validate($request, [
|
||||
'receiver' => 'required|in:all,normal,uid,email',
|
||||
'uid' => 'required_if:receiver,uid|nullable|integer|exists:users',
|
||||
'email' => 'required_if:receiver,email|nullable|email|exists:users',
|
||||
'title' => 'required|max:20',
|
||||
'content' => 'string|nullable',
|
||||
]);
|
||||
|
||||
$notification = new Notifications\SiteMessage($data['title'], $data['content']);
|
||||
|
||||
switch ($data['receiver']) {
|
||||
case 'all':
|
||||
$users = User::all();
|
||||
break;
|
||||
case 'normal':
|
||||
$users = User::where('permission', User::NORMAL)->get();
|
||||
break;
|
||||
case 'uid':
|
||||
$users = User::where('uid', $data['uid'])->get();
|
||||
break;
|
||||
case 'email':
|
||||
$users = User::where('email', $data['email'])->get();
|
||||
break;
|
||||
}
|
||||
Notification::send($users, $notification);
|
||||
|
||||
session(['sentResult' => trans('admin.notifications.send.success')]);
|
||||
|
||||
return redirect('/admin');
|
||||
}
|
||||
|
||||
public function customize(Request $request)
|
||||
{
|
||||
$homepage = Option::form('homepage', OptionForm::AUTO_DETECT, function ($form) {
|
||||
|
74
app/Http/Controllers/NotificationsController.php
Normal file
74
app/Http/Controllers/NotificationsController.php
Normal file
@ -0,0 +1,74 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\User;
|
||||
use App\Notifications;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Notification;
|
||||
use Parsedown;
|
||||
|
||||
class NotificationsController extends Controller
|
||||
{
|
||||
public function send(Request $request)
|
||||
{
|
||||
$data = $this->validate($request, [
|
||||
'receiver' => 'required|in:all,normal,uid,email',
|
||||
'uid' => 'required_if:receiver,uid|nullable|integer|exists:users',
|
||||
'email' => 'required_if:receiver,email|nullable|email|exists:users',
|
||||
'title' => 'required|max:20',
|
||||
'content' => 'string|nullable',
|
||||
]);
|
||||
|
||||
$notification = new Notifications\SiteMessage($data['title'], $data['content']);
|
||||
|
||||
switch ($data['receiver']) {
|
||||
case 'all':
|
||||
$users = User::all();
|
||||
break;
|
||||
case 'normal':
|
||||
$users = User::where('permission', User::NORMAL)->get();
|
||||
break;
|
||||
case 'uid':
|
||||
$users = User::where('uid', $data['uid'])->get();
|
||||
break;
|
||||
case 'email':
|
||||
$users = User::where('email', $data['email'])->get();
|
||||
break;
|
||||
}
|
||||
Notification::send($users, $notification);
|
||||
|
||||
session(['sentResult' => trans('admin.notifications.send.success')]);
|
||||
|
||||
return redirect('/admin');
|
||||
}
|
||||
|
||||
public function all()
|
||||
{
|
||||
return auth()->user()->unreadNotifications->map(function ($notification) {
|
||||
return [
|
||||
'id' => $notification->id,
|
||||
'title' => $notification->data['title'],
|
||||
];
|
||||
});
|
||||
}
|
||||
|
||||
public function read($id)
|
||||
{
|
||||
$notification = auth()
|
||||
->user()
|
||||
->unreadNotifications
|
||||
->first(function ($notification) use ($id) {
|
||||
return $notification->id === $id;
|
||||
});
|
||||
$notification->markAsRead();
|
||||
|
||||
$parsedown = new Parsedown();
|
||||
|
||||
return [
|
||||
'title' => $notification->data['title'],
|
||||
'content' => $parsedown->text($notification->data['content']),
|
||||
'time' => $notification->created_at->toDateTimeString(),
|
||||
];
|
||||
}
|
||||
}
|
@ -16,7 +16,6 @@ use Mail;
|
||||
use Parsedown;
|
||||
use Session;
|
||||
use URL;
|
||||
use View;
|
||||
|
||||
class UserController extends Controller
|
||||
{
|
||||
@ -370,23 +369,4 @@ class UserController extends Controller
|
||||
return json(trans('skinlib.non-existent'), 1);
|
||||
}
|
||||
}
|
||||
|
||||
public function readNotification($id)
|
||||
{
|
||||
$notification = auth()
|
||||
->user()
|
||||
->unreadNotifications
|
||||
->first(function ($notification) use ($id) {
|
||||
return $notification->id === $id;
|
||||
});
|
||||
$notification->markAsRead();
|
||||
|
||||
$parsedown = new Parsedown();
|
||||
|
||||
return [
|
||||
'title' => $notification->data['title'],
|
||||
'content' => $parsedown->text($notification->data['content']),
|
||||
'time' => $notification->created_at->toDateTimeString(),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
@ -10,6 +10,9 @@ Route::prefix('auth')->group(function () {
|
||||
|
||||
Route::prefix('user')->middleware('auth:jwt,oauth')->group(function () {
|
||||
Route::get('', 'UserController@user');
|
||||
|
||||
Route::get('notifications', 'NotificationsController@all');
|
||||
Route::get('notifications/{id}', 'NotificationsController@read');
|
||||
});
|
||||
|
||||
Route::prefix('players')->middleware('auth:jwt,oauth')->group(function () {
|
||||
@ -44,4 +47,6 @@ Route::prefix('admin')
|
||||
Route::post('{uid}', 'ClosetManagementController@add');
|
||||
Route::delete('{uid}', 'ClosetManagementController@remove');
|
||||
});
|
||||
|
||||
Route::post('notifications', 'NotificationsController@send');
|
||||
});
|
||||
|
@ -51,7 +51,7 @@ Route::prefix('user')
|
||||
->middleware(['authorize', Middleware\RequireBindPlayer::class])
|
||||
->group(function () {
|
||||
Route::get('', 'UserController@index')->name('home');
|
||||
Route::get('notifications/{id}', 'UserController@readNotification')->name('notification');
|
||||
Route::get('notifications/{id}', 'NotificationsController@read')->name('notification');
|
||||
Route::get('score-info', 'UserController@scoreInfo')->name('score');
|
||||
Route::post('sign', 'UserController@sign')->name('sign');
|
||||
|
||||
@ -118,7 +118,7 @@ Route::prefix('admin')
|
||||
->group(function () {
|
||||
Route::get('', 'AdminController@index');
|
||||
Route::get('chart', 'AdminController@chartData');
|
||||
Route::post('notifications/send', 'AdminController@sendNotification');
|
||||
Route::post('notifications/send', 'NotificationsController@send');
|
||||
|
||||
Route::any('customize', 'AdminController@customize');
|
||||
Route::any('score', 'AdminController@score');
|
||||
|
@ -5,11 +5,9 @@ namespace Tests;
|
||||
use App\Models\Player;
|
||||
use App\Models\Texture;
|
||||
use App\Models\User;
|
||||
use App\Notifications;
|
||||
use App\Services\Plugin;
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
use Illuminate\Support\Str;
|
||||
use Notification;
|
||||
|
||||
class AdminControllerTest extends TestCase
|
||||
{
|
||||
@ -43,88 +41,6 @@ class AdminControllerTest extends TestCase
|
||||
->assertJsonStructure(['labels', 'xAxis', 'data']);
|
||||
}
|
||||
|
||||
public function testSendNotification()
|
||||
{
|
||||
$admin = factory(User::class)->states('admin')->create();
|
||||
$normal = factory(User::class)->create();
|
||||
Notification::fake();
|
||||
|
||||
$this->actingAs($admin)
|
||||
->post('/admin/notifications/send', [
|
||||
'receiver' => 'all',
|
||||
'title' => 'all users',
|
||||
'content' => null,
|
||||
])
|
||||
->assertRedirect('/admin')
|
||||
->assertSessionHas('sentResult', trans('admin.notifications.send.success'));
|
||||
Notification::assertSentTo(
|
||||
[$admin, $normal],
|
||||
Notifications\SiteMessage::class,
|
||||
function ($notification) {
|
||||
$this->assertEquals('all users', $notification->title);
|
||||
|
||||
return true;
|
||||
}
|
||||
);
|
||||
|
||||
Notification::fake();
|
||||
Notification::assertNothingSent();
|
||||
$this->post('/admin/notifications/send', [
|
||||
'receiver' => 'normal',
|
||||
'title' => 'normal only',
|
||||
'content' => 'hi',
|
||||
]);
|
||||
Notification::assertSentTo(
|
||||
$normal,
|
||||
Notifications\SiteMessage::class,
|
||||
function ($notification) {
|
||||
$this->assertEquals('normal only', $notification->title);
|
||||
$this->assertEquals('hi', $notification->content);
|
||||
|
||||
return true;
|
||||
}
|
||||
);
|
||||
Notification::assertNotSentTo($admin, Notifications\SiteMessage::class);
|
||||
|
||||
Notification::fake();
|
||||
Notification::assertNothingSent();
|
||||
$this->post('/admin/notifications/send', [
|
||||
'receiver' => 'uid',
|
||||
'title' => 'uid',
|
||||
'content' => null,
|
||||
'uid' => $normal->uid,
|
||||
]);
|
||||
Notification::assertSentTo(
|
||||
$normal,
|
||||
Notifications\SiteMessage::class,
|
||||
function ($notification) {
|
||||
$this->assertEquals('uid', $notification->title);
|
||||
|
||||
return true;
|
||||
}
|
||||
);
|
||||
Notification::assertNotSentTo($admin, Notifications\SiteMessage::class);
|
||||
|
||||
Notification::fake();
|
||||
Notification::assertNothingSent();
|
||||
$this->post('/admin/notifications/send', [
|
||||
'receiver' => 'email',
|
||||
'title' => 'email',
|
||||
'content' => null,
|
||||
'email' => $normal->email,
|
||||
]);
|
||||
Notification::assertSentTo(
|
||||
$normal,
|
||||
Notifications\SiteMessage::class,
|
||||
function ($notification) {
|
||||
$this->assertEquals('email', $notification->title);
|
||||
|
||||
return true;
|
||||
}
|
||||
);
|
||||
Notification::assertNotSentTo($admin, Notifications\SiteMessage::class);
|
||||
}
|
||||
|
||||
public function testStatus()
|
||||
{
|
||||
$this->mock(\App\Services\PluginManager::class, function ($mock) {
|
||||
|
125
tests/HttpTest/ControllersTest/NotificationsControllerTest.php
Normal file
125
tests/HttpTest/ControllersTest/NotificationsControllerTest.php
Normal file
@ -0,0 +1,125 @@
|
||||
<?php
|
||||
|
||||
namespace Tests;
|
||||
|
||||
use App\Models\User;
|
||||
use App\Notifications;
|
||||
use Illuminate\Support\Facades\Notification;
|
||||
use Parsedown;
|
||||
|
||||
class NotificationsControllerTest extends TestCase
|
||||
{
|
||||
public function testSend()
|
||||
{
|
||||
$admin = factory(User::class)->states('admin')->create();
|
||||
$normal = factory(User::class)->create();
|
||||
Notification::fake();
|
||||
|
||||
$this->actingAs($admin)
|
||||
->post('/admin/notifications/send', [
|
||||
'receiver' => 'all',
|
||||
'title' => 'all users',
|
||||
'content' => null,
|
||||
])
|
||||
->assertRedirect('/admin')
|
||||
->assertSessionHas('sentResult', trans('admin.notifications.send.success'));
|
||||
Notification::assertSentTo(
|
||||
[$admin, $normal],
|
||||
Notifications\SiteMessage::class,
|
||||
function ($notification) {
|
||||
$this->assertEquals('all users', $notification->title);
|
||||
|
||||
return true;
|
||||
}
|
||||
);
|
||||
|
||||
Notification::fake();
|
||||
Notification::assertNothingSent();
|
||||
$this->post('/admin/notifications/send', [
|
||||
'receiver' => 'normal',
|
||||
'title' => 'normal only',
|
||||
'content' => 'hi',
|
||||
]);
|
||||
Notification::assertSentTo(
|
||||
$normal,
|
||||
Notifications\SiteMessage::class,
|
||||
function ($notification) {
|
||||
$this->assertEquals('normal only', $notification->title);
|
||||
$this->assertEquals('hi', $notification->content);
|
||||
|
||||
return true;
|
||||
}
|
||||
);
|
||||
Notification::assertNotSentTo($admin, Notifications\SiteMessage::class);
|
||||
|
||||
Notification::fake();
|
||||
Notification::assertNothingSent();
|
||||
$this->post('/admin/notifications/send', [
|
||||
'receiver' => 'uid',
|
||||
'title' => 'uid',
|
||||
'content' => null,
|
||||
'uid' => $normal->uid,
|
||||
]);
|
||||
Notification::assertSentTo(
|
||||
$normal,
|
||||
Notifications\SiteMessage::class,
|
||||
function ($notification) {
|
||||
$this->assertEquals('uid', $notification->title);
|
||||
|
||||
return true;
|
||||
}
|
||||
);
|
||||
Notification::assertNotSentTo($admin, Notifications\SiteMessage::class);
|
||||
|
||||
Notification::fake();
|
||||
Notification::assertNothingSent();
|
||||
$this->post('/admin/notifications/send', [
|
||||
'receiver' => 'email',
|
||||
'title' => 'email',
|
||||
'content' => null,
|
||||
'email' => $normal->email,
|
||||
]);
|
||||
Notification::assertSentTo(
|
||||
$normal,
|
||||
Notifications\SiteMessage::class,
|
||||
function ($notification) {
|
||||
$this->assertEquals('email', $notification->title);
|
||||
|
||||
return true;
|
||||
}
|
||||
);
|
||||
Notification::assertNotSentTo($admin, Notifications\SiteMessage::class);
|
||||
}
|
||||
|
||||
public function testAll()
|
||||
{
|
||||
$user = factory(User::class)->create();
|
||||
|
||||
$notification = new Notifications\SiteMessage('title', 'content');
|
||||
Notification::send([$user], $notification);
|
||||
|
||||
$id = $user->unreadNotifications->first()->id;
|
||||
|
||||
$this->actingAs($user, 'oauth')
|
||||
->getJson('/api/user/notifications')
|
||||
->assertJson([['id' => $id, 'title' => 'title']]);
|
||||
}
|
||||
|
||||
public function testRead()
|
||||
{
|
||||
$user = factory(User::class)->create();
|
||||
$user->notify(new Notifications\SiteMessage('Hyouka', 'Kotenbu?'));
|
||||
$user->refresh();
|
||||
$notification = $user->unreadNotifications->first();
|
||||
|
||||
$this->actingAs($user)
|
||||
->get('/user/notifications/'.$notification->id)
|
||||
->assertJson([
|
||||
'title' => $notification->data['title'],
|
||||
'content' => (new Parsedown())->text($notification->data['content']),
|
||||
'time' => $notification->created_at->toDateTimeString(),
|
||||
]);
|
||||
$notification->refresh();
|
||||
$this->assertNotNull($notification->read_at);
|
||||
}
|
||||
}
|
@ -5,7 +5,6 @@ namespace Tests;
|
||||
use App\Events;
|
||||
use App\Mail\EmailVerification;
|
||||
use App\Models\User;
|
||||
use App\Notifications;
|
||||
use Blessing\Filter;
|
||||
use Blessing\Rejection;
|
||||
use Carbon\Carbon;
|
||||
@ -601,22 +600,4 @@ class UserControllerTest extends TestCase
|
||||
->postJson('/user/profile/avatar', ['tid' => $steve->tid])
|
||||
->assertJson(['code' => 1, 'message' => 'rejected']);
|
||||
}
|
||||
|
||||
public function testReadNotification()
|
||||
{
|
||||
$user = factory(User::class)->create();
|
||||
$user->notify(new Notifications\SiteMessage('Hyouka', 'Kotenbu?'));
|
||||
$user->refresh();
|
||||
$notification = $user->unreadNotifications->first();
|
||||
|
||||
$this->actingAs($user)
|
||||
->get('/user/notifications/'.$notification->id)
|
||||
->assertJson([
|
||||
'title' => $notification->data['title'],
|
||||
'content' => (new Parsedown())->text($notification->data['content']),
|
||||
'time' => $notification->created_at->toDateTimeString(),
|
||||
]);
|
||||
$notification->refresh();
|
||||
$this->assertNotNull($notification->read_at);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user