diff --git a/d2m/converters/message-to-event.embeds.test.js b/d2m/converters/message-to-event.embeds.test.js
index 773f7b8..dfc5679 100644
--- a/d2m/converters/message-to-event.embeds.test.js
+++ b/d2m/converters/message-to-event.embeds.test.js
@@ -113,10 +113,10 @@ test("message2event embeds: blockquote in embed", async t => {
msgtype: "m.notice",
body: "| ## ⏺️ minimus https://matrix.to/#/!qzDBLKlildpzrrOnFZ:cadence.moe/$dVCLyj6kxb3DaAWDtjcv2kdSny8JMMHdDhCMz8mDxVo?via=cadence.moe&via=example.invalid\n| \n| reply draft\n| > The following is a message composed via consensus of the Stinker Council.\n| > \n| > For those who are not currently aware of our existence, we represent the organization known as Wonderland. Our previous mission centered around the assortment and study of puzzling objects, entities and other assorted phenomena. This mission was the focus of our organization for more than 28 years.\n| > \n| > Due to circumstances outside of our control, this directive has now changed. Our new mission will be the extermination of the stinker race.\n| > \n| > There will be no further communication.\n| \n| [Go to Message](https://matrix.to/#/!qzDBLKlildpzrrOnFZ:cadence.moe/$dVCLyj6kxb3DaAWDtjcv2kdSny8JMMHdDhCMz8mDxVo?via=cadence.moe&via=example.invalid)",
format: "org.matrix.custom.html",
- formatted_body: "
⏺️ minimus
reply draft
The following is a message composed via consensus of the Stinker Council.
For those who are not currently aware of our existence, we represent the organization known as Wonderland. Our previous mission centered around the assortment and study of puzzling objects, entities and other assorted phenomena. This mission was the focus of our organization for more than 28 years.
Due to circumstances outside of our control, this directive has now changed. Our new mission will be the extermination of the stinker race.
There will be no further communication.
Go to Message
",
+ formatted_body: "⏺️ minimus
reply draft
The following is a message composed via consensus of the Stinker Council.
For those who are not currently aware of our existence, we represent the organization known as Wonderland. Our previous mission centered around the assortment and study of puzzling objects, entities and other assorted phenomena. This mission was the focus of our organization for more than 28 years.
Due to circumstances outside of our control, this directive has now changed. Our new mission will be the extermination of the stinker race.
There will be no further communication.
Go to Message
",
"m.mentions": {}
}])
- t.equal(called, 2)
+ t.equal(called, 2, "should call getStateEvent and getJoinedMembers once each")
})
test("message2event embeds: crazy html is all escaped", async t => {
diff --git a/d2m/converters/message-to-event.js b/d2m/converters/message-to-event.js
index ba51abf..b8c7a13 100644
--- a/d2m/converters/message-to-event.js
+++ b/d2m/converters/message-to-event.js
@@ -39,13 +39,12 @@ function getDiscordParseCallbacks(message, guild, useHTML) {
return `@${username}:`
}
},
- // FIXME: type
- /** @param {{id: string, type: "discordChannel", row: any, via: URLSearchParams}} node */
+ /** @param {{id: string, type: "discordChannel", row: {room_id: string, name: string, nick: string?}?, via: string}} node */
channel: node => {
if (!node.row) {
return `#[channel-from-an-unknown-server]` // fallback for when this channel is not bridged
} else if (useHTML) {
- return `#${node.row.nick || node.row.name}`
+ return `#${node.row.nick || node.row.name}`
} else {
return `#${node.row.nick || node.row.name}`
}
@@ -283,6 +282,20 @@ async function messageToEvent(message, guild, options = {}, di) {
addMention(repliedToEventSenderMxid)
}
+ /** @type {Map>} */
+ const viaMemo = new Map()
+ /**
+ * @param {string} roomID
+ * @returns {Promise} string encoded URLSearchParams
+ */
+ function getViaServersMemo(roomID) {
+ // @ts-ignore
+ if (viaMemo.has(roomID)) return viaMemo.get(roomID)
+ const promise = mxUtils.getViaServersQuery(roomID, di.api).then(p => p.toString())
+ viaMemo.set(roomID, promise)
+ return promise
+ }
+
/**
* Translate Discord message links to Matrix event links.
* If OOYE has handled this message in the past, this is an instant database lookup.
@@ -299,12 +312,13 @@ async function messageToEvent(message, guild, options = {}, di) {
const roomID = select("channel_room", "room_id", {channel_id: channelID}).pluck().get()
if (roomID) {
const eventID = select("event_message", "event_id", {message_id: messageID}).pluck().get()
+ const via = await getViaServersMemo(roomID)
if (eventID && roomID) {
- result = `https://matrix.to/#/${roomID}/${eventID}`
+ result = `https://matrix.to/#/${roomID}/${eventID}?${via}`
} else {
const ts = dUtils.snowflakeToTimestampExact(messageID)
const {event_id} = await di.api.getEventForTimestamp(roomID, ts)
- result = `https://matrix.to/#/${roomID}/${event_id}`
+ result = `https://matrix.to/#/${roomID}/${event_id}?${via}`
}
} else {
result = `${match[0]} [event is from another server]`
@@ -341,7 +355,7 @@ async function messageToEvent(message, guild, options = {}, di) {
if (node.type === "discordChannel") {
node.row = select("channel_room", ["room_id", "name", "nick"], {channel_id: node.id}).get()
if (node.row?.room_id) {
- node.via = await mxUtils.getViaServersQuery(node.row.room_id, di.api)
+ node.via = await getViaServersMemo(node.row.room_id)
}
}
if (Array.isArray(node.content)) {
diff --git a/d2m/converters/message-to-event.test.js b/d2m/converters/message-to-event.test.js
index 9154ac4..78f64b2 100644
--- a/d2m/converters/message-to-event.test.js
+++ b/d2m/converters/message-to-event.test.js
@@ -96,7 +96,7 @@ test("message2event: simple room mention", async t => {
format: "org.matrix.custom.html",
formatted_body: '#main'
}])
- t.equal(called, 2)
+ t.equal(called, 2, "should call getStateEvent and getJoinedMembers once each")
})
test("message2event: unknown room mention", async t => {
@@ -134,15 +134,41 @@ test("message2event: manually constructed unknown roles should use fallback", as
})
test("message2event: simple message link", async t => {
- const events = await messageToEvent(data.message.simple_message_link, data.guild.general, {})
+ let called = 0
+ const events = await messageToEvent(data.message.simple_message_link, data.guild.general, {}, {
+ api: {
+ async getStateEvent(roomID, type, key) {
+ called++
+ t.equal(roomID, "!kLRqKKUQXcibIMtOpl:cadence.moe")
+ t.equal(type, "m.room.power_levels")
+ t.equal(key, "")
+ return {
+ users: {
+ "@_ooye_bot:cadence.moe": 100
+ }
+ }
+ },
+ async getJoinedMembers(roomID) {
+ called++
+ t.equal(roomID, "!kLRqKKUQXcibIMtOpl:cadence.moe")
+ return {
+ joined: {
+ "@_ooye_bot:cadence.moe": {display_name: null, avatar_url: null},
+ "@user:super.invalid": {display_name: null, avatar_url: null}
+ }
+ }
+ }
+ }
+ })
t.deepEqual(events, [{
$type: "m.room.message",
"m.mentions": {},
msgtype: "m.text",
- body: "https://matrix.to/#/!kLRqKKUQXcibIMtOpl:cadence.moe/$X16nfVks1wsrhq4E9SSLiqrf2N8KD0erD0scZG7U5xg",
+ body: "https://matrix.to/#/!kLRqKKUQXcibIMtOpl:cadence.moe/$X16nfVks1wsrhq4E9SSLiqrf2N8KD0erD0scZG7U5xg?via=cadence.moe&via=super.invalid",
format: "org.matrix.custom.html",
- formatted_body: 'https://matrix.to/#/!kLRqKKUQXcibIMtOpl:cadence.moe/$X16nfVks1wsrhq4E9SSLiqrf2N8KD0erD0scZG7U5xg'
+ formatted_body: 'https://matrix.to/#/!kLRqKKUQXcibIMtOpl:cadence.moe/$X16nfVks1wsrhq4E9SSLiqrf2N8KD0erD0scZG7U5xg?via=cadence.moe&via=super.invalid'
}])
+ t.equal(called, 2, "should call getStateEvent and getJoinedMembers once each")
})
test("message2event: message link that OOYE doesn't know about", async t => {
@@ -156,6 +182,27 @@ test("message2event: message link that OOYE doesn't know about", async t => {
event_id: "$E8IQDGFqYzOU7BwY5Z74Bg-cwaU9OthXSroaWtgYc7U",
origin_server_ts: 1613287812754
}
+ },
+ async getStateEvent(roomID, type, key) { // for ?via calculation
+ called++
+ t.equal(roomID, "!kLRqKKUQXcibIMtOpl:cadence.moe")
+ t.equal(type, "m.room.power_levels")
+ t.equal(key, "")
+ return {
+ users: {
+ "@_ooye_bot:cadence.moe": 100
+ }
+ }
+ },
+ async getJoinedMembers(roomID) { // for ?via calculation
+ called++
+ t.equal(roomID, "!kLRqKKUQXcibIMtOpl:cadence.moe")
+ return {
+ joined: {
+ "@_ooye_bot:cadence.moe": {display_name: null, avatar_url: null},
+ "@user:matrix.org": {display_name: null, avatar_url: null}
+ }
+ }
}
}
})
@@ -164,12 +211,12 @@ test("message2event: message link that OOYE doesn't know about", async t => {
"m.mentions": {},
msgtype: "m.text",
body: "Me: I'll scroll up to find a certain message I'll send\n_scrolls up and clicks message links for god knows how long_\n_completely forgets what they were looking for and simply begins scrolling up to find some fun moments_\n_stumbles upon:_ "
- + "https://matrix.to/#/!kLRqKKUQXcibIMtOpl:cadence.moe/$E8IQDGFqYzOU7BwY5Z74Bg-cwaU9OthXSroaWtgYc7U",
+ + "https://matrix.to/#/!kLRqKKUQXcibIMtOpl:cadence.moe/$E8IQDGFqYzOU7BwY5Z74Bg-cwaU9OthXSroaWtgYc7U?via=cadence.moe&via=matrix.org",
format: "org.matrix.custom.html",
formatted_body: "Me: I'll scroll up to find a certain message I'll send
scrolls up and clicks message links for god knows how long
completely forgets what they were looking for and simply begins scrolling up to find some fun moments
stumbles upon: "
- + 'https://matrix.to/#/!kLRqKKUQXcibIMtOpl:cadence.moe/$E8IQDGFqYzOU7BwY5Z74Bg-cwaU9OthXSroaWtgYc7U'
+ + 'https://matrix.to/#/!kLRqKKUQXcibIMtOpl:cadence.moe/$E8IQDGFqYzOU7BwY5Z74Bg-cwaU9OthXSroaWtgYc7U?via=cadence.moe&via=matrix.org'
}])
- t.equal(called, 1, "getEventForTimestamp should be called once")
+ t.equal(called, 3, "getEventForTimestamp, getStateEvent, and getJoinedMembers should be called once each")
})
test("message2event: message link from another server", async t => {
diff --git a/d2m/converters/thread-to-announcement.js b/d2m/converters/thread-to-announcement.js
index 8ef3a67..9d9302a 100644
--- a/d2m/converters/thread-to-announcement.js
+++ b/d2m/converters/thread-to-announcement.js
@@ -6,6 +6,8 @@ const passthrough = require("../../passthrough")
const {discord, sync, db, select} = passthrough
/** @type {import("../../matrix/read-registration")} */
const reg = sync.require("../../matrix/read-registration.js")
+/** @type {import("../../m2d/converters/utils")} */
+const mxUtils = sync.require("../../m2d/converters/utils")
const userRegex = reg.namespaces.users.map(u => new RegExp(u.regex))
@@ -29,8 +31,9 @@ async function threadToAnnouncement(parentRoomID, threadRoomID, creatorMxid, thr
const msgtype = creatorMxid ? "m.emote" : "m.text"
const template = creatorMxid ? "started a thread:" : "Thread started:"
- let body = `${template} ${thread.name} https://matrix.to/#/${threadRoomID}`
- let html = `${template} ${thread.name}`
+ const via = await mxUtils.getViaServersQuery(threadRoomID, di.api)
+ let body = `${template} ${thread.name} https://matrix.to/#/${threadRoomID}?${via.toString()}`
+ let html = `${template} ${thread.name}`
return {
msgtype,
diff --git a/d2m/converters/thread-to-announcement.test.js b/d2m/converters/thread-to-announcement.test.js
index 06d937f..3bbca20 100644
--- a/d2m/converters/thread-to-announcement.test.js
+++ b/d2m/converters/thread-to-announcement.test.js
@@ -29,16 +29,34 @@ function mockGetEvent(t, roomID_in, eventID_in, outer) {
}
}
+const viaApi = {
+ async getStateEvent(roomID, type, key) {
+ return {
+ users: {
+ "@_ooye_bot:cadence.moe": 100
+ }
+ }
+ },
+ async getJoinedMembers(roomID) {
+ return {
+ joined: {
+ "@_ooye_bot:cadence.moe": {display_name: null, avatar_url: null},
+ "@user:matrix.org": {display_name: null, avatar_url: null}
+ }
+ }
+ }
+}
+
test("thread2announcement: no known creator, no branched from event", async t => {
const content = await threadToAnnouncement("!parent", "!thread", null, {
name: "test thread",
id: "-1"
- })
+ }, {api: viaApi})
t.deepEqual(content, {
msgtype: "m.text",
- body: "Thread started: test thread https://matrix.to/#/!thread",
+ body: "Thread started: test thread https://matrix.to/#/!thread?via=cadence.moe&via=matrix.org",
format: "org.matrix.custom.html",
- formatted_body: `Thread started: test thread`,
+ formatted_body: `Thread started: test thread`,
"m.mentions": {}
})
})
@@ -47,12 +65,12 @@ test("thread2announcement: known creator, no branched from event", async t => {
const content = await threadToAnnouncement("!parent", "!thread", "@_ooye_crunch_god:cadence.moe", {
name: "test thread",
id: "-1"
- })
+ }, {api: viaApi})
t.deepEqual(content, {
msgtype: "m.emote",
- body: "started a thread: test thread https://matrix.to/#/!thread",
+ body: "started a thread: test thread https://matrix.to/#/!thread?via=cadence.moe&via=matrix.org",
format: "org.matrix.custom.html",
- formatted_body: `started a thread: test thread`,
+ formatted_body: `started a thread: test thread`,
"m.mentions": {}
})
})
@@ -70,14 +88,15 @@ test("thread2announcement: no known creator, branched from discord event", async
msgtype: 'm.text',
body: 'testing testing testing'
}
- })
+ }),
+ ...viaApi
}
})
t.deepEqual(content, {
msgtype: "m.text",
- body: "Thread started: test thread https://matrix.to/#/!thread",
+ body: "Thread started: test thread https://matrix.to/#/!thread?via=cadence.moe&via=matrix.org",
format: "org.matrix.custom.html",
- formatted_body: `Thread started: test thread`,
+ formatted_body: `Thread started: test thread`,
"m.mentions": {},
"m.relates_to": {
"m.in_reply_to": {
@@ -100,14 +119,15 @@ test("thread2announcement: known creator, branched from discord event", async t
msgtype: 'm.text',
body: 'testing testing testing'
}
- })
+ }),
+ ...viaApi
}
})
t.deepEqual(content, {
msgtype: "m.emote",
- body: "started a thread: test thread https://matrix.to/#/!thread",
+ body: "started a thread: test thread https://matrix.to/#/!thread?via=cadence.moe&via=matrix.org",
format: "org.matrix.custom.html",
- formatted_body: `started a thread: test thread`,
+ formatted_body: `started a thread: test thread`,
"m.mentions": {},
"m.relates_to": {
"m.in_reply_to": {
@@ -130,14 +150,15 @@ test("thread2announcement: no known creator, branched from matrix event", async
body: "so can you reply to my webhook uwu"
},
sender: "@cadence:cadence.moe"
- })
+ }),
+ ...viaApi
}
})
t.deepEqual(content, {
msgtype: "m.text",
- body: "Thread started: test thread https://matrix.to/#/!thread",
+ body: "Thread started: test thread https://matrix.to/#/!thread?via=cadence.moe&via=matrix.org",
format: "org.matrix.custom.html",
- formatted_body: `Thread started: test thread`,
+ formatted_body: `Thread started: test thread`,
"m.mentions": {
user_ids: ["@cadence:cadence.moe"]
},