widget parole 17
This commit is contained in:
parent
5a49f3f099
commit
5f41d9ed8d
@ -98,24 +98,29 @@ export async function GET(request: Request) {
|
|||||||
id: currentUser._id
|
id: currentUser._id
|
||||||
});
|
});
|
||||||
|
|
||||||
// Step 3: Create a user token for the current user using admin token
|
// Step 3: Login as the user to get their token
|
||||||
const createTokenResponse = await fetch(`${baseUrl}/api/v1/users.createToken`, {
|
const loginResponse = await fetch(`${baseUrl}/api/v1/login`, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: adminHeaders,
|
headers: {
|
||||||
|
'Content-Type': 'application/json'
|
||||||
|
},
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
userId: currentUser._id
|
user: currentUser.username,
|
||||||
|
password: process.env.ROCKET_CHAT_USER_PASSWORD // This should be the user's password or a shared password
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!createTokenResponse.ok) {
|
if (!loginResponse.ok) {
|
||||||
console.error('Failed to create user token:', createTokenResponse.status);
|
console.error('Failed to login as user:', loginResponse.status);
|
||||||
|
const errorText = await loginResponse.text();
|
||||||
|
console.error('Login error details:', errorText);
|
||||||
return NextResponse.json({ messages: [] }, { status: 200 });
|
return NextResponse.json({ messages: [] }, { status: 200 });
|
||||||
}
|
}
|
||||||
|
|
||||||
const tokenData = await createTokenResponse.json();
|
const loginData = await loginResponse.json();
|
||||||
const userHeaders = {
|
const userHeaders = {
|
||||||
'X-Auth-Token': tokenData.data.authToken,
|
'X-Auth-Token': loginData.data.authToken,
|
||||||
'X-User-Id': currentUser._id,
|
'X-User-Id': loginData.data.userId,
|
||||||
'Content-Type': 'application/json'
|
'Content-Type': 'application/json'
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -199,14 +204,36 @@ 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];
|
||||||
messages.push({
|
messages.push({
|
||||||
...messageData.messages[0],
|
id: message._id,
|
||||||
|
text: message.msg,
|
||||||
|
timestamp: message.ts,
|
||||||
roomName: subscription.fname || subscription.name || 'Direct Message',
|
roomName: subscription.fname || subscription.name || 'Direct Message',
|
||||||
roomType: subscription.t,
|
roomType: subscription.t,
|
||||||
unread: subscription.unread || 0,
|
unread: subscription.unread || 0,
|
||||||
userMentions: subscription.userMentions || 0,
|
userMentions: subscription.userMentions || 0,
|
||||||
alert: subscription.alert || false,
|
alert: subscription.alert || false,
|
||||||
lastSeen: subscription.ls
|
lastSeen: subscription.ls,
|
||||||
|
sender: {
|
||||||
|
username: message.u.username,
|
||||||
|
name: message.u.name || message.u.username,
|
||||||
|
initials: (message.u.name || message.u.username || '')
|
||||||
|
.split(' ')
|
||||||
|
.map((n: string) => n[0])
|
||||||
|
.slice(0, 2)
|
||||||
|
.join('')
|
||||||
|
.toUpperCase(),
|
||||||
|
color: message.u.username === currentUser.username ? '#E3E3E3' : getAvatarColor(message.u.username)
|
||||||
|
},
|
||||||
|
isOwnMessage: message.u.username === currentUser.username,
|
||||||
|
room: {
|
||||||
|
id: subscription.rid,
|
||||||
|
type: subscription.t,
|
||||||
|
name: subscription.fname || subscription.name,
|
||||||
|
isChannel: subscription.t === 'c',
|
||||||
|
isDirect: subscription.t === 'd'
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@ -216,12 +243,37 @@ export async function GET(request: Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Sort messages by timestamp (newest first) and limit to 7
|
// Sort messages by timestamp (newest first) and limit to 7
|
||||||
messages.sort((a, b) => new Date(b.ts).getTime() - new Date(a.ts).getTime());
|
messages.sort((a, b) => new Date(b.timestamp).getTime() - new Date(a.timestamp).getTime());
|
||||||
const limitedMessages = messages.slice(0, 7);
|
const limitedMessages = messages.slice(0, 7);
|
||||||
|
|
||||||
return NextResponse.json({ messages: limitedMessages }, { status: 200 });
|
return NextResponse.json({
|
||||||
|
messages: limitedMessages,
|
||||||
|
total: messages.length,
|
||||||
|
hasMore: messages.length > 7
|
||||||
|
}, { status: 200 });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error in messages endpoint:', error);
|
console.error('Error in messages endpoint:', error);
|
||||||
return NextResponse.json({ messages: [] }, { status: 200 });
|
return NextResponse.json({ messages: [], total: 0, hasMore: false }, { status: 200 });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Helper function to generate consistent avatar colors
|
||||||
|
function getAvatarColor(username: string): string {
|
||||||
|
const colors = [
|
||||||
|
'#FF7452', // Coral
|
||||||
|
'#4CAF50', // Green
|
||||||
|
'#2196F3', // Blue
|
||||||
|
'#9C27B0', // Purple
|
||||||
|
'#FF9800', // Orange
|
||||||
|
'#00BCD4', // Cyan
|
||||||
|
'#795548', // Brown
|
||||||
|
'#607D8B' // Blue Grey
|
||||||
|
];
|
||||||
|
|
||||||
|
// Generate a consistent index based on username
|
||||||
|
const index = username
|
||||||
|
.split('')
|
||||||
|
.reduce((acc, char) => acc + char.charCodeAt(0), 0) % colors.length;
|
||||||
|
|
||||||
|
return colors[index];
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user