const {test} = require("supertape") const {eventToMessage} = require("./event-to-message") const data = require("../../test/data") const {MatrixServerError} = require("../../matrix/mreq") const {db} = require("../../passthrough") /** * @param {string} roomID * @param {string} eventID * @returns {(roomID: string, eventID: string) => Promise>} */ 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 }) }) }) } } function sameFirstContentAndWhitespace(t, a, b) { const a2 = JSON.stringify(a.messagesToSend[0].content) const b2 = JSON.stringify(b.messagesToSend[0].content) t.equal(a2, b2) } test("event2message: body is used when there is no formatted_body", async t => { t.deepEqual( await eventToMessage({ content: { body: "testing plaintext", msgtype: "m.text" }, event_id: "$g07oYSZFWBkxohNEfywldwgcWj1hbhDzQ1sBAKvqOOU", origin_server_ts: 1688301929913, room_id: "!kLRqKKUQXcibIMtOpl:cadence.moe", sender: "@cadence:cadence.moe", type: "m.room.message", unsigned: { age: 405299 } }), { messagesToDelete: [], messagesToEdit: [], messagesToSend: [{ username: "cadence [they]", content: "testing plaintext", avatar_url: undefined }] } ) }) test("event2message: any markdown in body is escaped, except strikethrough", async t => { t.deepEqual( await eventToMessage({ content: { body: "testing **special** ~~things~~ which _should_ *not* `trigger` @any , except strikethrough", msgtype: "m.text" }, event_id: "$g07oYSZFWBkxohNEfywldwgcWj1hbhDzQ1sBAKvqOOU", origin_server_ts: 1688301929913, room_id: "!kLRqKKUQXcibIMtOpl:cadence.moe", sender: "@cadence:cadence.moe", type: "m.room.message", unsigned: { age: 405299 } }), { messagesToDelete: [], messagesToEdit: [], messagesToSend: [{ username: "cadence [they]", content: "testing \\*\\*special\\*\\* ~~things~~ which \\_should\\_ \\*not\\* \\`trigger\\` @any , except strikethrough", avatar_url: undefined }] } ) }) test("event2message: links in formatted body are not broken", async t => { t.deepEqual( await eventToMessage({ type: "m.room.message", sender: "@cadence:cadence.moe", content: { msgtype: "m.text", body: "kyuugryphon I wonder what the midjourney text description of this photo is https://upload.wikimedia.org/wikipedia/commons/f/f3/After_gay_pride%2C_rainbow_flags_flying_along_Beach_Street_%2814853144744%29.jpg", format: "org.matrix.custom.html", formatted_body: "kyuugryphon I wonder what the midjourney text description of this photo is https://upload.wikimedia.org/wikipedia/commons/f/f3/After_gay_pride%2C_rainbow_flags_flying_along_Beach_Street_%2814853144744%29.jpg" }, origin_server_ts: 1693739630700, unsigned: { age: 39, transaction_id: "m1693739630587.160" }, event_id: "$zANQGOdnHKZj48lrajojsejH86KNYST26imgb2Sw1Jg", room_id: "!kLRqKKUQXcibIMtOpl:cadence.moe" }), { messagesToDelete: [], messagesToEdit: [], messagesToSend: [{ username: "cadence [they]", content: "<@111604486476181504> I wonder what the midjourney text description of this photo is https://upload.wikimedia.org/wikipedia/commons/f/f3/After_gay_pride%2C_rainbow_flags_flying_along_Beach_Street_%2814853144744%29.jpg", avatar_url: undefined }] } ) }) test("event2message: links in plaintext body are not broken", async t => { t.deepEqual( await eventToMessage({ type: "m.room.message", sender: "@cadence:cadence.moe", content: { msgtype: "m.text", body: "I wonder what the midjourney text description of this photo is https://upload.wikimedia.org/wikipedia/commons/f/f3/After_gay_pride%2C_rainbow_flags_flying_along_Beach_Street_%2814853144744%29.jpg", }, origin_server_ts: 1693739630700, unsigned: { age: 39, transaction_id: "m1693739630587.160" }, event_id: "$zANQGOdnHKZj48lrajojsejH86KNYST26imgb2Sw1Jg", room_id: "!kLRqKKUQXcibIMtOpl:cadence.moe" }), { messagesToDelete: [], messagesToEdit: [], messagesToSend: [{ username: "cadence [they]", content: "I wonder what the midjourney text description of this photo is https://upload.wikimedia.org/wikipedia/commons/f/f3/After_gay_pride%2C_rainbow_flags_flying_along_Beach_Street_%2814853144744%29.jpg", avatar_url: undefined }] } ) }) test("event2message: basic html is converted to markdown", async t => { t.deepEqual( await eventToMessage({ content: { msgtype: "m.text", body: "wrong body", format: "org.matrix.custom.html", formatted_body: "this is a test of formatting" }, event_id: "$g07oYSZFWBkxohNEfywldwgcWj1hbhDzQ1sBAKvqOOU", origin_server_ts: 1688301929913, room_id: "!kLRqKKUQXcibIMtOpl:cadence.moe", sender: "@cadence:cadence.moe", type: "m.room.message", unsigned: { age: 405299 } }), { messagesToDelete: [], messagesToEdit: [], messagesToSend: [{ username: "cadence [they]", content: "this **is** a _**test** __of___ ~~_formatting_~~", avatar_url: undefined }] } ) }) test("event2message: spoilers work", async t => { t.deepEqual( await eventToMessage({ content: { msgtype: "m.text", body: "wrong body", format: "org.matrix.custom.html", formatted_body: `this is a test of spoilers` }, event_id: "$g07oYSZFWBkxohNEfywldwgcWj1hbhDzQ1sBAKvqOOU", origin_server_ts: 1688301929913, room_id: "!kLRqKKUQXcibIMtOpl:cadence.moe", sender: "@cadence:cadence.moe", type: "m.room.message", unsigned: { age: 405299 } }), { messagesToDelete: [], messagesToEdit: [], messagesToSend: [{ username: "cadence [they]", content: "this **is** a ||_test_|| of ||spoilers||", avatar_url: undefined }] } ) }) test("event2message: markdown syntax is escaped", async t => { t.deepEqual( await eventToMessage({ content: { msgtype: "m.text", body: "wrong body", format: "org.matrix.custom.html", formatted_body: "this **is** an extreme \\*test\\* of" }, event_id: "$g07oYSZFWBkxohNEfywldwgcWj1hbhDzQ1sBAKvqOOU", origin_server_ts: 1688301929913, room_id: "!kLRqKKUQXcibIMtOpl:cadence.moe", sender: "@cadence:cadence.moe", type: "m.room.message", unsigned: { age: 405299 } }), { messagesToDelete: [], messagesToEdit: [], messagesToSend: [{ username: "cadence [they]", content: "this \\*\\*is\\*\\* an **_extreme_** \\\\\\*test\\\\\\* of", avatar_url: undefined }] } ) }) test("event2message: html lines are bridged correctly", async t => { t.deepEqual( await eventToMessage({ content: { msgtype: "m.text", body: "wrong body", format: "org.matrix.custom.html", formatted_body: "

paragraph one
line two
line three

paragraph two\nline two\nline three\n\nparagraph three

paragraph four\nline two
line three\nline four

paragraph five" }, event_id: "$g07oYSZFWBkxohNEfywldwgcWj1hbhDzQ1sBAKvqOOU", origin_server_ts: 1688301929913, room_id: "!kLRqKKUQXcibIMtOpl:cadence.moe", sender: "@cadence:cadence.moe", type: "m.room.message", unsigned: { age: 405299 } }), { messagesToDelete: [], messagesToEdit: [], messagesToSend: [{ username: "cadence [they]", content: "paragraph one\nline _two_\nline three\n\nparagraph two\nline _two_\nline three\n\nparagraph three\n\nparagraph four\nline two\nline three\nline four\n\nparagraph five", avatar_url: undefined }] } ) }) /*test("event2message: whitespace is retained", async t => { t.deepEqual( await eventToMessage({ content: { msgtype: "m.text", body: "wrong body", format: "org.matrix.custom.html", formatted_body: "line one: test test
line two: test test
line three: test test
line four: test test
line five" }, event_id: "$g07oYSZFWBkxohNEfywldwgcWj1hbhDzQ1sBAKvqOOU", origin_server_ts: 1688301929913, room_id: "!kLRqKKUQXcibIMtOpl:cadence.moe", sender: "@cadence:cadence.moe", type: "m.room.message", unsigned: { age: 405299 } }), { messagesToDelete: [], messagesToEdit: [], messagesToSend: [{ username: "cadence [they]", content: "line one: test test\nline two: **test** **test**\nline three: **test test**\nline four: test test\n line five", avatar_url: undefined }] } ) })*/ test("event2message: whitespace is collapsed", async t => { sameFirstContentAndWhitespace( t, await eventToMessage({ content: { msgtype: "m.text", body: "wrong body", format: "org.matrix.custom.html", formatted_body: "line one: test test
line two: test test
line three: test test
line four: test test
line five" }, event_id: "$g07oYSZFWBkxohNEfywldwgcWj1hbhDzQ1sBAKvqOOU", origin_server_ts: 1688301929913, room_id: "!kLRqKKUQXcibIMtOpl:cadence.moe", sender: "@cadence:cadence.moe", type: "m.room.message", unsigned: { age: 405299 } }), { messagesToDelete: [], messagesToEdit: [], messagesToSend: [{ username: "cadence [they]", content: "line one: test test\nline two: **test** **test**\nline three: **test test**\nline four: test test\nline five", avatar_url: undefined }] } ) }) test("event2message: lists are bridged correctly", async t => { sameFirstContentAndWhitespace( t, await eventToMessage({ "type": "m.room.message", "sender": "@cadence:cadence.moe", "content": { "msgtype": "m.text", "body": "* line one\n* line two\n* line three\n * nested one\n * nested two\n* line four", "format": "org.matrix.custom.html", "formatted_body": "
    \n
  • line one
  • \n
  • line two
  • \n
  • line three\n
      \n
    • nested one
    • \n
    • nested two
    • \n
    \n
  • \n
  • line four
  • \n
\n" }, "origin_server_ts": 1692967314062, "unsigned": { "age": 112, "transaction_id": "m1692967313951.441" }, "event_id": "$l-xQPY5vNJo3SNxU9d8aOWNVD1glMslMyrp4M_JEF70", "room_id": "!BpMdOUkWWhFxmTrENV:cadence.moe" }), { messagesToDelete: [], messagesToEdit: [], messagesToSend: [{ username: "cadence [they]", content: "* line one\n* line two\n* line three\n * nested one\n * nested two\n* line four", avatar_url: undefined }] } ) }) test("event2message: long messages are split", async t => { t.deepEqual( await eventToMessage({ content: { body: ("a".repeat(130) + " ").repeat(19), msgtype: "m.text" }, event_id: "$g07oYSZFWBkxohNEfywldwgcWj1hbhDzQ1sBAKvqOOU", origin_server_ts: 1688301929913, room_id: "!kLRqKKUQXcibIMtOpl:cadence.moe", sender: "@cadence:cadence.moe", type: "m.room.message", unsigned: { age: 405299 } }), { messagesToDelete: [], messagesToEdit: [], messagesToSend: [{ username: "cadence [they]", content: (("a".repeat(130) + " ").repeat(15)).slice(0, -1), avatar_url: undefined }, { username: "cadence [they]", content: (("a".repeat(130) + " ").repeat(4)).slice(0, -1), avatar_url: undefined }] } ) }) test("event2message: code blocks work", async t => { t.deepEqual( await eventToMessage({ content: { msgtype: "m.text", body: "wrong body", format: "org.matrix.custom.html", formatted_body: "

preceding

\n
code block\n
\n

following code is inline

\n" }, event_id: "$g07oYSZFWBkxohNEfywldwgcWj1hbhDzQ1sBAKvqOOU", origin_server_ts: 1688301929913, room_id: "!kLRqKKUQXcibIMtOpl:cadence.moe", sender: "@cadence:cadence.moe", type: "m.room.message", unsigned: { age: 405299 } }), { messagesToDelete: [], messagesToEdit: [], messagesToSend: [{ username: "cadence [they]", content: "preceding\n\n```\ncode block\n```\n\nfollowing `code` is inline", avatar_url: undefined }] } ) }) test("event2message: code block contents are formatted correctly and not escaped", async t => { t.deepEqual( await eventToMessage({ "type": "m.room.message", "sender": "@cadence:cadence.moe", "content": { "msgtype": "m.text", "body": "wrong body", "format": "org.matrix.custom.html", "formatted_body": "
input = input.replace(/(<\\/?([^ >]+)[^>]*>)?\\n(<\\/?([^ >]+)[^>]*>)?/g,\n_input_ = input = input.replace(/(<\\/?([^ >]+)[^>]*>)?\\n(<\\/?([^ >]+)[^>]*>)?/g,\n
\n

input = input.replace(/(<\\/?([^ >]+)[^>]*>)?\\n(<\\/?([^ >]+)[^>]*>)?/g,

\n" }, "origin_server_ts": 1693031482275, "unsigned": { "age": 99, "transaction_id": "m1693031482146.511" }, "event_id": "$pGkWQuGVmrPNByrFELxhzI6MCBgJecr5I2J3z88Gc2s", "room_id": "!BpMdOUkWWhFxmTrENV:cadence.moe" }), { messagesToDelete: [], messagesToEdit: [], messagesToSend: [{ username: "cadence [they]", content: "```\ninput = input.replace(/(<\\/?([^ >]+)[^>]*>)?\\n(<\\/?([^ >]+)[^>]*>)?/g,\n_input_ = input = input.replace(/(<\\/?([^ >]+)[^>]*>)?\\n(<\\/?([^ >]+)[^>]*>)?/g,\n```\n\n`input = input.replace(/(<\\/?([^ >]+)[^>]*>)?\\n(<\\/?([^ >]+)[^>]*>)?/g,`", avatar_url: undefined }] } ) }) test("event2message: quotes have an appropriate amount of whitespace", async t => { t.deepEqual( await eventToMessage({ content: { msgtype: "m.text", body: "wrong body", format: "org.matrix.custom.html", formatted_body: "
Chancellor of Germany Angela Merkel, on March 17, 2017: they did not shake hands



🤨" }, event_id: "$g07oYSZFWBkxohNEfywldwgcWj1hbhDzQ1sBAKvqOOU", origin_server_ts: 1688301929913, room_id: "!kLRqKKUQXcibIMtOpl:cadence.moe", sender: "@cadence:cadence.moe", type: "m.room.message", unsigned: { age: 405299 } }), { messagesToDelete: [], messagesToEdit: [], messagesToSend: [{ username: "cadence [they]", content: "> Chancellor of Germany Angela Merkel, on March 17, 2017: they did not shake hands\n🤨", avatar_url: undefined }] } ) }) test("event2message: m.emote plaintext works", async t => { t.deepEqual( await eventToMessage({ content: { msgtype: "m.emote", body: "tests an m.emote message" }, event_id: "$g07oYSZFWBkxohNEfywldwgcWj1hbhDzQ1sBAKvqOOU", origin_server_ts: 1688301929913, room_id: "!kLRqKKUQXcibIMtOpl:cadence.moe", sender: "@cadence:cadence.moe", type: "m.room.message", unsigned: { age: 405299 } }), { messagesToDelete: [], messagesToEdit: [], messagesToSend: [{ username: "cadence [they]", content: "\\* cadence \\[they\\] tests an m.emote message", avatar_url: undefined }] } ) }) test("event2message: m.emote markdown syntax is escaped", async t => { t.deepEqual( await eventToMessage({ content: { msgtype: "m.emote", body: "wrong body", format: "org.matrix.custom.html", formatted_body: "shows you **her** extreme \\*test\\* of" }, event_id: "$g07oYSZFWBkxohNEfywldwgcWj1hbhDzQ1sBAKvqOOU", origin_server_ts: 1688301929913, room_id: "!kLRqKKUQXcibIMtOpl:cadence.moe", sender: "@cadence:cadence.moe", type: "m.room.message", unsigned: { age: 405299 } }), { messagesToDelete: [], messagesToEdit: [], messagesToSend: [{ username: "cadence [they]", content: "\\* cadence \\[they\\] shows you \\*\\*her\\*\\* **_extreme_** \\\\\\*test\\\\\\* of", avatar_url: undefined }] } ) }) test("event2message: rich reply to a sim user", async t => { t.deepEqual( await eventToMessage({ "type": "m.room.message", "sender": "@cadence:cadence.moe", "content": { "msgtype": "m.text", "body": "> <@_ooye_kyuugryphon:cadence.moe> Slow news day.\n\nTesting this reply, ignore", "format": "org.matrix.custom.html", "formatted_body": "
In reply to @_ooye_kyuugryphon:cadence.moe
Slow news day.
Testing this reply, ignore", "m.relates_to": { "m.in_reply_to": { "event_id": "$Fxy8SMoJuTduwReVkHZ1uHif9EuvNx36Hg79cltiA04" } } }, "origin_server_ts": 1693029683016, "unsigned": { "age": 91, "transaction_id": "m1693029682894.510" }, "event_id": "$v_Gtr-bzv9IVlSLBO5DstzwmiDd-GSFaNfHX66IupV8", "room_id": "!fGgIymcYWOqjbSRUdV:cadence.moe" }, data.guild.general, { api: { getEvent: mockGetEvent(t, "!fGgIymcYWOqjbSRUdV:cadence.moe", "$Fxy8SMoJuTduwReVkHZ1uHif9EuvNx36Hg79cltiA04", { type: "m.room.message", content: { msgtype: "m.text", body: "Slow news day." }, sender: "@_ooye_kyuugryphon:cadence.moe" }) } }), { messagesToDelete: [], messagesToEdit: [], messagesToSend: [{ username: "cadence [they]", content: "> <:L1:1144820033948762203><:L2:1144820084079087647>https://discord.com/channels/112760669178241024/687028734322147344/1144865310588014633 <@111604486476181504>:" + "\n> Slow news day." + "\nTesting this reply, ignore", avatar_url: "https://matrix.cadence.moe/_matrix/media/r0/download/cadence.moe/azCAhThKTojXSZJRoWwZmhvU" }] } ) }) test("event2message: should avoid using blockquote contents as reply preview in rich reply to a sim user", async t => { t.deepEqual( await eventToMessage({ type: "m.room.message", sender: "@cadence:cadence.moe", content: { msgtype: "m.text", body: "> <@_ooye_kyuugryphon:cadence.moe> > well, you said this, so...\n> \n> that can't be true! there's no way :o\n\nI agree!", format: "org.matrix.custom.html", formatted_body: "
In reply to @_ooye_kyuugryphon:cadence.moe
well, you said this, so...

that can't be true! there's no way :o
I agree!", "m.relates_to": { "m.in_reply_to": { event_id: "$Fxy8SMoJuTduwReVkHZ1uHif9EuvNx36Hg79cltiA04" } } }, event_id: "$BpGx8_vqHyN6UQDARPDU51ftrlRBhleutRSgpAJJ--g", room_id: "!fGgIymcYWOqjbSRUdV:cadence.moe" }, data.guild.general, { api: { getEvent: mockGetEvent(t, "!fGgIymcYWOqjbSRUdV:cadence.moe", "$Fxy8SMoJuTduwReVkHZ1uHif9EuvNx36Hg79cltiA04", { "type": "m.room.message", "sender": "@_ooye_kyuugryphon:cadence.moe", "content": { "m.mentions": {}, "msgtype": "m.text", "body": "> well, you said this, so...\n\nthat can't be true! there's no way :o", "format": "org.matrix.custom.html", "formatted_body": "
well, you said this, so...

that can't be true! there's no way :o" } }) } }), { messagesToDelete: [], messagesToEdit: [], messagesToSend: [{ username: "cadence [they]", content: "> <:L1:1144820033948762203><:L2:1144820084079087647>https://discord.com/channels/112760669178241024/687028734322147344/1144865310588014633 <@111604486476181504>:" + "\n> that can't be true! there's no way :o" + "\nI agree!", 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( await eventToMessage({ "type": "m.room.message", "sender": "@cadence:cadence.moe", "content": { "msgtype": "m.text", "body": "> <@_ooye_kyuugryphon:cadence.moe> Slow news day.\n\n * Editing this reply, which is also a test", "m.new_content": { "msgtype": "m.text", "body": "Editing this reply, which is also a test", "format": "org.matrix.custom.html", "formatted_body": "Editing this reply, which is also a test" }, "format": "org.matrix.custom.html", "formatted_body": "
In reply to @_ooye_kyuugryphon:cadence.moe
Slow news day.
* Editing this reply, which is also a test", "m.relates_to": { "rel_type": "m.replace", "event_id": "$v_Gtr-bzv9IVlSLBO5DstzwmiDd-GSFaNfHX66IupV8" } }, "origin_server_ts": 1693222931237, "unsigned": { "age": 44, "transaction_id": "m1693222931143.837" }, "event_id": "$XEgssz13q-a7NLO7UZO2Oepq7tSiDBD7YRfr7Xu_QiA", "room_id": "!fGgIymcYWOqjbSRUdV:cadence.moe" }, data.guild.general, { api: { getEvent: (roomID, eventID) => { if (eventID === "$Fxy8SMoJuTduwReVkHZ1uHif9EuvNx36Hg79cltiA04") { eventsFetched.push("past") return mockGetEvent(t, "!fGgIymcYWOqjbSRUdV:cadence.moe", "$Fxy8SMoJuTduwReVkHZ1uHif9EuvNx36Hg79cltiA04", { type: "m.room.message", content: { msgtype: "m.text", body: "Slow news day." }, sender: "@_ooye_kyuugryphon:cadence.moe" })(roomID, eventID) } else if (eventID === "$v_Gtr-bzv9IVlSLBO5DstzwmiDd-GSFaNfHX66IupV8") { eventsFetched.push("original") return mockGetEvent(t, "!fGgIymcYWOqjbSRUdV:cadence.moe", "$v_Gtr-bzv9IVlSLBO5DstzwmiDd-GSFaNfHX66IupV8", { type: "m.room.message", sender: "@cadence:cadence.moe", content: { msgtype: "m.text", body: "> <@_ooye_kyuugryphon:cadence.moe> Slow news day.\n\nTesting this reply, ignore", format: "org.matrix.custom.html", formatted_body: "
In reply to @_ooye_kyuugryphon:cadence.moe
Slow news day.
Testing this reply, ignore", "m.relates_to": { "m.in_reply_to": { event_id: "$Fxy8SMoJuTduwReVkHZ1uHif9EuvNx36Hg79cltiA04" } } } })(roomID, eventID) } else { throw new Error(`This test wasn't meant to fetch event ID: ${eventID}`) } } } }), { messagesToDelete: [], messagesToEdit: [{ id: "1144874214311067708", message: { username: "cadence [they]", content: "> <:L1:1144820033948762203><:L2:1144820084079087647>https://discord.com/channels/112760669178241024/687028734322147344/1144865310588014633 <@111604486476181504>:" + "\n> Slow news day." + "\nEditing this reply, which is also a test", avatar_url: "https://matrix.cadence.moe/_matrix/media/r0/download/cadence.moe/azCAhThKTojXSZJRoWwZmhvU" } }], messagesToSend: [] } ) t.deepEqual(eventsFetched, ["original", "past"]) }) test("event2message: editing a plaintext body message", async t => { t.deepEqual( await eventToMessage({ "type": "m.room.message", "sender": "@cadence:cadence.moe", "content": { "msgtype": "m.text", "body": " * well, I guess it's no longer brand new... it's existed for mere seconds...", "m.new_content": { "msgtype": "m.text", "body": "well, I guess it's no longer brand new... it's existed for mere seconds..." }, "m.relates_to": { "rel_type": "m.replace", "event_id": "$7LIdiJCEqjcWUrpzWzS8TELOlFfBEe4ytgS7zn2lbSs" } }, "origin_server_ts": 1693223873912, "unsigned": { "age": 42, "transaction_id": "m1693223873796.842" }, "event_id": "$KxGwvVNzNcmlVbiI2m5kX-jMFNi3Jle71-uu1j7P7vM", "room_id": "!PnyBKvUBOhjuCucEfk:cadence.moe" }, data.guild.general, { api: { getEvent: mockGetEvent(t, "!PnyBKvUBOhjuCucEfk:cadence.moe", "$7LIdiJCEqjcWUrpzWzS8TELOlFfBEe4ytgS7zn2lbSs", { type: "m.room.message", sender: "@cadence:cadence.moe", content: { msgtype: "m.text", body: "brand new, never before seen message", } }) } }), { messagesToDelete: [], messagesToEdit: [{ id: "1145688633186193479", message: { username: "cadence [they]", content: "well, I guess it's no longer brand new... it's existed for mere seconds...", avatar_url: "https://matrix.cadence.moe/_matrix/media/r0/download/cadence.moe/azCAhThKTojXSZJRoWwZmhvU" } }], messagesToSend: [] } ) }) test("event2message: editing a plaintext message to be longer", async t => { t.deepEqual( await eventToMessage({ "type": "m.room.message", "sender": "@cadence:cadence.moe", "content": { "msgtype": "m.text", "body": " * " + "aaaaaaaaa ".repeat(198) + "well, I guess it's no longer brand new... it's existed for mere seconds..." + "aaaaaaaaa ".repeat(20), "m.new_content": { "msgtype": "m.text", "body": "aaaaaaaaa ".repeat(198) + "well, I guess it's no longer brand new... it's existed for mere seconds..." + "aaaaaaaaa ".repeat(20) }, "m.relates_to": { "rel_type": "m.replace", "event_id": "$7LIdiJCEqjcWUrpzWzS8TELOlFfBEe4ytgS7zn2lbSs" } }, "origin_server_ts": 1693223873912, "unsigned": { "age": 42, "transaction_id": "m1693223873796.842" }, "event_id": "$KxGwvVNzNcmlVbiI2m5kX-jMFNi3Jle71-uu1j7P7vM", "room_id": "!PnyBKvUBOhjuCucEfk:cadence.moe" }, data.guild.general, { api: { getEvent: mockGetEvent(t, "!PnyBKvUBOhjuCucEfk:cadence.moe", "$7LIdiJCEqjcWUrpzWzS8TELOlFfBEe4ytgS7zn2lbSs", { type: "m.room.message", sender: "@cadence:cadence.moe", content: { msgtype: "m.text", body: "brand new, never before seen message", } }) } }), { messagesToDelete: [], messagesToEdit: [{ id: "1145688633186193479", message: { content: "aaaaaaaaa ".repeat(198) + "well, I guess it's", username: "cadence [they]", avatar_url: "https://matrix.cadence.moe/_matrix/media/r0/download/cadence.moe/azCAhThKTojXSZJRoWwZmhvU" } }], messagesToSend: [{ content: "no longer brand new... it's existed for mere seconds..." + ("aaaaaaaaa ".repeat(20)).slice(0, -1), username: "cadence [they]", avatar_url: "https://matrix.cadence.moe/_matrix/media/r0/download/cadence.moe/azCAhThKTojXSZJRoWwZmhvU" }] } ) }) test("event2message: editing a plaintext message to be shorter", async t => { t.deepEqual( await eventToMessage({ "type": "m.room.message", "sender": "@cadence:cadence.moe", "content": { "msgtype": "m.text", "body": " * well, I guess it's no longer brand new... it's existed for mere seconds...", "m.new_content": { "msgtype": "m.text", "body": "well, I guess it's no longer brand new... it's existed for mere seconds..." }, "m.relates_to": { "rel_type": "m.replace", "event_id": "$7LIdiJCEqjcWUrpzWzS8TELOlFfBEe4ytgS7zn2lbSt" } }, "origin_server_ts": 1693223873912, "unsigned": { "age": 42, "transaction_id": "m1693223873796.842" }, "event_id": "$KxGwvVNzNcmlVbiI2m5kX-jMFNi3Jle71-uu1j7P7vM", "room_id": "!PnyBKvUBOhjuCucEfk:cadence.moe" }, data.guild.general, { api: { getEvent: mockGetEvent(t, "!PnyBKvUBOhjuCucEfk:cadence.moe", "$7LIdiJCEqjcWUrpzWzS8TELOlFfBEe4ytgS7zn2lbSt", { type: "m.room.message", sender: "@cadence:cadence.moe", content: { msgtype: "m.text", body: "aaaaaaaaa ".repeat(198) + "well, I guess it's no longer brand new... it's existed for mere seconds..." + "aaaaaaaaa ".repeat(20) } }) } }), { messagesToDelete: ["1145688633186193481"], messagesToEdit: [{ id: "1145688633186193480", message: { username: "cadence [they]", content: "well, I guess it's no longer brand new... it's existed for mere seconds...", avatar_url: "https://matrix.cadence.moe/_matrix/media/r0/download/cadence.moe/azCAhThKTojXSZJRoWwZmhvU" } }], messagesToSend: [] } ) }) test("event2message: editing a formatted body message", async t => { t.deepEqual( await eventToMessage({ "type": "m.room.message", "sender": "@cadence:cadence.moe", "content": { "msgtype": "m.text", "body": " * **well, I guess it's no longer brand new... it's existed for mere seconds...**", "format": "org.matrix.custom.html", "formatted_body": "* well, I guess it's no longer brand new... it's existed for mere seconds...", "m.new_content": { "msgtype": "m.text", "body": "**well, I guess it's no longer brand new... it's existed for mere seconds...**", "format": "org.matrix.custom.html", "formatted_body": "well, I guess it's no longer brand new... it's existed for mere seconds..." }, "m.relates_to": { "rel_type": "m.replace", "event_id": "$7LIdiJCEqjcWUrpzWzS8TELOlFfBEe4ytgS7zn2lbSs" } }, "origin_server_ts": 1693223873912, "unsigned": { "age": 42, "transaction_id": "m1693223873796.842" }, "event_id": "$KxGwvVNzNcmlVbiI2m5kX-jMFNi3Jle71-uu1j7P7vM", "room_id": "!PnyBKvUBOhjuCucEfk:cadence.moe" }, data.guild.general, { api: { getEvent: mockGetEvent(t, "!PnyBKvUBOhjuCucEfk:cadence.moe", "$7LIdiJCEqjcWUrpzWzS8TELOlFfBEe4ytgS7zn2lbSs", { type: "m.room.message", sender: "@cadence:cadence.moe", content: { msgtype: "m.text", body: "**brand new, never before seen message**", format: "org.matrix.custom.html", formatted_body: "brand new, never before seen message" } }) } }), { messagesToDelete: [], messagesToEdit: [{ id: "1145688633186193479", message: { username: "cadence [they]", content: "**well, I guess it's no longer brand new... it's existed for mere seconds...**", avatar_url: "https://matrix.cadence.moe/_matrix/media/r0/download/cadence.moe/azCAhThKTojXSZJRoWwZmhvU" } }], messagesToSend: [] } ) }) test("event2message: rich reply to a matrix user's long message with formatting", async t => { t.deepEqual( await eventToMessage({ "type": "m.room.message", "sender": "@cadence:cadence.moe", "content": { "msgtype": "m.text", "body": "> <@cadence:cadence.moe> ```\n> i should have a little happy test\n> ```\n> * list **bold** _em_ ~~strike~~\n> # heading 1\n> ## heading 2\n> ### heading 3\n> https://cadence.moe\n> [legit website](https://cadence.moe)\n\nno you can't!!!", "format": "org.matrix.custom.html", "formatted_body": "
In reply to @cadence:cadence.moe
i should have a little happy test\n
\n
    \n
  • list bold em ~~strike~~
  • \n
\n

heading 1

\n

heading 2

\n

heading 3

\n

https://cadence.moe
legit website

\n
no you can't!!!", "m.relates_to": { "m.in_reply_to": { "event_id": "$Fxy8SMoJuTduwReVkHZ1uHif9EuvNx36Hg79cltiA04" } } }, "origin_server_ts": 1693037401693, "unsigned": { "age": 381, "transaction_id": "m1693037401592.521" }, "event_id": "$v_Gtr-bzv9IVlSLBO5DstzwmiDd-GSFaNfHX66IupV8", "room_id": "!fGgIymcYWOqjbSRUdV:cadence.moe" }, data.guild.general, { api: { getEvent: mockGetEvent(t, "!fGgIymcYWOqjbSRUdV:cadence.moe", "$Fxy8SMoJuTduwReVkHZ1uHif9EuvNx36Hg79cltiA04", { "type": "m.room.message", "sender": "@cadence:cadence.moe", "content": { "msgtype": "m.text", "body": "```\ni should have a little happy test\n```\n* list **bold** _em_ ~~strike~~\n# heading 1\n## heading 2\n### heading 3\nhttps://cadence.moe\n[legit website](https://cadence.moe)", "format": "org.matrix.custom.html", "formatted_body": "
i should have a little happy test\n
\n
    \n
  • list bold em ~~strike~~
  • \n
\n

heading 1

\n

heading 2

\n

heading 3

\n

https://cadence.moe
legit website

\n" } }) } }), { messagesToDelete: [], messagesToEdit: [], messagesToSend: [{ username: "cadence [they]", content: "> <:L1:1144820033948762203><:L2:1144820084079087647>https://discord.com/channels/112760669178241024/687028734322147344/1144865310588014633 Ⓜ️**cadence**:" + "\n> i should have a little happy test list bold em..." + "\n**no you can't!!!**", avatar_url: "https://matrix.cadence.moe/_matrix/media/r0/download/cadence.moe/azCAhThKTojXSZJRoWwZmhvU" }] } ) }) test("event2message: with layered rich replies, the preview should only be the real text", async t => { t.deepEqual( await eventToMessage({ type: "m.room.message", sender: "@cadence:cadence.moe", content: { msgtype: "m.text", body: "> <@cadence:cadence.moe> two\n\nthree", format: "org.matrix.custom.html", formatted_body: "
In reply to @cadence:cadence.moe
two
three", "m.relates_to": { "m.in_reply_to": { event_id: "$Fxy8SMoJuTduwReVkHZ1uHif9EuvNx36Hg79cltiA04" } } }, event_id: "$v_Gtr-bzv9IVlSLBO5DstzwmiDd-GSFaNfHX66IupV8", room_id: "!fGgIymcYWOqjbSRUdV:cadence.moe" }, data.guild.general, { api: { getEvent: mockGetEvent(t, "!fGgIymcYWOqjbSRUdV:cadence.moe", "$Fxy8SMoJuTduwReVkHZ1uHif9EuvNx36Hg79cltiA04", { "type": "m.room.message", "sender": "@cadence:cadence.moe", "content": { "msgtype": "m.text", "body": "> <@cadence:cadence.moe> one\n\ntwo", "format": "org.matrix.custom.html", "formatted_body": "
In reply to @cadence:cadence.moe
one
two", "m.relates_to": { "m.in_reply_to": { "event_id": "$5UtboIC30EFlAYD_Oh0pSYVW8JqOp6GsDIJZHtT0Wls" } } } }) } }), { messagesToDelete: [], messagesToEdit: [], messagesToSend: [{ username: "cadence [they]", content: "> <:L1:1144820033948762203><:L2:1144820084079087647>https://discord.com/channels/112760669178241024/687028734322147344/1144865310588014633 Ⓜ️**cadence**:" + "\n> two" + "\nthree", avatar_url: "https://matrix.cadence.moe/_matrix/media/r0/download/cadence.moe/azCAhThKTojXSZJRoWwZmhvU" }] } ) }) test("event2message: mentioning discord users works", async t => { t.deepEqual( await eventToMessage({ content: { msgtype: "m.text", body: "wrong body", format: "org.matrix.custom.html", formatted_body: `I'm just testing mentions` }, event_id: "$g07oYSZFWBkxohNEfywldwgcWj1hbhDzQ1sBAKvqOOU", origin_server_ts: 1688301929913, room_id: "!kLRqKKUQXcibIMtOpl:cadence.moe", sender: "@cadence:cadence.moe", type: "m.room.message", unsigned: { age: 405299 } }), { messagesToDelete: [], messagesToEdit: [], messagesToSend: [{ username: "cadence [they]", content: "I'm just <@114147806469554185> testing mentions", avatar_url: undefined }] } ) }) test("event2message: mentioning matrix users works", async t => { t.deepEqual( await eventToMessage({ content: { msgtype: "m.text", body: "wrong body", format: "org.matrix.custom.html", formatted_body: `I'm just testing mentions` }, event_id: "$g07oYSZFWBkxohNEfywldwgcWj1hbhDzQ1sBAKvqOOU", origin_server_ts: 1688301929913, room_id: "!kLRqKKUQXcibIMtOpl:cadence.moe", sender: "@cadence:cadence.moe", type: "m.room.message", unsigned: { age: 405299 } }), { messagesToDelete: [], messagesToEdit: [], messagesToSend: [{ username: "cadence [they]", content: "I'm just [▲]() testing mentions", avatar_url: undefined }] } ) }) test("event2message: mentioning bridged rooms works", async t => { t.deepEqual( await eventToMessage({ content: { msgtype: "m.text", body: "wrong body", format: "org.matrix.custom.html", formatted_body: `I'm just worm-form testing channel mentions` }, event_id: "$g07oYSZFWBkxohNEfywldwgcWj1hbhDzQ1sBAKvqOOU", origin_server_ts: 1688301929913, room_id: "!kLRqKKUQXcibIMtOpl:cadence.moe", sender: "@cadence:cadence.moe", type: "m.room.message", unsigned: { age: 405299 } }), { messagesToDelete: [], messagesToEdit: [], messagesToSend: [{ username: "cadence [they]", content: "I'm just <#1100319550446252084> testing channel mentions", avatar_url: undefined }] } ) }) test("event2message: caches the member if the member is not known", async t => { let called = 0 t.deepEqual( await eventToMessage({ content: { body: "testing the member state cache", msgtype: "m.text" }, event_id: "$g07oYSZFWBkxohNEfywldwgcWj1hbhDzQ1sBAKvqOOU", origin_server_ts: 1688301929913, room_id: "!should_be_newly_cached:cadence.moe", sender: "@should_be_newly_cached:cadence.moe", type: "m.room.message", unsigned: { age: 405299 } }, {}, { api: { getStateEvent: async (roomID, type, stateKey) => { called++ t.equal(roomID, "!should_be_newly_cached:cadence.moe") t.equal(type, "m.room.member") t.equal(stateKey, "@should_be_newly_cached:cadence.moe") return { displayname: "this is the username", avatar_url: undefined } } } }), { messagesToDelete: [], messagesToEdit: [], messagesToSend: [{ username: "this is the username", content: "testing the member state cache", avatar_url: undefined }] } ) t.deepEqual(db.prepare("SELECT avatar_url, displayname, mxid FROM member_cache WHERE room_id = '!should_be_newly_cached:cadence.moe'").all(), [ {avatar_url: null, displayname: "this is the username", mxid: "@should_be_newly_cached:cadence.moe"} ]) t.equal(called, 1, "getStateEvent should be called once") }) test("event2message: skips caching the member if the member does not exist, somehow", async t => { let called = 0 t.deepEqual( await eventToMessage({ content: { body: "should honestly never happen", msgtype: "m.text" }, event_id: "$g07oYSZFWBkxohNEfywldwgcWj1hbhDzQ1sBAKvqOOU", origin_server_ts: 1688301929913, room_id: "!not_real:cadence.moe", sender: "@not_real:cadence.moe", type: "m.room.message", unsigned: { age: 405299 } }, {}, { api: { getStateEvent: async (roomID, type, stateKey) => { called++ t.equal(roomID, "!not_real:cadence.moe") t.equal(type, "m.room.member") t.equal(stateKey, "@not_real:cadence.moe") throw new MatrixServerError("State event doesn't exist or something") } } }), { messagesToDelete: [], messagesToEdit: [], messagesToSend: [{ username: "not_real", content: "should honestly never happen", avatar_url: undefined }] } ) t.deepEqual(db.prepare("SELECT avatar_url, displayname, mxid FROM member_cache WHERE room_id = '!not_real:cadence.moe'").all(), []) t.equal(called, 1, "getStateEvent should be called once") }) test("event2message: text attachments work", async t => { t.deepEqual( await eventToMessage({ type: "m.room.message", content: { body: "chiki-powerups.txt", info: { size: 971, mimetype: "text/plain" }, msgtype: "m.file", url: "mxc://cadence.moe/zyThGlYQxvlvBVbVgKDDbiHH" }, sender: "@cadence:cadence.moe", event_id: "$c2WVyP6KcfAqh5imOa8e0xzt2C8JTR-cWbEd3GargEQ", room_id: "!PnyBKvUBOhjuCucEfk:cadence.moe" }), { messagesToDelete: [], messagesToEdit: [], messagesToSend: [{ username: "cadence [they]", content: "", avatar_url: "https://matrix.cadence.moe/_matrix/media/r0/download/cadence.moe/azCAhThKTojXSZJRoWwZmhvU", attachments: [{id: "0", filename: "chiki-powerups.txt"}], pendingFiles: [{name: "chiki-powerups.txt", url: "https://matrix.cadence.moe/_matrix/media/r0/download/cadence.moe/zyThGlYQxvlvBVbVgKDDbiHH"}] }] } ) }) test("event2message: image attachments work", async t => { t.deepEqual( await eventToMessage({ type: "m.room.message", sender: "@cadence:cadence.moe", content: { body: "cool cat.png", info: { size: 43170, mimetype: "image/png", w: 480, h: 480, "xyz.amorgan.blurhash": "URTHsVaTpdj2eKZgkkkXp{pHl7feo@lSl9Z$" }, msgtype: "m.image", url: "mxc://cadence.moe/IvxVJFLEuksCNnbojdSIeEvn" }, event_id: "$CXQy3Wmg1A-gL_xAesC1HQcQTEXwICLdSwwUx55FBTI", room_id: "!PnyBKvUBOhjuCucEfk:cadence.moe" }), { messagesToDelete: [], messagesToEdit: [], messagesToSend: [{ username: "cadence [they]", content: "", avatar_url: "https://matrix.cadence.moe/_matrix/media/r0/download/cadence.moe/azCAhThKTojXSZJRoWwZmhvU", attachments: [{id: "0", filename: "cool cat.png"}], pendingFiles: [{name: "cool cat.png", url: "https://matrix.cadence.moe/_matrix/media/r0/download/cadence.moe/IvxVJFLEuksCNnbojdSIeEvn"}] }] } ) }) test("event2message: encrypted image attachments work", async t => { t.deepEqual( await eventToMessage({ type: "m.room.message", sender: "@cadence:cadence.moe", content: { info: { mimetype: "image/png", size: 105691, w: 1192, h: 309, "xyz.amorgan.blurhash": "U17USN~q9FtQ-;Rjxuj[9FIUoMM|-=WB9Ft7" }, msgtype: "m.image", body: "image.png", file: { v: "v2", key: { alg: "A256CTR", ext: true, k: "QTo-oMPnN1Rbc7vBFg9WXMgoctscdyxdFEIYm8NYceo", key_ops: ["encrypt", "decrypt"], kty: "oct" }, iv: "Va9SHZpIn5kAAAAAAAAAAA", hashes: { sha256: "OUZqZFBcANFt42iAKET9YXfWMCdT0BX7QO0Eyk9q4Js" }, url: "mxc://heyquark.com/LOGkUTlVFrqfiExlGZNgCJJX", mimetype: "image/png" } }, event_id: "$JNhONhXO-5jrztZz8b7mbTMJasbU78TwQr4tog-3Mnk", room_id: "!PnyBKvUBOhjuCucEfk:cadence.moe" }), { messagesToDelete: [], messagesToEdit: [], messagesToSend: [{ username: "cadence [they]", content: "", avatar_url: "https://matrix.cadence.moe/_matrix/media/r0/download/cadence.moe/azCAhThKTojXSZJRoWwZmhvU", attachments: [{id: "0", filename: "image.png"}], pendingFiles: [{ name: "image.png", url: "https://matrix.cadence.moe/_matrix/media/r0/download/heyquark.com/LOGkUTlVFrqfiExlGZNgCJJX", key: "QTo-oMPnN1Rbc7vBFg9WXMgoctscdyxdFEIYm8NYceo", iv: "Va9SHZpIn5kAAAAAAAAAAA" }] }] } ) }) test("event2message: stickers work", async t => { t.deepEqual( await eventToMessage({ type: "m.sticker", sender: "@cadence:cadence.moe", content: { body: "get_real2", url: "mxc://cadence.moe/NyMXQFAAdniImbHzsygScbmN", info: { w: 320, h: 298, mimetype: "image/gif", size: 331394, thumbnail_info: { w: 320, h: 298, mimetype: "image/gif", size: 331394 }, thumbnail_url: "mxc://cadence.moe/NyMXQFAAdniImbHzsygScbmN" } }, event_id: "$PdI-KjdQ8Z_Tb4x9_7wKRPZCsrrXym4BXtbAPekypuM", room_id: "!PnyBKvUBOhjuCucEfk:cadence.moe" }), { messagesToDelete: [], messagesToEdit: [], messagesToSend: [{ username: "cadence [they]", content: "", avatar_url: "https://matrix.cadence.moe/_matrix/media/r0/download/cadence.moe/azCAhThKTojXSZJRoWwZmhvU", attachments: [{id: "0", filename: "get_real2.gif"}], pendingFiles: [{name: "get_real2.gif", url: "https://matrix.cadence.moe/_matrix/media/r0/download/cadence.moe/NyMXQFAAdniImbHzsygScbmN"}] }] } ) })