( chatId: string, accessToken: string, chatTopic?: string )
| 39 | * @returns A meaningful display name for the chat |
| 40 | */ |
| 41 | const getChatDisplayName = async ( |
| 42 | chatId: string, |
| 43 | accessToken: string, |
| 44 | chatTopic?: string |
| 45 | ): Promise<string> => { |
| 46 | try { |
| 47 | const chatIdValidation = validateMicrosoftGraphId(chatId, 'chatId') |
| 48 | if (!chatIdValidation.isValid) { |
| 49 | logger.warn('Invalid chat ID in getChatDisplayName', { |
| 50 | error: chatIdValidation.error, |
| 51 | chatId: chatId.substring(0, 50), |
| 52 | }) |
| 53 | return `Chat ${chatId.substring(0, 8)}...` |
| 54 | } |
| 55 | |
| 56 | if (chatTopic?.trim() && chatTopic !== 'null') { |
| 57 | return chatTopic |
| 58 | } |
| 59 | |
| 60 | const membersResponse = await fetch( |
| 61 | `https://graph.microsoft.com/v1.0/chats/${encodeURIComponent(chatId)}/members`, |
| 62 | { |
| 63 | method: 'GET', |
| 64 | headers: { |
| 65 | Authorization: `Bearer ${accessToken}`, |
| 66 | 'Content-Type': 'application/json', |
| 67 | }, |
| 68 | } |
| 69 | ) |
| 70 | |
| 71 | if (membersResponse.ok) { |
| 72 | const membersData = await membersResponse.json() |
| 73 | const members = membersData.value || [] |
| 74 | |
| 75 | const memberNames = members |
| 76 | .filter((member: any) => member.displayName && member.displayName !== 'Unknown') |
| 77 | .map((member: any) => member.displayName) |
| 78 | .slice(0, 3) |
| 79 | |
| 80 | if (memberNames.length > 0) { |
| 81 | if (memberNames.length === 1) { |
| 82 | return memberNames[0] |
| 83 | } |
| 84 | if (memberNames.length === 2) { |
| 85 | return memberNames.join(' & ') |
| 86 | } |
| 87 | return `${memberNames.slice(0, 2).join(', ')} & ${memberNames.length - 2} more` |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | try { |
| 92 | const messagesResponse = await fetch( |
| 93 | `https://graph.microsoft.com/v1.0/chats/${encodeURIComponent(chatId)}/messages?$top=10&$orderby=createdDateTime desc`, |
| 94 | { |
| 95 | method: 'GET', |
| 96 | headers: { |
| 97 | Authorization: `Bearer ${accessToken}`, |
| 98 | 'Content-Type': 'application/json', |
no test coverage detected