forked from cadence/out-of-your-element
Comparison of all the stuff I did. #6
4 changed files with 36 additions and 10 deletions
|
|
@ -222,7 +222,7 @@ async event => {
|
|||
)
|
||||
}
|
||||
if (event.content["m.relates_to"]?.rel_type === "m.thread" && executedCommand !== "thread"){
|
||||
const bridgedTo = utils.getThreadRoomFromThreadEvent(event.event_id)
|
||||
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",
|
||||
|
|
|
|||
|
|
@ -297,8 +297,6 @@ const commands = [{
|
|||
const branchedFromDiscordMessage = select("event_message", "message_id", {event_id: branchedFromMxEvent}).pluck().get()
|
||||
|
||||
if (words.length < 2){
|
||||
words[1] = (await api.getEvent(event.room_id, branchedFromMxEvent)).content.body
|
||||
words[1] = words[1].length < 100 ? words[1] : words[1].slice(0, 96) + "..."
|
||||
if (isFallingBack) return api.sendEvent(event.room_id, "m.room.message", {
|
||||
...ctx,
|
||||
msgtype: "m.text",
|
||||
|
|
@ -306,6 +304,8 @@ const commands = [{
|
|||
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
|
||||
words[1] = words[1].length < 100 ? words[1] : words[1].slice(0, 96) + "..."
|
||||
}
|
||||
|
||||
try {
|
||||
|
|
@ -321,28 +321,40 @@ const commands = [{
|
|||
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
|
||||
const thread = mxUtils.getThreadRoomFromThreadEvent(event.event_id)
|
||||
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 exist 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 (or have someone send a message in that thread on Discord side to bridge it automatically, if that's enabled for this space/server).")
|
||||
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 (or have someone send a message in that thread on Discord side to bridge it automatically, if that's enabled for this space/server) by manually creating the bridge. (If you're said admin and you can see that said bridge already exists, but this 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 channel, if the command was ran inside a thread) for details."
|
||||
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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -399,12 +399,11 @@ async function setUserPowerCascade(spaceID, mxid, power, api) {
|
|||
}
|
||||
|
||||
/**
|
||||
* @param {string} eventID
|
||||
*/
|
||||
* @param {undefined|string?} eventID
|
||||
*/ //^For some reason, „?” doesn't include Undefined and it needs to be explicitly specified
|
||||
function getThreadRoomFromThreadEvent(eventID){
|
||||
console.log("searching for: "+eventID)
|
||||
if (!eventID) return eventID;
|
||||
const threadID = select("event_message", "message_id", {event_id: eventID}).pluck().get() //Discord thread ID === its message ID
|
||||
console.log("matched to: "+threadID)
|
||||
if (!threadID) return threadID;
|
||||
return select("channel_room", "room_id", {channel_id: threadID}).pluck().get()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -436,4 +436,19 @@ test("getThreadRoomFromThreadEvent: fake message", t => {
|
|||
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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue