Fix joining guessed @mentions to room

This commit is contained in:
Cadence Ember 2023-11-25 23:09:28 +13:00
parent 4313aa668d
commit cd2e012b8e
3 changed files with 12 additions and 5 deletions

View File

@ -15,6 +15,8 @@ const channelWebhook = sync.require("./channel-webhook")
const eventToMessage = sync.require("../converters/event-to-message")
/** @type {import("../../matrix/api")}) */
const api = sync.require("../../matrix/api")
/** @type {import("../../d2m/actions/register-user")} */
const registerUser = sync.require("../../d2m/actions/register-user")
/**
* @param {DiscordTypes.RESTPostAPIWebhookWithTokenJSONBody & {files?: {name: string, file: Buffer | Readable}[], pendingFiles?: ({name: string, url: string} | {name: string, url: string, key: string, iv: string} | {name: string, buffer: Buffer | Readable})[]}} message
@ -73,7 +75,7 @@ async function sendEvent(event) {
// no need to sync the matrix member to the other side. but if I did need to, this is where I'd do it
let {messagesToEdit, messagesToSend, messagesToDelete} = await eventToMessage.eventToMessage(event, guild, {api, snow: discord.snow})
let {messagesToEdit, messagesToSend, messagesToDelete, ensureJoined} = await eventToMessage.eventToMessage(event, guild, {api, snow: discord.snow})
messagesToEdit = await Promise.all(messagesToEdit.map(async e => {
e.message = await resolvePendingFiles(e.message)
@ -107,6 +109,10 @@ async function sendEvent(event) {
messageResponses.push(messageResponse)
}
for (const user of ensureJoined) {
registerUser.ensureSimJoined(user, event.room_id)
}
return messageResponses
}

View File

@ -505,12 +505,13 @@ async function eventToMessage(event, guild, di) {
content = displayNameRunoff + replyLine + content
// Handling written @mentions: we need to look for candidate Discord members to join to the room
let writtenMentionMatch = content.match(/(?:^|[^"<>/A-Za-z0-9])@([A-Za-z][A-Za-z0-9._\[\]\(\)-]+):?/)
let writtenMentionMatch = content.match(/(?:^|[^"<>/A-Za-z0-9])@([A-Za-z][A-Za-z0-9._\[\]\(\)-]+):?/d) // /d flag for indices requires node.js 16+
if (writtenMentionMatch) {
const results = await di.snow.guild.searchGuildMembers(guild.id, {query: writtenMentionMatch[1]})
if (results[0]) {
assert(results[0].user)
content = content.slice(0, writtenMentionMatch.index) + `<@${results[0].user.id}>` + content.slice(writtenMentionMatch.index + writtenMentionMatch[0].length)
// @ts-ignore - typescript doesn't know about indices yet
content = content.slice(0, writtenMentionMatch.indices[1][0]-1) + `<@${results[0].user.id}>` + content.slice(writtenMentionMatch.indices[1][1])
ensureJoined.push(results[0].user)
}
}

View File

@ -2179,7 +2179,7 @@ test("event2message: guessed @mentions may join members to mention", async t =>
sender: "@cadence:cadence.moe",
content: {
msgtype: "m.text",
body: "@subtext: what food would you like to order?"
body: "hey @subtext, what food would you like to order?"
},
event_id: "$u5gSwSzv_ZQS3eM00mnTBCor8nx_A_AwuQz7e59PZk8",
room_id: "!kLRqKKUQXcibIMtOpl:cadence.moe"
@ -2202,7 +2202,7 @@ test("event2message: guessed @mentions may join members to mention", async t =>
messagesToEdit: [],
messagesToSend: [{
username: "cadence [they]",
content: "<@321876634777218072> what food would you like to order?",
content: "hey <@321876634777218072>, what food would you like to order?",
avatar_url: undefined
}],
ensureJoined: [subtext.user]