the browser-facing portion of osu!
at master 2.4 kB view raw
1<?php 2 3// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the GNU Affero General Public License v3.0. 4// See the LICENCE file in the repository root for full licence text. 5 6namespace App\Http\Controllers; 7 8use App\Jobs\UpdateUserFollowerCountCache; 9use App\Models\User; 10use App\Models\UserRelation; 11use App\Transformers\UserRelationTransformer; 12 13class BlocksController extends Controller 14{ 15 public function __construct() 16 { 17 $this->middleware('auth'); 18 19 $this->middleware('verify-user', [ 20 'only' => [ 21 'store', 22 'destroy', 23 ], 24 ]); 25 26 parent::__construct(); 27 } 28 29 public function index() 30 { 31 return json_collection( 32 \Auth::user()->relations()->blocks()->get(), 33 new UserRelationTransformer(), 34 ); 35 } 36 37 public function store() 38 { 39 $currentUser = \Auth::user(); 40 41 if ($currentUser->blocks()->count() >= $currentUser->maxBlocks()) { 42 return error_popup(osu_trans('users.blocks.too_many')); 43 } 44 45 $targetId = get_int(\Request::input('target')); 46 $targetUser = User::lookup($targetId, 'id'); 47 48 if (!$targetUser) { 49 abort(404); 50 } 51 52 $relationQuery = $currentUser->relations()->where('zebra_id', $targetId); 53 54 $existingRelation = $relationQuery->first(); 55 if ($existingRelation) { 56 $existingRelation->update([ 57 'foe' => true, 58 'friend' => false, 59 ]); 60 61 // users get unfollowed when blocked, so update follower count 62 dispatch(new UpdateUserFollowerCountCache($targetId)); 63 } else { 64 UserRelation::create([ 65 'user_id' => $currentUser->getKey(), 66 'zebra_id' => $targetId, 67 'foe' => true, 68 ]); 69 } 70 71 return [ 72 'user_relation' => json_item( 73 $relationQuery->first(), 74 new UserRelationTransformer(), 75 ), 76 ]; 77 } 78 79 public function destroy($id) 80 { 81 $user = \Auth::user(); 82 83 $block = $user->blocks() 84 ->where('zebra_id', $id) 85 ->first(); 86 87 if (!$block) { 88 abort(404, osu_trans('users.blocks.not_blocked')); 89 } 90 91 $user->blocks()->detach($block); 92 93 return response(null, 204); 94 } 95}