Compare commits

...
Sign in to create a new pull request.

59 commits
main ... main

Author SHA1 Message Date
86cfbd21a9 I broke Git
of course I did
2026-03-02 15:27:54 +00:00
438ed2b4eb revert e807d1fbf2 2026-03-02 15:24:11 +00:00
56f7c4c09a Merge pull request 'sync-back' (#9) from main into fuckery
Reviewed-on: Guzio/out-of-your-element#9
2026-03-02 15:21:12 +00:00
e807d1fbf2 Merge branch 'fuckery' into main 2026-03-02 15:20:07 +00:00
748e851b39 Improve threads UX
/This is a squash-commit - the following is a rough summary of all sub-commits, written in style of commit messages (not necessarily those commits themselves), ie. short and in present tense./

* Document design choice to not bridge Discord threads as Matrix threads [by directly quoting Cadence]
* Alter thread-to-announcement, so that it replies in-thread [with this, Matrix users will get a list of almost all (exl. those that don't branch from anything) open threads on a given channel, whereas before it wasn't possible. Also features slight alterations to the text]
* Notify the user, whenever an in-thread message on Matrix is sent, that this isn't how they're supposed to do threads on OOYE
* Detect /thread being ran as a reply or in-thread to branch the thread from the relevant message
* Handle various /thread errors [notably being ran without args (infer the title if ran in the context above, simply show help if not)]
*  Whenever possible, direct the user to an already-existing thread-room [if /thread was ran as a reply or in-thread, or as part of the notification mentioned in point 3 (feat. a new utility method)]

AUXILIARY TYPE CHANGES (not always relevant to UX-improvement-related changes):
* Fix „boolean” being referred to as „bool” in types.d.ts
* Rename execute(event) in matrix-command-handler is now parseAndExecute(event) [and it is no longer of type CommandExecute, but has its own custom definition, because a) it has a different return now (returns what command was ran (needed for point 3 in section above) instead of always undefined and b) other params from CommandExecute (like ctx or words) weren't being used - quite the contrary, their values were only being created at that stage (as part of command parsing - hence the rename, too), so telling that they're values you pass into execute() was at least somewhat confusing]
* Further narrow-down the type of guard() in m2d event-dispatcher

TEST CHANGES:
* Create 7 new tests, all pass
* Update 4 existing threads, all pass
* Pass all other relevant tests [and almost all other tests, too - there are some issues with event2message for stickers, but given the fact that this commit does not touch the stickers subsystem in any way at all,  it does not seem like they are any way related to my changes and they must've been failing before]
* Do extensive manual testing and debugging
Co-authored-by: Guzio <guzekk@protonmail.com>
Co-committed-by: Guzio <guzekk@protonmail.com>
2026-03-02 15:17:08 +00:00
b38abe81a6 reworded the error
Turns out that auto-create is ALWAYS on for threads (which creates some hilarious situations, where the channel gets duplicated if it ever got unbridged). Also, manual bridging isn't even possible. Uhh... Sure! Let's just say, then, that it's the admin's problem to auto-create it (given the duplication - this is probably a better idea to leave it to them).

A proper fix for this (and also to limit (tho not fix) the dupe-by-autocreate problem) would probably be to allow for manual bridging on threads, but I really don't have time for this before The Merge (my ADHD is kicking-in on this update, and I have a feeling that if I don't PR soon, I'm gonna not do it for another 3 months).
2026-03-02 13:44:36 +00:00
b84b848d04 temporary change to VSC settings
So that I can squash-merge it all without leaving the trace of any extra unsolicited changes
2026-03-02 13:15:29 +00:00
20ce420303 copy-pasted Cadence's message as documentation 2026-03-02 13:12:17 +00:00
a877122ef6 fix one more final UX pet-peeve 2026-03-02 13:06:30 +00:00
de6ce38c2d Merge pull request 'bbbbbbb' (#8) from main into fuckery
Reviewed-on: Guzio/out-of-your-element#8
2026-03-02 13:00:11 +00:00
b90592cbe9 Prevent merge conflicts once again 2026-03-02 12:59:20 +00:00
8260396254 replace newlines instead of stripping them (what DC does by default) 2026-02-28 14:14:27 +00:00
c691274dd9 git pisses me off
just a little bit

I like it.

But it's just a little bit annoying.
2026-02-28 13:49:59 +00:00
5a0e7f6a66 Prevent more merge conflicts 2026-02-28 13:47:49 +00:00
69b128a598 debug done; turns out that I'm just stupid
I passed a completely wrong event ID and was confused as to why could it possibly be failing. (Btw, as part od fixing that - my new function from function is utils.js now supports blank values.)

Also - and that's unrelated to the bug I was debugging - I put a guard clause in my if (words.length < 2) backwards. If->return should canonically be above the logic, even if it technically doesn't break said logic in this case (all it was doing was creating an extra step that says „yea, name the newly-created thread /thread, even if this name is very stupid, and also pointless because no thread creation is about to take place”. And while fixing that, I also did some minor changes to error handling.
2026-02-28 13:37:16 +00:00
7895f89cc0 debug slop v2 2026-02-28 11:52:18 +00:00
10fbb9e696 improved consistency 2026-02-27 23:11:50 +00:00
edfbdc567f Used getThreadRoomFromThreadEvent in practice 2026-02-27 23:07:43 +00:00
c9509bb938 figured out how tests work, yaaayyyy
As a part of that:
* rewrote the tests to support my changed behaviors
* added a missing case
* Made my „threads get attached to” wording more consistent with the test cases („threads branch from”), as I always felt that there was something off about my phrasing, but I couldn't quite tell what was it. OOYE's „branch” term seems much more fitting there
* slightly cooked the testing data (changed the „Hey.” thread from „floating” to being a branch of a message, to accommodate...)
* 3 NEW TESTS: of the function created in my previous commit (I'm not sure if this *REALLY* needed testing, given how braindead-simple that function is, but everything else in utils.js is covered, so I figured it's only fair to test this, too)

EXTRA CHANGE: fixed that function's name (we're getting the thread from a (Matrix) Event, not a (Discord) Message) and description (I totally didn't copy-paste the JSDoc from above........)
2026-02-27 20:42:16 +00:00
42c32ba749 explained my technical decisions; made a function that'll help me later 2026-02-27 13:06:52 +00:00
ffed434c6a rewrote the f#cker as a switch statement 2026-02-26 16:05:53 +00:00
0557c7b143 works yay 2026-02-26 16:00:22 +00:00
3e42616065 It just occurred to me that I have no way of testing the „fallback case” now 2026-02-26 15:55:18 +00:00
22ff10222c fixed error details showing up as [object Object] 2026-02-26 15:38:20 +00:00
3f7a7aa10f handled overyapping 2026-02-26 15:37:17 +00:00
266f46563b Possibly? fixed error handling???
and yea, ofc it was a string......
2026-02-26 12:49:21 +00:00
06962c217e unspecified horsing around 2026-02-26 12:23:09 +00:00
9424b5e517 Apparenly, what I completley missed, is that „code” is overriden by something later in the error stack. Trying out other keys...... 2026-02-25 11:04:55 +00:00
9bf6e50ae9 SO WHAT DO YOU WANT? TELL ME WHAT'S YOUR POINT, JS!
So the code-key DOES exist. HuuuHhhhhh???
2026-02-25 10:56:51 +00:00
fa916699a7 Nope, it's an object. But, like... A weird one. It doesn't seem to behave like objects normally do. It it a wrapper around stuff? 2026-02-25 10:48:12 +00:00
bea0b9370d Type of e is its own content. Apparently. What the fuck? 2026-02-25 10:39:56 +00:00
c283528d72 Is this LITERALLY just a String????? 2026-02-25 10:32:37 +00:00
0ad4b41ae9 Iiiiiii........... I have no idea what was I trying to accomplish here... 2026-02-25 10:28:09 +00:00
4a26001382 Debug-slop begins! 2026-02-25 10:23:13 +00:00
f0515ceecf UX testing revealed that og messages looked awkward 2026-02-25 10:11:18 +00:00
69d07c1a7b I lied, that's the final patch (my C# past got the better of me lol) 2026-02-25 09:48:22 +00:00
3aa5f1b7ce Synced my branches, again 2026-02-25 09:33:45 +00:00
e146faced1 Prevent merge conflicts 2026-02-25 09:31:41 +00:00
23cdf54982 AEUGH it turns out that replying was already handled.
In other news: Made /thread work without args (in SOME cases).

I'm pretty sure this is the final patch before we go PR.
2026-02-25 09:25:02 +00:00
b53b2f56b6 Improved error handling 2026-02-24 19:26:34 +00:00
d7aadc3079 AAAAAAAAAAAAAAAAAAAA 2026-02-20 04:02:01 +00:00
f9e303f018 stray whitespace 2026-02-20 03:52:03 +00:00
e44f1041b6 Turns out that creating a thread-in-thread (which is what the stuff I was doing in matrix-command-handler effectively amounted to) KINDA breaks Element. Whoops!
Also, that message in thread-to-announcement was misleading, as now there's no guarantee that a thread was newly created (it could be very old, but freshly /thread-ed). So I changed that, too.

Also, while updating messages, I decided to slightly alter the „may not have been bridged to Discord in the way you thought it was gonna be”-warning in event-dispatcher.
2026-02-20 03:50:06 +00:00
f734b0619f Don't warn the user that they should use /thread if they literally just did it. 2026-02-20 03:10:09 +00:00
b542a81ee1 first time actually interacting with the DB 2026-02-20 02:21:52 +00:00
ac421e6c74 this looks better 2026-02-20 00:00:42 +00:00
7afcbfaa06 I have no idea if this works; just throwing random ideas together.
It's 23:42; I'm going just purely based on vibes at this point.

vibecoding but no AI, just eepy
2026-02-19 22:44:20 +00:00
abe42aaa92 Updated the message to its final form.
At least final-until-we-make-it-so-that-new-rooms-are-autogenerated-when-a-thread-is-opened. Then we'd need to include the link to it instead of a command-help.
2026-02-19 21:26:15 +00:00
aaf8dea104 Reply-related metadata 2026-02-19 19:27:13 +00:00
486959be0b forgor 2026-02-19 19:05:25 +00:00
01b82e7b68 I think I got SOMETHING up and running! 2026-02-19 18:48:24 +00:00
dca53752bb Reverse-engineering the docs 2026-02-19 18:34:30 +00:00
8676a73620 Testing BEGINS! 2026-02-19 17:53:56 +00:00
10099142c2 Merge branch 'main' into fuckery 2026-02-19 17:53:10 +00:00
aedd30ab4a Update "m.relates_to" type definition @ types.d.ts
* To better reflect reality ("m.in_reply_to" will not always be present - it's not (always?) found on "rel_type":"m.replace" relation-events)

* To support "rel_type":"m.replace" relation-events (added "m.replace" option to existing key "rel_type" and a new "is_falling_back" key)

AFFECTED TYPES: M_Room_Message, M_Room_Message_File, M_Room_Message_Encrypted_File

BREAKS: Nothing, as .d.ts files don't affect buisness logic. In terms of lint errors: Marking "m.in_reply_to" as optional is indeed technically a "breaking change" (TypeScript may complain about „is probably undefined” in places where it didn't before), but from early "testing" (ie. looking at VSCode's errors tab), it doesn't seem like anything broke, as no file that imports any of those 3 types (Or their Outer_ counterparts) has „lit up” with errors (unless I missed something). There was one type error found in m2d/converters/event-to-message.js, at line 1009, but that seemed unrelated to types.d.ts - nevertheless, that error was also corrected in this commit, by adding proper type annotations somewhere else in the affected file.
2026-02-19 17:48:32 +00:00
a66b93ed26 Merged my branches 2026-02-19 16:25:03 +00:00
5a853249a2 I prefer 4 spaces 2026-02-19 16:21:27 +00:00
2c7831c587 Small TypeScript coverage expansion
* The guard() function in m2d/event-dispatcher.js no longer takes (any, any), but a string and a function.

* m2d/send-event.js no longer complains that res.body has some missing fields. It would appear as though those missing fields weren't revelant to the fromWeb() function (into which res.body is passed), given that this code worked before and still contunes to work, so I just @ts-ignore'd res.body

This commit's developer's off-topic personal comment, related to this commit: This has nothing to do with improving thread UX, even tho this is what I was supposed to work on. However, in my attempts to discover in what file should I start, I stumbled upon those errors from m2d/send-event.js, so I fixed them. And after establishing that m2d/event-dispatcher.js is the file that I'm looking for, I also noticed that guard()'s @parm definitions could be improved, so I did that. Now - back to thread work...
2026-02-19 16:19:39 +00:00
ae6b730c26 Update .gitignore 2026-02-19 13:03:16 +00:00
12 changed files with 282 additions and 39 deletions

5
.gitignore vendored
View file

@ -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
View 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.

View file

@ -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,

View file

@ -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",
}
})
})

View file

@ -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

View file

@ -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 = []

View file

@ -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)
}))

View file

@ -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

View file

@ -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

View file

@ -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
View file

@ -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"
}
}

View file

@ -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),