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
|
||||
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);
|
||||
|
||||
try {
|
||||
// Determine the correct endpoint based on room type
|
||||
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(
|
||||
`${baseUrl}/api/v1/${endpoint}?roomId=${subscription.rid}&count=1`, {
|
||||
method: 'GET',
|
||||
headers: userHeaders
|
||||
});
|
||||
`${baseUrl}/api/v1/${endpoint}?roomId=${subscription.rid}&count=3`, {
|
||||
method: 'GET',
|
||||
headers: userHeaders
|
||||
}
|
||||
);
|
||||
|
||||
if (!messagesResponse.ok) {
|
||||
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) {
|
||||
const message = messageData.messages[0];
|
||||
const messageUser = message.u || {};
|
||||
const username = messageUser.username || subscription.name || 'unknown';
|
||||
const displayName = subscription.fname || subscription.name || username;
|
||||
|
||||
// Format the timestamp
|
||||
const timestamp = new Date(message.ts);
|
||||
const now = new Date();
|
||||
let formattedTime = '';
|
||||
|
||||
if (isNaN(timestamp.getTime())) {
|
||||
formattedTime = 'Invalid Date';
|
||||
} else if (timestamp.toDateString() === now.toDateString()) {
|
||||
// Today - show time only
|
||||
formattedTime = timestamp.toLocaleTimeString('fr-FR', {
|
||||
hour: '2-digit',
|
||||
minute: '2-digit'
|
||||
});
|
||||
} else {
|
||||
// Not today - show date
|
||||
formattedTime = timestamp.toLocaleDateString('fr-FR', {
|
||||
day: '2-digit',
|
||||
month: 'short'
|
||||
// Process each message from the room
|
||||
for (const message of messageData.messages) {
|
||||
const messageUser = message.u || {};
|
||||
const username = messageUser.username || subscription.name || 'unknown';
|
||||
const displayName = subscription.fname || subscription.name || username;
|
||||
|
||||
// Format the timestamp
|
||||
const timestamp = new Date(message.ts);
|
||||
const now = new Date();
|
||||
let formattedTime = '';
|
||||
|
||||
if (isNaN(timestamp.getTime())) {
|
||||
formattedTime = 'Invalid Date';
|
||||
} else if (timestamp.toDateString() === now.toDateString()) {
|
||||
// Today - show time only
|
||||
formattedTime = timestamp.toLocaleTimeString('fr-FR', {
|
||||
hour: '2-digit',
|
||||
minute: '2-digit'
|
||||
});
|
||||
} else {
|
||||
// Not today - show date
|
||||
formattedTime = timestamp.toLocaleDateString('fr-FR', {
|
||||
day: '2-digit',
|
||||
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) {
|
||||
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
|
||||
messages.sort((a, b) => {
|
||||
const dateA = new Date(a.timestamp);
|
||||
const dateB = new Date(b.timestamp);
|
||||
return dateB.getTime() - dateA.getTime();
|
||||
});
|
||||
// Sort messages by raw timestamp (newest first) and limit to 7
|
||||
messages.sort((a, b) => new Date(b.rawTimestamp).getTime() - new Date(a.rawTimestamp).getTime());
|
||||
|
||||
const limitedMessages = messages.slice(0, 7);
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user