Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions platforms/blabsy/src/components/chat/chat-window.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ function MessageItem({
<div
dangerouslySetInnerHTML={{
__html: displayText.replace(
/<a href="([^"]+)">([^<]+)<\/a>/g,
'<a href="$1" class="text-blue-600 hover:text-blue-800 underline">$2</a>'
/<a href="([^"]+)"([^>]*)>([^<]+)<\/a>/g,
'<a href="$1"$2 class="text-blue-600 hover:text-blue-800 underline cursor-pointer">$3</a>'
)
}}
/>
Expand Down Expand Up @@ -119,8 +119,8 @@ function MessageItem({
className='break-words whitespace-pre-wrap'
dangerouslySetInnerHTML={{
__html: displayText.replace(
/<a href="([^"]+)">([^<]+)<\/a>/g,
'<a href="$1" class="text-blue-600 hover:text-blue-800 underline">$2</a>'
/<a href="([^"]+)"([^>]*)>([^<]+)<\/a>/g,
'<a href="$1"$2 class="text-blue-600 hover:text-blue-800 underline cursor-pointer">$3</a>'
)
}}
/>
Expand Down
51 changes: 25 additions & 26 deletions platforms/blabsy/src/lib/context/chat-context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -103,33 +103,32 @@ export function ChatContextProvider({
(snapshot) => {
const chatsData = snapshot.docs.map((doc) => doc.data());

// Sort chats by last message timestamp (most recent first)
// Sort chats by most recent activity (most recent first)
// Priority: lastMessage timestamp > updatedAt > createdAt
const sortedChats = chatsData.sort((a, b) => {
// If both have lastMessage, sort by timestamp
if (a.lastMessage?.timestamp && b.lastMessage?.timestamp) {
return (
b.lastMessage.timestamp.toMillis() -
a.lastMessage.timestamp.toMillis()
);
}
// If only one has lastMessage, prioritize it
if (a.lastMessage?.timestamp && !b.lastMessage?.timestamp)
return -1;
if (!a.lastMessage?.timestamp && b.lastMessage?.timestamp)
return 1;
// If neither has lastMessage, sort by updatedAt (with null checks)
if (a.updatedAt && b.updatedAt) {
return b.updatedAt.toMillis() - a.updatedAt.toMillis();
}
// If only one has updatedAt, prioritize it
if (a.updatedAt && !b.updatedAt) return -1;
if (!a.updatedAt && b.updatedAt) return 1;
// If both are null, sort by createdAt as fallback
if (a.createdAt && b.createdAt) {
return b.createdAt.toMillis() - a.createdAt.toMillis();
}
// If all else fails, maintain order
return 0;
// Get the most recent activity timestamp for each chat
const getMostRecentTimestamp = (chat: typeof a): number => {
// Priority 1: lastMessage timestamp (most recent activity)
if (chat.lastMessage?.timestamp) {
return chat.lastMessage.timestamp.toMillis();
}
// Priority 2: updatedAt (for updated chats without messages)
if (chat.updatedAt) {
return chat.updatedAt.toMillis();
}
// Priority 3: createdAt (for new chats)
if (chat.createdAt) {
return chat.createdAt.toMillis();
}
// Fallback: 0 for chats with no timestamps
return 0;
};

const aTimestamp = getMostRecentTimestamp(a);
const bTimestamp = getMostRecentTimestamp(b);

// Sort by most recent timestamp (descending)
return bTimestamp - aTimestamp;
});

setChats(sortedChats);
Expand Down
47 changes: 37 additions & 10 deletions platforms/pictique-api/src/services/ChatService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,11 @@ export class ChatService {

const savedMessage = await this.messageRepository.save(message);

// Explicitly update the chat's updatedAt to ensure proper sorting
await this.chatRepository.update(chatId, {
updatedAt: new Date()
});

console.log("Sent event", `chat:${chatId}`);
this.eventEmitter.emit(`chat:${chatId}`, [savedMessage]);

Expand Down Expand Up @@ -361,24 +366,46 @@ export class ChatService {
],
});

// Sort the chats by latest message timestamp (most recent first)
// Sort chats by most recent activity (most recent first)
// Priority: latest message timestamp > updatedAt > createdAt
const sortedChats = chatsWithRelations.sort((a, b) => {
const aLatestMessage = a.messages[a.messages.length - 1];
const bLatestMessage = b.messages[b.messages.length - 1];
const getMostRecentTimestamp = (chat: typeof a): number => {
// Priority 1: latest message timestamp (most recent activity)
// Sort messages by createdAt ascending to get the latest one
const sortedMessages = [...(chat.messages || [])].sort(
(m1, m2) => m1.createdAt.getTime() - m2.createdAt.getTime()
);
const latestMessage = sortedMessages[sortedMessages.length - 1];
if (latestMessage) {
return latestMessage.createdAt.getTime();
}
// Priority 2: updatedAt (for updated chats without messages)
if (chat.updatedAt) {
return chat.updatedAt.getTime();
}
// Priority 3: createdAt (for new chats)
if (chat.createdAt) {
return chat.createdAt.getTime();
}
// Fallback: 0 for chats with no timestamps
return 0;
};

if (!aLatestMessage && !bLatestMessage) {
return b.createdAt.getTime() - a.createdAt.getTime();
}
if (!aLatestMessage) return 1;
if (!bLatestMessage) return -1;
const aTimestamp = getMostRecentTimestamp(a);
const bTimestamp = getMostRecentTimestamp(b);

return bLatestMessage.createdAt.getTime() - aLatestMessage.createdAt.getTime();
// Sort by most recent timestamp (descending)
return bTimestamp - aTimestamp;
});

// For each chat, get the latest message and its read status
const chatsWithLatestMessage = await Promise.all(
sortedChats.map(async (chat) => {
const latestMessage = chat.messages[chat.messages.length - 1];
// Sort messages by createdAt ascending to get the latest one
const sortedMessages = [...(chat.messages || [])].sort(
(m1, m2) => m1.createdAt.getTime() - m2.createdAt.getTime()
);
const latestMessage = sortedMessages[sortedMessages.length - 1];
if (!latestMessage) {
return { ...chat, latestMessage: undefined };
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@
<div class="inline-block rounded-[10px] border border-gray-300 bg-gray-200 px-4 py-2">
<div class="text-left text-sm font-medium whitespace-pre-wrap text-black">
{@html displayText.replace(
/<a href="([^"]+)">([^<]+)<\/a>/g,
'<a href="$1" class="text-blue-600 hover:text-blue-800 underline">$2</a>'
/<a href="([^"]+)"([^>]*)>([^<]+)<\/a>/g,
'<a href="$1"$2 class="text-blue-600 hover:text-blue-800 underline cursor-pointer">$3</a>'
)}
</div>
</div>
Expand Down Expand Up @@ -102,8 +102,8 @@

<div class={cn(`${!isOwn ? 'text-white' : 'text-black-600'} whitespace-pre-wrap`)}>
{@html displayText.replace(
/<a href="([^"]+)">([^<]+)<\/a>/g,
'<a href="$1" class="text-blue-600 hover:text-blue-800 underline">$2</a>'
/<a href="([^"]+)"([^>]*)>([^<]+)<\/a>/g,
'<a href="$1"$2 class="text-blue-600 hover:text-blue-800 underline cursor-pointer">$3</a>'
)}
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,13 @@

<div>
<Label>eName</Label>
<Input type="text" placeholder="Your eName" bind:value={handle} disabled class="cursor-not-allowed opacity-70" />
<Input
type="text"
placeholder="Your eName"
bind:value={handle}
disabled
class="cursor-not-allowed opacity-70"
/>
</div>
<div>
<Label>Profile Name</Label>
Expand Down