widget parole 20
This commit is contained in:
parent
73fe73ee5d
commit
e07db4584f
@ -170,19 +170,20 @@ export async function GET(request: Request) {
|
|||||||
|
|
||||||
// Step 5: Fetch messages using user token
|
// Step 5: Fetch messages using user token
|
||||||
for (const subscription of userSubscriptions) {
|
for (const subscription of userSubscriptions) {
|
||||||
if (messages.length >= 7 || processedRooms.has(subscription._id)) continue;
|
if (messages.length >= 10 || processedRooms.has(subscription._id)) continue;
|
||||||
processedRooms.add(subscription._id);
|
processedRooms.add(subscription._id);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Determine the correct endpoint based on room type
|
// Determine the correct endpoint based on room type
|
||||||
const endpoint = subscription.t === 'c' ? 'channels.messages' : 'im.messages';
|
const endpoint = subscription.t === 'c' ? 'channels.messages' : 'im.messages';
|
||||||
|
|
||||||
// Get the latest messages from the room using user token
|
// Get more messages from each room
|
||||||
const messagesResponse = await fetch(
|
const messagesResponse = await fetch(
|
||||||
`${baseUrl}/api/v1/${endpoint}?roomId=${subscription.rid}&count=1`, {
|
`${baseUrl}/api/v1/${endpoint}?roomId=${subscription.rid}&count=3`, {
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
headers: userHeaders
|
headers: userHeaders
|
||||||
});
|
}
|
||||||
|
);
|
||||||
|
|
||||||
if (!messagesResponse.ok) {
|
if (!messagesResponse.ok) {
|
||||||
console.error(`Failed to get messages for room ${subscription.name}:`, messagesResponse.status);
|
console.error(`Failed to get messages for room ${subscription.name}:`, messagesResponse.status);
|
||||||
@ -199,74 +200,73 @@ export async function GET(request: Request) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
if (messageData.success && messageData.messages?.length > 0) {
|
if (messageData.success && messageData.messages?.length > 0) {
|
||||||
const message = messageData.messages[0];
|
// Process each message from the room
|
||||||
const messageUser = message.u || {};
|
for (const message of messageData.messages) {
|
||||||
const username = messageUser.username || subscription.name || 'unknown';
|
const messageUser = message.u || {};
|
||||||
const displayName = subscription.fname || subscription.name || username;
|
const username = messageUser.username || subscription.name || 'unknown';
|
||||||
|
const displayName = subscription.fname || subscription.name || username;
|
||||||
|
|
||||||
// Format the timestamp
|
// Format the timestamp
|
||||||
const timestamp = new Date(message.ts);
|
const timestamp = new Date(message.ts);
|
||||||
const now = new Date();
|
const now = new Date();
|
||||||
let formattedTime = '';
|
let formattedTime = '';
|
||||||
|
|
||||||
if (isNaN(timestamp.getTime())) {
|
if (isNaN(timestamp.getTime())) {
|
||||||
formattedTime = 'Invalid Date';
|
formattedTime = 'Invalid Date';
|
||||||
} else if (timestamp.toDateString() === now.toDateString()) {
|
} else if (timestamp.toDateString() === now.toDateString()) {
|
||||||
// Today - show time only
|
// Today - show time only
|
||||||
formattedTime = timestamp.toLocaleTimeString('fr-FR', {
|
formattedTime = timestamp.toLocaleTimeString('fr-FR', {
|
||||||
hour: '2-digit',
|
hour: '2-digit',
|
||||||
minute: '2-digit'
|
minute: '2-digit'
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
// Not today - show date
|
// Not today - show date
|
||||||
formattedTime = timestamp.toLocaleDateString('fr-FR', {
|
formattedTime = timestamp.toLocaleDateString('fr-FR', {
|
||||||
day: '2-digit',
|
day: '2-digit',
|
||||||
month: 'short'
|
month: 'short'
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create initials from display name
|
||||||
|
const initials = displayName
|
||||||
|
.split(' ')
|
||||||
|
.map((n: string) => n[0])
|
||||||
|
.slice(0, 2)
|
||||||
|
.join('')
|
||||||
|
.toUpperCase();
|
||||||
|
|
||||||
|
messages.push({
|
||||||
|
id: message._id,
|
||||||
|
text: message.msg || '',
|
||||||
|
timestamp: formattedTime,
|
||||||
|
rawTimestamp: message.ts,
|
||||||
|
roomName: displayName,
|
||||||
|
roomType: subscription.t,
|
||||||
|
unread: subscription.unread || 0,
|
||||||
|
userMentions: subscription.userMentions || 0,
|
||||||
|
alert: subscription.alert || false,
|
||||||
|
lastSeen: subscription.ls,
|
||||||
|
u: messageUser,
|
||||||
|
sender: {
|
||||||
|
_id: messageUser._id,
|
||||||
|
username: username,
|
||||||
|
name: displayName,
|
||||||
|
initials: initials,
|
||||||
|
color: getAvatarColor(username)
|
||||||
|
},
|
||||||
|
isOwnMessage: messageUser.username === currentUser.username,
|
||||||
|
room: {
|
||||||
|
id: subscription.rid,
|
||||||
|
type: subscription.t,
|
||||||
|
name: displayName,
|
||||||
|
isChannel: subscription.t === 'c',
|
||||||
|
isDirect: subscription.t === 'd',
|
||||||
|
link: subscription.t === 'c'
|
||||||
|
? `${baseUrl}/channel/${subscription.name}`
|
||||||
|
: `${baseUrl}/direct/${subscription.name}`
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create initials from display name
|
|
||||||
const initials = displayName
|
|
||||||
.split(' ')
|
|
||||||
.map((n: string) => n[0])
|
|
||||||
.slice(0, 2)
|
|
||||||
.join('')
|
|
||||||
.toUpperCase();
|
|
||||||
|
|
||||||
messages.push({
|
|
||||||
id: message._id,
|
|
||||||
text: message.msg || '',
|
|
||||||
timestamp: formattedTime,
|
|
||||||
roomName: displayName,
|
|
||||||
roomType: subscription.t,
|
|
||||||
unread: subscription.unread || 0,
|
|
||||||
userMentions: subscription.userMentions || 0,
|
|
||||||
alert: subscription.alert || false,
|
|
||||||
lastSeen: subscription.ls,
|
|
||||||
u: { // Add back the u object that the component expects
|
|
||||||
_id: messageUser._id || subscription.u?._id,
|
|
||||||
username: username,
|
|
||||||
name: displayName
|
|
||||||
},
|
|
||||||
sender: {
|
|
||||||
_id: messageUser._id || subscription.u?._id,
|
|
||||||
username: username,
|
|
||||||
name: displayName,
|
|
||||||
initials: initials,
|
|
||||||
color: getAvatarColor(username)
|
|
||||||
},
|
|
||||||
isOwnMessage: username === currentUser.username,
|
|
||||||
room: {
|
|
||||||
id: subscription.rid,
|
|
||||||
type: subscription.t,
|
|
||||||
name: displayName,
|
|
||||||
isChannel: subscription.t === 'c',
|
|
||||||
isDirect: subscription.t === 'd',
|
|
||||||
link: subscription.t === 'c'
|
|
||||||
? `${baseUrl}/channel/${subscription.name}`
|
|
||||||
: `${baseUrl}/direct/${subscription.name}`
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(`Error fetching message for room ${subscription.name}:`, error);
|
console.error(`Error fetching message for room ${subscription.name}:`, error);
|
||||||
@ -274,12 +274,8 @@ export async function GET(request: Request) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sort messages by timestamp (newest first) and limit to 7
|
// Sort messages by raw timestamp (newest first) and limit to 7
|
||||||
messages.sort((a, b) => {
|
messages.sort((a, b) => new Date(b.rawTimestamp).getTime() - new Date(a.rawTimestamp).getTime());
|
||||||
const dateA = new Date(a.timestamp);
|
|
||||||
const dateB = new Date(b.timestamp);
|
|
||||||
return dateB.getTime() - dateA.getTime();
|
|
||||||
});
|
|
||||||
|
|
||||||
const limitedMessages = messages.slice(0, 7);
|
const limitedMessages = messages.slice(0, 7);
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user