From 11bd3a161e2079f799fb1650e223f9a0e9f5c786 Mon Sep 17 00:00:00 2001 From: Jaton David Date: Tue, 17 Dec 2024 17:34:35 +0100 Subject: [PATCH] Comments + augments profile picture file size --- app/Http/Controllers/ChatController.php | 8 +++- .../Controllers/FriendshipsController.php | 42 ++++++++++++++++++- app/Http/Requests/ProfileUpdateRequest.php | 2 +- resources/views/dashboard.blade.php | 18 +++++++- resources/views/friendships/create.blade.php | 3 ++ 5 files changed, 66 insertions(+), 7 deletions(-) diff --git a/app/Http/Controllers/ChatController.php b/app/Http/Controllers/ChatController.php index 192e721..6f6afdd 100644 --- a/app/Http/Controllers/ChatController.php +++ b/app/Http/Controllers/ChatController.php @@ -263,7 +263,7 @@ public function leaveChat($chatId) * @return \Illuminate\Http\JsonResponse A JSON response with a message (or an error). */ public function deleteMessage($discussionId, $messageId) - { + { $message = Message::where('id', $messageId)->where('chat_id', $discussionId)->first(); if (!$message) { @@ -273,8 +273,10 @@ public function deleteMessage($discussionId, $messageId) if ($message->user_id !== auth()->id()) { return response()->json(['error' => 'Unauthorized.'], 403); } + //Check if the message has a corresponding capsule if (Message::find($message->id + 10000000)) - { + { + //If the message has a corresponding capsule, we delete the capsule and the message $capsule = Message::where('id', $messageId + 10000000)->where('chat_id', $discussionId)->first(); if ($message->media_url) { $mediaPath = public_path('source/assets/media/' . $message->media_url); @@ -285,6 +287,8 @@ public function deleteMessage($discussionId, $messageId) $capsule->delete(); $message->delete(); } + //If the message doesn't have a corresponding capsule, we delete the message + //We also check if the message has a media file and delete it if it exists else{ if ($message->media_url) { $mediaPath = public_path("/source/media/" . $message->media_url); diff --git a/app/Http/Controllers/FriendshipsController.php b/app/Http/Controllers/FriendshipsController.php index 3a8d296..bb86be2 100644 --- a/app/Http/Controllers/FriendshipsController.php +++ b/app/Http/Controllers/FriendshipsController.php @@ -10,8 +10,13 @@ class FriendshipsController extends Controller { + /** + * Display a listing of the friendships of the user. + */ public function index() { + + // Auth id can be the user_id or the friend_id in the db so we need to check both $friendsFromUser = Friendship::where('user_id', Auth::id()) ->where('status', 'accepted') ->with(['user', 'friend']) @@ -29,6 +34,9 @@ public function index() ]); } + /** + * Show the form for creating a new friendship. + */ public function create() { return view('friendships.create'); @@ -53,6 +61,12 @@ public function pending() ]); } + /** + * Store a newly created friendship in storage. + * + * @param \Illuminate\Http\Request $request + * @return \Illuminate\Http\Response + */ public function store(Request $request) { $validated = $request->validate([ @@ -86,7 +100,13 @@ public function store(Request $request) return back()->with('success', 'Friend request sent !'); } - public function accept(Request $request, $friendship_id) + /** + * Accept a friendship request. + * + * @param int $friendship_id + * @return \Illuminate\Http\Response + */ + public function accept($friendship_id) { $friendship = Friendship::find($friendship_id); @@ -99,7 +119,13 @@ public function accept(Request $request, $friendship_id) return back()->with('error', 'Could not accept the friend request.'); } - public function decline(Request $request, $friendship_id) + /** + * Decline a friendship request. + * + * @param int $friendship_id + * @return \Illuminate\Http\Response + */ + public function decline($friendship_id) { $friendship = Friendship::find($friendship_id); @@ -111,6 +137,12 @@ public function decline(Request $request, $friendship_id) return back()->with('error', 'Could not decline the friend request.'); } + /** + * Remove the specified friendship from storage. + * + * @param int $friend + * @return \Illuminate\Http\Response + */ public function destroy($friend) { $friendship = Friendship::where(function ($query) use ($friend) { @@ -129,6 +161,12 @@ public function destroy($friend) return back()->with('error', 'Could not find the friend to remove.'); } + /** + * Block a user. + * + * @param int $friend + * @return \Illuminate\Http\Response + */ public function block($friend) { $friend = User::findOrFail($friend); diff --git a/app/Http/Requests/ProfileUpdateRequest.php b/app/Http/Requests/ProfileUpdateRequest.php index 469f892..13b73a9 100644 --- a/app/Http/Requests/ProfileUpdateRequest.php +++ b/app/Http/Requests/ProfileUpdateRequest.php @@ -26,7 +26,7 @@ public function rules(): array Rule::unique(User::class)->ignore($this->user()->id), ], - 'image' => ['nullable', 'image', 'mimes:jpeg,png,jpg', 'max:2048'], + 'image' => ['nullable', 'image', 'mimes:jpeg,png,jpg', 'max:10240'], ]; } } diff --git a/resources/views/dashboard.blade.php b/resources/views/dashboard.blade.php index 5b39409..bde5d79 100644 --- a/resources/views/dashboard.blade.php +++ b/resources/views/dashboard.blade.php @@ -367,8 +367,9 @@ function leaveChat() { alert("Informations de message ou discussion manquantes."); return; } - console.log("messageId", messageId) + // if the message has an id greater than 10000000, it means it is a capsule message + // so we need to delete the message with the id - 10000000 to delete the original message if(messageId >= 10000000) { const response = await fetch(`/chat/${discussionId}/${messageId - 10000000}/delete`, { @@ -379,6 +380,7 @@ function leaveChat() { } }); } + // if the message has an id less than 10000000, it means it is a normal message else{ const response = await fetch(`/chat/${discussionId}/${messageId}/delete`, { method: 'DELETE', @@ -393,7 +395,7 @@ function leaveChat() { console.error("Erreur de suppression, statut:", response.status); throw new Error("Impossible de supprimer le message."); } - + const messageElement = document.getElementById(`message-div-${messageId}`); if (messageElement) { messageElement.remove(); @@ -401,6 +403,11 @@ function leaveChat() { } +/** + * Function to leave a chat with a loader + * + * @returns {void} + */ function leaveChatWithLoad() { const loader = document.getElementById('leave-chat-loader'); @@ -420,6 +427,13 @@ function leaveChatWithLoad() }); } +/** + * Function to delete a message with a loader + * + * @param {number} messageId The ID of the message + * @param {number} chatId The ID of the chat + * @returns {void} + */ async function deleteMessageWithLoader(messageId, chatId) { const loader = document.getElementById(`loader-${messageId}`); const button = event.target; diff --git a/resources/views/friendships/create.blade.php b/resources/views/friendships/create.blade.php index 4b3019d..b30c136 100644 --- a/resources/views/friendships/create.blade.php +++ b/resources/views/friendships/create.blade.php @@ -44,6 +44,9 @@ class="mt-1 block w-full px-4 py-2 border border-gray-300 rounded-md shadow-sm f