Session api token validation
This commit is contained in:
@@ -7,6 +7,8 @@ use App\Models\Notification;
|
||||
use App\Models\User;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Facades\Gate;
|
||||
|
||||
class FollowerController extends Controller
|
||||
{
|
||||
@@ -24,6 +26,43 @@ class FollowerController extends Controller
|
||||
return response()->json($followers);
|
||||
}
|
||||
|
||||
public function follow(User $user): JsonResponse
|
||||
{
|
||||
abort_if($user->id === auth()->id(), 403);
|
||||
|
||||
$existing = Followee::where('user_id', auth()->id())
|
||||
->where('followee_id', $user->id)
|
||||
->first();
|
||||
|
||||
if ($existing) {
|
||||
$existing->delete();
|
||||
$this->clearFollowingCache(auth()->user());
|
||||
return response()->json(['status' => 'none']);
|
||||
}
|
||||
|
||||
$canView = Gate::allows('viewProfileData', $user);
|
||||
|
||||
Followee::create([
|
||||
'user_id' => auth()->id(),
|
||||
'followee_id' => $user->id,
|
||||
'verified' => $canView,
|
||||
]);
|
||||
|
||||
$this->clearFollowingCache(auth()->user());
|
||||
|
||||
Notification::create([
|
||||
'user_id' => $user->id,
|
||||
'title' => $canView ? 'New follower' : 'Follow request',
|
||||
'body' => $canView
|
||||
? auth()->user()->name . ' is now following you.'
|
||||
: auth()->user()->name . ' wants to follow you.',
|
||||
'is_achievement' => false,
|
||||
'url' => $canView ? '/u/' . auth()->user()->name : '/follow-requests',
|
||||
]);
|
||||
|
||||
return response()->json(['status' => $canView ? 'following' : 'requested']);
|
||||
}
|
||||
|
||||
public function approve(User $follower): JsonResponse
|
||||
{
|
||||
$followee = Followee::where('user_id', $follower->id)
|
||||
@@ -33,6 +72,8 @@ class FollowerController extends Controller
|
||||
|
||||
$followee->update(['verified' => true]);
|
||||
|
||||
$this->clearFollowingCache($follower);
|
||||
|
||||
Notification::create([
|
||||
'user_id' => $follower->id,
|
||||
'title' => 'Follow request accepted',
|
||||
@@ -51,6 +92,8 @@ class FollowerController extends Controller
|
||||
->pending()
|
||||
->delete();
|
||||
|
||||
$this->clearFollowingCache($follower);
|
||||
|
||||
return response()->json(['status' => 'denied']);
|
||||
}
|
||||
|
||||
@@ -61,6 +104,14 @@ class FollowerController extends Controller
|
||||
->verified()
|
||||
->delete();
|
||||
|
||||
$this->clearFollowingCache($follower);
|
||||
|
||||
return response()->json(['status' => 'removed']);
|
||||
}
|
||||
|
||||
protected function clearFollowingCache(User $user): void
|
||||
{
|
||||
Cache::forget("user:{$user->id}:following");
|
||||
Cache::forget("user_following_flights_{$user->id}");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user