Skip to content

Commit

Permalink
Add user block management api
Browse files Browse the repository at this point in the history
  • Loading branch information
nanaya committed Oct 22, 2024
1 parent f4f6d25 commit ba4742b
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 8 deletions.
23 changes: 15 additions & 8 deletions app/Http/Controllers/BlocksController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
use App\Jobs\UpdateUserFollowerCountCache;
use App\Models\User;
use App\Models\UserRelation;
use Auth;
use Request;
use App\Transformers\UserRelationTransformer;

class BlocksController extends Controller
{
Expand All @@ -27,15 +26,23 @@ public function __construct()
parent::__construct();
}

public function index()
{
return json_collection(
\Auth::user()->relations()->blocks()->get(),
new UserRelationTransformer(),
);
}

public function store()
{
$currentUser = Auth::user();
$currentUser = \Auth::user();

if ($currentUser->blocks()->count() >= $currentUser->maxBlocks()) {
return error_popup(osu_trans('users.blocks.too_many'));
}

$targetId = get_int(Request::input('target'));
$targetId = get_int(\Request::input('target'));
$targetUser = User::lookup($targetId, 'id');

if (!$targetUser) {
Expand All @@ -57,21 +64,21 @@ public function store()
dispatch(new UpdateUserFollowerCountCache($targetId));
} else {
UserRelation::create([
'user_id' => $currentUser->user_id,
'user_id' => $currentUser->getKey(),
'zebra_id' => $targetId,
'foe' => true,
]);
}

return json_collection(
$currentUser->relations()->visible()->withMutual()->get(),
'UserRelation'
new UserRelationTransformer(),
);
}

public function destroy($id)
{
$user = Auth::user();
$user = \Auth::user();

$block = $user->blocks()
->where('zebra_id', $id)
Expand All @@ -85,7 +92,7 @@ public function destroy($id)

return json_collection(
$user->relations()->visible()->withMutual()->get(),
'UserRelation'
new UserRelationTransformer(),
);
}
}
2 changes: 2 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,8 @@
});
});

Route::resource('blocks', 'BlocksController', ['only' => ['destroy', 'index', 'store']]);

Route::apiResource('comments', 'CommentsController');
Route::post('comments/{comment}/vote', 'CommentsController@voteStore')->name('comments.vote');
Route::delete('comments/{comment}/vote', 'CommentsController@voteDestroy');
Expand Down
42 changes: 42 additions & 0 deletions tests/api_routes.json
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,48 @@
],
"scopes": []
},
{
"uri": "api/v2/blocks",
"methods": [
"GET",
"HEAD"
],
"controller": "App\\Http\\Controllers\\BlocksController@index",
"middlewares": [
"App\\Http\\Middleware\\ThrottleRequests:1200,1,api:",
"App\\Http\\Middleware\\RequireScopes",
"Illuminate\\Auth\\Middleware\\Authenticate"
],
"scopes": []
},
{
"uri": "api/v2/blocks",
"methods": [
"POST"
],
"controller": "App\\Http\\Controllers\\BlocksController@store",
"middlewares": [
"App\\Http\\Middleware\\ThrottleRequests:1200,1,api:",
"App\\Http\\Middleware\\RequireScopes",
"Illuminate\\Auth\\Middleware\\Authenticate",
"App\\Http\\Middleware\\VerifyUser"
],
"scopes": []
},
{
"uri": "api/v2/blocks/{block}",
"methods": [
"DELETE"
],
"controller": "App\\Http\\Controllers\\BlocksController@destroy",
"middlewares": [
"App\\Http\\Middleware\\ThrottleRequests:1200,1,api:",
"App\\Http\\Middleware\\RequireScopes",
"Illuminate\\Auth\\Middleware\\Authenticate",
"App\\Http\\Middleware\\VerifyUser"
],
"scopes": []
},
{
"uri": "api/v2/comments",
"methods": [
Expand Down

0 comments on commit ba4742b

Please sign in to comment.