don't send "" for attachments without content
This commit is contained in:
		
							parent
							
								
									1591bfc578
								
							
						
					
					
						commit
						d592a3c82e
					
				
					 5 changed files with 104 additions and 42 deletions
				
			
		| 
						 | 
				
			
			@ -16,48 +16,50 @@ async function messageToEvent(message, guild) {
 | 
			
		|||
	const events = []
 | 
			
		||||
 | 
			
		||||
	// Text content appears first
 | 
			
		||||
	const body = message.content
 | 
			
		||||
	const html = markdown.toHTML(body, {
 | 
			
		||||
		discordCallback: {
 | 
			
		||||
			user: node => {
 | 
			
		||||
				const mxid = db.prepare("SELECT mxid FROM sim WHERE discord_id = ?").pluck().get(node.id)
 | 
			
		||||
				if (mxid) {
 | 
			
		||||
					return "https://matrix.to/#/" + mxid
 | 
			
		||||
				} else {
 | 
			
		||||
					return "@" + node.id
 | 
			
		||||
				}
 | 
			
		||||
			},
 | 
			
		||||
			channel: node => {
 | 
			
		||||
				const roomID = db.prepare("SELECT room_id FROM channel_room WHERE channel_id = ?").pluck().get(node.id)
 | 
			
		||||
				if (roomID) {
 | 
			
		||||
					return "https://matrix.to/#/" + roomID
 | 
			
		||||
				} else {
 | 
			
		||||
					return "#" + node.id
 | 
			
		||||
				}
 | 
			
		||||
			},
 | 
			
		||||
			role: node =>
 | 
			
		||||
				"@&" + node.id,
 | 
			
		||||
			everyone: node =>
 | 
			
		||||
				"@room",
 | 
			
		||||
			here: node =>
 | 
			
		||||
				"@here"
 | 
			
		||||
	if (message.content) {
 | 
			
		||||
		const body = message.content
 | 
			
		||||
		const html = markdown.toHTML(body, {
 | 
			
		||||
			discordCallback: {
 | 
			
		||||
				user: node => {
 | 
			
		||||
					const mxid = db.prepare("SELECT mxid FROM sim WHERE discord_id = ?").pluck().get(node.id)
 | 
			
		||||
					if (mxid) {
 | 
			
		||||
						return "https://matrix.to/#/" + mxid
 | 
			
		||||
					} else {
 | 
			
		||||
						return "@" + node.id
 | 
			
		||||
					}
 | 
			
		||||
				},
 | 
			
		||||
				channel: node => {
 | 
			
		||||
					const roomID = db.prepare("SELECT room_id FROM channel_room WHERE channel_id = ?").pluck().get(node.id)
 | 
			
		||||
					if (roomID) {
 | 
			
		||||
						return "https://matrix.to/#/" + roomID
 | 
			
		||||
					} else {
 | 
			
		||||
						return "#" + node.id
 | 
			
		||||
					}
 | 
			
		||||
				},
 | 
			
		||||
				role: node =>
 | 
			
		||||
					"@&" + node.id,
 | 
			
		||||
				everyone: node =>
 | 
			
		||||
					"@room",
 | 
			
		||||
				here: node =>
 | 
			
		||||
					"@here"
 | 
			
		||||
			}
 | 
			
		||||
		}, null, null)
 | 
			
		||||
		const isPlaintext = body === html
 | 
			
		||||
		if (isPlaintext) {
 | 
			
		||||
			events.push({
 | 
			
		||||
				$type: "m.room.message",
 | 
			
		||||
				msgtype: "m.text",
 | 
			
		||||
				body: body
 | 
			
		||||
			})
 | 
			
		||||
		} else {
 | 
			
		||||
			events.push({
 | 
			
		||||
				$type: "m.room.message",
 | 
			
		||||
				msgtype: "m.text",
 | 
			
		||||
				body: body,
 | 
			
		||||
				format: "org.matrix.custom.html",
 | 
			
		||||
				formatted_body: html
 | 
			
		||||
			})
 | 
			
		||||
		}
 | 
			
		||||
	}, null, null)
 | 
			
		||||
	const isPlaintext = body === html
 | 
			
		||||
	if (isPlaintext) {
 | 
			
		||||
		events.push({
 | 
			
		||||
			$type: "m.room.message",
 | 
			
		||||
			msgtype: "m.text",
 | 
			
		||||
			body: body
 | 
			
		||||
		})
 | 
			
		||||
	} else {
 | 
			
		||||
		events.push({
 | 
			
		||||
			$type: "m.room.message",
 | 
			
		||||
			msgtype: "m.text",
 | 
			
		||||
			body: body,
 | 
			
		||||
			format: "org.matrix.custom.html",
 | 
			
		||||
			formatted_body: html
 | 
			
		||||
		})
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// Then attachments
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -3,6 +3,23 @@ const assert = require("assert")
 | 
			
		|||
const {messageToEvent} = require("./message-to-event")
 | 
			
		||||
const data = require("../../test/data")
 | 
			
		||||
 | 
			
		||||
test("message2event: attachment with no content", async t => {
 | 
			
		||||
   const events = await messageToEvent(data.message.attachment_no_content, data.guild.general)
 | 
			
		||||
   t.deepEqual(events, [{
 | 
			
		||||
      $type: "m.room.message",
 | 
			
		||||
      msgtype: "m.image",
 | 
			
		||||
      url: "mxc://cadence.moe/qXoZktDqNtEGuOCZEADAMvhM",
 | 
			
		||||
      body: "image.png",
 | 
			
		||||
      external_url: "https://cdn.discordapp.com/attachments/497161332244742154/1124628646431297546/image.png",
 | 
			
		||||
      info: {
 | 
			
		||||
         mimetype: "image/png",
 | 
			
		||||
         w: 466,
 | 
			
		||||
         h: 85,
 | 
			
		||||
         size: 12919,
 | 
			
		||||
      },
 | 
			
		||||
   }])
 | 
			
		||||
})
 | 
			
		||||
 | 
			
		||||
test("message2event: stickers", async t => {
 | 
			
		||||
   const events = await messageToEvent(data.message.sticker, data.guild.general)
 | 
			
		||||
   t.deepEqual(events, [{
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue