Compare commits
59 commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 86cfbd21a9 | |||
| 438ed2b4eb | |||
| 56f7c4c09a | |||
| e807d1fbf2 | |||
| 748e851b39 | |||
| b38abe81a6 | |||
| b84b848d04 | |||
| 20ce420303 | |||
| a877122ef6 | |||
| de6ce38c2d | |||
| b90592cbe9 | |||
| 8260396254 | |||
| c691274dd9 | |||
| 5a0e7f6a66 | |||
| 69b128a598 | |||
| 7895f89cc0 | |||
| 10fbb9e696 | |||
| edfbdc567f | |||
| c9509bb938 | |||
| 42c32ba749 | |||
| ffed434c6a | |||
| 0557c7b143 | |||
| 3e42616065 | |||
| 22ff10222c | |||
| 3f7a7aa10f | |||
| 266f46563b | |||
| 06962c217e | |||
| 9424b5e517 | |||
| 9bf6e50ae9 | |||
| fa916699a7 | |||
| bea0b9370d | |||
| c283528d72 | |||
| 0ad4b41ae9 | |||
| 4a26001382 | |||
| f0515ceecf | |||
| 69d07c1a7b | |||
| 3aa5f1b7ce | |||
| e146faced1 | |||
| 23cdf54982 | |||
| b53b2f56b6 | |||
| d7aadc3079 | |||
| f9e303f018 | |||
| e44f1041b6 | |||
| f734b0619f | |||
| b542a81ee1 | |||
| ac421e6c74 | |||
| 7afcbfaa06 | |||
| abe42aaa92 | |||
| aaf8dea104 | |||
| 486959be0b | |||
| 01b82e7b68 | |||
| dca53752bb | |||
| 8676a73620 | |||
| 10099142c2 | |||
| aedd30ab4a | |||
| a66b93ed26 | |||
| 5a853249a2 | |||
| 2c7831c587 | |||
| ae6b730c26 |
12 changed files with 282 additions and 39 deletions
5
.gitignore
vendored
5
.gitignore
vendored
|
|
@ -1,17 +1,18 @@
|
|||
# Secrets
|
||||
# Personal
|
||||
config.js
|
||||
registration.yaml
|
||||
ooye.db*
|
||||
events.db*
|
||||
backfill.db*
|
||||
custom-webroot
|
||||
icon.svg
|
||||
.devcontainer
|
||||
|
||||
# Automatically generated
|
||||
node_modules
|
||||
coverage
|
||||
test/res/*
|
||||
!test/res/lottie*
|
||||
icon.svg
|
||||
*~
|
||||
.#*
|
||||
\#*#
|
||||
|
|
|
|||
9
docs/threads-as-rooms.md
Normal file
9
docs/threads-as-rooms.md
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
I thought pretty hard about it and I opted to make threads separate rooms because
|
||||
|
||||
1. parity: discord has separate things like permissions and pins for threads, matrix cannot do this at all unless the thread is a separate room
|
||||
2. usage styles: most discord threads I've seen tend to be long-lived, spanning months or years, which isn't suited to matrix because of the timeline
|
||||
- I'm in a discord thread for posting photos of food that gets a couple posts a week and has a timeline going back to 2023
|
||||
3. the timeline: if a matrix room has threads, and you want to scroll back through the timeline of a room OR of one of its threads, the timeline is merged, so you have to download every message linearised and throw them away if they aren't part of the thread you're looking through. it's bad for threads and it's bad for the main room
|
||||
4. it is also very very complex for clients to implement read receipts and typing indicators correctly for the merged timeline. if your client doesn't implement this, or doesn't do it correctly, you have a bad experience. many clients don't. element seems to have done it well enough, but is an exception
|
||||
|
||||
overall in my view, threads-as-rooms has better parity and fewer downsides over native threads. but if there are things you don't like about this approach, I'm happy to discuss and see if we can improve them.
|
||||
|
|
@ -19,19 +19,21 @@ const userRegex = reg.namespaces.users.map(u => new RegExp(u.regex))
|
|||
*/
|
||||
async function threadToAnnouncement(parentRoomID, threadRoomID, creatorMxid, thread, di) {
|
||||
const branchedFromEventID = select("event_message", "event_id", {message_id: thread.id}).pluck().get()
|
||||
/** @type {{"m.mentions"?: any, "m.in_reply_to"?: any}} */
|
||||
/** @type {{"m.mentions"?: any, "m.relates_to"?: {event_id?: string, is_falling_back?: boolean, "m.in_reply_to"?: {event_id: string}, rel_type?: "m.replace"|"m.thread"}}} */
|
||||
const context = {}
|
||||
let suffix = "";
|
||||
if (branchedFromEventID) {
|
||||
// Need to figure out who sent that event...
|
||||
const event = await di.api.getEvent(parentRoomID, branchedFromEventID)
|
||||
context["m.relates_to"] = {"m.in_reply_to": {event_id: event.event_id}}
|
||||
suffix = "\n[Note: You should continue the conversation in that room, rather than in this thread. Any messages sent in Matrix threads will be bridged to Discord as replies, not in-thread messages, which is probably not what you want.]";
|
||||
context["m.relates_to"] = {"m.in_reply_to": {event_id: event.event_id}, is_falling_back:false, event_id: event.event_id, rel_type: "m.thread"}
|
||||
if (event.sender && !userRegex.some(rx => event.sender.match(rx))) context["m.mentions"] = {user_ids: [event.sender]}
|
||||
}
|
||||
|
||||
const msgtype = creatorMxid ? "m.emote" : "m.text"
|
||||
const template = creatorMxid ? "started a thread:" : "Thread started:"
|
||||
const template = creatorMxid ? "started a thread" : "New thread started:"
|
||||
const via = await mxUtils.getViaServersQuery(threadRoomID, di.api)
|
||||
let body = `${template} ${thread.name} https://matrix.to/#/${threadRoomID}?${via.toString()}`
|
||||
let body = `${template} „${thread.name}” in room: https://matrix.to/#/${threadRoomID}?${via.toString()}${suffix}`
|
||||
|
||||
return {
|
||||
msgtype,
|
||||
|
|
|
|||
|
|
@ -49,7 +49,7 @@ test("thread2announcement: no known creator, no branched from event", async t =>
|
|||
}, {api: viaApi})
|
||||
t.deepEqual(content, {
|
||||
msgtype: "m.text",
|
||||
body: "Thread started: test thread https://matrix.to/#/!thread?via=cadence.moe&via=matrix.org",
|
||||
body: "New thread started: „test thread” in room: https://matrix.to/#/!thread?via=cadence.moe&via=matrix.org",
|
||||
"m.mentions": {}
|
||||
})
|
||||
})
|
||||
|
|
@ -61,7 +61,7 @@ test("thread2announcement: known creator, no branched from event", async t => {
|
|||
}, {api: viaApi})
|
||||
t.deepEqual(content, {
|
||||
msgtype: "m.emote",
|
||||
body: "started a thread: test thread https://matrix.to/#/!thread?via=cadence.moe&via=matrix.org",
|
||||
body: "started a thread „test thread” in room: https://matrix.to/#/!thread?via=cadence.moe&via=matrix.org",
|
||||
"m.mentions": {}
|
||||
})
|
||||
})
|
||||
|
|
@ -85,12 +85,15 @@ test("thread2announcement: no known creator, branched from discord event", async
|
|||
})
|
||||
t.deepEqual(content, {
|
||||
msgtype: "m.text",
|
||||
body: "Thread started: test thread https://matrix.to/#/!thread?via=cadence.moe&via=matrix.org",
|
||||
body: "New thread started: „test thread” in room: https://matrix.to/#/!thread?via=cadence.moe&via=matrix.org\n[Note: You should continue the conversation in that room, rather than in this thread. Any messages sent in Matrix threads will be bridged to Discord as replies, not in-thread messages, which is probably not what you want.]",
|
||||
"m.mentions": {},
|
||||
"m.relates_to": {
|
||||
"event_id": "$X16nfVks1wsrhq4E9SSLiqrf2N8KD0erD0scZG7U5xg",
|
||||
"is_falling_back": false,
|
||||
"m.in_reply_to": {
|
||||
event_id: "$X16nfVks1wsrhq4E9SSLiqrf2N8KD0erD0scZG7U5xg"
|
||||
}
|
||||
"event_id": "$X16nfVks1wsrhq4E9SSLiqrf2N8KD0erD0scZG7U5xg",
|
||||
},
|
||||
"rel_type": "m.thread",
|
||||
}
|
||||
})
|
||||
})
|
||||
|
|
@ -114,12 +117,15 @@ test("thread2announcement: known creator, branched from discord event", async t
|
|||
})
|
||||
t.deepEqual(content, {
|
||||
msgtype: "m.emote",
|
||||
body: "started a thread: test thread https://matrix.to/#/!thread?via=cadence.moe&via=matrix.org",
|
||||
body: "started a thread „test thread” in room: https://matrix.to/#/!thread?via=cadence.moe&via=matrix.org\n[Note: You should continue the conversation in that room, rather than in this thread. Any messages sent in Matrix threads will be bridged to Discord as replies, not in-thread messages, which is probably not what you want.]",
|
||||
"m.mentions": {},
|
||||
"m.relates_to": {
|
||||
"event_id": "$X16nfVks1wsrhq4E9SSLiqrf2N8KD0erD0scZG7U5xg",
|
||||
"is_falling_back": false,
|
||||
"m.in_reply_to": {
|
||||
event_id: "$X16nfVks1wsrhq4E9SSLiqrf2N8KD0erD0scZG7U5xg"
|
||||
}
|
||||
"event_id": "$X16nfVks1wsrhq4E9SSLiqrf2N8KD0erD0scZG7U5xg",
|
||||
},
|
||||
"rel_type": "m.thread",
|
||||
}
|
||||
})
|
||||
})
|
||||
|
|
@ -143,14 +149,51 @@ test("thread2announcement: no known creator, branched from matrix event", async
|
|||
})
|
||||
t.deepEqual(content, {
|
||||
msgtype: "m.text",
|
||||
body: "Thread started: test thread https://matrix.to/#/!thread?via=cadence.moe&via=matrix.org",
|
||||
body: "New thread started: „test thread” in room: https://matrix.to/#/!thread?via=cadence.moe&via=matrix.org\n[Note: You should continue the conversation in that room, rather than in this thread. Any messages sent in Matrix threads will be bridged to Discord as replies, not in-thread messages, which is probably not what you want.]",
|
||||
"m.mentions": {
|
||||
user_ids: ["@cadence:cadence.moe"]
|
||||
},
|
||||
"m.relates_to": {
|
||||
"event_id": "$Ij3qo7NxMA4VPexlAiIx2CB9JbsiGhJeyt-2OvkAUe4",
|
||||
"is_falling_back": false,
|
||||
"m.in_reply_to": {
|
||||
event_id: "$Ij3qo7NxMA4VPexlAiIx2CB9JbsiGhJeyt-2OvkAUe4"
|
||||
}
|
||||
"event_id": "$Ij3qo7NxMA4VPexlAiIx2CB9JbsiGhJeyt-2OvkAUe4",
|
||||
},
|
||||
"rel_type": "m.thread",
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
test("thread2announcement: known creator, branched from matrix event", async t => {
|
||||
const content = await threadToAnnouncement("!kLRqKKUQXcibIMtOpl:cadence.moe", "!thread", "@_ooye_crunch_god:cadence.moe", {
|
||||
name: "test thread",
|
||||
id: "1128118177155526666"
|
||||
}, {
|
||||
api: {
|
||||
getEvent: mockGetEvent(t, "!kLRqKKUQXcibIMtOpl:cadence.moe", "$Ij3qo7NxMA4VPexlAiIx2CB9JbsiGhJeyt-2OvkAUe4", {
|
||||
type: "m.room.message",
|
||||
content: {
|
||||
msgtype: "m.text",
|
||||
body: "so can you reply to my webhook uwu"
|
||||
},
|
||||
sender: "@cadence:cadence.moe"
|
||||
}),
|
||||
...viaApi
|
||||
}
|
||||
})
|
||||
t.deepEqual(content, {
|
||||
msgtype: "m.emote",
|
||||
body: "started a thread „test thread” in room: https://matrix.to/#/!thread?via=cadence.moe&via=matrix.org\n[Note: You should continue the conversation in that room, rather than in this thread. Any messages sent in Matrix threads will be bridged to Discord as replies, not in-thread messages, which is probably not what you want.]",
|
||||
"m.mentions": {
|
||||
user_ids: ["@cadence:cadence.moe"]
|
||||
},
|
||||
"m.relates_to": {
|
||||
"event_id": "$Ij3qo7NxMA4VPexlAiIx2CB9JbsiGhJeyt-2OvkAUe4",
|
||||
"is_falling_back": false,
|
||||
"m.in_reply_to": {
|
||||
"event_id": "$Ij3qo7NxMA4VPexlAiIx2CB9JbsiGhJeyt-2OvkAUe4",
|
||||
},
|
||||
"rel_type": "m.thread",
|
||||
}
|
||||
})
|
||||
})
|
||||
|
|
|
|||
|
|
@ -39,14 +39,20 @@ async function resolvePendingFiles(message) {
|
|||
if ("key" in p) {
|
||||
// Encrypted file
|
||||
const d = crypto.createDecipheriv("aes-256-ctr", Buffer.from(p.key, "base64url"), Buffer.from(p.iv, "base64url"))
|
||||
await api.getMedia(p.mxc).then(res => stream.Readable.fromWeb(res.body).pipe(d))
|
||||
await api.getMedia(p.mxc).then(res => stream.Readable.fromWeb(
|
||||
// @ts-ignore
|
||||
res.body
|
||||
).pipe(d))
|
||||
return {
|
||||
name: p.name,
|
||||
file: d
|
||||
}
|
||||
} else {
|
||||
// Unencrypted file
|
||||
const body = await api.getMedia(p.mxc).then(res => stream.Readable.fromWeb(res.body))
|
||||
const body = await api.getMedia(p.mxc).then(res => stream.Readable.fromWeb(
|
||||
// @ts-ignore
|
||||
res.body
|
||||
))
|
||||
return {
|
||||
name: p.name,
|
||||
file: body
|
||||
|
|
|
|||
|
|
@ -471,6 +471,7 @@ async function checkWrittenMentions(content, senderMxid, roomID, guild, di) {
|
|||
// @ts-ignore - typescript doesn't know about indices yet
|
||||
content: content.slice(0, writtenMentionMatch.indices[1][0]-1) + `@everyone` + content.slice(writtenMentionMatch.indices[1][1]),
|
||||
ensureJoined: [],
|
||||
/**@type {DiscordTypes.AllowedMentionsTypes[]}*/ // @ts-ignore - TypeScript is for whatever reason conviced that "everyone" cannot be assigned to AllowedMentionsTypes, but if you „Go to Definition”, you'll see that "everyone" is a valid enum value.
|
||||
allowedMentionsParse: ["everyone"]
|
||||
}
|
||||
}
|
||||
|
|
@ -543,6 +544,7 @@ async function getL1L2ReplyLine(called = false) {
|
|||
async function eventToMessage(event, guild, channel, di) {
|
||||
let displayName = event.sender
|
||||
let avatarURL = undefined
|
||||
/**@type {DiscordTypes.AllowedMentionsTypes[]}*/ // @ts-ignore - TypeScript is for whatever reason conviced that neither "users" no "roles" cannot be assigned to AllowedMentionsTypes, but if you „Go to Definition”, you'll see that both are valid enum values.
|
||||
const allowedMentionsParse = ["users", "roles"]
|
||||
/** @type {string[]} */
|
||||
let messageIDsToEdit = []
|
||||
|
|
|
|||
|
|
@ -156,9 +156,14 @@ async function sendError(roomID, source, type, e, payload) {
|
|||
} catch (e) {}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} type
|
||||
* @param {(event: Ty.Event.Outer<any> & {type: any, redacts:any, state_key:any}, ...args: any)=>any} fn
|
||||
*/
|
||||
function guard(type, fn) {
|
||||
return async function(event, ...args) {
|
||||
return async function(/** @type {Ty.Event.Outer<any>} */ event, /** @type {any} */ ...args) {
|
||||
try {
|
||||
// @ts-ignore
|
||||
return await fn(event, ...args)
|
||||
} catch (e) {
|
||||
await sendError(event.room_id, "Matrix", type, e, event)
|
||||
|
|
@ -207,10 +212,32 @@ async event => {
|
|||
if (utils.eventSenderIsFromDiscord(event.sender)) return
|
||||
const messageResponses = await sendEvent.sendEvent(event)
|
||||
if (!messageResponses.length) return
|
||||
|
||||
/** @type {string|undefined} */
|
||||
let executedCommand
|
||||
if (event.type === "m.room.message" && event.content.msgtype === "m.text") {
|
||||
// @ts-ignore
|
||||
await matrixCommandHandler.execute(event)
|
||||
executedCommand = await matrixCommandHandler.parseAndExecute(
|
||||
// @ts-ignore - TypeScript doesn't know that the event.content.msgtype === "m.text" check ensures that event isn't of type Ty.Event.Outer_M_Room_Message_File (which, indeed, wouldn't fit here)
|
||||
event
|
||||
)
|
||||
}
|
||||
if (event.content["m.relates_to"]?.rel_type === "m.thread" && executedCommand !== "thread"){
|
||||
const bridgedTo = utils.getThreadRoomFromThreadEvent(event.content["m.relates_to"].event_id)
|
||||
api.sendEvent(event.room_id, "m.room.message", {
|
||||
body: "⚠️ **This message may not have been bridged to Discord in the way you thought it was gonna be!**\n\nIt seems like you sent this message inside a Matrix thread. Matrix threads don't work like Discord threads - they are effectively just „fancy replies”, not independent rooms/channels (any „thread-like appearance” is handled purely client-side - and even then, most Matrix clients don't handle it particularly well, with Element being the only one known to actually render threads as threads), and as such, they are bridged as replies to Discord. *In other words: __Discord users will not be aware that you sent this message inside a thread - the reply will go directly onto the main channel.__ If the thread you sent this message in is old, such a random reply **may be distracting** to Discord users!*\n\nFor the sake of Discord parity (and for better support in numerous Matrix clients - as stated above, most Matrix clients don't handle threads particularly well, and they just render in-thread messages as fancy replies), it is recommended to send threaded messages inside a separate Matrix room that gets bridged to Discord. "+ (bridgedTo ? "Luckily for you, this thread already has one! You can access it on https://matrix.to/#/"+bridgedTo+"?"+(await utils.getViaServersQuery(bridgedTo, api)).toString() : "Please run `/thread [Optional: Thread Name]` to create such a room for this thread, or get a link to it if someone else has already done so. If you run `/thread` (without any arguments) outside any threads and not as a reply, you'll get more info about this command")+".\n\n*You can read more about the rationale behind this design choice [here](https://gitdab.com/cadence/out-of-your-element/src/branch/main/docs/threads-as-rooms.md).*",
|
||||
format: "org.matrix.custom.html",
|
||||
formatted_body: "⚠️ <strong>This message may not have been bridged to Discord in the way you thought it was gonna be!</strong><br><br>It seems like you sent this message inside a Matrix thread. Matrix threads don't work like Discord threads - they are effectively just „fancy replies”, not independent rooms/channels (any „thread-like appearance” is handled purely client-side - and even then, most Matrix clients don't handle it particularly well, with Element being the only one known to actually render threads as threads), and as such, they are bridged as replies to Discord. <em>In other words: <u>Discord users will not be aware that you sent this message inside a thread - the reply will go directly onto the main channel.</u> If the thread you sent this message in is old, such a random reply <strong>may be distracting</strong> to Discord users!</em><br><br>For the sake of Discord parity (and for better support in numerous Matrix clients - as stated above, most Matrix clients don't handle threads particularly well, and they just render in-thread messages as fancy replies), it is recommended to send threaded messages inside a separate Matrix room that gets bridged to Discord. "+ (bridgedTo ? "Luckily for you, this thread already has one! You can access it on <a href=\"https://matrix.to/#/"+bridgedTo+"?"+(await utils.getViaServersQuery(bridgedTo, api)).toString()+"\">"+bridgedTo+"</a>" : "Please run <code>/thread [Optional: Thread Name]</code> to create such a room for this thread, or get a link to it if someone else has already done so. If you run <code>/thread</code> (without any arguments) outside any threads and not as a reply, you'll get more info about this command")+".<br><br><em>You can read more about the rationale behind this design choice <a href=\"https://gitdab.com/cadence/out-of-your-element/src/branch/main/docs/threads-as-rooms.md\">here</a>.</em>",
|
||||
"m.mentions": { "user_ids": [event.sender]},
|
||||
"m.relates_to": {
|
||||
event_id: event.content["m.relates_to"].event_id,
|
||||
is_falling_back: false,
|
||||
"m.in_reply_to": { event_id: event.event_id },
|
||||
rel_type: "m.thread"
|
||||
},
|
||||
msgtype: "m.text"
|
||||
})
|
||||
}
|
||||
|
||||
retrigger.messageFinishedBridging(event.event_id)
|
||||
await api.ackEvent(event)
|
||||
}))
|
||||
|
|
|
|||
|
|
@ -96,6 +96,33 @@ function replyctx(execute) {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Error & {code?: string|number}} e
|
||||
* @returns {e}
|
||||
*/
|
||||
function unmarshallDiscordError(e) {
|
||||
if (e.name === "DiscordAPIError"){
|
||||
try{
|
||||
const unmarshaled = JSON.parse(e.message)
|
||||
return {
|
||||
...e,
|
||||
...unmarshaled
|
||||
}
|
||||
} catch (err) {
|
||||
return {
|
||||
...err,
|
||||
code: "JSON_PARSE_FAILED",
|
||||
message: JSON.stringify({
|
||||
original_error_where_message_failed_to_parse: e,
|
||||
json_parser_error_message: err.message,
|
||||
json_parser_error_code: err.code,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
return e;
|
||||
}
|
||||
|
||||
/** @type {Command[]} */
|
||||
const commands = [{
|
||||
aliases: ["emoji"],
|
||||
|
|
@ -255,18 +282,93 @@ const commands = [{
|
|||
return api.sendEvent(event.room_id, "m.room.message", {
|
||||
...ctx,
|
||||
msgtype: "m.text",
|
||||
body: "This command creates a thread on Discord. But you aren't allowed to do this, because if you were a Discord user, you wouldn't have the Create Public Threads permission."
|
||||
body: "This command creates a thread on Discord. But you aren't allowed to do this, because if you were a Discord user, you wouldn't have the Create Public Threads permission." // NOTE: Currently, this assumes that all Matrix users have no Discord roles (see: empty [] in the getPermissions call above), and makes that „If you were a Discord user...” claim based on that assumption. If Discord permission emulation is gonna happen in the future, this is certainly one among many places that will need to be changed.
|
||||
})
|
||||
}
|
||||
|
||||
const relation = event.content["m.relates_to"]
|
||||
let isFallingBack = false;
|
||||
let branchedFromMxEvent = relation?.["m.in_reply_to"]?.event_id // By default, attempt to branch the thread from the message to which /thread was replying.
|
||||
if (relation?.rel_type === "m.thread") branchedFromMxEvent = relation?.event_id // If /thread was sent inside a Matrix thread, attempt to branch the Discord thread from the message, which that Matrix thread already is branching from.
|
||||
if (!branchedFromMxEvent){
|
||||
branchedFromMxEvent = event.event_id // If /thread wasn't replying to anything (ie. branchedFromMxEvent was undefined at initial assignment), or if the event was somehow malformed (in such a way that that - one way or another - branchedFromMxEvent ended up being undefined, even if according to the spec it shouldn't), branch the thread from the /thread command-message that created it.
|
||||
isFallingBack = true;
|
||||
}
|
||||
const branchedFromDiscordMessage = select("event_message", "message_id", {event_id: branchedFromMxEvent}).pluck().get()
|
||||
|
||||
await discord.snow.channel.createThreadWithoutMessage(channelID, {type: 11, name: words.slice(1).join(" ")})
|
||||
if (words.length < 2){
|
||||
if (isFallingBack) return api.sendEvent(event.room_id, "m.room.message", {
|
||||
...ctx,
|
||||
msgtype: "m.text",
|
||||
body: "**`/thread` usage:**\nRun this command as `/thread [Thread Name]` to create a thread on Discord (and optionally Matrix, if one doesn't exist already). The message from which said thread will branch, is chosen based on the following rules:\n* If ran stand-alone (not as a reply, nor in a Matrix thread), the created thread will branch from the command-message itself. The `Thread Name` argument must be provided in this case, otherwise you get this help message.\n* If sent as a reply (outside a Matrix thread), the thread will branch from the message to which you replied.\n* If ran inside an existing Matrix thread (regardless of whether it's a reply or not), the created Discord thread will be branching from the same message as the Matrix thread already is.",
|
||||
format: "org.matrix.custom.html",
|
||||
formatted_body: "<strong><code>/thread</code> usage:</strong><br>Run this command as <code>/thread [Thread Name]</code> to create a thread on Discord (and optionally Matrix, if one doesn't exist already). The message from which said thread will branch, is chosen based on the following rules:<br><ul><li>If ran stand-alone (not as a reply, nor in a Matrix thread), the created thread will branch from the command-message itself. The <code>Thread Name</code> argument must be provided in this case, otherwise you get this help message.</li><li>If sent as a reply (outside a Matrix thread), the thread will branch from the message to which you replied.</li><li>If ran inside an existing Matrix thread (regardless of whether it's a reply or not), the created Discord thread will be branching from the same message as the Matrix thread already is.</li></ul>"
|
||||
})
|
||||
words[1] = (await api.getEvent(event.room_id, branchedFromMxEvent)).content.body.replaceAll("\n", " ")
|
||||
words[1] = words[1].length < 100 ? words[1] : words[1].slice(0, 96) + "..."
|
||||
}
|
||||
|
||||
try {
|
||||
if (branchedFromDiscordMessage) await discord.snow.channel.createThreadWithMessage(channelID, branchedFromDiscordMessage, {name: words.slice(1).join(" ")})
|
||||
else throw {code: "NO_BRANCH_SOURCE", was_supposed_to_be: branchedFromMxEvent};
|
||||
}
|
||||
catch (e){
|
||||
switch (unmarshallDiscordError(e).code) {
|
||||
case "NO_BRANCH_SOURCE": return api.sendEvent(event.room_id, "m.room.message", {
|
||||
...ctx,
|
||||
msgtype: "m.text",
|
||||
body: "⚠️ Couldn't find a Discord representation of the message from which you're trying to branch this thread (event ID `"+e.was_supposed_to_be+"` on Matrix), so it wasn't created. Either you ran this command on an unbridged message (one sent by this bot or one that failed to bridge due to a previous error), or this is an error on our side and should be reported.",
|
||||
format: "org.matrix.custom.html",
|
||||
formatted_body: "⚠️ Couldn't find a Discord representation of the message from which you're trying to branch this thread (event ID <code>"+e.was_supposed_to_be+"</code> on Matrix), so it wasn't created. Either you ran this command on an unbridged message (one sent by this bot or one that failed to bridge due to a previous error), or this is an error on our side and should be reported."
|
||||
})
|
||||
|
||||
case (160004): // see: https://docs.discord.com/developers/topics/opcodes-and-status-codes
|
||||
if (isFallingBack){
|
||||
await api.sendEvent(event.room_id, "m.room.message", {
|
||||
...ctx,
|
||||
msgtype: "m.text",
|
||||
body: "⚠️ Discord claims that there already exists a thread for the message you ran this command on, but that doesn't make logical sense, as it doesn't seem like you ran this command on any message. Either your Matrix client did something funny with reply/thread tags, or this is a logic error on OOYE's side. At any rate, this should be reported for further investigation. you should also attach the error message that's about to be sent below (or on the main room timeline, if the command was ran inside a thread).",
|
||||
})
|
||||
throw e;
|
||||
}
|
||||
const thread = mxUtils.getThreadRoomFromThreadEvent(branchedFromMxEvent)
|
||||
return api.sendEvent(event.room_id, "m.room.message", {
|
||||
...ctx,
|
||||
msgtype: "m.text",
|
||||
body: "There already exists a Discord thread for the message you ran this command on" + (thread ? " - you may join its bridged room here: https://matrix.to/#/"+thread+"?"+(await mxUtils.getViaServersQuery(thread, api)).toString() : ", so a new one cannot be crated. However, it seems like that thread isn't bridged to any Matrix rooms. Please ask the space/server admins to rectify this issue by creating the bridge. (If you're said admin and you can see that said bridge already exists, but this error message is still showing up, please report that as a bug.)")
|
||||
})
|
||||
|
||||
case (50024): return api.sendEvent(event.room_id, "m.room.message", {
|
||||
...ctx,
|
||||
msgtype: "m.text",
|
||||
body: "You cannot create threads in a Discord channel of the type, to which this Matrix room is bridged to. Did you try to create a thread inside a thread?"
|
||||
})
|
||||
|
||||
case (50035): return api.sendEvent(event.room_id, "m.room.message", {
|
||||
...ctx,
|
||||
msgtype: "m.text",
|
||||
body: "Specified thread name is too long - thread creation failed. Please yap a bit less in the title, the thread body is for that. ;)"
|
||||
})
|
||||
|
||||
default:
|
||||
await api.sendEvent(event.room_id, "m.room.message", {
|
||||
...ctx,
|
||||
msgtype: "m.text",
|
||||
body: "⚠️ Unknown error occurred during thread creation. See error message below (or on the main room timeline, if the command was ran inside a thread) for details."
|
||||
})
|
||||
throw e
|
||||
}
|
||||
}
|
||||
}
|
||||
)
|
||||
}]
|
||||
|
||||
|
||||
/** @type {CommandExecute} */
|
||||
async function execute(event) {
|
||||
/**
|
||||
* @param {Ty.Event.Outer_M_Room_Message} event
|
||||
* @returns {Promise<string|undefined>} the executed command's name or undefined if no command execution was performed
|
||||
*/
|
||||
async function parseAndExecute(event) {
|
||||
let realBody = event.content.body
|
||||
while (realBody.startsWith("> ")) {
|
||||
const i = realBody.indexOf("\n")
|
||||
|
|
@ -287,7 +389,8 @@ async function execute(event) {
|
|||
if (!command) return
|
||||
|
||||
await command.execute(event, realBody, words)
|
||||
return words[0]
|
||||
}
|
||||
|
||||
module.exports.execute = execute
|
||||
module.exports.parseAndExecute = parseAndExecute
|
||||
module.exports.onReactionAdd = onReactionAdd
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ const assert = require("assert").strict
|
|||
const Ty = require("../types")
|
||||
const {tag} = require("@cloudrac3r/html-template-tag")
|
||||
const passthrough = require("../passthrough")
|
||||
const {db} = passthrough
|
||||
const {db, select} = passthrough
|
||||
|
||||
const {reg} = require("./read-registration")
|
||||
const userRegex = reg.namespaces.users.map(u => new RegExp(u.regex))
|
||||
|
|
@ -398,6 +398,16 @@ async function setUserPowerCascade(spaceID, mxid, power, api) {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {undefined|string?} eventID
|
||||
*/ //^For some reason, „?” doesn't include Undefined and it needs to be explicitly specified
|
||||
function getThreadRoomFromThreadEvent(eventID){
|
||||
if (!eventID) return eventID;
|
||||
const threadID = select("event_message", "message_id", {event_id: eventID}).pluck().get() //Discord thread ID === its message ID
|
||||
if (!threadID) return threadID;
|
||||
return select("channel_room", "room_id", {channel_id: threadID}).pluck().get()
|
||||
}
|
||||
|
||||
module.exports.bot = bot
|
||||
module.exports.BLOCK_ELEMENTS = BLOCK_ELEMENTS
|
||||
module.exports.eventSenderIsFromDiscord = eventSenderIsFromDiscord
|
||||
|
|
@ -413,3 +423,4 @@ module.exports.removeCreatorsFromPowerLevels = removeCreatorsFromPowerLevels
|
|||
module.exports.getEffectivePower = getEffectivePower
|
||||
module.exports.setUserPower = setUserPower
|
||||
module.exports.setUserPowerCascade = setUserPowerCascade
|
||||
module.exports.getThreadRoomFromThreadEvent = getThreadRoomFromThreadEvent
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
const {select} = require("../passthrough")
|
||||
const {test} = require("supertape")
|
||||
const {eventSenderIsFromDiscord, getEventIDHash, MatrixStringBuilder, getViaServers, roomHasAtLeastVersion, removeCreatorsFromPowerLevels, setUserPower} = require("./utils")
|
||||
const {eventSenderIsFromDiscord, getEventIDHash, MatrixStringBuilder, getViaServers, roomHasAtLeastVersion, removeCreatorsFromPowerLevels, setUserPower, getThreadRoomFromThreadEvent} = require("./utils")
|
||||
const util = require("util")
|
||||
|
||||
/** @param {string[]} mxids */
|
||||
|
|
@ -417,4 +417,38 @@ test("set user power: privileged users must demote themselves", async t => {
|
|||
t.equal(called, 3)
|
||||
})
|
||||
|
||||
test("getThreadRoomFromThreadEvent: real message, but without a thread", t => {
|
||||
const room = getThreadRoomFromThreadEvent("$Ij3qo7NxMA4VPexlAiIx2CB9JbsiGhJeyt-2OvkAUe4")
|
||||
const msg = "Expected null/undefined, got: "+room
|
||||
if(room) t.fail(msg);
|
||||
else t.pass(msg)
|
||||
})
|
||||
|
||||
test("getThreadRoomFromThreadEvent: real message with a thread", t => {
|
||||
const room = getThreadRoomFromThreadEvent("$fdD9o7NxMA4VPexlAiIx2CB9JbsiGhJeyJgnZG7U5xg")
|
||||
t.equal(room, "!FuDZhlOAtqswlyxzeR:cadence.moe")
|
||||
})
|
||||
|
||||
test("getThreadRoomFromThreadEvent: fake message", t => {
|
||||
const room = getThreadRoomFromThreadEvent("$ThisEvent-IdDoesNotExistInTheDatabase4Sure")
|
||||
const msg = "Expected null/undefined, got: "+room
|
||||
if(room) t.fail(msg);
|
||||
else t.pass(msg)
|
||||
})
|
||||
|
||||
test("getThreadRoomFromThreadEvent: null", t => {
|
||||
const room = getThreadRoomFromThreadEvent(null)
|
||||
t.equal(room, null)
|
||||
})
|
||||
|
||||
test("getThreadRoomFromThreadEvent: undefined", t => {
|
||||
const room = getThreadRoomFromThreadEvent(undefined)
|
||||
t.equal(room, undefined)
|
||||
})
|
||||
|
||||
test("getThreadRoomFromThreadEvent: no value at all", t => {
|
||||
const room = getThreadRoomFromThreadEvent() //This line should be giving a type-error, so it's not @ts-ignored on purpose. This is to test the desired behavior of that function, ie. „it CAN TAKE an undefined VALUE (as tested above), but you can just LEAVE the value completely undefined” (well, you can leave it like that from JS syntax perspective (which is why this test passes), but it makes no sense from usage standpoint, as it just gives back undefined). So this isn't a logic test (that's handled above), as much as it is a TypeScript test.
|
||||
t.equal(room, undefined)
|
||||
})
|
||||
|
||||
module.exports.mockGetEffectivePower = mockGetEffectivePower
|
||||
|
|
|
|||
21
src/types.d.ts
vendored
21
src/types.d.ts
vendored
|
|
@ -190,11 +190,12 @@ export namespace Event {
|
|||
format?: "org.matrix.custom.html"
|
||||
formatted_body?: string,
|
||||
"m.relates_to"?: {
|
||||
"m.in_reply_to": {
|
||||
event_id?: string
|
||||
is_falling_back?: boolean
|
||||
"m.in_reply_to"?: {
|
||||
event_id: string
|
||||
}
|
||||
rel_type?: "m.replace"
|
||||
event_id?: string
|
||||
rel_type?: "m.replace"|"m.thread"
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -210,11 +211,12 @@ export namespace Event {
|
|||
info?: any
|
||||
"page.codeberg.everypizza.msc4193.spoiler"?: boolean
|
||||
"m.relates_to"?: {
|
||||
"m.in_reply_to": {
|
||||
event_id?: string
|
||||
is_falling_back?: boolean
|
||||
"m.in_reply_to"?: {
|
||||
event_id: string
|
||||
}
|
||||
rel_type?: "m.replace"
|
||||
event_id?: string
|
||||
rel_type?: "m.replace"|"m.thread"
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -246,11 +248,12 @@ export namespace Event {
|
|||
},
|
||||
info?: any
|
||||
"m.relates_to"?: {
|
||||
"m.in_reply_to": {
|
||||
event_id?: string
|
||||
is_falling_back?: boolean
|
||||
"m.in_reply_to"?: {
|
||||
event_id: string
|
||||
}
|
||||
rel_type?: "m.replace"
|
||||
event_id?: string
|
||||
rel_type?: "m.replace"|"m.thread"
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -82,12 +82,14 @@ WITH a (message_id, channel_id) AS (VALUES
|
|||
('1381212840957972480', '112760669178241024'),
|
||||
('1401760355339862066', '112760669178241024'),
|
||||
('1439351590262800565', '1438284564815548418'),
|
||||
('1404133238414376971', '112760669178241024'))
|
||||
('1404133238414376971', '112760669178241024'),
|
||||
('1162005314908999790', '1100319550446252084'))
|
||||
SELECT message_id, max(historical_room_index) as historical_room_index FROM a INNER JOIN historical_channel_room ON historical_channel_room.reference_channel_id = a.channel_id GROUP BY message_id;
|
||||
|
||||
INSERT INTO event_message (event_id, event_type, event_subtype, message_id, part, reaction_part, source) VALUES
|
||||
('$X16nfVks1wsrhq4E9SSLiqrf2N8KD0erD0scZG7U5xg', 'm.room.message', 'm.text', '1126786462646550579', 0, 0, 1),
|
||||
('$Ij3qo7NxMA4VPexlAiIx2CB9JbsiGhJeyt-2OvkAUe4', 'm.room.message', 'm.text', '1128118177155526666', 0, 0, 0),
|
||||
('$fdD9o7NxMA4VPexlAiIx2CB9JbsiGhJeyJgnZG7U5xg', 'm.room.message', 'm.text', '1162005314908999790', 0, 0, 1),
|
||||
('$zXSlyI78DQqQwwfPUSzZ1b-nXzbUrCDljJgnGDdoI10', 'm.room.message', 'm.text', '1141619794500649020', 0, 0, 1),
|
||||
('$fdD9OZ55xg3EAsfvLZza5tMhtjUO91Wg3Otuo96TplY', 'm.room.message', 'm.text', '1141206225632112650', 0, 0, 1),
|
||||
('$mtR8cJqM4fKno1bVsm8F4wUVqSntt2sq6jav1lyavuA', 'm.room.message', 'm.text', '1141501302736695316', 0, 1, 1),
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue