m->d: Fix HTML entities showing in reply preview
This commit is contained in:
		
							parent
							
								
									480c7a6bd9
								
							
						
					
					
						commit
						999276e407
					
				
					 5 changed files with 71 additions and 5 deletions
				
			
		| 
						 | 
				
			
			@ -5,6 +5,7 @@ const DiscordTypes = require("discord-api-types/v10")
 | 
			
		|||
const chunk = require("chunk-text")
 | 
			
		||||
const TurndownService = require("turndown")
 | 
			
		||||
const assert = require("assert").strict
 | 
			
		||||
const entities = require("entities")
 | 
			
		||||
 | 
			
		||||
const passthrough = require("../../passthrough")
 | 
			
		||||
const {sync, db, discord, select, from} = passthrough
 | 
			
		||||
| 
						 | 
				
			
			@ -349,11 +350,13 @@ async function eventToMessage(event, guild, di) {
 | 
			
		|||
			} else {
 | 
			
		||||
				const repliedToContent = repliedToEvent.content.formatted_body || repliedToEvent.content.body
 | 
			
		||||
				const contentPreviewChunks = chunk(
 | 
			
		||||
					repliedToContent.replace(/.*<\/mx-reply>/, "") // Remove everything before replies, so just use the actual message body
 | 
			
		||||
					.replace(/<blockquote>.*?<\/blockquote>/, "") // If the message starts with a blockquote, don't count it and use the message body afterwards
 | 
			
		||||
					.replace(/(?:\n|<br>)+/g, " ") // Should all be on one line
 | 
			
		||||
					.replace(/<span [^>]*data-mx-spoiler\b[^>]*>.*?<\/span>/g, "[spoiler]") // Good enough method of removing spoiler content. (I don't want to break out the HTML parser unless I have to.)
 | 
			
		||||
					.replace(/<[^>]+>/g, ""), 50) // Completely strip all other formatting.
 | 
			
		||||
					entities.decodeHTML5Strict( // Remove entities like & "
 | 
			
		||||
						repliedToContent.replace(/.*<\/mx-reply>/, "") // Remove everything before replies, so just use the actual message body
 | 
			
		||||
						.replace(/<blockquote>.*?<\/blockquote>/, "") // If the message starts with a blockquote, don't count it and use the message body afterwards
 | 
			
		||||
						.replace(/(?:\n|<br>)+/g, " ") // Should all be on one line
 | 
			
		||||
						.replace(/<span [^>]*data-mx-spoiler\b[^>]*>.*?<\/span>/g, "[spoiler]") // Good enough method of removing spoiler content. (I don't want to break out the HTML parser unless I have to.)
 | 
			
		||||
						.replace(/<[^>]+>/g, "") // Completely strip all HTML tags and formatting.
 | 
			
		||||
					), 50)
 | 
			
		||||
				contentPreview = ":\n> "
 | 
			
		||||
				contentPreview += contentPreviewChunks.length > 1 ? contentPreviewChunks[0] + "..." : contentPreviewChunks[0]
 | 
			
		||||
			}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -813,6 +813,55 @@ test("event2message: should include a reply preview when message ends with a blo
 | 
			
		|||
	)
 | 
			
		||||
})
 | 
			
		||||
 | 
			
		||||
test("event2message: entities are not escaped in main message or reply preview", async t => {
 | 
			
		||||
	// Intended result: Testing? in italics, followed by the sequence "':.`[]&things
 | 
			
		||||
	t.deepEqual(
 | 
			
		||||
		await eventToMessage({
 | 
			
		||||
			type: "m.room.message",
 | 
			
		||||
			sender: "@cadence:cadence.moe",
 | 
			
		||||
			content: {
 | 
			
		||||
				msgtype: "m.text",
 | 
			
		||||
				body: "> <@cadence:cadence.moe> _Testing?_ \"':.`[]&things\n\n_Testing?_ \"':.`[]&things",
 | 
			
		||||
				format: "org.matrix.custom.html",
 | 
			
		||||
				formatted_body: "<mx-reply><blockquote><a href=\"https://matrix.to/#/!fGgIymcYWOqjbSRUdV:cadence.moe/$yIWjZPi6Xk56fBxJwqV4ANs_hYLjnWI2cNKbZ2zwk60?via=cadence.moe&via=feather.onl&via=mythic.onl\">In reply to</a> <a href=\"https://matrix.to/#/@cadence:cadence.moe\">@cadence:cadence.moe</a><br><em>Testing?</em> \"':.`[]&things</blockquote></mx-reply><em>Testing?</em> "':.`[]&things",
 | 
			
		||||
				"m.relates_to": {
 | 
			
		||||
					"m.in_reply_to": {
 | 
			
		||||
						event_id: "$yIWjZPi6Xk56fBxJwqV4ANs_hYLjnWI2cNKbZ2zwk60"
 | 
			
		||||
					}
 | 
			
		||||
				}
 | 
			
		||||
			},
 | 
			
		||||
			event_id: "$2I7odT9okTdpwDcqOjkJb_A3utdO4V8Cp3LK6-Rvwcs",
 | 
			
		||||
			room_id: "!fGgIymcYWOqjbSRUdV:cadence.moe"
 | 
			
		||||
		}, data.guild.general, {
 | 
			
		||||
			api: {
 | 
			
		||||
				getEvent: mockGetEvent(t, "!fGgIymcYWOqjbSRUdV:cadence.moe", "$yIWjZPi6Xk56fBxJwqV4ANs_hYLjnWI2cNKbZ2zwk60", {
 | 
			
		||||
					type: "m.room.message",
 | 
			
		||||
					sender: "@cadence:cadence.moe",
 | 
			
		||||
					content: {
 | 
			
		||||
					  "msgtype": "m.text",
 | 
			
		||||
					  "body": "_Testing?_ \"':.`[]&things",
 | 
			
		||||
					  "format": "org.matrix.custom.html",
 | 
			
		||||
					  "formatted_body": "<em>Testing?</em> "':.`[]&things"
 | 
			
		||||
					},
 | 
			
		||||
					event_id: "$yIWjZPi6Xk56fBxJwqV4ANs_hYLjnWI2cNKbZ2zwk60",
 | 
			
		||||
					room_id: "!fGgIymcYWOqjbSRUdV:cadence.moe"
 | 
			
		||||
				})
 | 
			
		||||
			}
 | 
			
		||||
		}),
 | 
			
		||||
		{
 | 
			
		||||
			messagesToDelete: [],
 | 
			
		||||
			messagesToEdit: [],
 | 
			
		||||
			messagesToSend: [{
 | 
			
		||||
				username: "cadence [they]",
 | 
			
		||||
				content: "> <:L1:1144820033948762203><:L2:1144820084079087647>Ⓜ️**cadence [they]**:"
 | 
			
		||||
					+ "\n> Testing? \"':.`[]&things"
 | 
			
		||||
					+ "\n_Testing?_ \"':.\\`\\[\\]&things",
 | 
			
		||||
				avatar_url: "https://matrix.cadence.moe/_matrix/media/r0/download/cadence.moe/azCAhThKTojXSZJRoWwZmhvU"
 | 
			
		||||
			}]
 | 
			
		||||
		}
 | 
			
		||||
	)
 | 
			
		||||
})
 | 
			
		||||
 | 
			
		||||
test("event2message: editing a rich reply to a sim user", async t => {
 | 
			
		||||
	const eventsFetched = []
 | 
			
		||||
	t.deepEqual(
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue