Fix converting discord channel links
This commit is contained in:
		
							parent
							
								
									5b7433de32
								
							
						
					
					
						commit
						cffd3c9f2e
					
				
					 5 changed files with 102 additions and 7 deletions
				
			
		
							
								
								
									
										8
									
								
								package-lock.json
									
										
									
										generated
									
									
									
								
							
							
						
						
									
										8
									
								
								package-lock.json
									
										
									
										generated
									
									
									
								
							| 
						 | 
				
			
			@ -10,7 +10,7 @@
 | 
			
		|||
      "license": "AGPL-3.0-or-later",
 | 
			
		||||
      "dependencies": {
 | 
			
		||||
        "@chriscdn/promise-semaphore": "^3.0.1",
 | 
			
		||||
        "@cloudrac3r/discord-markdown": "^2.6.6",
 | 
			
		||||
        "@cloudrac3r/discord-markdown": "^2.6.7",
 | 
			
		||||
        "@cloudrac3r/giframe": "^0.4.3",
 | 
			
		||||
        "@cloudrac3r/html-template-tag": "^5.0.1",
 | 
			
		||||
        "@cloudrac3r/in-your-element": "^1.1.1",
 | 
			
		||||
| 
						 | 
				
			
			@ -225,9 +225,9 @@
 | 
			
		|||
      }
 | 
			
		||||
    },
 | 
			
		||||
    "node_modules/@cloudrac3r/discord-markdown": {
 | 
			
		||||
      "version": "2.6.6",
 | 
			
		||||
      "resolved": "https://registry.npmjs.org/@cloudrac3r/discord-markdown/-/discord-markdown-2.6.6.tgz",
 | 
			
		||||
      "integrity": "sha512-4FNO7WmACPvcTrQjeLQLr9WRuP7JDUVUGFrRJvmAjiMs2UlUAsShfSRuU2SCqz3QqmX8vyJ06wy2hkjTTyRtbw==",
 | 
			
		||||
      "version": "2.6.7",
 | 
			
		||||
      "resolved": "https://registry.npmjs.org/@cloudrac3r/discord-markdown/-/discord-markdown-2.6.7.tgz",
 | 
			
		||||
      "integrity": "sha512-bWLmBYWaNEDcQfZHDz4jaAxLKA9161ruEnHo3ms6kfRw8uYku/Uz7U1xTmQ2dQF/q1PiuBvM9I37pLiotlQj8A==",
 | 
			
		||||
      "license": "MIT",
 | 
			
		||||
      "dependencies": {
 | 
			
		||||
        "simple-markdown": "^0.7.3"
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -19,7 +19,7 @@
 | 
			
		|||
  },
 | 
			
		||||
  "dependencies": {
 | 
			
		||||
    "@chriscdn/promise-semaphore": "^3.0.1",
 | 
			
		||||
    "@cloudrac3r/discord-markdown": "^2.6.6",
 | 
			
		||||
    "@cloudrac3r/discord-markdown": "^2.6.7",
 | 
			
		||||
    "@cloudrac3r/giframe": "^0.4.3",
 | 
			
		||||
    "@cloudrac3r/html-template-tag": "^5.0.1",
 | 
			
		||||
    "@cloudrac3r/in-your-element": "^1.1.1",
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -408,13 +408,13 @@ async function messageToEvent(message, guild, options = {}, di) {
 | 
			
		|||
 | 
			
		||||
		async function transformParsedVia(parsed) {
 | 
			
		||||
			for (const node of parsed) {
 | 
			
		||||
				if (node.type === "discordChannel") {
 | 
			
		||||
				if (node.type === "discordChannel" || node.type === "discordChannelLink") {
 | 
			
		||||
					node.row = select("channel_room", ["room_id", "name", "nick"], {channel_id: node.id}).get()
 | 
			
		||||
					if (node.row?.room_id) {
 | 
			
		||||
						node.via = await getViaServersMemo(node.row.room_id)
 | 
			
		||||
					}
 | 
			
		||||
				}
 | 
			
		||||
				;for (const maybeChildNodesArray of [node, node.content, node.items]) {
 | 
			
		||||
				for (const maybeChildNodesArray of [node, node.content, node.items]) {
 | 
			
		||||
					if (Array.isArray(maybeChildNodesArray)) {
 | 
			
		||||
						await transformParsedVia(maybeChildNodesArray)
 | 
			
		||||
					}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -100,6 +100,44 @@ test("message2event: simple room mention", async t => {
 | 
			
		|||
	t.equal(called, 2, "should call getStateEvent and getJoinedMembers once each")
 | 
			
		||||
})
 | 
			
		||||
 | 
			
		||||
test("message2event: simple room link", async t => {
 | 
			
		||||
	let called = 0
 | 
			
		||||
	const events = await messageToEvent(data.message.simple_room_link, data.guild.general, {}, {
 | 
			
		||||
		api: {
 | 
			
		||||
			async getStateEvent(roomID, type, key) {
 | 
			
		||||
				called++
 | 
			
		||||
				t.equal(roomID, "!BnKuBPCvyfOkhcUjEu:cadence.moe")
 | 
			
		||||
				t.equal(type, "m.room.power_levels")
 | 
			
		||||
				t.equal(key, "")
 | 
			
		||||
				return {
 | 
			
		||||
					users: {
 | 
			
		||||
						"@_ooye_bot:cadence.moe": 100
 | 
			
		||||
					}
 | 
			
		||||
				}
 | 
			
		||||
			},
 | 
			
		||||
			async getJoinedMembers(roomID) {
 | 
			
		||||
				called++
 | 
			
		||||
				t.equal(roomID, "!BnKuBPCvyfOkhcUjEu:cadence.moe")
 | 
			
		||||
				return {
 | 
			
		||||
					joined: {
 | 
			
		||||
						"@_ooye_bot:cadence.moe": {display_name: null, avatar_url: null},
 | 
			
		||||
						"@user:matrix.org": {display_name: null, avatar_url: null}
 | 
			
		||||
					}
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	})
 | 
			
		||||
	t.deepEqual(events, [{
 | 
			
		||||
		$type: "m.room.message",
 | 
			
		||||
		"m.mentions": {},
 | 
			
		||||
		msgtype: "m.text",
 | 
			
		||||
		body: "#worm-farm",
 | 
			
		||||
		format: "org.matrix.custom.html",
 | 
			
		||||
		formatted_body: '<a href="https://matrix.to/#/!BnKuBPCvyfOkhcUjEu:cadence.moe?via=cadence.moe&via=matrix.org">#worm-farm</a>'
 | 
			
		||||
	}])
 | 
			
		||||
	t.equal(called, 2, "should call getStateEvent and getJoinedMembers once each")
 | 
			
		||||
})
 | 
			
		||||
 | 
			
		||||
test("message2event: nicked room mention", async t => {
 | 
			
		||||
	let called = 0
 | 
			
		||||
	const events = await messageToEvent(data.message.nicked_room_mention, data.guild.general, {}, {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										57
									
								
								test/data.js
									
										
									
									
									
								
							
							
						
						
									
										57
									
								
								test/data.js
									
										
									
									
									
								
							| 
						 | 
				
			
			@ -1398,6 +1398,63 @@ module.exports = {
 | 
			
		|||
			attachments: [],
 | 
			
		||||
			guild_id: "112760669178241024"
 | 
			
		||||
		},
 | 
			
		||||
		simple_room_link: {
 | 
			
		||||
			type: 0,
 | 
			
		||||
			tts: false,
 | 
			
		||||
			timestamp: "2023-07-10T20:04:25.939000+00:00",
 | 
			
		||||
			referenced_message: null,
 | 
			
		||||
			pinned: false,
 | 
			
		||||
			nonce: "1128054139385806848",
 | 
			
		||||
			mentions: [],
 | 
			
		||||
			mention_roles: [],
 | 
			
		||||
			mention_everyone: false,
 | 
			
		||||
			member: {
 | 
			
		||||
				roles: [
 | 
			
		||||
					"112767366235959296",  "118924814567211009",
 | 
			
		||||
					"204427286542417920",  "199995902742626304",
 | 
			
		||||
					"222168467627835392",  "238028326281805825",
 | 
			
		||||
					"259806643414499328",  "265239342648131584",
 | 
			
		||||
					"271173313575780353",  "287733611912757249",
 | 
			
		||||
					"225744901915148298",  "305775031223320577",
 | 
			
		||||
					"318243902521868288",  "348651574924541953",
 | 
			
		||||
					"349185088157777920",  "378402925128712193",
 | 
			
		||||
					"392141548932038658",  "393912152173576203",
 | 
			
		||||
					"482860581670486028",  "495384759074160642",
 | 
			
		||||
					"638988388740890635",  "373336013109461013",
 | 
			
		||||
					"530220455085473813",  "454567553738473472",
 | 
			
		||||
					"790724320824655873",  "1123518980456452097",
 | 
			
		||||
					"1040735082610167858", "695946570482450442",
 | 
			
		||||
					"1123460940935991296", "849737964090556488"
 | 
			
		||||
				],
 | 
			
		||||
				premium_since: null,
 | 
			
		||||
				pending: false,
 | 
			
		||||
				nick: null,
 | 
			
		||||
				mute: false,
 | 
			
		||||
				joined_at: "2015-11-11T09:55:40.321000+00:00",
 | 
			
		||||
				flags: 0,
 | 
			
		||||
				deaf: false,
 | 
			
		||||
				communication_disabled_until: null,
 | 
			
		||||
				avatar: null
 | 
			
		||||
			},
 | 
			
		||||
			id: "1128054143064494233",
 | 
			
		||||
			flags: 0,
 | 
			
		||||
			embeds: [],
 | 
			
		||||
			edited_timestamp: null,
 | 
			
		||||
			content: "https://discord.com/channels/112760669178241024/1100319550446252084",
 | 
			
		||||
			components: [],
 | 
			
		||||
			channel_id: "266767590641238027",
 | 
			
		||||
			author: {
 | 
			
		||||
				username: "kumaccino",
 | 
			
		||||
				public_flags: 128,
 | 
			
		||||
				id: "113340068197859328",
 | 
			
		||||
				global_name: "kumaccino",
 | 
			
		||||
				discriminator: "0",
 | 
			
		||||
				avatar_decoration: null,
 | 
			
		||||
				avatar: "b48302623a12bc7c59a71328f72ccb39"
 | 
			
		||||
			},
 | 
			
		||||
			attachments: [],
 | 
			
		||||
			guild_id: "112760669178241024"
 | 
			
		||||
		},
 | 
			
		||||
		nicked_room_mention: {
 | 
			
		||||
			type: 0,
 | 
			
		||||
			tts: false,
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue