WIP: feature: threads'n'forums #74
17 changed files with 358 additions and 58 deletions
5
.gitignore
vendored
5
.gitignore
vendored
|
|
@ -1,17 +1,18 @@
|
||||||
# Secrets
|
# Personal
|
||||||
config.js
|
config.js
|
||||||
registration.yaml
|
registration.yaml
|
||||||
ooye.db*
|
ooye.db*
|
||||||
events.db*
|
events.db*
|
||||||
backfill.db*
|
backfill.db*
|
||||||
custom-webroot
|
custom-webroot
|
||||||
|
icon.svg
|
||||||
|
.devcontainer
|
||||||
|
|
||||||
# Automatically generated
|
# Automatically generated
|
||||||
node_modules
|
node_modules
|
||||||
coverage
|
coverage
|
||||||
test/res/*
|
test/res/*
|
||||||
!test/res/lottie*
|
!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.
|
||||||
|
|
@ -54,7 +54,7 @@ function convertNameAndTopic(channel, guild, customName) {
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
const parentChannel = discord.channels.get(channel.parent_id)
|
const parentChannel = discord.channels.get(channel.parent_id)
|
||||||
let channelPrefix =
|
let channelPrefix =
|
||||||
( parentChannel?.type === DiscordTypes.ChannelType.GuildForum ? ""
|
( parentChannel?.type === DiscordTypes.ChannelType.GuildForum ? "[❓] "
|
||||||
: channel.type === DiscordTypes.ChannelType.PublicThread ? "[⛓️] "
|
: channel.type === DiscordTypes.ChannelType.PublicThread ? "[⛓️] "
|
||||||
: channel.type === DiscordTypes.ChannelType.AnnouncementThread ? "[⛓️] "
|
: channel.type === DiscordTypes.ChannelType.AnnouncementThread ? "[⛓️] "
|
||||||
: channel.type === DiscordTypes.ChannelType.PrivateThread ? "[🔒⛓️] "
|
: channel.type === DiscordTypes.ChannelType.PrivateThread ? "[🔒⛓️] "
|
||||||
|
|
@ -65,10 +65,11 @@ function convertNameAndTopic(channel, guild, customName) {
|
||||||
const maybeTopicWithNewlines = channel.topic ? `${channel.topic}\n\n` : '';
|
const maybeTopicWithNewlines = channel.topic ? `${channel.topic}\n\n` : '';
|
||||||
const channelIDPart = `Channel ID: ${channel.id}`;
|
const channelIDPart = `Channel ID: ${channel.id}`;
|
||||||
const guildIDPart = `Guild ID: ${guild.id}`;
|
const guildIDPart = `Guild ID: ${guild.id}`;
|
||||||
|
const maybeThreadWithinPart = parentChannel ? `Thread within: ${parentChannel.name} (ID: ${parentChannel.id})\n` : '';
|
||||||
|
|
||||||
const convertedTopic = customName
|
const convertedTopic = customName
|
||||||
? `#${channel.name}${maybeTopicWithPipe}\n\n${channelIDPart}\n${guildIDPart}`
|
? `#${channel.name}${maybeTopicWithPipe}\n\n${channelIDPart}\n${maybeThreadWithinPart}${guildIDPart}`
|
||||||
: `${maybeTopicWithNewlines}${channelIDPart}\n${guildIDPart}`;
|
: `${maybeTopicWithNewlines}${channelIDPart}\n${maybeThreadWithinPart}${guildIDPart}`;
|
||||||
|
|
||||||
return [chosenName, convertedTopic];
|
return [chosenName, convertedTopic];
|
||||||
}
|
}
|
||||||
|
|
@ -194,7 +195,7 @@ async function channelToKState(channel, guild, di) {
|
||||||
if (hasCustomTopic) delete channelKState["m.room.topic/"]
|
if (hasCustomTopic) delete channelKState["m.room.topic/"]
|
||||||
|
|
||||||
// Make voice channels be a Matrix voice room (MSC3417)
|
// Make voice channels be a Matrix voice room (MSC3417)
|
||||||
if (channel.type === DiscordTypes.ChannelType.GuildVoice) {
|
if (channel.type === DiscordTypes.ChannelType.GuildVoice || channel.type === DiscordTypes.ChannelType.GuildStageVoice) {
|
||||||
creationContent.type = "org.matrix.msc3417.call"
|
creationContent.type = "org.matrix.msc3417.call"
|
||||||
channelKState["org.matrix.msc3401.call/"] = {
|
channelKState["org.matrix.msc3401.call/"] = {
|
||||||
"m.intent": "m.room",
|
"m.intent": "m.room",
|
||||||
|
|
@ -439,12 +440,12 @@ async function _syncRoom(channelID, shouldActuallySync) {
|
||||||
return roomID
|
return roomID
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Ensures the room exists. If it doesn't, creates the room with an accurate initial state. Please check that a channel_room entry exists or guild autocreate = 1 before calling this. */
|
/** Ensures the room exists. If it doesn't, creates the room with an accurate initial state. Before calling this, please make sure that: a channel_room entry exists, guild autocreate = 1, or you're operating on a thread.*/
|
||||||
function ensureRoom(channelID) {
|
function ensureRoom(channelID) {
|
||||||
return _syncRoom(channelID, false)
|
return _syncRoom(channelID, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Actually syncs. Gets all room state from the homeserver in order to diff, and uploads the icon to mxc if it has changed. Please check that a channel_room entry exists or guild autocreate = 1 before calling this. */
|
/** Actually syncs. Gets all room state from the homeserver in order to diff, and uploads the icon to mxc if it has changed. Before calling this, please make sure that: a channel_room entry exists, guild autocreate = 1, or you're operating on a thread.*/
|
||||||
function syncRoom(channelID) {
|
function syncRoom(channelID) {
|
||||||
return _syncRoom(channelID, true)
|
return _syncRoom(channelID, true)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,7 @@
|
||||||
// @ts-check
|
// @ts-check
|
||||||
|
|
||||||
const assert = require("assert").strict
|
|
||||||
|
|
||||||
const passthrough = require("../../passthrough")
|
const passthrough = require("../../passthrough")
|
||||||
const {discord, sync, db, select} = passthrough
|
const {sync, select} = passthrough
|
||||||
/** @type {import("../../matrix/utils")} */
|
/** @type {import("../../matrix/utils")} */
|
||||||
const mxUtils = sync.require("../../matrix/utils")
|
const mxUtils = sync.require("../../matrix/utils")
|
||||||
const {reg} = require("../../matrix/read-registration.js")
|
const {reg} = require("../../matrix/read-registration.js")
|
||||||
|
|
@ -19,24 +17,32 @@ const userRegex = reg.namespaces.users.map(u => new RegExp(u.regex))
|
||||||
*/
|
*/
|
||||||
async function threadToAnnouncement(parentRoomID, threadRoomID, creatorMxid, thread, di) {
|
async function threadToAnnouncement(parentRoomID, threadRoomID, creatorMxid, thread, di) {
|
||||||
const branchedFromEventID = select("event_message", "event_id", {message_id: thread.id}).pluck().get()
|
const branchedFromEventID = select("event_message", "event_id", {message_id: thread.id}).pluck().get()
|
||||||
/** @type {{"m.mentions"?: any, "m.in_reply_to"?: any}} */
|
const ellieMode = false //See: https://matrix.to/#/!PuFmbgRjaJsAZTdSja:cadence.moe?via=cadence.moe&via=chat.untitledzero.dev&via=guziohub.ovh - TL;DR: Ellie's idea was to not leave any sign of Matrix threads existing, while Guzio preferred that the link to created thread-rooms stay within the Matrix thread (to better approximate Discord UI on compatible clients). This settings-option-but-not-really (it's not meant to be changed by end users unless they know what they're doing) would let Cadence switch between both approaches over some time period, to test the feeling of both, and finally land on whichever UX she prefers best. TODO: Remove this toggle (and make the chosen solution permanent) once those tests conclude.
|
||||||
|
|
||||||
|
/** @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 = {}
|
const context = {}
|
||||||
|
let suffix = ""
|
||||||
if (branchedFromEventID) {
|
if (branchedFromEventID) {
|
||||||
// Need to figure out who sent that event...
|
// Need to figure out who sent that event...
|
||||||
const event = await di.api.getEvent(parentRoomID, branchedFromEventID)
|
const event = await di.api.getEvent(parentRoomID, branchedFromEventID)
|
||||||
|
suffix = "\n[Note: You really should move the conversation to that room, rather than continuing to reply via a Matrix thread. Any messages sent in threads will be DELETED and instead moved to that room by the bot, which makes it the author and strips you from control (edits/deletions) over your own message!]"
|
||||||
context["m.relates_to"] = {"m.in_reply_to": {event_id: event.event_id}}
|
context["m.relates_to"] = {"m.in_reply_to": {event_id: event.event_id}}
|
||||||
if (event.sender && !userRegex.some(rx => event.sender.match(rx))) context["m.mentions"] = {user_ids: [event.sender]}
|
if (event.sender && !userRegex.some(rx => event.sender.match(rx))) context["m.mentions"] = {user_ids: [event.sender]}
|
||||||
|
if (!ellieMode) {
|
||||||
|
//...And actually branch from that event (if configured to do so)
|
||||||
|
suffix = "\n[Note: You really should continue the conversation in that room, rather than in this Matrix thread. Any messages sent here will be DELETED and instead moved there by the bot, which makes it the author and strips you from control (edits/deletions) over your own message!]"
|
||||||
|
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"}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const msgtype = creatorMxid ? "m.emote" : "m.text"
|
const msgtype = creatorMxid ? "m.emote" : "m.text"
|
||||||
const template = creatorMxid ? "started a thread:" : "Thread started:"
|
const template = creatorMxid ? "started a thread called" : "New thread started:"
|
||||||
const via = await mxUtils.getViaServersQuery(threadRoomID, di.api)
|
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 {
|
return {
|
||||||
msgtype,
|
msgtype,
|
||||||
body,
|
body,
|
||||||
"m.mentions": {},
|
|
||||||
...context
|
...context
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -49,8 +49,7 @@ test("thread2announcement: no known creator, no branched from event", async t =>
|
||||||
}, {api: viaApi})
|
}, {api: viaApi})
|
||||||
t.deepEqual(content, {
|
t.deepEqual(content, {
|
||||||
msgtype: "m.text",
|
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,8 +60,7 @@ test("thread2announcement: known creator, no branched from event", async t => {
|
||||||
}, {api: viaApi})
|
}, {api: viaApi})
|
||||||
t.deepEqual(content, {
|
t.deepEqual(content, {
|
||||||
msgtype: "m.emote",
|
msgtype: "m.emote",
|
||||||
body: "started a thread: test thread https://matrix.to/#/!thread?via=cadence.moe&via=matrix.org",
|
body: "started a thread called \"test thread\" in room: https://matrix.to/#/!thread?via=cadence.moe&via=matrix.org",
|
||||||
"m.mentions": {}
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
@ -85,12 +83,14 @@ test("thread2announcement: no known creator, branched from discord event", async
|
||||||
})
|
})
|
||||||
t.deepEqual(content, {
|
t.deepEqual(content, {
|
||||||
msgtype: "m.text",
|
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 really should continue the conversation in that room, rather than in this Matrix thread. Any messages sent here will be DELETED and instead moved there by the bot, which makes it the author and strips you from control (edits/deletions) over your own message!]",
|
||||||
"m.mentions": {},
|
|
||||||
"m.relates_to": {
|
"m.relates_to": {
|
||||||
|
"event_id": "$X16nfVks1wsrhq4E9SSLiqrf2N8KD0erD0scZG7U5xg",
|
||||||
|
"is_falling_back": false,
|
||||||
"m.in_reply_to": {
|
"m.in_reply_to": {
|
||||||
event_id: "$X16nfVks1wsrhq4E9SSLiqrf2N8KD0erD0scZG7U5xg"
|
"event_id": "$X16nfVks1wsrhq4E9SSLiqrf2N8KD0erD0scZG7U5xg",
|
||||||
}
|
},
|
||||||
|
"rel_type": "m.thread",
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
@ -114,12 +114,14 @@ test("thread2announcement: known creator, branched from discord event", async t
|
||||||
})
|
})
|
||||||
t.deepEqual(content, {
|
t.deepEqual(content, {
|
||||||
msgtype: "m.emote",
|
msgtype: "m.emote",
|
||||||
body: "started a thread: test thread https://matrix.to/#/!thread?via=cadence.moe&via=matrix.org",
|
body: "started a thread called \"test thread\" in room: https://matrix.to/#/!thread?via=cadence.moe&via=matrix.org\n[Note: You really should continue the conversation in that room, rather than in this Matrix thread. Any messages sent here will be DELETED and instead moved there by the bot, which makes it the author and strips you from control (edits/deletions) over your own message!]",
|
||||||
"m.mentions": {},
|
|
||||||
"m.relates_to": {
|
"m.relates_to": {
|
||||||
|
"event_id": "$X16nfVks1wsrhq4E9SSLiqrf2N8KD0erD0scZG7U5xg",
|
||||||
|
"is_falling_back": false,
|
||||||
"m.in_reply_to": {
|
"m.in_reply_to": {
|
||||||
event_id: "$X16nfVks1wsrhq4E9SSLiqrf2N8KD0erD0scZG7U5xg"
|
"event_id": "$X16nfVks1wsrhq4E9SSLiqrf2N8KD0erD0scZG7U5xg",
|
||||||
}
|
},
|
||||||
|
"rel_type": "m.thread",
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
@ -143,14 +145,51 @@ test("thread2announcement: no known creator, branched from matrix event", async
|
||||||
})
|
})
|
||||||
t.deepEqual(content, {
|
t.deepEqual(content, {
|
||||||
msgtype: "m.text",
|
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 really should continue the conversation in that room, rather than in this Matrix thread. Any messages sent here will be DELETED and instead moved there by the bot, which makes it the author and strips you from control (edits/deletions) over your own message!]",
|
||||||
"m.mentions": {
|
"m.mentions": {
|
||||||
user_ids: ["@cadence:cadence.moe"]
|
user_ids: ["@cadence:cadence.moe"]
|
||||||
},
|
},
|
||||||
"m.relates_to": {
|
"m.relates_to": {
|
||||||
|
"event_id": "$Ij3qo7NxMA4VPexlAiIx2CB9JbsiGhJeyt-2OvkAUe4",
|
||||||
|
"is_falling_back": false,
|
||||||
"m.in_reply_to": {
|
"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 called \"test thread\" in room: https://matrix.to/#/!thread?via=cadence.moe&via=matrix.org\n[Note: You really should continue the conversation in that room, rather than in this Matrix thread. Any messages sent here will be DELETED and instead moved there by the bot, which makes it the author and strips you from control (edits/deletions) over your own message!]",
|
||||||
|
"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",
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
|
||||||
|
|
@ -212,7 +212,7 @@ module.exports = {
|
||||||
const channelID = thread.parent_id || undefined
|
const channelID = thread.parent_id || undefined
|
||||||
const parentRoomID = select("channel_room", "room_id", {channel_id: channelID}).pluck().get()
|
const parentRoomID = select("channel_room", "room_id", {channel_id: channelID}).pluck().get()
|
||||||
if (!parentRoomID) return // Not interested in a thread if we aren't interested in its wider channel (won't autocreate)
|
if (!parentRoomID) return // Not interested in a thread if we aren't interested in its wider channel (won't autocreate)
|
||||||
const threadRoomID = await createRoom.syncRoom(thread.id) // Create room (will share the same inflight as the initial message to the thread)
|
const threadRoomID = await createRoom.ensureRoom(thread.id)
|
||||||
await announceThread.announceThread(parentRoomID, threadRoomID, thread)
|
await announceThread.announceThread(parentRoomID, threadRoomID, thread)
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -39,14 +39,20 @@ async function resolvePendingFiles(message) {
|
||||||
if ("key" in p) {
|
if ("key" in p) {
|
||||||
// Encrypted file
|
// Encrypted file
|
||||||
const d = crypto.createDecipheriv("aes-256-ctr", Buffer.from(p.key, "base64url"), Buffer.from(p.iv, "base64url"))
|
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 {
|
return {
|
||||||
name: p.name,
|
name: p.name,
|
||||||
file: d
|
file: d
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Unencrypted file
|
// 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 {
|
return {
|
||||||
name: p.name,
|
name: p.name,
|
||||||
file: body
|
file: body
|
||||||
|
|
|
||||||
|
|
@ -471,6 +471,7 @@ async function checkWrittenMentions(content, senderMxid, roomID, guild, di) {
|
||||||
// @ts-ignore - typescript doesn't know about indices yet
|
// @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]),
|
content: content.slice(0, writtenMentionMatch.indices[1][0]-1) + `@everyone` + content.slice(writtenMentionMatch.indices[1][1]),
|
||||||
ensureJoined: [],
|
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"],
|
allowedMentionsParse: ["everyone"],
|
||||||
allowedMentionsUsers: []
|
allowedMentionsUsers: []
|
||||||
}
|
}
|
||||||
|
|
@ -545,6 +546,7 @@ async function getL1L2ReplyLine(called = false) {
|
||||||
async function eventToMessage(event, guild, channel, di) {
|
async function eventToMessage(event, guild, channel, di) {
|
||||||
let displayName = event.sender
|
let displayName = event.sender
|
||||||
let avatarURL = undefined
|
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"]
|
const allowedMentionsParse = ["users", "roles"]
|
||||||
const allowedMentionsUsers = []
|
const allowedMentionsUsers = []
|
||||||
/** @type {string[]} */
|
/** @type {string[]} */
|
||||||
|
|
|
||||||
106
src/m2d/converters/threads-and-forums.js
Normal file
106
src/m2d/converters/threads-and-forums.js
Normal file
|
|
@ -0,0 +1,106 @@
|
||||||
|
//@ts-check
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Misc. utils for transforming various Matrix events (eg. those sent in Forum-bridged channels; those sent) so that they're usable as threads, and for creating said threads.
|
||||||
|
*/
|
||||||
|
|
||||||
|
const Ty = require("../../types")
|
||||||
|
const {discord, sync, select, from} = require("../../passthrough")
|
||||||
|
const DiscordTypes = require("discord-api-types/v10")
|
||||||
|
|
||||||
|
/** @type {import("../../matrix/api")}) */
|
||||||
|
const api = sync.require("../../matrix/api")
|
||||||
|
/** @type {import("../../d2m/actions/create-room")} */
|
||||||
|
const createRoom = sync.require("../../d2m/actions/create-room")
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {Ty.Event.Outer_M_Room_Message | Ty.Event.Outer_M_Room_Message_File} event Used for determining the branching-point and the title; any relation data will be stripped and its room_id will mutate to the target thread-room, if one gets created.
|
||||||
|
* @returns {Promise<boolean>} whether a thread-room was created
|
||||||
|
*/
|
||||||
|
async function bridgeThread(event) {
|
||||||
|
/** @type {string} */ // @ts-ignore
|
||||||
|
const channelID = select("channel_room", "channel_id", {room_id: event.room_id}).pluck().get()
|
||||||
|
const channel = discord.channels.get(channelID)
|
||||||
|
const guildID = channel?.["guild_id"]
|
||||||
|
if (!guildID) return false; //Room not bridged? We don't care. It's a Matrix-native room, let Matrix users have standard Matrix-native threads there.
|
||||||
|
|
||||||
|
const threadEventID = event.content["m.relates_to"]?.event_id
|
||||||
|
if (!threadEventID) throw new Error("There was an event sent inside SOME Matrix thread, but it lacked any information as to what thread it actually was!"); //An „ugly error” is justified because if something like this DOES happen, then that means that it should be reported to us, as there is some broken client out there that we should account for.
|
||||||
|
const messageID = select("event_message", "message_id", {event_id: threadEventID}).pluck().get()
|
||||||
|
if (!messageID) return false; //Message not bridged? Too bad! Discord users will just see normal replies, and Matrix uses won't get a thread-room. We COULD technically create a "headless" thread on Discord side and bridge it to a new thread-room, but that comes with a whole host of complications on its own (notably: what do we do if the message gets bridged later (by reaction emoji), and then hypothetically gets its own thread; and: getThreadRoomFromThreadEvent will have to be much more complex than a simple DB call (probably a whole new DB table would have to be created, just to hold these Matrix-branched-but-headless-on-Discord threads) because the simple „MX event --(db)--> Discord Message --(Discord spec)--> Discord thread” relation would no longer hold true), which may not be worth it, as an unbridged message in a bridged channel is already an edge-case (so it seems pointless to introduce a whole bunch of edgier-cases that handling this edge-case "properly" would bring).
|
||||||
|
|
||||||
|
try {
|
||||||
|
event.room_id = await createRoom.ensureRoom((await discord.snow.channel.createThreadWithMessage(channelID, messageID, {name: computeName(event, await api.getEvent(event.room_id, threadEventID)).name})).id)
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
catch (e){
|
||||||
|
if (e.message?.includes("50024")){ //see: https://docs.discord.com/developers/topics/opcodes-and-status-codes
|
||||||
|
api.sendEvent(event.room_id, "m.room.message", {
|
||||||
|
body: "Hey, please don't do that! This room is already a thread on Discord (or it could also be a voice-chat or something adjacent) - trying to embed threads inside it, like you just did, will not work. DC users will just see a regular reply, which is distracting and also probably not what you want.",
|
||||||
|
"m.mentions": { "user_ids": [event.sender]},
|
||||||
|
"m.relates_to": {
|
||||||
|
event_id: threadEventID,
|
||||||
|
is_falling_back: false,
|
||||||
|
"m.in_reply_to": { event_id: event.event_id },
|
||||||
|
rel_type: "m.thread"
|
||||||
|
},
|
||||||
|
msgtype: "m.text"
|
||||||
|
})
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
else throw e
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {Ty.Event.Outer_M_Room_Message | Ty.Event.Outer_M_Room_Message_File} event
|
||||||
|
* @returns {Promise<boolean>} whether a forum-thread-room was created
|
||||||
|
*/
|
||||||
|
async function handleForums(event) {
|
||||||
|
if (event.content.body === "/thread") return false; //Let the help be shown normally
|
||||||
|
|
||||||
|
const row = from("channel_room").where({room_id: event.room_id}).select("channel_id").get()
|
||||||
|
/** @type {string}*/ //@ts-ignore the possibility that it's undefined - get() will return back an undefined if it's fed one (so that's undefined-safe), and createThreadWithoutMessage() won't be reached because "undefined" is neither DiscordTypes.ChannelType.GuildMedia nor DiscordTypes.ChannelType.GuildForum, so the guard clause kicks in.
|
||||||
|
let channelID = row?.channel_id
|
||||||
|
const channel = discord.channels.get(channelID)
|
||||||
|
if (channel?.type != DiscordTypes.ChannelType.GuildForum && channel?.type != DiscordTypes.ChannelType.GuildMedia) return false
|
||||||
|
|
||||||
|
const name = computeName(event)
|
||||||
|
let resetNeeded = false
|
||||||
|
try {
|
||||||
|
if(channel.flags && channel.flags & DiscordTypes.ChannelFlags.RequireTag){
|
||||||
|
await discord.snow.channel.updateChannel(channelID, {flags:(channel.flags^DiscordTypes.ChannelFlags.RequireTag)}, "Temporary override of tagging requirements because Matrix threads that can't be tagged yet.")
|
||||||
|
resetNeeded = true
|
||||||
|
}
|
||||||
|
//@ts-ignore the presence of message:{} and the absence of type:11 - that's intended for threads in Forum channels (*and no, createThreadWithMessage wouldn't work here, either - that one takes an ALREADY EXISTING message ID, whereas this needs a new message)
|
||||||
|
await discord.snow.channel.createThreadWithoutMessage(channelID, {name: name.name, message:{content:"**Created by: `"+ event.sender +"`**"}})
|
||||||
|
if (!name.truncated) api.redactEvent(event.room_id, event.event_id) //Don't destroy people's texts - only remove if no truncation is guaranteed.
|
||||||
|
if (resetNeeded) discord.snow.channel.updateChannel(channelID, {flags:channel.flags}, "Restoring flags to their original state.")
|
||||||
|
}
|
||||||
|
catch (e){
|
||||||
|
if (e.message?.includes("50013")){
|
||||||
|
api.sendEvent(event.room_id, "m.room.message", {
|
||||||
|
body: "You can't create threads in this forum right now! This forum is configured to require tags on post (Matrix users can't yet use tags yet), and OOYE doesn't have the permission to edit this channel on Discord (needed to bypass the requirement of tags). Unless this is intentional (see room description - the admins may have left a note), please ask someone on the Discord side to either grant OOYE the necessary permissions, or to remove tagging requirements.",
|
||||||
|
msgtype: "m.text"
|
||||||
|
})
|
||||||
|
}
|
||||||
|
else throw e
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {Ty.Event.Outer_M_Room_Message | Ty.Event.Outer_M_Room_Message_File} from event
|
||||||
|
* @param {Ty.Event.Outer<any> | null | false | undefined} fallback Reuses the "from" param value if empty.
|
||||||
|
* @returns {{name: string, truncated: boolean}}
|
||||||
|
*/
|
||||||
|
function computeName(from, fallback=null){
|
||||||
|
let name = from.content.body
|
||||||
|
if (name.startsWith("/thread ") && name.length > 8) name = name.substring(8);
|
||||||
|
else name = (fallback ? fallback : from).content.body;
|
||||||
|
return name.length < 100 ? {name: name.replaceAll("\n", " "), truncated: false} : {name: name.slice(0, 96).replaceAll("\n", " ") + "...", truncated: true}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports.handleForums = handleForums
|
||||||
|
module.exports.bridgeThread = bridgeThread
|
||||||
|
|
@ -9,6 +9,7 @@ const Ty = require("../types")
|
||||||
const {discord, db, sync, as, select} = require("../passthrough")
|
const {discord, db, sync, as, select} = require("../passthrough")
|
||||||
const {tag} = require("@cloudrac3r/html-template-tag")
|
const {tag} = require("@cloudrac3r/html-template-tag")
|
||||||
const {Semaphore} = require("@chriscdn/promise-semaphore")
|
const {Semaphore} = require("@chriscdn/promise-semaphore")
|
||||||
|
const { bridgeThread, handleForums } = require("./converters/threads-and-forums")
|
||||||
|
|
||||||
/** @type {import("./actions/send-event")} */
|
/** @type {import("./actions/send-event")} */
|
||||||
const sendEvent = sync.require("./actions/send-event")
|
const sendEvent = sync.require("./actions/send-event")
|
||||||
|
|
@ -156,9 +157,14 @@ async function sendError(roomID, source, type, e, payload) {
|
||||||
} catch (e) {}
|
} 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) {
|
function guard(type, fn) {
|
||||||
return async function(event, ...args) {
|
return async function(/** @type {Ty.Event.Outer<any>} */ event, /** @type {any} */ ...args) {
|
||||||
try {
|
try {
|
||||||
|
// @ts-ignore
|
||||||
return await fn(event, ...args)
|
return await fn(event, ...args)
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
await sendError(event.room_id, "Matrix", type, e, event)
|
await sendError(event.room_id, "Matrix", type, e, event)
|
||||||
|
|
@ -205,12 +211,35 @@ sync.addTemporaryListener(as, "type:m.room.message", guard("m.room.message",
|
||||||
*/
|
*/
|
||||||
async event => {
|
async event => {
|
||||||
if (utils.eventSenderIsFromDiscord(event.sender)) return
|
if (utils.eventSenderIsFromDiscord(event.sender)) return
|
||||||
|
if (await handleForums(event)) return
|
||||||
|
|
||||||
|
let processCommands = true
|
||||||
|
if (event.content["m.relates_to"]?.rel_type === "m.thread") {
|
||||||
|
/**@type {string|null} */
|
||||||
|
let toRedact = event.room_id
|
||||||
|
const bridgedTo = utils.getThreadRoomFromThreadEvent(event.content["m.relates_to"].event_id)
|
||||||
|
processCommands = false
|
||||||
|
|
||||||
|
if (bridgedTo) event.room_id = bridgedTo;
|
||||||
|
else if (!await bridgeThread(event)) toRedact = null; //Don't remove anything, if there is nowhere to relocate it to.
|
||||||
|
|
||||||
|
if (toRedact) {
|
||||||
|
api.redactEvent(toRedact, event.event_id)
|
||||||
|
event.content["m.relates_to"] = undefined
|
||||||
|
api.sendEvent(event.room_id, event.type, {...event.content, body: event.content.body+"\n ~ "+event.sender, formatted_body: event.content.formatted_body ? event.content.formatted_body+"<br> ~ "+event.sender :undefined })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const messageResponses = await sendEvent.sendEvent(event)
|
const messageResponses = await sendEvent.sendEvent(event)
|
||||||
if (!messageResponses.length) return
|
if (!messageResponses.length) return
|
||||||
if (event.type === "m.room.message" && event.content.msgtype === "m.text") {
|
|
||||||
// @ts-ignore
|
if (event.type === "m.room.message" && event.content.msgtype === "m.text" && processCommands) {
|
||||||
await matrixCommandHandler.execute(event)
|
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
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
retrigger.messageFinishedBridging(event.event_id)
|
retrigger.messageFinishedBridging(event.event_id)
|
||||||
await api.ackEvent(event)
|
await api.ackEvent(event)
|
||||||
}))
|
}))
|
||||||
|
|
|
||||||
|
|
@ -262,7 +262,64 @@ const commands = [{
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
await discord.snow.channel.createThreadWithoutMessage(channelID, {type: 11, name: words.slice(1).join(" ")})
|
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()
|
||||||
|
|
||||||
|
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. 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) return await discord.snow.channel.createThreadWithMessage(channelID, branchedFromDiscordMessage, {name: words.slice(1).join(" ")}) //can't just return the promise directly like in 99% of other cases here in commands, otherwise the error-handling below will not work
|
||||||
|
else {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 `"+branchedFromMxEvent+"` 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>"+branchedFromMxEvent+"</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."
|
||||||
|
})};
|
||||||
|
}
|
||||||
|
catch (e){
|
||||||
|
/**@type {string|undefined} */
|
||||||
|
let err = e.message // see: https://docs.discord.com/developers/topics/opcodes-and-status-codes
|
||||||
|
|
||||||
|
if (err?.includes("160004")) {
|
||||||
|
if (isFallingBack) throw e; //Discord claims that there already exists a thread for the message ran this command was ran on, but that doesn't make logical sense, as it doesn't seem like it was ran on any message. Either the Matrix client did something funny with reply/thread tags, or this is a logic error on our side. At any rate, this should be reported to OOYE for further investigation, which the user should do when encountering an „ugly error” (if they follow the „every error should be reported” directive), so this is re-thrown as-is (no stacktrace-breaking exception wrapping) to be turned into such an „ugly error” upstream.
|
||||||
|
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.)")
|
||||||
|
})
|
||||||
|
}
|
||||||
|
if (err?.includes("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. It could be something like a VC, or perhaps... Did you try to embed a thread inside a thread, silly?"
|
||||||
|
})
|
||||||
|
if (err?.includes("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. ;)"
|
||||||
|
})
|
||||||
|
|
||||||
|
throw e //Some other error happened, one that OOYE didn't anticipate the possibility of? It should be reported to us for further investigation, which the user should do when encountering an „ugly error” (if they follow the „every error should be reported” directive), so this is re-thrown as-is (no stacktrace-breaking exception wrapping) to be turned into such an „ugly error” upstream.
|
||||||
|
}
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
}, {
|
}, {
|
||||||
|
|
@ -321,8 +378,11 @@ const commands = [{
|
||||||
}]
|
}]
|
||||||
|
|
||||||
|
|
||||||
/** @type {CommandExecute} */
|
/**
|
||||||
async function execute(event) {
|
* @param {Ty.Event.Outer_M_Room_Message} event
|
||||||
|
* @returns {Promise<any>|undefined} the executed command's in-process promise or undefined if no command execution was performed
|
||||||
|
*/
|
||||||
|
function parseAndExecute(event) {
|
||||||
let realBody = event.content.body
|
let realBody = event.content.body
|
||||||
while (realBody.startsWith("> ")) {
|
while (realBody.startsWith("> ")) {
|
||||||
const i = realBody.indexOf("\n")
|
const i = realBody.indexOf("\n")
|
||||||
|
|
@ -342,8 +402,8 @@ async function execute(event) {
|
||||||
const command = commands.find(c => c.aliases.includes(commandName))
|
const command = commands.find(c => c.aliases.includes(commandName))
|
||||||
if (!command) return
|
if (!command) return
|
||||||
|
|
||||||
await command.execute(event, realBody, words)
|
return command.execute(event, realBody, words)
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports.execute = execute
|
module.exports.parseAndExecute = parseAndExecute
|
||||||
module.exports.onReactionAdd = onReactionAdd
|
module.exports.onReactionAdd = onReactionAdd
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@ const assert = require("assert").strict
|
||||||
const Ty = require("../types")
|
const Ty = require("../types")
|
||||||
const {tag} = require("@cloudrac3r/html-template-tag")
|
const {tag} = require("@cloudrac3r/html-template-tag")
|
||||||
const passthrough = require("../passthrough")
|
const passthrough = require("../passthrough")
|
||||||
const {db} = passthrough
|
const {db, select} = passthrough
|
||||||
|
|
||||||
const {reg} = require("./read-registration")
|
const {reg} = require("./read-registration")
|
||||||
const userRegex = reg.namespaces.users.map(u => new RegExp(u.regex))
|
const userRegex = reg.namespaces.users.map(u => new RegExp(u.regex))
|
||||||
|
|
@ -385,6 +385,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.bot = bot
|
||||||
module.exports.BLOCK_ELEMENTS = BLOCK_ELEMENTS
|
module.exports.BLOCK_ELEMENTS = BLOCK_ELEMENTS
|
||||||
module.exports.eventSenderIsFromDiscord = eventSenderIsFromDiscord
|
module.exports.eventSenderIsFromDiscord = eventSenderIsFromDiscord
|
||||||
|
|
@ -400,3 +410,4 @@ module.exports.removeCreatorsFromPowerLevels = removeCreatorsFromPowerLevels
|
||||||
module.exports.getEffectivePower = getEffectivePower
|
module.exports.getEffectivePower = getEffectivePower
|
||||||
module.exports.setUserPower = setUserPower
|
module.exports.setUserPower = setUserPower
|
||||||
module.exports.setUserPowerCascade = setUserPowerCascade
|
module.exports.setUserPowerCascade = setUserPowerCascade
|
||||||
|
module.exports.getThreadRoomFromThreadEvent = getThreadRoomFromThreadEvent
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
const {select} = require("../passthrough")
|
const {select} = require("../passthrough")
|
||||||
const {test} = require("supertape")
|
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")
|
const util = require("util")
|
||||||
|
|
||||||
/** @param {string[]} mxids */
|
/** @param {string[]} mxids */
|
||||||
|
|
@ -417,4 +417,23 @@ test("set user power: privileged users must demote themselves", async t => {
|
||||||
t.equal(called, 3)
|
t.equal(called, 3)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
test("getThreadRoomFromThreadEvent: real message with a thread", t => {
|
||||||
|
const room = getThreadRoomFromThreadEvent("$fdD9o7NxMA4VPexlAiIx2CB9JbsiGhJeyJgnZG7U5xg")
|
||||||
|
t.equal(room, "!FuDZhlOAtqswlyxzeR:cadence.moe")
|
||||||
|
})
|
||||||
|
|
||||||
|
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: fake message", t => {
|
||||||
|
const room = getThreadRoomFromThreadEvent("$ThisEvent-IdDoesNotExistInTheDatabase4Sure")
|
||||||
|
const msg = "Expected null/undefined, got: "+room
|
||||||
|
if(room) t.fail(msg);
|
||||||
|
else t.pass(msg)
|
||||||
|
})
|
||||||
|
|
||||||
module.exports.mockGetEffectivePower = mockGetEffectivePower
|
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"
|
format?: "org.matrix.custom.html"
|
||||||
formatted_body?: string,
|
formatted_body?: string,
|
||||||
"m.relates_to"?: {
|
"m.relates_to"?: {
|
||||||
"m.in_reply_to": {
|
event_id?: string
|
||||||
|
is_falling_back?: boolean
|
||||||
|
"m.in_reply_to"?: {
|
||||||
event_id: string
|
event_id: string
|
||||||
}
|
}
|
||||||
rel_type?: "m.replace"
|
rel_type?: "m.replace"|"m.thread"
|
||||||
event_id?: string
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -210,11 +211,12 @@ export namespace Event {
|
||||||
info?: any
|
info?: any
|
||||||
"page.codeberg.everypizza.msc4193.spoiler"?: boolean
|
"page.codeberg.everypizza.msc4193.spoiler"?: boolean
|
||||||
"m.relates_to"?: {
|
"m.relates_to"?: {
|
||||||
"m.in_reply_to": {
|
event_id?: string
|
||||||
|
is_falling_back?: boolean
|
||||||
|
"m.in_reply_to"?: {
|
||||||
event_id: string
|
event_id: string
|
||||||
}
|
}
|
||||||
rel_type?: "m.replace"
|
rel_type?: "m.replace"|"m.thread"
|
||||||
event_id?: string
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -246,11 +248,12 @@ export namespace Event {
|
||||||
},
|
},
|
||||||
info?: any
|
info?: any
|
||||||
"m.relates_to"?: {
|
"m.relates_to"?: {
|
||||||
"m.in_reply_to": {
|
event_id?: string
|
||||||
|
is_falling_back?: boolean
|
||||||
|
"m.in_reply_to"?: {
|
||||||
event_id: string
|
event_id: string
|
||||||
}
|
}
|
||||||
rel_type?: "m.replace"
|
rel_type?: "m.replace"|"m.thread"
|
||||||
event_id?: string
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -112,7 +112,7 @@ function getChannelRoomsLinks(guild, rooms, roles) {
|
||||||
let unlinkedChannelIDs = channelIDs.filter(c => !linkedChannelIDs.includes(c))
|
let unlinkedChannelIDs = channelIDs.filter(c => !linkedChannelIDs.includes(c))
|
||||||
/** @type {DiscordTypes.APIGuildChannel[]} */ // @ts-ignore
|
/** @type {DiscordTypes.APIGuildChannel[]} */ // @ts-ignore
|
||||||
let unlinkedChannels = unlinkedChannelIDs.map(c => discord.channels.get(c))
|
let unlinkedChannels = unlinkedChannelIDs.map(c => discord.channels.get(c))
|
||||||
let removedWrongTypeChannels = dUtils.filterTo(unlinkedChannels, c => c && [0, 5].includes(c.type))
|
let removedWrongTypeChannels = dUtils.filterTo(unlinkedChannels, c => c && [0, 2, 5, 13, 15, 16].includes(c.type))
|
||||||
let removedPrivateChannels = dUtils.filterTo(unlinkedChannels, c => {
|
let removedPrivateChannels = dUtils.filterTo(unlinkedChannels, c => {
|
||||||
const permissions = dUtils.getPermissions(guild.id, roles, guild.roles, botID, c["permission_overwrites"])
|
const permissions = dUtils.getPermissions(guild.id, roles, guild.roles, botID, c["permission_overwrites"])
|
||||||
return dUtils.hasSomePermissions(permissions, ["Administrator", "ViewChannel"])
|
return dUtils.hasSomePermissions(permissions, ["Administrator", "ViewChannel"])
|
||||||
|
|
@ -122,7 +122,7 @@ function getChannelRoomsLinks(guild, rooms, roles) {
|
||||||
let linkedRoomIDs = linkedChannels.map(c => c.room_id)
|
let linkedRoomIDs = linkedChannels.map(c => c.room_id)
|
||||||
let unlinkedRooms = [...rooms]
|
let unlinkedRooms = [...rooms]
|
||||||
let removedLinkedRooms = dUtils.filterTo(unlinkedRooms, r => !linkedRoomIDs.includes(r.room_id))
|
let removedLinkedRooms = dUtils.filterTo(unlinkedRooms, r => !linkedRoomIDs.includes(r.room_id))
|
||||||
let removedWrongTypeRooms = dUtils.filterTo(unlinkedRooms, r => !r.room_type)
|
let removedWrongTypeRooms = dUtils.filterTo(unlinkedRooms, r => !(r.room_type && r.room_type === "m.space"))
|
||||||
let removedEncryptedRooms = dUtils.filterTo(unlinkedRooms, r => !r.encryption && !r["im.nheko.summary.encryption"])
|
let removedEncryptedRooms = dUtils.filterTo(unlinkedRooms, r => !r.encryption && !r["im.nheko.summary.encryption"])
|
||||||
// https://discord.com/developers/docs/topics/threads#active-archived-threads
|
// https://discord.com/developers/docs/topics/threads#active-archived-threads
|
||||||
// need to filter out linked archived threads from unlinkedRooms, will just do that by comparing against the name
|
// need to filter out linked archived threads from unlinkedRooms, will just do that by comparing against the name
|
||||||
|
|
|
||||||
|
|
@ -173,8 +173,9 @@ as.router.post("/api/link", defineEventHandler(async event => {
|
||||||
const row = from("channel_room").select("channel_id", "room_id").and("WHERE channel_id = ? OR room_id = ?").get(channel.id, parsedBody.matrix)
|
const row = from("channel_room").select("channel_id", "room_id").and("WHERE channel_id = ? OR room_id = ?").get(channel.id, parsedBody.matrix)
|
||||||
if (row) throw createError({status: 400, message: "Bad Request", data: `Channel ID ${row.channel_id} or room ID ${parsedBody.matrix} are already bridged and cannot be reused`})
|
if (row) throw createError({status: 400, message: "Bad Request", data: `Channel ID ${row.channel_id} or room ID ${parsedBody.matrix} are already bridged and cannot be reused`})
|
||||||
|
|
||||||
// Check room is part of the guild's space
|
// Check room is an actual room (not space) and is part of the guild's space
|
||||||
let foundRoom = false
|
let foundRoom = false
|
||||||
|
let foundSpace = false
|
||||||
/** @type {string[]?} */
|
/** @type {string[]?} */
|
||||||
let foundVia = null
|
let foundVia = null
|
||||||
for await (const room of api.generateFullHierarchy(spaceID)) {
|
for await (const room of api.generateFullHierarchy(spaceID)) {
|
||||||
|
|
@ -186,13 +187,18 @@ as.router.post("/api/link", defineEventHandler(async event => {
|
||||||
}
|
}
|
||||||
|
|
||||||
// When finding a room during iteration, see if it was the requested room (to confirm that the room is in the space)
|
// When finding a room during iteration, see if it was the requested room (to confirm that the room is in the space)
|
||||||
if (room.room_id === parsedBody.matrix && !room.room_type) {
|
if (room.room_id === parsedBody.matrix) {
|
||||||
foundRoom = true
|
foundRoom = true
|
||||||
}
|
}
|
||||||
|
|
||||||
if (foundRoom && foundVia) break
|
if (foundRoom && foundVia) break
|
||||||
|
|
||||||
|
if (room.room_type && room.room_type === "m.space") {
|
||||||
|
foundSpace = true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (!foundRoom) throw createError({status: 400, message: "Bad Request", data: "Matrix room needs to be part of the bridged space"})
|
if (!foundRoom) throw createError({status: 400, message: "Bad Request", data: "Matrix room needs to be part of the bridged space"})
|
||||||
|
else if (!foundRoom) throw createError({status: 400, message: "Bad Request", data: "Matrix room cannot be of type m.space"})
|
||||||
|
|
||||||
// Check room exists and bridge is joined
|
// Check room exists and bridge is joined
|
||||||
try {
|
try {
|
||||||
|
|
|
||||||
|
|
@ -95,12 +95,14 @@ WITH a (message_id, channel_id) AS (VALUES
|
||||||
('1381212840957972480', '112760669178241024'),
|
('1381212840957972480', '112760669178241024'),
|
||||||
('1401760355339862066', '112760669178241024'),
|
('1401760355339862066', '112760669178241024'),
|
||||||
('1439351590262800565', '1438284564815548418'),
|
('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;
|
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
|
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),
|
('$X16nfVks1wsrhq4E9SSLiqrf2N8KD0erD0scZG7U5xg', 'm.room.message', 'm.text', '1126786462646550579', 0, 0, 1),
|
||||||
('$Ij3qo7NxMA4VPexlAiIx2CB9JbsiGhJeyt-2OvkAUe4', 'm.room.message', 'm.text', '1128118177155526666', 0, 0, 0),
|
('$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),
|
('$zXSlyI78DQqQwwfPUSzZ1b-nXzbUrCDljJgnGDdoI10', 'm.room.message', 'm.text', '1141619794500649020', 0, 0, 1),
|
||||||
('$fdD9OZ55xg3EAsfvLZza5tMhtjUO91Wg3Otuo96TplY', 'm.room.message', 'm.text', '1141206225632112650', 0, 0, 1),
|
('$fdD9OZ55xg3EAsfvLZza5tMhtjUO91Wg3Otuo96TplY', 'm.room.message', 'm.text', '1141206225632112650', 0, 0, 1),
|
||||||
('$mtR8cJqM4fKno1bVsm8F4wUVqSntt2sq6jav1lyavuA', 'm.room.message', 'm.text', '1141501302736695316', 0, 1, 1),
|
('$mtR8cJqM4fKno1bVsm8F4wUVqSntt2sq6jav1lyavuA', 'm.room.message', 'm.text', '1141501302736695316', 0, 1, 1),
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue