diff --git a/app/Http/Controllers/AdminController.php b/app/Http/Controllers/AdminController.php index c53ef313..86effa97 100644 --- a/app/Http/Controllers/AdminController.php +++ b/app/Http/Controllers/AdminController.php @@ -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) { diff --git a/app/Http/Controllers/NotificationsController.php b/app/Http/Controllers/NotificationsController.php new file mode 100644 index 00000000..1fda0925 --- /dev/null +++ b/app/Http/Controllers/NotificationsController.php @@ -0,0 +1,74 @@ +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(), + ]; + } +} diff --git a/app/Http/Controllers/UserController.php b/app/Http/Controllers/UserController.php index e0d58046..8559015a 100644 --- a/app/Http/Controllers/UserController.php +++ b/app/Http/Controllers/UserController.php @@ -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(), - ]; - } } diff --git a/routes/api.php b/routes/api.php index 10ab62e5..41fb4b00 100644 --- a/routes/api.php +++ b/routes/api.php @@ -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'); }); diff --git a/routes/web.php b/routes/web.php index 28cc3100..6defaf8c 100644 --- a/routes/web.php +++ b/routes/web.php @@ -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'); diff --git a/tests/HttpTest/ControllersTest/AdminControllerTest.php b/tests/HttpTest/ControllersTest/AdminControllerTest.php index 35695fcd..9b7742d3 100644 --- a/tests/HttpTest/ControllersTest/AdminControllerTest.php +++ b/tests/HttpTest/ControllersTest/AdminControllerTest.php @@ -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) { diff --git a/tests/HttpTest/ControllersTest/NotificationsControllerTest.php b/tests/HttpTest/ControllersTest/NotificationsControllerTest.php new file mode 100644 index 00000000..f1d95cad --- /dev/null +++ b/tests/HttpTest/ControllersTest/NotificationsControllerTest.php @@ -0,0 +1,125 @@ +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); + } +} diff --git a/tests/HttpTest/ControllersTest/UserControllerTest.php b/tests/HttpTest/ControllersTest/UserControllerTest.php index 18f2639f..f7c6e44e 100644 --- a/tests/HttpTest/ControllersTest/UserControllerTest.php +++ b/tests/HttpTest/ControllersTest/UserControllerTest.php @@ -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); - } }