this repo has no description
at main 2.3 kB view raw
1import { getCachetUser } from "./cachet"; 2import { userMappings } from "./db"; 3 4/** 5 * Converts IRC @mentions and nick: mentions to Slack user mentions 6 */ 7export function convertIrcMentionsToSlack(messageText: string): string { 8 let result = messageText; 9 10 // Find all @mentions and nick: mentions in the IRC message 11 const atMentionPattern = /@(\w+)/g; 12 const nickMentionPattern = /(\w+):/g; 13 14 const atMentions = Array.from(result.matchAll(atMentionPattern)); 15 const nickMentions = Array.from(result.matchAll(nickMentionPattern)); 16 17 for (const match of atMentions) { 18 const mentionedNick = match[1] as string; 19 const mentionedUserMapping = userMappings.getByIrcNick(mentionedNick); 20 if (mentionedUserMapping) { 21 result = result.replace( 22 match[0], 23 `<@${mentionedUserMapping.slack_user_id}>`, 24 ); 25 } 26 } 27 28 for (const match of nickMentions) { 29 const mentionedNick = match[1] as string; 30 const mentionedUserMapping = userMappings.getByIrcNick(mentionedNick); 31 if (mentionedUserMapping) { 32 result = result.replace( 33 match[0], 34 `<@${mentionedUserMapping.slack_user_id}>:`, 35 ); 36 } 37 } 38 39 return result; 40} 41 42/** 43 * Converts Slack user mentions to IRC @mentions 44 * Priority: user mappings > display name from mention > Cachet lookup 45 */ 46export async function convertSlackMentionsToIrc( 47 messageText: string, 48): Promise<string> { 49 let result = messageText; 50 const mentionRegex = /<@(U[A-Z0-9]+)(\|([^>]+))?>/g; 51 const mentions = Array.from(result.matchAll(mentionRegex)); 52 53 for (const match of mentions) { 54 const userId = match[1] as string; 55 const displayName = match[3] as string; // The name part after | 56 57 // Check if user has a mapped IRC nick 58 const mentionedUserMapping = userMappings.getBySlackUser(userId); 59 if (mentionedUserMapping) { 60 result = result.replace(match[0], `@${mentionedUserMapping.irc_nick}`); 61 } else { 62 // Try Cachet lookup if enabled 63 if (process.env.CACHET_ENABLED === "true") { 64 const data = await getCachetUser(userId); 65 if (data) { 66 result = result.replace(match[0], `@${data.displayName}`); 67 continue; 68 } 69 } 70 71 // Fallback to display name from the mention format <@U123|name> 72 if (displayName) { 73 result = result.replace(match[0], `@${displayName}`); 74 } 75 } 76 } 77 78 return result; 79}