test that edits by webhook work
This commit is contained in:
		
							parent
							
								
									b9de84e586
								
							
						
					
					
						commit
						e3737997ec
					
				
					 5 changed files with 158 additions and 90 deletions
				
			
		| 
						 | 
					@ -22,7 +22,14 @@ async function editToChanges(message, guild, api) {
 | 
				
			||||||
	// Figure out what events we will be replacing
 | 
						// Figure out what events we will be replacing
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	const roomID = db.prepare("SELECT room_id FROM channel_room WHERE channel_id = ?").pluck().get(message.channel_id)
 | 
						const roomID = db.prepare("SELECT room_id FROM channel_room WHERE channel_id = ?").pluck().get(message.channel_id)
 | 
				
			||||||
	const senderMxid = await registerUser.ensureSimJoined(message.author, roomID)
 | 
						/** @type {string?} */
 | 
				
			||||||
 | 
						let senderMxid = db.prepare("SELECT mxid FROM sim WHERE discord_id = ?").pluck().get(message.author.id) ?? null
 | 
				
			||||||
 | 
						if (senderMxid) {
 | 
				
			||||||
 | 
							const senderIsInRoom = db.prepare("SELECT * FROM sim_member WHERE room_id = ? and mxid = ?").get(roomID, senderMxid)
 | 
				
			||||||
 | 
							if (!senderIsInRoom) {
 | 
				
			||||||
 | 
								senderMxid = null // just send as ooye bot
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
	/** @type {{event_id: string, event_type: string, event_subtype: string?, part: number}[]} */
 | 
						/** @type {{event_id: string, event_type: string, event_subtype: string?, part: number}[]} */
 | 
				
			||||||
   const oldEventRows = db.prepare("SELECT event_id, event_type, event_subtype, part FROM event_message WHERE message_id = ?").all(message.id)
 | 
					   const oldEventRows = db.prepare("SELECT event_id, event_type, event_subtype, part FROM event_message WHERE message_id = ?").all(message.id)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -3,104 +3,131 @@ const {editToChanges} = require("./edit-to-changes")
 | 
				
			||||||
const data = require("../../test/data")
 | 
					const data = require("../../test/data")
 | 
				
			||||||
const Ty = require("../../types")
 | 
					const Ty = require("../../types")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					test("edit2changes: edit by webhook", async t => {
 | 
				
			||||||
 | 
						const {senderMxid, eventsToRedact, eventsToReplace, eventsToSend} = await editToChanges(data.message_update.edit_by_webhook, data.guild.general, {})
 | 
				
			||||||
 | 
						t.deepEqual(eventsToRedact, [])
 | 
				
			||||||
 | 
						t.deepEqual(eventsToSend, [])
 | 
				
			||||||
 | 
						t.deepEqual(eventsToReplace, [{
 | 
				
			||||||
 | 
							oldID: "$zXSlyI78DQqQwwfPUSzZ1b-nXzbUrCDljJgnGDdoI10",
 | 
				
			||||||
 | 
							newContent: {
 | 
				
			||||||
 | 
								$type: "m.room.message",
 | 
				
			||||||
 | 
								msgtype: "m.text",
 | 
				
			||||||
 | 
								body: "* test 2",
 | 
				
			||||||
 | 
								"m.mentions": {},
 | 
				
			||||||
 | 
								"m.new_content": {
 | 
				
			||||||
 | 
									// *** Replaced With: ***
 | 
				
			||||||
 | 
									msgtype: "m.text",
 | 
				
			||||||
 | 
									body: "test 2",
 | 
				
			||||||
 | 
									"m.mentions": {}
 | 
				
			||||||
 | 
								},
 | 
				
			||||||
 | 
								"m.relates_to": {
 | 
				
			||||||
 | 
									rel_type: "m.replace",
 | 
				
			||||||
 | 
									event_id: "$zXSlyI78DQqQwwfPUSzZ1b-nXzbUrCDljJgnGDdoI10"
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}])
 | 
				
			||||||
 | 
						t.equal(senderMxid, null)
 | 
				
			||||||
 | 
					})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
test("edit2changes: bot response", async t => {
 | 
					test("edit2changes: bot response", async t => {
 | 
				
			||||||
   const {eventsToRedact, eventsToReplace, eventsToSend} = await editToChanges(data.message_update.bot_response, data.guild.general, {
 | 
						const {senderMxid, eventsToRedact, eventsToReplace, eventsToSend} = await editToChanges(data.message_update.bot_response, data.guild.general, {
 | 
				
			||||||
      async getJoinedMembers(roomID) {
 | 
							async getJoinedMembers(roomID) {
 | 
				
			||||||
         t.equal(roomID, "!uCtjHhfGlYbVnPVlkG:cadence.moe")
 | 
								t.equal(roomID, "!uCtjHhfGlYbVnPVlkG:cadence.moe")
 | 
				
			||||||
         return new Promise(resolve => {
 | 
								return new Promise(resolve => {
 | 
				
			||||||
            setTimeout(() => {
 | 
									setTimeout(() => {
 | 
				
			||||||
               resolve({
 | 
										resolve({
 | 
				
			||||||
                  joined: {
 | 
											joined: {
 | 
				
			||||||
                     "@cadence:cadence.moe": {
 | 
												"@cadence:cadence.moe": {
 | 
				
			||||||
                        display_name: "cadence [they]",
 | 
													display_name: "cadence [they]",
 | 
				
			||||||
                        avatar_url: "whatever"
 | 
													avatar_url: "whatever"
 | 
				
			||||||
                     },
 | 
												},
 | 
				
			||||||
                     "@_ooye_botrac4r:cadence.moe": {
 | 
												"@_ooye_botrac4r:cadence.moe": {
 | 
				
			||||||
                        display_name: "botrac4r",
 | 
													display_name: "botrac4r",
 | 
				
			||||||
                        avatar_url: "whatever"
 | 
													avatar_url: "whatever"
 | 
				
			||||||
                     }
 | 
												}
 | 
				
			||||||
                  }
 | 
											}
 | 
				
			||||||
               })
 | 
										})
 | 
				
			||||||
            })
 | 
									})
 | 
				
			||||||
         })
 | 
								})
 | 
				
			||||||
      }
 | 
							}
 | 
				
			||||||
   })
 | 
						})
 | 
				
			||||||
   t.deepEqual(eventsToRedact, [])
 | 
						t.deepEqual(eventsToRedact, [])
 | 
				
			||||||
   t.deepEqual(eventsToSend, [])
 | 
						t.deepEqual(eventsToSend, [])
 | 
				
			||||||
   t.deepEqual(eventsToReplace, [{
 | 
						t.deepEqual(eventsToReplace, [{
 | 
				
			||||||
      oldID: "$fdD9OZ55xg3EAsfvLZza5tMhtjUO91Wg3Otuo96TplY",
 | 
							oldID: "$fdD9OZ55xg3EAsfvLZza5tMhtjUO91Wg3Otuo96TplY",
 | 
				
			||||||
      newContent: {
 | 
							newContent: {
 | 
				
			||||||
         $type: "m.room.message",
 | 
								$type: "m.room.message",
 | 
				
			||||||
         msgtype: "m.text",
 | 
								msgtype: "m.text",
 | 
				
			||||||
         body: "* :ae_botrac4r: @cadence asked ````, I respond: Stop drinking paint. (No)\n\nHit :bn_re: to reroll.",
 | 
								body: "* :ae_botrac4r: @cadence asked ````, I respond: Stop drinking paint. (No)\n\nHit :bn_re: to reroll.",
 | 
				
			||||||
         format: "org.matrix.custom.html",
 | 
								format: "org.matrix.custom.html",
 | 
				
			||||||
         formatted_body: '* <img src="mxc://cadence.moe/551636841284108289" data-mx-emoticon alt=":ae_botrac4r:" title=":ae_botrac4r:" height="24"> @cadence asked <code></code>, I respond: Stop drinking paint. (No)<br><br>Hit <img src="mxc://cadence.moe/362741439211503616" data-mx-emoticon alt=":bn_re:" title=":bn_re:" height="24"> to reroll.',
 | 
								formatted_body: '* <img src="mxc://cadence.moe/551636841284108289" data-mx-emoticon alt=":ae_botrac4r:" title=":ae_botrac4r:" height="24"> @cadence asked <code></code>, I respond: Stop drinking paint. (No)<br><br>Hit <img src="mxc://cadence.moe/362741439211503616" data-mx-emoticon alt=":bn_re:" title=":bn_re:" height="24"> to reroll.',
 | 
				
			||||||
         "m.mentions": {
 | 
								"m.mentions": {
 | 
				
			||||||
            // Client-Server API spec 11.37.7: Copy Discord's behaviour by not re-notifying anyone that an *edit occurred*
 | 
									// Client-Server API spec 11.37.7: Copy Discord's behaviour by not re-notifying anyone that an *edit occurred*
 | 
				
			||||||
         },
 | 
								},
 | 
				
			||||||
         // *** Replaced With: ***
 | 
								// *** Replaced With: ***
 | 
				
			||||||
         "m.new_content": {
 | 
								"m.new_content": {
 | 
				
			||||||
            msgtype: "m.text",
 | 
									msgtype: "m.text",
 | 
				
			||||||
            body: ":ae_botrac4r: @cadence asked ````, I respond: Stop drinking paint. (No)\n\nHit :bn_re: to reroll.",
 | 
									body: ":ae_botrac4r: @cadence asked ````, I respond: Stop drinking paint. (No)\n\nHit :bn_re: to reroll.",
 | 
				
			||||||
            format: "org.matrix.custom.html",
 | 
									format: "org.matrix.custom.html",
 | 
				
			||||||
            formatted_body: '<img src="mxc://cadence.moe/551636841284108289" data-mx-emoticon alt=":ae_botrac4r:" title=":ae_botrac4r:" height="24"> @cadence asked <code></code>, I respond: Stop drinking paint. (No)<br><br>Hit <img src="mxc://cadence.moe/362741439211503616" data-mx-emoticon alt=":bn_re:" title=":bn_re:" height="24"> to reroll.',
 | 
									formatted_body: '<img src="mxc://cadence.moe/551636841284108289" data-mx-emoticon alt=":ae_botrac4r:" title=":ae_botrac4r:" height="24"> @cadence asked <code></code>, I respond: Stop drinking paint. (No)<br><br>Hit <img src="mxc://cadence.moe/362741439211503616" data-mx-emoticon alt=":bn_re:" title=":bn_re:" height="24"> to reroll.',
 | 
				
			||||||
            "m.mentions": {
 | 
									"m.mentions": {
 | 
				
			||||||
               // Client-Server API spec 11.37.7: This should contain the mentions for the final version of the event
 | 
										// Client-Server API spec 11.37.7: This should contain the mentions for the final version of the event
 | 
				
			||||||
               "user_ids": ["@cadence:cadence.moe"]
 | 
										"user_ids": ["@cadence:cadence.moe"]
 | 
				
			||||||
            }
 | 
									}
 | 
				
			||||||
         },
 | 
								},
 | 
				
			||||||
         "m.relates_to": {
 | 
								"m.relates_to": {
 | 
				
			||||||
            rel_type: "m.replace",
 | 
									rel_type: "m.replace",
 | 
				
			||||||
            event_id: "$fdD9OZ55xg3EAsfvLZza5tMhtjUO91Wg3Otuo96TplY"
 | 
									event_id: "$fdD9OZ55xg3EAsfvLZza5tMhtjUO91Wg3Otuo96TplY"
 | 
				
			||||||
         }
 | 
								}
 | 
				
			||||||
      }
 | 
							}
 | 
				
			||||||
   }])
 | 
						}])
 | 
				
			||||||
 | 
						t.equal(senderMxid, "@_ooye_bojack_horseman:cadence.moe")
 | 
				
			||||||
})
 | 
					})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
test("edit2changes: remove caption from image", async t => {
 | 
					test("edit2changes: remove caption from image", async t => {
 | 
				
			||||||
   const {eventsToRedact, eventsToReplace, eventsToSend} = await editToChanges(data.message_update.removed_caption_from_image, data.guild.general, {})
 | 
						const {eventsToRedact, eventsToReplace, eventsToSend} = await editToChanges(data.message_update.removed_caption_from_image, data.guild.general, {})
 | 
				
			||||||
   t.deepEqual(eventsToRedact, ["$mtR8cJqM4fKno1bVsm8F4wUVqSntt2sq6jav1lyavuA"])
 | 
						t.deepEqual(eventsToRedact, ["$mtR8cJqM4fKno1bVsm8F4wUVqSntt2sq6jav1lyavuA"])
 | 
				
			||||||
   t.deepEqual(eventsToSend, [])
 | 
						t.deepEqual(eventsToSend, [])
 | 
				
			||||||
   t.deepEqual(eventsToReplace, [])
 | 
						t.deepEqual(eventsToReplace, [])
 | 
				
			||||||
})
 | 
					})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
test("edit2changes: add caption back to that image", async t => {
 | 
					test("edit2changes: add caption back to that image", async t => {
 | 
				
			||||||
   const {eventsToRedact, eventsToReplace, eventsToSend} = await editToChanges(data.message_update.added_caption_to_image, data.guild.general, {})
 | 
						const {eventsToRedact, eventsToReplace, eventsToSend} = await editToChanges(data.message_update.added_caption_to_image, data.guild.general, {})
 | 
				
			||||||
   t.deepEqual(eventsToRedact, [])
 | 
						t.deepEqual(eventsToRedact, [])
 | 
				
			||||||
   t.deepEqual(eventsToSend, [{
 | 
						t.deepEqual(eventsToSend, [{
 | 
				
			||||||
      $type: "m.room.message",
 | 
							$type: "m.room.message",
 | 
				
			||||||
      msgtype: "m.text",
 | 
							msgtype: "m.text",
 | 
				
			||||||
      body: "some text",
 | 
							body: "some text",
 | 
				
			||||||
      "m.mentions": {}
 | 
							"m.mentions": {}
 | 
				
			||||||
   }])
 | 
						}])
 | 
				
			||||||
   t.deepEqual(eventsToReplace, [])
 | 
						t.deepEqual(eventsToReplace, [])
 | 
				
			||||||
})
 | 
					})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
test("edit2changes: edit of reply to skull webp attachment with content", async t => {
 | 
					test("edit2changes: edit of reply to skull webp attachment with content", async t => {
 | 
				
			||||||
   const {eventsToRedact, eventsToReplace, eventsToSend} = await editToChanges(data.message_update.edit_of_reply_to_skull_webp_attachment_with_content, data.guild.general, {})
 | 
						const {eventsToRedact, eventsToReplace, eventsToSend} = await editToChanges(data.message_update.edit_of_reply_to_skull_webp_attachment_with_content, data.guild.general, {})
 | 
				
			||||||
	t.deepEqual(eventsToRedact, [])
 | 
						t.deepEqual(eventsToRedact, [])
 | 
				
			||||||
   t.deepEqual(eventsToSend, [])
 | 
						t.deepEqual(eventsToSend, [])
 | 
				
			||||||
   t.deepEqual(eventsToReplace, [{
 | 
						t.deepEqual(eventsToReplace, [{
 | 
				
			||||||
      oldID: "$vgTKOR5ZTYNMKaS7XvgEIDaOWZtVCEyzLLi5Pc5Gz4M",
 | 
							oldID: "$vgTKOR5ZTYNMKaS7XvgEIDaOWZtVCEyzLLi5Pc5Gz4M",
 | 
				
			||||||
      newContent: {
 | 
							newContent: {
 | 
				
			||||||
         $type: "m.room.message",
 | 
								$type: "m.room.message",
 | 
				
			||||||
         msgtype: "m.text",
 | 
								msgtype: "m.text",
 | 
				
			||||||
         body: "> Extremity: Image\n\n* Edit",
 | 
								body: "> Extremity: Image\n\n* Edit",
 | 
				
			||||||
         format: "org.matrix.custom.html",
 | 
								format: "org.matrix.custom.html",
 | 
				
			||||||
         formatted_body:
 | 
								formatted_body:
 | 
				
			||||||
            '<mx-reply><blockquote><a href="https://matrix.to/#/!kLRqKKUQXcibIMtOpl:cadence.moe/$oLyUTyZ_7e_SUzGNWZKz880ll9amLZvXGbArJCKai2Q">In reply to</a> Extremity'
 | 
									'<mx-reply><blockquote><a href="https://matrix.to/#/!kLRqKKUQXcibIMtOpl:cadence.moe/$oLyUTyZ_7e_SUzGNWZKz880ll9amLZvXGbArJCKai2Q">In reply to</a> Extremity'
 | 
				
			||||||
            + '<br>Image</blockquote></mx-reply>'
 | 
									+ '<br>Image</blockquote></mx-reply>'
 | 
				
			||||||
            + '* Edit',
 | 
									+ '* Edit',
 | 
				
			||||||
         "m.mentions": {},
 | 
								"m.mentions": {},
 | 
				
			||||||
         "m.new_content": {
 | 
								"m.new_content": {
 | 
				
			||||||
            msgtype: "m.text",
 | 
									msgtype: "m.text",
 | 
				
			||||||
            body: "Edit",
 | 
									body: "Edit",
 | 
				
			||||||
            "m.mentions": {}
 | 
									"m.mentions": {}
 | 
				
			||||||
         },
 | 
								},
 | 
				
			||||||
         "m.relates_to": {
 | 
								"m.relates_to": {
 | 
				
			||||||
            rel_type: "m.replace",
 | 
									rel_type: "m.replace",
 | 
				
			||||||
            event_id: "$vgTKOR5ZTYNMKaS7XvgEIDaOWZtVCEyzLLi5Pc5Gz4M"
 | 
									event_id: "$vgTKOR5ZTYNMKaS7XvgEIDaOWZtVCEyzLLi5Pc5Gz4M"
 | 
				
			||||||
         }
 | 
								}
 | 
				
			||||||
      }
 | 
							}
 | 
				
			||||||
   }])
 | 
						}])
 | 
				
			||||||
})
 | 
					})
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -37,6 +37,13 @@ module.exports = {
 | 
				
			||||||
	 * @param {import("discord-api-types/v10").GatewayMessageUpdateDispatchData} message
 | 
						 * @param {import("discord-api-types/v10").GatewayMessageUpdateDispatchData} message
 | 
				
			||||||
	 */
 | 
						 */
 | 
				
			||||||
	onMessageUpdate(client, data) {
 | 
						onMessageUpdate(client, data) {
 | 
				
			||||||
 | 
							if (data.webhook_id) {
 | 
				
			||||||
 | 
								const row = db.prepare("SELECT webhook_id FROM webhook WHERE webhook_id = ?").pluck().get(message.webhook_id)
 | 
				
			||||||
 | 
								if (row) {
 | 
				
			||||||
 | 
									// The update was sent by the bridge's own webhook on discord. We don't want to reflect this back, so just drop it.
 | 
				
			||||||
 | 
									return
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
		// Based on looking at data they've sent me over the gateway, this is the best way to check for meaningful changes.
 | 
							// Based on looking at data they've sent me over the gateway, this is the best way to check for meaningful changes.
 | 
				
			||||||
		// If the message content is a string then it includes all interesting fields and is meaningful.
 | 
							// If the message content is a string then it includes all interesting fields and is meaningful.
 | 
				
			||||||
		if (typeof data.content === "string") {
 | 
							if (typeof data.content === "string") {
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										27
									
								
								test/data.js
									
										
									
									
									
								
							
							
						
						
									
										27
									
								
								test/data.js
									
										
									
									
									
								
							| 
						 | 
					@ -788,6 +788,33 @@ module.exports = {
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	},
 | 
						},
 | 
				
			||||||
	message_update: {
 | 
						message_update: {
 | 
				
			||||||
 | 
							edit_by_webhook: {
 | 
				
			||||||
 | 
								application_id: "684280192553844747",
 | 
				
			||||||
 | 
								attachments: [],
 | 
				
			||||||
 | 
								author: {
 | 
				
			||||||
 | 
									avatar: null,
 | 
				
			||||||
 | 
									bot: true,
 | 
				
			||||||
 | 
									discriminator: "0000",
 | 
				
			||||||
 | 
									id: "700285844094845050",
 | 
				
			||||||
 | 
									username: "cadence [they]"
 | 
				
			||||||
 | 
								},
 | 
				
			||||||
 | 
								channel_id: "497161350934560778",
 | 
				
			||||||
 | 
								components: [],
 | 
				
			||||||
 | 
								content: "test 2",
 | 
				
			||||||
 | 
								edited_timestamp: "2023-08-17T06:29:34.167314+00:00",
 | 
				
			||||||
 | 
								embeds: [],
 | 
				
			||||||
 | 
								flags: 0,
 | 
				
			||||||
 | 
								guild_id: "497159726455455754",
 | 
				
			||||||
 | 
								id: "1141619794500649020",
 | 
				
			||||||
 | 
								mention_everyone: false,
 | 
				
			||||||
 | 
								mention_roles: [],
 | 
				
			||||||
 | 
								mentions: [],
 | 
				
			||||||
 | 
								pinned: false,
 | 
				
			||||||
 | 
								timestamp: "2023-08-17T06:29:29.279000+00:00",
 | 
				
			||||||
 | 
								tts: false,
 | 
				
			||||||
 | 
								type: 0,
 | 
				
			||||||
 | 
								webhook_id: "700285844094845050"
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
		bot_response: {
 | 
							bot_response: {
 | 
				
			||||||
			attachments: [],
 | 
								attachments: [],
 | 
				
			||||||
			author: {
 | 
								author: {
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue