Skip to content

Commit

Permalink
Block a user
Browse files Browse the repository at this point in the history
  • Loading branch information
Saodus committed Nov 12, 2024
1 parent d0ebafb commit 2afbf5c
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 7 deletions.
45 changes: 39 additions & 6 deletions app/Http/Controllers/FriendshipsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function store(Request $request)
})->exists();

if ($friendshipExists) {
return back()->with('error', 'You are already friends or a request is pending.');
return back()->with('error', 'You are already friends, blocked or a request is pending.');
}

Friendship::create([
Expand Down Expand Up @@ -115,17 +115,50 @@ public function destroy($friend)
{
$friendship = Friendship::where(function ($query) use ($friend) {
$query->where('user_id', Auth::id())
->where('friend_id', $friend);
->where('friend_id', $friend);
})->orWhere(function ($query) use ($friend) {
$query->where('user_id', $friend)
->where('friend_id', Auth::id());
->where('friend_id', Auth::id());
})->first();

if ($friendship) {
$friendship->delete();
return back()->with('success', 'Friend removed successfully!');
}

return back()->with('error', 'Could not find the friend to remove.');
}
}

public function block($friend)
{
$friend = User::findOrFail($friend);

if ($friend->id === Auth::id()) {
return back()->with('error', 'You cannot block yourself.');
}

$friendship = Friendship::where(function ($query) use ($friend) {
$query->where('user_id', auth()->id())
->where('friend_id', $friend->id);
})->orWhere(function ($query) use ($friend) {
$query->where('user_id', $friend->id)
->where('friend_id', auth()->id());
})->first();

if ($friendship) {
if ($friendship->status === 'blocked') {
return back()->with('error', 'This user is already blocked.');
}
$friendship->update(['status' => 'blocked']);
return back()->with('success', 'User blocked successfully!');
} else {
// Create a new friendship with 'blocked' status if no existing relation is found
Friendship::create([
'user_id' => auth()->id(),
'friend_id' => $friend->id,
'status' => 'blocked',
]);
return back()->with('success', 'User blocked successfully!');
}
}
}
Binary file added public/source/assets/images/block_icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions resources/views/friendships/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@
<img src="{{ asset('source/assets/images/delete_friend_icon.png') }}" alt="Supprimer" class="h-10 w-10">
</button>
</form>

<form action="{{ route('friends.block', ['friend' => $friend->user_id == Auth::id() ? $friend->friend->id : $friend->user->id]) }}" method="POST" onsubmit="return confirm('Are you sure you want to block this user?');">
@csrf
<button type="submit">
<img src="{{ asset('source/assets/images/block_icon.png') }}" alt="Block" class="h-10 w-10">
</button>
</form>
</div>
</div>
@endforeach
Expand Down
2 changes: 1 addition & 1 deletion routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
Route::get('/friends/pending', [FriendshipsController::class, 'pending'])->name('friends.pending');
Route::post('/friends/{friendship_id}/accept', [FriendshipsController::class, 'accept'])->name('friends.accept');
Route::post('/friends/{friendship_id}/decline', [FriendshipsController::class, 'decline'])->name('friends.decline');

Route::post('/friends/{friend}/block', [FriendshipsController::class, 'block'])->name('friends.block');


});
Expand Down

0 comments on commit 2afbf5c

Please sign in to comment.