Add tests for matrix info interaction
This commit is contained in:
		| @@ -6,46 +6,58 @@ const {discord, sync, db, select, from} = require("../../passthrough") | ||||
| /** @type {import("../../matrix/api")} */ | ||||
| const api = sync.require("../../matrix/api") | ||||
|  | ||||
| /** @param {DiscordTypes.APIContextMenuGuildInteraction} interaction */ | ||||
| /** @param {DiscordTypes.APIMessageApplicationCommandGuildInteraction} interaction */ | ||||
| async function interact({id, token, guild_id, channel, data}) { | ||||
| /** | ||||
|  * @param {DiscordTypes.APIMessageApplicationCommandGuildInteraction} interaction | ||||
|  * @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") | ||||
| 		.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) { | ||||
| 		return discord.snow.interaction.createInteractionResponse(id, token, { | ||||
| 		return { | ||||
| 			type: DiscordTypes.InteractionResponseType.ChannelMessageWithSource, | ||||
| 			data: { | ||||
| 				content: "This message hasn't been bridged to Matrix.", | ||||
| 				flags: DiscordTypes.MessageFlags.Ephemeral | ||||
| 			} | ||||
| 		}) | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	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 | ||||
| 		const userID = data.resolved.messages[data.target_id].author.id | ||||
| 		return discord.snow.interaction.createInteractionResponse(id, token, { | ||||
| 		return { | ||||
| 			type: DiscordTypes.InteractionResponseType.ChannelMessageWithSource, | ||||
| 			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, | ||||
| 				flags: DiscordTypes.MessageFlags.Ephemeral | ||||
| 			} | ||||
| 		}) | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	// from Matrix | ||||
| 	const event = await api.getEvent(message.room_id, message.event_id) | ||||
| 	return discord.snow.interaction.createInteractionResponse(id, token, { | ||||
| 	return { | ||||
| 		type: DiscordTypes.InteractionResponseType.ChannelMessageWithSource, | ||||
| 		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, | ||||
| 			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 | ||||
|   | ||||
							
								
								
									
										76
									
								
								src/discord/interactions/matrix-info.test.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										76
									
								
								src/discord/interactions/matrix-info.test.js
									
									
									
									
									
										Normal 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) | ||||
| }) | ||||
		Reference in New Issue
	
	Block a user
	 Cadence Ember
					Cadence Ember