Skip to content

Commit

Permalink
Sort discussions by last message date
Browse files Browse the repository at this point in the history
  • Loading branch information
max15015 committed Dec 18, 2024
1 parent 9cb9593 commit 40bd0c3
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 14 deletions.
33 changes: 19 additions & 14 deletions app/Http/Controllers/ChatController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,16 @@
*/
class ChatController extends Controller
{
/**
/**
* Show the dashboard with the list of discussions and pass the friends list to the view.
*/
public function index()
{
// Get the list of discussions with the last message and the members
$discussions = auth()->user()->chats()
->with(['messages' => function ($query) {
$query->latest()->limit(1);
$query->latest()->limit(1); // Last message
}, 'members'])
->latest()
->get()
->map(function ($discussion) {
// We check if the discussion is a private chat
Expand All @@ -43,8 +42,13 @@ public function index()
$discussion->discussionPicture = asset('source/assets/images/group.png');
}

// Add `lastMessageCreatedAt` field for sorting
$discussion->lastMessageCreatedAt = $discussion->messages->first()->created_at ?? null;

return $discussion;
});
})
->sortByDesc('lastMessageCreatedAt') // Sort by last message date
->values(); // Reindex the collection

// Get the list of friends (accepted friendships)
$friends = Friendship::where(function ($query) {
Expand All @@ -65,9 +69,10 @@ public function index()
]);
}


/**
* Return the messages of a discussion (with the closed capsules and opened capsules)
*
*
* @param int $chatId The ID of the discussion
* @return \Illuminate\Http\JsonResponse A JSON response containing the array of messages.
*/
Expand Down Expand Up @@ -124,7 +129,7 @@ public function getMessages($chatId)

/**
* Store a new message sent by the user in a specific chat.
*
*
* @param \Illuminate\Http\Request $request The request containing the message content.
* @param int $chatId The ID of the chat.
* @return \Illuminate\Http\JsonResponse A JSON response containing the message (or an error).
Expand Down Expand Up @@ -170,7 +175,7 @@ public function storeMessage(Request $request, $chatId)

/**
* Store a new capsule sent by the user in a specific chat.
*
*
* @param \Illuminate\Http\Request $request The request containing the message content and the media file.
* @param int $chatId The ID of the chat.
* @return \Illuminate\Http\JsonResponse A JSON response containing the capsule (or an error).
Expand Down Expand Up @@ -210,7 +215,7 @@ public function storeCapsule(Request $request, $chatId)

/**
* Store a new chat with the name and the friends selected by the user.
*
*
* @param \Illuminate\Http\Request $request The request containing the chat name and the friends.
* @return \Illuminate\Http\RedirectResponse A redirect response to the dashboard with a success message.
*/
Expand Down Expand Up @@ -238,7 +243,7 @@ public function storeChat(Request $request)

/**
* Leave a chat (remove the user from the chat).
*
*
* @param int $chatId The ID of the chat.
* @return \Illuminate\Http\JsonResponse A JSON response with a message.
*/
Expand All @@ -257,13 +262,13 @@ public function leaveChat($chatId)

/**
* Delete a message from a discussion.
*
*
* @param int $discussionId The ID of the discussion.
* @param int $messageId The ID of the message.
* @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) {
Expand All @@ -275,13 +280,13 @@ public function deleteMessage($discussionId, $messageId)
}
//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);
if (file_exists($mediaPath)) {
unlink($mediaPath);
unlink($mediaPath);
}
}
$capsule->delete();
Expand All @@ -293,7 +298,7 @@ public function deleteMessage($discussionId, $messageId)
if ($message->media_url) {
$mediaPath = public_path("/source/media/" . $message->media_url);
if (file_exists($mediaPath)) {
unlink($mediaPath);
unlink($mediaPath);
}
}
$message->delete();
Expand Down
Binary file not shown.
Binary file not shown.

0 comments on commit 40bd0c3

Please sign in to comment.