catch up on missed d->m messages when logging in
This commit is contained in:
		
							parent
							
								
									0f20dcab6d
								
							
						
					
					
						commit
						3436759504
					
				
					 16 changed files with 268 additions and 16 deletions
				
			
		
							
								
								
									
										40
									
								
								d2m/converters/message-to-event.embeds.test.js
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										40
									
								
								d2m/converters/message-to-event.embeds.test.js
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,40 @@
 | 
			
		|||
const {test} = require("supertape")
 | 
			
		||||
const {messageToEvent} = require("./message-to-event")
 | 
			
		||||
const data = require("../../test/data")
 | 
			
		||||
const Ty = require("../../types")
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @param {string} roomID
 | 
			
		||||
 * @param {string} eventID
 | 
			
		||||
 * @returns {(roomID: string, eventID: string) => Promise<Ty.Event.Outer<Ty.Event.M_Room_Message>>}
 | 
			
		||||
 */
 | 
			
		||||
function mockGetEvent(t, roomID_in, eventID_in, outer) {
 | 
			
		||||
	return async function(roomID, eventID) {
 | 
			
		||||
		t.equal(roomID, roomID_in)
 | 
			
		||||
		t.equal(eventID, eventID_in)
 | 
			
		||||
		return new Promise(resolve => {
 | 
			
		||||
			setTimeout(() => {
 | 
			
		||||
				resolve({
 | 
			
		||||
					event_id: eventID_in,
 | 
			
		||||
					room_id: roomID_in,
 | 
			
		||||
					origin_server_ts: 1680000000000,
 | 
			
		||||
					unsigned: {
 | 
			
		||||
						age: 2245,
 | 
			
		||||
						transaction_id: "$local.whatever"
 | 
			
		||||
					},
 | 
			
		||||
					...outer
 | 
			
		||||
				})
 | 
			
		||||
			})
 | 
			
		||||
		})
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
test("message2event embeds: nothing but a field", async t => {
 | 
			
		||||
	const events = await messageToEvent(data.message_with_embeds.nothing_but_a_field, data.guild.general, {})
 | 
			
		||||
	t.deepEqual(events, [{
 | 
			
		||||
		$type: "m.room.message",
 | 
			
		||||
		"m.mentions": {},
 | 
			
		||||
		msgtype: "m.text",
 | 
			
		||||
		body: "Amanda"
 | 
			
		||||
	}])
 | 
			
		||||
})
 | 
			
		||||
| 
						 | 
				
			
			@ -108,6 +108,13 @@ async function messageToEvent(message, guild, options = {}, di) {
 | 
			
		|||
		addMention(repliedToEventSenderMxid)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	let msgtype = "m.text"
 | 
			
		||||
	// Handle message type 4, channel name changed
 | 
			
		||||
	if (message.type === DiscordTypes.MessageType.ChannelNameChange) {
 | 
			
		||||
		msgtype = "m.emote"
 | 
			
		||||
		message.content = "changed the channel name to **" + message.content + "**"
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// Text content appears first
 | 
			
		||||
	if (message.content) {
 | 
			
		||||
		let content = message.content
 | 
			
		||||
| 
						 | 
				
			
			@ -188,7 +195,7 @@ async function messageToEvent(message, guild, options = {}, di) {
 | 
			
		|||
		const newTextMessageEvent = {
 | 
			
		||||
			$type: "m.room.message",
 | 
			
		||||
			"m.mentions": mentions,
 | 
			
		||||
			msgtype: "m.text",
 | 
			
		||||
			msgtype,
 | 
			
		||||
			body: body
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -239,6 +246,22 @@ async function messageToEvent(message, guild, options = {}, di) {
 | 
			
		|||
					size: attachment.size
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
		} else if (attachment.content_type?.startsWith("video/") && attachment.width && attachment.height) {
 | 
			
		||||
			return {
 | 
			
		||||
				$type: "m.room.message",
 | 
			
		||||
				"m.mentions": mentions,
 | 
			
		||||
				msgtype: "m.video",
 | 
			
		||||
				url: await file.uploadDiscordFileToMxc(attachment.url),
 | 
			
		||||
				external_url: attachment.url,
 | 
			
		||||
				body: attachment.description || attachment.filename,
 | 
			
		||||
				filename: attachment.filename,
 | 
			
		||||
				info: {
 | 
			
		||||
					mimetype: attachment.content_type,
 | 
			
		||||
					w: attachment.width,
 | 
			
		||||
					h: attachment.height,
 | 
			
		||||
					size: attachment.size
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
		} else {
 | 
			
		||||
			return {
 | 
			
		||||
				$type: "m.room.message",
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -330,4 +330,14 @@ test("message2event: very large attachment is linked instead of being uploaded",
 | 
			
		|||
	}])
 | 
			
		||||
})
 | 
			
		||||
 | 
			
		||||
// TODO: read "edits of replies" in the spec
 | 
			
		||||
test("message2event: type 4 channel name change", async t => {
 | 
			
		||||
	const events = await messageToEvent(data.special_message.thread_name_change, data.guild.general)
 | 
			
		||||
	t.deepEqual(events, [{
 | 
			
		||||
		$type: "m.room.message",
 | 
			
		||||
		"m.mentions": {},
 | 
			
		||||
		msgtype: "m.emote",
 | 
			
		||||
		body: "changed the channel name to **worming**",
 | 
			
		||||
		format: "org.matrix.custom.html",
 | 
			
		||||
		formatted_body: "changed the channel name to <strong>worming</strong>"
 | 
			
		||||
	}])
 | 
			
		||||
})
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -16,6 +16,10 @@ test("user2name: works on emojis", t => {
 | 
			
		|||
   t.equal(userToSimName({username: "🍪 Cookie Monster 🍪", discriminator: "0001"}), "cookie_monster")
 | 
			
		||||
})
 | 
			
		||||
 | 
			
		||||
test("user2name: works on single emoji at the end", t => {
 | 
			
		||||
   t.equal(userToSimName({username: "Amanda 🎵", discriminator: "2192"}), "amanda")
 | 
			
		||||
})
 | 
			
		||||
 | 
			
		||||
test("user2name: works on crazy name", t => {
 | 
			
		||||
   t.equal(userToSimName({username: "*** D3 &W (89) _7//-", discriminator: "0001"}), "d3_w_89__7//")
 | 
			
		||||
})
 | 
			
		||||
| 
						 | 
				
			
			@ -34,4 +38,4 @@ test("user2name: uses ID if name becomes too short", t => {
 | 
			
		|||
 | 
			
		||||
test("user2name: uses ID when name has only disallowed characters", t => {
 | 
			
		||||
   t.equal(userToSimName({username: "!@#$%^&*", discriminator: "0001", id: "9"}), "9")
 | 
			
		||||
})
 | 
			
		||||
})
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue