1
0
Fork 0

Add tests for matrix info interaction

This commit is contained in:
Cadence Ember 2024-09-30 01:18:56 +13:00
parent bad8c5b8c2
commit 61803c3838
3 changed files with 101 additions and 12 deletions

View file

@ -6,46 +6,58 @@ const {discord, sync, db, select, from} = require("../../passthrough")
/** @type {import("../../matrix/api")} */ /** @type {import("../../matrix/api")} */
const api = sync.require("../../matrix/api") const api = sync.require("../../matrix/api")
/** @param {DiscordTypes.APIContextMenuGuildInteraction} interaction */ /**
/** @param {DiscordTypes.APIMessageApplicationCommandGuildInteraction} interaction */ * @param {DiscordTypes.APIMessageApplicationCommandGuildInteraction} interaction
async function interact({id, token, guild_id, channel, data}) { * @param {{api: typeof api}} di
* @returns {Promise<DiscordTypes.APIInteractionResponse>}
*/
async function _interact({guild_id, data}, {api}) {
const message = from("event_message").join("message_channel", "message_id").join("channel_room", "channel_id") const message = from("event_message").join("message_channel", "message_id").join("channel_room", "channel_id")
.select("name", "nick", "source", "room_id", "event_id").where({message_id: data.target_id}).get() .select("name", "nick", "source", "channel_id", "room_id", "event_id").where({message_id: data.target_id, part: 0}).get()
if (!message) { if (!message) {
return discord.snow.interaction.createInteractionResponse(id, token, { return {
type: DiscordTypes.InteractionResponseType.ChannelMessageWithSource, type: DiscordTypes.InteractionResponseType.ChannelMessageWithSource,
data: { data: {
content: "This message hasn't been bridged to Matrix.", content: "This message hasn't been bridged to Matrix.",
flags: DiscordTypes.MessageFlags.Ephemeral flags: DiscordTypes.MessageFlags.Ephemeral
} }
}) }
} }
const idInfo = `\n-# Room ID: \`${message.room_id}\`\n-# Event ID: \`${message.event_id}\`` const idInfo = `\n-# Room ID: \`${message.room_id}\`\n-# Event ID: \`${message.event_id}\``
const roomName = message.nick || message.name
if (message.source === 1) { // from Discord if (message.source === 1) { // from Discord
const userID = data.resolved.messages[data.target_id].author.id const userID = data.resolved.messages[data.target_id].author.id
return discord.snow.interaction.createInteractionResponse(id, token, { return {
type: DiscordTypes.InteractionResponseType.ChannelMessageWithSource, type: DiscordTypes.InteractionResponseType.ChannelMessageWithSource,
data: { data: {
content: `Bridged <@${userID}> https://discord.com/channels/${guild_id}/${channel.id}/${data.target_id} on Discord to [${message.nick || message.name}](<https://matrix.to/#/${message.room_id}/${message.event_id}>) on Matrix.` content: `Bridged <@${userID}> https://discord.com/channels/${guild_id}/${message.channel_id}/${data.target_id} on Discord to [${roomName}](<https://matrix.to/#/${message.room_id}/${message.event_id}>) on Matrix.`
+ idInfo, + idInfo,
flags: DiscordTypes.MessageFlags.Ephemeral flags: DiscordTypes.MessageFlags.Ephemeral
} }
}) }
} }
// from Matrix // from Matrix
const event = await api.getEvent(message.room_id, message.event_id) const event = await api.getEvent(message.room_id, message.event_id)
return discord.snow.interaction.createInteractionResponse(id, token, { return {
type: DiscordTypes.InteractionResponseType.ChannelMessageWithSource, type: DiscordTypes.InteractionResponseType.ChannelMessageWithSource,
data: { data: {
content: `Bridged [${event.sender}](<https://matrix.to/#/${event.sender}>)'s message in [${message.nick || message.name}](<https://matrix.to/#/${message.room_id}/${message.event_id}>) on Matrix to https://discord.com/channels/${guild_id}/${channel.id}/${data.target_id} on Discord.` content: `Bridged [${event.sender}](<https://matrix.to/#/${event.sender}>)'s message in [${roomName}](<https://matrix.to/#/${message.room_id}/${message.event_id}>) on Matrix to https://discord.com/channels/${guild_id}/${message.channel_id}/${data.target_id} on Discord.`
+ idInfo, + idInfo,
flags: DiscordTypes.MessageFlags.Ephemeral flags: DiscordTypes.MessageFlags.Ephemeral
} }
}) }
}
/* c8 ignore start */
/** @param {DiscordTypes.APIMessageApplicationCommandGuildInteraction} interaction */
async function interact(interaction) {
await discord.snow.interaction.createInteractionResponse(interaction.id, interaction.token, await _interact(interaction, {api}))
} }
module.exports.interact = interact module.exports.interact = interact
module.exports._interact = _interact

View file

@ -0,0 +1,76 @@
const {test} = require("supertape")
const data = require("../../../test/data")
const DiscordTypes = require("discord-api-types/v10")
const {db, discord} = require("../../passthrough")
const {MatrixServerError} = require("../../matrix/mreq")
const {_interact, _interactButton} = require("./matrix-info")
test("matrix info: checks if message has bridged", async t => {
const msg = await _interact({
data: {
target_id: "0"
},
guild_id: "112760669178241024"
}, {})
t.equal(msg.data.content, "This message hasn't been bridged to Matrix.")
})
test("matrix info: shows info for discord source message", async t => {
const msg = await _interact({
data: {
target_id: "1141619794500649020",
resolved: {
messages: {
"1141619794500649020": data.message_update.edit_by_webhook
}
}
},
guild_id: "497159726455455754"
}, {})
t.equal(
msg.data.content,
"Bridged <@700285844094845050> https://discord.com/channels/497159726455455754/497161350934560778/1141619794500649020 on Discord to [amanda-spam](<https://matrix.to/#/!CzvdIdUQXgUjDVKxeU:cadence.moe/$zXSlyI78DQqQwwfPUSzZ1b-nXzbUrCDljJgnGDdoI10>) on Matrix."
+ "\n-# Room ID: `!CzvdIdUQXgUjDVKxeU:cadence.moe`"
+ "\n-# Event ID: `$zXSlyI78DQqQwwfPUSzZ1b-nXzbUrCDljJgnGDdoI10`"
)
})
test("matrix info: shows info for matrix source message", async t => {
let called = 0
const msg = await _interact({
data: {
target_id: "1128118177155526666",
resolved: {
messages: {
"1141501302736695316": data.message.simple_reply_to_matrix_user
}
}
},
guild_id: "112760669178241024"
}, {
api: {
async getEvent(roomID, eventID) {
called++
t.equal(roomID, "!kLRqKKUQXcibIMtOpl:cadence.moe")
t.equal(eventID, "$Ij3qo7NxMA4VPexlAiIx2CB9JbsiGhJeyt-2OvkAUe4")
return {
event_id: eventID,
room_id: roomID,
type: "m.room.message",
content: {
msgtype: "m.text",
body: "so can you reply to my webhook uwu"
},
sender: "@cadence:cadence.moe"
}
}
}
})
t.equal(
msg.data.content,
"Bridged [@cadence:cadence.moe](<https://matrix.to/#/@cadence:cadence.moe>)'s message in [main](<https://matrix.to/#/!kLRqKKUQXcibIMtOpl:cadence.moe/$Ij3qo7NxMA4VPexlAiIx2CB9JbsiGhJeyt-2OvkAUe4>) on Matrix to https://discord.com/channels/112760669178241024/112760669178241024/1128118177155526666 on Discord."
+ "\n-# Room ID: `!kLRqKKUQXcibIMtOpl:cadence.moe`"
+ "\n-# Event ID: `$Ij3qo7NxMA4VPexlAiIx2CB9JbsiGhJeyt-2OvkAUe4`"
)
t.equal(called, 1)
})

View file

@ -137,4 +137,5 @@ file._actuallyUploadDiscordFileToMxc = function(url, res) { throw new Error(`Not
require("../src/m2d/converters/utils.test") require("../src/m2d/converters/utils.test")
require("../src/m2d/converters/emoji-sheet.test") require("../src/m2d/converters/emoji-sheet.test")
require("../src/discord/interactions/invite.test") require("../src/discord/interactions/invite.test")
require("../src/discord/interactions/matrix-info.test")
})() })()