forked from cadence/out-of-your-element
Comparison of all the stuff I did. #6
5 changed files with 94 additions and 31 deletions
|
|
@ -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",
|
||||
}
|
||||
})
|
||||
})
|
||||
|
|
|
|||
|
|
@ -288,47 +288,47 @@ const commands = [{
|
|||
|
||||
const relation = event.content["m.relates_to"]
|
||||
let isFallingBack = false;
|
||||
let attachedToEvent = relation?.["m.in_reply_to"]?.event_id // By default, attempt to attach the thread to the message to which /thread was replying.
|
||||
if (relation?.rel_type === "m.thread") attachedToEvent = relation?.event_id // If /thread was sent inside a Matrix thread, attempt to attach the Discord thread to the message around which that Matrix thread was based on.
|
||||
if (!attachedToEvent){
|
||||
attachedToEvent = event.event_id // If /thread wasn't replying to anything (ie. attachedToEvent was undefined at initial assignment), or if the event was somehow malformed (in such a way that that - one way or another - attachedToEvent ended up being undefined, even if according to the spec it shouldn't), attach the thread to the /thread command-message that created it.
|
||||
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 attachedToMessage = select("event_message", "message_id", {event_id: attachedToEvent}).pluck().get()
|
||||
const branchedFromDiscordMessage = select("event_message", "message_id", {event_id: branchedFromMxEvent}).pluck().get()
|
||||
|
||||
if (words.length < 2){
|
||||
words[1] = (await api.getEvent(event.room_id, attachedToEvent)).content.body
|
||||
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",
|
||||
"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 under which said thread will be attached, is chosen based on the following rules:\n* If ran stand-alone (not as a reply, nor in a Matrix thread), the created thread gets attached to 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 be attached to the message to which you replied.\n* If ran inside an existing Matrix thread (regardless of whether it's a reply or not), the Discord thread will be attached to the same message as the Matrix thread already is.",
|
||||
"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 under which said thread will be attached, 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 gets attached to 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 be attached to 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 Discord thread will be attached to the same message as the Matrix thread already is.</li></ul>"
|
||||
"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>"
|
||||
})
|
||||
}
|
||||
|
||||
try {
|
||||
if (attachedToMessage) await discord.snow.channel.createThreadWithMessage(channelID, attachedToMessage, {name: words.slice(1).join(" ")})
|
||||
else throw {code: "NO_ATTACH_TARGET", was_supposed_to_be: attachedToEvent};
|
||||
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_raw){
|
||||
const e = unmarshallDiscordError(e_raw)
|
||||
switch (e.code) {
|
||||
case "NO_ATTACH_TARGET": return api.sendEvent(event.room_id, "m.room.message", {
|
||||
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 under which you're trying to attach this thread (event ID `"+e_raw.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.",
|
||||
"body": "⚠️ Couldn't find a Discord representation of the message from which you're trying to branch this thread (event ID `"+e_raw.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 under which you're trying to attach this thread (event ID <code>"+e_raw.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."
|
||||
"formatted_body": "⚠️ Couldn't find a Discord representation of the message from which you're trying to branch this thread (event ID <code>"+e_raw.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
|
||||
return api.sendEvent(event.room_id, "m.room.message", {
|
||||
...ctx,
|
||||
msgtype: "m.text",
|
||||
"body": "There already exist a thread for the message you ran this command on (please run `/thread` (with no arguments and outside any threads) for details on attachment rules, if you're unsure what „ran this command on” refers to); cannot create a new one. Scroll up and see where the link could be.",
|
||||
"body": "There already exist a thread for the message you ran this command on (please run `/thread` (with no arguments and outside any threads) for details on branching rules, if you're unsure what „ran this command on” refers to); cannot create a new one. Scroll up and see where the link could be.",
|
||||
"format": "org.matrix.custom.html",
|
||||
"formatted_body": "There already exist a thread for the message you ran this command on (please run <code>/thread</code> (with no arguments and outside any threads) for details on attachment rules, if you're unsure what „ran this command on” refers to); cannot create a new one. Scroll up and see where the link could be.",
|
||||
"formatted_body": "There already exist a thread for the message you ran this command on (please run <code>/thread</code> (with no arguments and outside any threads) for details on branching rules, if you're unsure what „ran this command on” refers to); cannot create a new one. Scroll up and see where the link could be.",
|
||||
})
|
||||
case (50024): return api.sendEvent(event.room_id, "m.room.message", {
|
||||
...ctx,
|
||||
|
|
|
|||
|
|
@ -399,10 +399,9 @@ async function setUserPowerCascade(spaceID, mxid, power, api) {
|
|||
}
|
||||
|
||||
/**
|
||||
* Set a user's power level for a whole room hierarchy.
|
||||
* @param {string} eventID
|
||||
*/
|
||||
function getThreadRoomFromThreadMessage(eventID){
|
||||
function getThreadRoomFromThreadEvent(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()
|
||||
|
|
@ -423,4 +422,4 @@ module.exports.removeCreatorsFromPowerLevels = removeCreatorsFromPowerLevels
|
|||
module.exports.getEffectivePower = getEffectivePower
|
||||
module.exports.setUserPower = setUserPower
|
||||
module.exports.setUserPowerCascade = setUserPowerCascade
|
||||
module.exports.getThreadRoomFromThreadMessage = getThreadRoomFromThreadMessage
|
||||
module.exports.getThreadRoomFromThreadEvent = getThreadRoomFromThreadEvent
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
const {select} = require("../passthrough")
|
||||
const {test} = require("supertape")
|
||||
const {eventSenderIsFromDiscord, getEventIDHash, MatrixStringBuilder, getViaServers, roomHasAtLeastVersion, removeCreatorsFromPowerLevels, setUserPower} = require("./utils")
|
||||
const {eventSenderIsFromDiscord, getEventIDHash, MatrixStringBuilder, getViaServers, roomHasAtLeastVersion, removeCreatorsFromPowerLevels, setUserPower, getThreadRoomFromThreadEvent} = require("./utils")
|
||||
const util = require("util")
|
||||
|
||||
/** @param {string[]} mxids */
|
||||
|
|
@ -417,4 +417,23 @@ 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)
|
||||
})
|
||||
|
||||
module.exports.mockGetEffectivePower = mockGetEffectivePower
|
||||
|
|
|
|||
|
|
@ -82,12 +82,14 @@ WITH a (message_id, channel_id) AS (VALUES
|
|||
('1381212840957972480', '112760669178241024'),
|
||||
('1401760355339862066', '112760669178241024'),
|
||||
('1439351590262800565', '1438284564815548418'),
|
||||
('1404133238414376971', '112760669178241024'))
|
||||
('1404133238414376971', '112760669178241024'),
|
||||
('1162005314908999790', '1100319550446252084'))
|
||||
SELECT message_id, max(historical_room_index) as historical_room_index FROM a INNER JOIN historical_channel_room ON historical_channel_room.reference_channel_id = a.channel_id GROUP BY message_id;
|
||||
|
||||
INSERT INTO event_message (event_id, event_type, event_subtype, message_id, part, reaction_part, source) VALUES
|
||||
('$X16nfVks1wsrhq4E9SSLiqrf2N8KD0erD0scZG7U5xg', 'm.room.message', 'm.text', '1126786462646550579', 0, 0, 1),
|
||||
('$Ij3qo7NxMA4VPexlAiIx2CB9JbsiGhJeyt-2OvkAUe4', 'm.room.message', 'm.text', '1128118177155526666', 0, 0, 0),
|
||||
('$fdD9o7NxMA4VPexlAiIx2CB9JbsiGhJeyJgnZG7U5xg', 'm.room.message', 'm.text', '1162005314908999790', 0, 0, 1),
|
||||
('$zXSlyI78DQqQwwfPUSzZ1b-nXzbUrCDljJgnGDdoI10', 'm.room.message', 'm.text', '1141619794500649020', 0, 0, 1),
|
||||
('$fdD9OZ55xg3EAsfvLZza5tMhtjUO91Wg3Otuo96TplY', 'm.room.message', 'm.text', '1141206225632112650', 0, 0, 1),
|
||||
('$mtR8cJqM4fKno1bVsm8F4wUVqSntt2sq6jav1lyavuA', 'm.room.message', 'm.text', '1141501302736695316', 0, 1, 1),
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue