Compare commits

...

3 commits

21 changed files with 476 additions and 153 deletions

View file

@ -1,2 +1,5 @@
{ {
"editor.insertSpaces": false,
"editor.detectIndentation": false,
"editor.tabSize": 3
} }

View file

@ -15,20 +15,20 @@ const createRoom = sync.require("../actions/create-room")
* @param {import("discord-api-types/v10").GatewayMessageReactionAddDispatchData} data * @param {import("discord-api-types/v10").GatewayMessageReactionAddDispatchData} data
*/ */
async function addReaction(data) { async function addReaction(data) {
const user = data.member?.user const user = data.member?.user
assert.ok(user && user.username) assert.ok(user && user.username)
const parentID = db.prepare("SELECT event_id FROM event_message WHERE message_id = ? AND part = 0").pluck().get(data.message_id) // 0 = primary const parentID = db.prepare("SELECT event_id FROM event_message WHERE message_id = ? AND part = 0").pluck().get(data.message_id) // 0 = primary
if (!parentID) return // Nothing can be done if the parent message was never bridged. if (!parentID) return // Nothing can be done if the parent message was never bridged.
assert.equal(typeof parentID, "string") assert.equal(typeof parentID, "string")
const roomID = await createRoom.ensureRoom(data.channel_id) const roomID = await createRoom.ensureRoom(data.channel_id)
const senderMxid = await registerUser.ensureSimJoined(user, roomID) const senderMxid = await registerUser.ensureSimJoined(user, roomID)
const eventID = await api.sendEvent(roomID, "m.reaction", { const eventID = await api.sendEvent(roomID, "m.reaction", {
"m.relates_to": { "m.relates_to": {
rel_type: "m.annotation", rel_type: "m.annotation",
event_id: parentID, event_id: parentID,
key: data.emoji.name key: data.emoji.name
} }
}, senderMxid) }, senderMxid)
return eventID return eventID
} }

View file

@ -4,14 +4,10 @@ const assert = require("assert")
const passthrough = require("../../passthrough") const passthrough = require("../../passthrough")
const { discord, sync, db } = passthrough const { discord, sync, db } = passthrough
/** @type {import("../converters/message-to-event")} */ /** @type {import("../converters/thread-to-announcement")} */
const messageToEvent = sync.require("../converters/message-to-event") const threadToAnnouncement = sync.require("../converters/thread-to-announcement")
/** @type {import("../../matrix/api")} */ /** @type {import("../../matrix/api")} */
const api = sync.require("../../matrix/api") const api = sync.require("../../matrix/api")
/** @type {import("./register-user")} */
const registerUser = sync.require("./register-user")
/** @type {import("../actions/create-room")} */
const createRoom = sync.require("../actions/create-room")
/** /**
* @param {string} parentRoomID * @param {string} parentRoomID
@ -21,24 +17,10 @@ const createRoom = sync.require("../actions/create-room")
async function announceThread(parentRoomID, threadRoomID, thread) { async function announceThread(parentRoomID, threadRoomID, thread) {
/** @type {string?} */ /** @type {string?} */
const creatorMxid = db.prepare("SELECT mxid FROM sim WHERE discord_id = ?").pluck().get(thread.owner_id) const creatorMxid = db.prepare("SELECT mxid FROM sim WHERE discord_id = ?").pluck().get(thread.owner_id)
/** @type {string?} */
const branchedFromEventID = db.prepare("SELECT event_id FROM event_message WHERE message_id = ?").get(thread.id)
const msgtype = creatorMxid ? "m.emote" : "m.text" const content = await threadToAnnouncement.threadToAnnouncement(parentRoomID, threadRoomID, creatorMxid, thread, {api})
const template = creatorMxid ? "started a thread:" : "Thread started:"
let body = `${template} ${thread.name} https://matrix.to/#/${threadRoomID}`
let html = `${template} <a href="https://matrix.to/#/${threadRoomID}">${thread.name}</a>`
const mentions = {} await api.sendEvent(parentRoomID, "m.room.message", content, creatorMxid)
await api.sendEvent(parentRoomID, "m.room.message", {
msgtype,
body: `${template} ,
format: "org.matrix.custom.html",
formatted_body: "",
"m.mentions": mentions
}, creatorMxid)
} }
module.exports.announceThread = announceThread module.exports.announceThread = announceThread

View file

@ -70,12 +70,15 @@ async function channelToKState(channel, guild) {
avatarEventContent.url = await file.uploadDiscordFileToMxc(avatarEventContent.discord_path) // TODO: somehow represent future values in kstate (callbacks?), while still allowing for diffing, so test cases don't need to touch the media API avatarEventContent.url = await file.uploadDiscordFileToMxc(avatarEventContent.discord_path) // TODO: somehow represent future values in kstate (callbacks?), while still allowing for diffing, so test cases don't need to touch the media API
} }
let history_visibility = "invited"
if (channel["thread_metadata"]) history_visibility = "world_readable"
const channelKState = { const channelKState = {
"m.room.name/": {name: convertedName}, "m.room.name/": {name: convertedName},
"m.room.topic/": {topic: convertedTopic}, "m.room.topic/": {topic: convertedTopic},
"m.room.avatar/": avatarEventContent, "m.room.avatar/": avatarEventContent,
"m.room.guest_access/": {guest_access: "can_join"}, "m.room.guest_access/": {guest_access: "can_join"},
"m.room.history_visibility/": {history_visibility: "invited"}, "m.room.history_visibility/": {history_visibility},
[`m.space.parent/${spaceID}`]: { [`m.space.parent/${spaceID}`]: {
via: ["cadence.moe"], // TODO: put the proper server here via: ["cadence.moe"], // TODO: put the proper server here
canonical: true canonical: true
@ -234,19 +237,23 @@ async function _unbridgeRoom(channelID) {
* @returns {Promise<string[]>} * @returns {Promise<string[]>}
*/ */
async function _syncSpaceMember(channel, spaceID, roomID) { async function _syncSpaceMember(channel, spaceID, roomID) {
console.error(channel)
console.error("syncing space for", roomID)
const spaceKState = await roomToKState(spaceID) const spaceKState = await roomToKState(spaceID)
let spaceEventContent = {} let spaceEventContent = {}
if ( if (
channel.type !== DiscordTypes.ChannelType.PrivateThread // private threads do not belong in the space (don't offer people something they can't join) channel.type !== DiscordTypes.ChannelType.PrivateThread // private threads do not belong in the space (don't offer people something they can't join)
|| channel["thread_metadata"]?.archived // archived threads do not belong in the space (don't offer people conversations that are no longer relevant) && !channel["thread_metadata"]?.archived // archived threads do not belong in the space (don't offer people conversations that are no longer relevant)
) { ) {
spaceEventContent = { spaceEventContent = {
via: ["cadence.moe"] // TODO: use the proper server via: ["cadence.moe"] // TODO: use the proper server
} }
} }
console.error(spaceEventContent)
const spaceDiff = ks.diffKState(spaceKState, { const spaceDiff = ks.diffKState(spaceKState, {
[`m.space.child/${roomID}`]: spaceEventContent [`m.space.child/${roomID}`]: spaceEventContent
}) })
console.error(spaceDiff)
return applyKStateDiffToRoom(spaceID, spaceDiff) return applyKStateDiffToRoom(spaceID, spaceDiff)
} }

View file

@ -38,7 +38,7 @@ async function sendMessage(message, guild) {
const eventWithoutType = {...event} const eventWithoutType = {...event}
delete eventWithoutType.$type delete eventWithoutType.$type
const eventID = await api.sendEvent(roomID, eventType, event, senderMxid, new Date(message.timestamp).getTime()) const eventID = await api.sendEvent(roomID, eventType, eventWithoutType, senderMxid, new Date(message.timestamp).getTime())
db.prepare("INSERT INTO event_message (event_id, event_type, event_subtype, message_id, channel_id, part, source) VALUES (?, ?, ?, ?, ?, ?, 1)").run(eventID, eventType, event.msgtype || null, message.id, message.channel_id, eventPart) // source 1 = discord db.prepare("INSERT INTO event_message (event_id, event_type, event_subtype, message_id, channel_id, part, source) VALUES (?, ?, ?, ?, ?, ?, 1)").run(eventID, eventType, event.msgtype || null, message.id, message.channel_id, eventPart) // source 1 = discord
eventPart = 1 // TODO: use more intelligent algorithm to determine whether primary or supporting eventPart = 1 // TODO: use more intelligent algorithm to determine whether primary or supporting

View file

@ -65,6 +65,29 @@ function getDiscordParseCallbacks(message, useHTML) {
async function messageToEvent(message, guild, options = {}, di) { async function messageToEvent(message, guild, options = {}, di) {
const events = [] const events = []
if (message.type === DiscordTypes.MessageType.ThreadCreated) {
// This is the kind of message that appears when somebody makes a thread which isn't close enough to the message it's based off.
// It lacks the lines and the pill, so it looks kind of like a member join message, and it says:
// [#] NICKNAME started a thread: __THREAD NAME__. __See all threads__
// We're already bridging the THREAD_CREATED gateway event to make a comparable message, so drop this one.
return []
}
if (message.type === DiscordTypes.MessageType.ThreadStarterMessage) {
// This is the message that appears at the top of a thread when the thread was based off an existing message.
// It's just a message reference, no content.
const ref = message.message_reference
assert(ref)
assert(ref.message_id)
const row = db.prepare("SELECT room_id, event_id FROM event_message INNER JOIN channel_room USING (channel_id) WHERE channel_id = ? AND message_id = ?").get(ref.channel_id, ref.message_id)
if (!row) return []
const event = await di.api.getEvent(row.room_id, row.event_id)
return [{
...event.content,
$type: event.type
}]
}
/** /**
@type {{room?: boolean, user_ids?: string[]}} @type {{room?: boolean, user_ids?: string[]}}
We should consider the following scenarios for mentions: We should consider the following scenarios for mentions:

View file

@ -341,3 +341,25 @@ test("message2event: type 4 channel name change", async t => {
formatted_body: "changed the channel name to <strong>worming</strong>" formatted_body: "changed the channel name to <strong>worming</strong>"
}]) }])
}) })
test("message2event: thread start message reference", async t => {
const events = await messageToEvent(data.special_message.thread_start_context, data.guild.general, {}, {
api: {
getEvent: mockGetEvent(t, "!PnyBKvUBOhjuCucEfk:cadence.moe", "$FchUVylsOfmmbj-VwEs5Z9kY49_dt2zd0vWfylzy5Yo", {
"type": "m.room.message",
"sender": "@_ooye_cadence:cadence.moe",
"content": {
"m.mentions": {},
"msgtype": "m.text",
"body": "layer 4"
}
})
}
})
t.deepEqual(events, [{
$type: "m.room.message",
msgtype: "m.text",
body: "layer 4",
"m.mentions": {}
}])
})

View file

@ -0,0 +1,46 @@
// @ts-check
const assert = require("assert")
const passthrough = require("../../passthrough")
const { discord, sync, db } = passthrough
/** @type {import("../../matrix/read-registration")} */
const reg = sync.require("../../matrix/read-registration.js")
const userRegex = reg.namespaces.users.map(u => new RegExp(u.regex))
/**
* @param {string} parentRoomID
* @param {string} threadRoomID
* @param {string?} creatorMxid
* @param {import("discord-api-types/v10").APIThreadChannel} thread
* @param {{api: import("../../matrix/api")}} di simple-as-nails dependency injection for the matrix API
*/
async function threadToAnnouncement(parentRoomID, threadRoomID, creatorMxid, thread, di) {
/** @type {string?} */
const branchedFromEventID = db.prepare("SELECT event_id FROM event_message WHERE message_id = ?").pluck().get(thread.id)
/** @type {{"m.mentions"?: any, "m.in_reply_to"?: any}} */
const context = {}
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}}
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:"
let body = `${template} ${thread.name} https://matrix.to/#/${threadRoomID}`
let html = `${template} <a href="https://matrix.to/#/${threadRoomID}">${thread.name}</a>`
return {
msgtype,
body,
format: "org.matrix.custom.html",
formatted_body: html,
"m.mentions": {},
...context
}
}
module.exports.threadToAnnouncement = threadToAnnouncement

View file

@ -0,0 +1,150 @@
const {test} = require("supertape")
const {threadToAnnouncement} = require("./thread-to-announcement")
const data = require("../../test/data")
const Ty = require("../../types")
/**
* @param {string} roomID
* @param {string} eventID
* @returns {(roomID: string, eventID: string) => Promise<Ty.Event.Outer<Ty.Event.M_Room_Message>>}
*/
function mockGetEvent(t, roomID_in, eventID_in, outer) {
return async function(roomID, eventID) {
t.equal(roomID, roomID_in)
t.equal(eventID, eventID_in)
return new Promise(resolve => {
setTimeout(() => {
resolve({
event_id: eventID_in,
room_id: roomID_in,
origin_server_ts: 1680000000000,
unsigned: {
age: 2245,
transaction_id: "$local.whatever"
},
...outer
})
})
})
}
}
test("thread2announcement: no known creator, no branched from event", async t => {
const content = await threadToAnnouncement("!parent", "!thread", null, {
name: "test thread",
id: "-1"
})
t.deepEqual(content, {
msgtype: "m.text",
body: "Thread started: test thread https://matrix.to/#/!thread",
format: "org.matrix.custom.html",
formatted_body: `Thread started: <a href="https://matrix.to/#/!thread">test thread</a>`,
"m.mentions": {}
})
})
test("thread2announcement: known creator, no branched from event", async t => {
const content = await threadToAnnouncement("!parent", "!thread", "@_ooye_crunch_god:cadence.moe", {
name: "test thread",
id: "-1"
})
t.deepEqual(content, {
msgtype: "m.emote",
body: "started a thread: test thread https://matrix.to/#/!thread",
format: "org.matrix.custom.html",
formatted_body: `started a thread: <a href="https://matrix.to/#/!thread">test thread</a>`,
"m.mentions": {}
})
})
test("thread2announcement: no known creator, branched from discord event", async t => {
const content = await threadToAnnouncement("!kLRqKKUQXcibIMtOpl:cadence.moe", "!thread", null, {
name: "test thread",
id: "1126786462646550579"
}, {
api: {
getEvent: mockGetEvent(t, "!kLRqKKUQXcibIMtOpl:cadence.moe", "$X16nfVks1wsrhq4E9SSLiqrf2N8KD0erD0scZG7U5xg", {
type: 'm.room.message',
sender: '@_ooye_bot:cadence.moe',
content: {
msgtype: 'm.text',
body: 'testing testing testing'
}
})
}
})
t.deepEqual(content, {
msgtype: "m.text",
body: "Thread started: test thread https://matrix.to/#/!thread",
format: "org.matrix.custom.html",
formatted_body: `Thread started: <a href="https://matrix.to/#/!thread">test thread</a>`,
"m.mentions": {},
"m.relates_to": {
"m.in_reply_to": {
event_id: "$X16nfVks1wsrhq4E9SSLiqrf2N8KD0erD0scZG7U5xg"
}
}
})
})
test("thread2announcement: known creator, branched from discord event", async t => {
const content = await threadToAnnouncement("!kLRqKKUQXcibIMtOpl:cadence.moe", "!thread", "@_ooye_crunch_god:cadence.moe", {
name: "test thread",
id: "1126786462646550579"
}, {
api: {
getEvent: mockGetEvent(t, "!kLRqKKUQXcibIMtOpl:cadence.moe", "$X16nfVks1wsrhq4E9SSLiqrf2N8KD0erD0scZG7U5xg", {
type: 'm.room.message',
sender: '@_ooye_bot:cadence.moe',
content: {
msgtype: 'm.text',
body: 'testing testing testing'
}
})
}
})
t.deepEqual(content, {
msgtype: "m.emote",
body: "started a thread: test thread https://matrix.to/#/!thread",
format: "org.matrix.custom.html",
formatted_body: `started a thread: <a href="https://matrix.to/#/!thread">test thread</a>`,
"m.mentions": {},
"m.relates_to": {
"m.in_reply_to": {
event_id: "$X16nfVks1wsrhq4E9SSLiqrf2N8KD0erD0scZG7U5xg"
}
}
})
})
test("thread2announcement: no known creator, branched from matrix event", async t => {
const content = await threadToAnnouncement("!kLRqKKUQXcibIMtOpl:cadence.moe", "!thread", null, {
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"
})
}
})
t.deepEqual(content, {
msgtype: "m.text",
body: "Thread started: test thread https://matrix.to/#/!thread",
format: "org.matrix.custom.html",
formatted_body: `Thread started: <a href="https://matrix.to/#/!thread">test thread</a>`,
"m.mentions": {
user_ids: ["@cadence:cadence.moe"]
},
"m.relates_to": {
"m.in_reply_to": {
event_id: "$Ij3qo7NxMA4VPexlAiIx2CB9JbsiGhJeyt-2OvkAUe4"
}
}
})
})

View file

@ -86,18 +86,16 @@ const utils = {
await eventDispatcher.onChannelOrThreadUpdate(client, message.d, false) await eventDispatcher.onChannelOrThreadUpdate(client, message.d, false)
} else if (message.t === "THREAD_CREATE") { } else if (message.t === "THREAD_CREATE") {
console.log(message) // @ts-ignore
// await eventDispatcher.onThreadCreate(client, message.d) await eventDispatcher.onThreadCreate(client, message.d)
} else if (message.t === "THREAD_UPDATE") { } else if (message.t === "THREAD_UPDATE") {
await eventDispatcher.onChannelOrThreadUpdate(client, message.d, true) await eventDispatcher.onChannelOrThreadUpdate(client, message.d, true)
} else if (message.t === "MESSAGE_CREATE") { } else if (message.t === "MESSAGE_CREATE") {
console.log(message)
await eventDispatcher.onMessageCreate(client, message.d) await eventDispatcher.onMessageCreate(client, message.d)
} else if (message.t === "MESSAGE_UPDATE") { } else if (message.t === "MESSAGE_UPDATE") {
console.log(message)
await eventDispatcher.onMessageUpdate(client, message.d) await eventDispatcher.onMessageUpdate(client, message.d)
} else if (message.t === "MESSAGE_DELETE") { } else if (message.t === "MESSAGE_DELETE") {

View file

@ -10,6 +10,8 @@ const editMessage = sync.require("./actions/edit-message")
const deleteMessage = sync.require("./actions/delete-message") const deleteMessage = sync.require("./actions/delete-message")
/** @type {import("./actions/add-reaction")}) */ /** @type {import("./actions/add-reaction")}) */
const addReaction = sync.require("./actions/add-reaction") const addReaction = sync.require("./actions/add-reaction")
/** @type {import("./actions/announce-thread")}) */
const announceThread = sync.require("./actions/announce-thread")
/** @type {import("./actions/create-room")}) */ /** @type {import("./actions/create-room")}) */
const createRoom = sync.require("./actions/create-room") const createRoom = sync.require("./actions/create-room")
/** @type {import("../matrix/api")}) */ /** @type {import("../matrix/api")}) */
@ -108,8 +110,7 @@ module.exports = {
* @param {import("discord-api-types/v10").APIThreadChannel} thread * @param {import("discord-api-types/v10").APIThreadChannel} thread
*/ */
async onThreadCreate(client, thread) { async onThreadCreate(client, thread) {
console.log(thread) const parentRoomID = db.prepare("SELECT room_id FROM channel_room WHERE channel_id = ?").pluck().get(thread.parent_id)
const parentRoomID = db.prepare("SELECT room_id FROM channel_room WHERE channel_id = ?").get(thread.parent_id)
if (!parentRoomID) return // Not interested in a thread if we aren't interested in its wider channel if (!parentRoomID) return // Not interested in a thread if we aren't interested in its wider channel
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.syncRoom(thread.id) // Create room (will share the same inflight as the initial message to the thread)
await announceThread.announceThread(parentRoomID, threadRoomID, thread) await announceThread.announceThread(parentRoomID, threadRoomID, thread)

View file

@ -58,7 +58,8 @@ INSERT INTO guild_space (guild_id, space_id) VALUES
INSERT INTO channel_room (channel_id, room_id, name, nick, thread_parent) VALUES INSERT INTO channel_room (channel_id, room_id, name, nick, thread_parent) VALUES
('112760669178241024', '!kLRqKKUQXcibIMtOpl:cadence.moe', 'heave', 'main', NULL), ('112760669178241024', '!kLRqKKUQXcibIMtOpl:cadence.moe', 'heave', 'main', NULL),
('497161350934560778', '!edUxjVdzgUvXDUIQCK:cadence.moe', 'amanda-spam', NULL, NULL), ('497161350934560778', '!edUxjVdzgUvXDUIQCK:cadence.moe', 'amanda-spam', NULL, NULL),
('160197704226439168', '!uCtjHhfGlYbVnPVlkG:cadence.moe', 'the-stanley-parable-channel', 'bots', NULL); ('160197704226439168', '!uCtjHhfGlYbVnPVlkG:cadence.moe', 'the-stanley-parable-channel', 'bots', NULL),
('1100319550446252084', '!PnyBKvUBOhjuCucEfk:cadence.moe', 'worm-farm', NULL, NULL);
INSERT INTO sim (discord_id, sim_name, localpart, mxid) VALUES INSERT INTO sim (discord_id, sim_name, localpart, mxid) VALUES
('0', 'bot', '_ooye_bot', '@_ooye_bot:cadence.moe'), ('0', 'bot', '_ooye_bot', '@_ooye_bot:cadence.moe'),
@ -80,7 +81,8 @@ INSERT INTO event_message (event_id, event_type, event_subtype, message_id, chan
('$51f4yqHinwnSbPEQ9dCgoyy4qiIJSX0QYYVUnvwyTCJ', 'm.room.message', 'm.image', '1141501302736695317', '112760669178241024', 0, 1), ('$51f4yqHinwnSbPEQ9dCgoyy4qiIJSX0QYYVUnvwyTCJ', 'm.room.message', 'm.image', '1141501302736695317', '112760669178241024', 0, 1),
('$vgTKOR5ZTYNMKaS7XvgEIDaOWZtVCEyzLLi5Pc5Gz4M', 'm.room.message', 'm.text', '1128084851279536279', '112760669178241024', 0, 1), ('$vgTKOR5ZTYNMKaS7XvgEIDaOWZtVCEyzLLi5Pc5Gz4M', 'm.room.message', 'm.text', '1128084851279536279', '112760669178241024', 0, 1),
('$YUJFa5j0ZJe7PUvD2DykRt9g51RoadUEYmuJLdSEbJ0', 'm.room.message', 'm.image', '1128084851279536279', '112760669178241024', 1, 1), ('$YUJFa5j0ZJe7PUvD2DykRt9g51RoadUEYmuJLdSEbJ0', 'm.room.message', 'm.image', '1128084851279536279', '112760669178241024', 1, 1),
('$oLyUTyZ_7e_SUzGNWZKz880ll9amLZvXGbArJCKai2Q', 'm.room.message', 'm.text', '1128084748338741392', '112760669178241024', 0, 1); ('$oLyUTyZ_7e_SUzGNWZKz880ll9amLZvXGbArJCKai2Q', 'm.room.message', 'm.text', '1128084748338741392', '112760669178241024', 0, 1),
('$FchUVylsOfmmbj-VwEs5Z9kY49_dt2zd0vWfylzy5Yo', 'm.room.message', 'm.text', '1143121514925928541', '1100319550446252084', 0, 1);
INSERT INTO file (discord_url, mxc_url) VALUES INSERT INTO file (discord_url, mxc_url) VALUES
('https://cdn.discordapp.com/attachments/497161332244742154/1124628646431297546/image.png', 'mxc://cadence.moe/qXoZktDqNtEGuOCZEADAMvhM'), ('https://cdn.discordapp.com/attachments/497161332244742154/1124628646431297546/image.png', 'mxc://cadence.moe/qXoZktDqNtEGuOCZEADAMvhM'),

View file

@ -52,10 +52,10 @@ async function withWebhook(channelID, callback) {
* @param {string} [threadID] * @param {string} [threadID]
*/ */
async function sendMessageWithWebhook(channelID, data, threadID) { async function sendMessageWithWebhook(channelID, data, threadID) {
const result = await withWebhook(channelID, async webhook => { const result = await withWebhook(channelID, async webhook => {
return discord.snow.webhook.executeWebhook(webhook.id, webhook.token, data, {wait: true, thread_id: threadID, disableEveryone: true}) return discord.snow.webhook.executeWebhook(webhook.id, webhook.token, data, {wait: true, thread_id: threadID, disableEveryone: true})
}) })
return result return result
} }
module.exports.ensureWebhook = ensureWebhook module.exports.ensureWebhook = ensureWebhook

View file

@ -12,7 +12,7 @@ const eventToMessage = sync.require("../converters/event-to-message")
/** @param {import("../../types").Event.Outer<any>} event */ /** @param {import("../../types").Event.Outer<any>} event */
async function sendEvent(event) { async function sendEvent(event) {
// TODO: we just assume the bridge has already been created // TODO: we just assume the bridge has already been created
const row = db.prepare("SELECT channel_id, thread_parent FROM channel_room WHERE room_id = ?").get(event.room_id) const row = db.prepare("SELECT channel_id, thread_parent FROM channel_room WHERE room_id = ?").get(event.room_id)
let channelID = row.channel_id let channelID = row.channel_id
let threadID = undefined let threadID = undefined
@ -21,16 +21,16 @@ async function sendEvent(event) {
channelID = row.thread_parent // it's the thread's parent... get with the times... channelID = row.thread_parent // it's the thread's parent... get with the times...
} }
// no need to sync the matrix member to the other side. but if I did need to, this is where I'd do it // no need to sync the matrix member to the other side. but if I did need to, this is where I'd do it
const messages = eventToMessage.eventToMessage(event) const messages = eventToMessage.eventToMessage(event)
assert(Array.isArray(messages)) // sanity assert(Array.isArray(messages)) // sanity
/** @type {DiscordTypes.APIMessage[]} */ /** @type {DiscordTypes.APIMessage[]} */
const messageResponses = [] const messageResponses = []
let eventPart = 0 // 0 is primary, 1 is supporting let eventPart = 0 // 0 is primary, 1 is supporting
for (const message of messages) { for (const message of messages) {
const messageResponse = await channelWebhook.sendMessageWithWebhook(channelID, message, threadID) const messageResponse = await channelWebhook.sendMessageWithWebhook(channelID, message, threadID)
db.prepare("INSERT INTO event_message (event_id, event_type, event_subtype, message_id, channel_id, part, source) VALUES (?, ?, ?, ?, ?, ?, 0)").run(event.event_id, event.type, event.content.msgtype || null, messageResponse.id, channelID, eventPart) // source 0 = matrix db.prepare("INSERT INTO event_message (event_id, event_type, event_subtype, message_id, channel_id, part, source) VALUES (?, ?, ?, ?, ?, ?, 0)").run(event.event_id, event.type, event.content.msgtype || null, messageResponse.id, channelID, eventPart) // source 0 = matrix
eventPart = 1 // TODO: use more intelligent algorithm to determine whether primary or supporting? eventPart = 1 // TODO: use more intelligent algorithm to determine whether primary or supporting?

View file

@ -19,15 +19,15 @@ const makeTxnId = sync.require("./txnid")
* @returns {string} the new endpoint * @returns {string} the new endpoint
*/ */
function path(p, mxid, otherParams = {}) { function path(p, mxid, otherParams = {}) {
if (!mxid) return p if (!mxid) return p
const u = new URL(p, "http://localhost") const u = new URL(p, "http://localhost")
u.searchParams.set("user_id", mxid) u.searchParams.set("user_id", mxid)
for (const entry of Object.entries(otherParams)) { for (const entry of Object.entries(otherParams)) {
if (entry[1] != undefined) { if (entry[1] != undefined) {
u.searchParams.set(entry[0], entry[1]) u.searchParams.set(entry[0], entry[1])
} }
} }
return u.pathname + "?" + u.searchParams.toString() return u.pathname + "?" + u.searchParams.toString()
} }
/** /**
@ -35,40 +35,40 @@ function path(p, mxid, otherParams = {}) {
* @returns {Promise<Ty.R.Registered>} * @returns {Promise<Ty.R.Registered>}
*/ */
function register(username) { function register(username) {
console.log(`[api] register: ${username}`) console.log(`[api] register: ${username}`)
return mreq.mreq("POST", "/client/v3/register", { return mreq.mreq("POST", "/client/v3/register", {
type: "m.login.application_service", type: "m.login.application_service",
username username
}) })
} }
/** /**
* @returns {Promise<string>} room ID * @returns {Promise<string>} room ID
*/ */
async function createRoom(content) { async function createRoom(content) {
console.log(`[api] create room:`, content) console.log(`[api] create room:`, content)
/** @type {Ty.R.RoomCreated} */ /** @type {Ty.R.RoomCreated} */
const root = await mreq.mreq("POST", "/client/v3/createRoom", content) const root = await mreq.mreq("POST", "/client/v3/createRoom", content)
return root.room_id return root.room_id
} }
/** /**
* @returns {Promise<string>} room ID * @returns {Promise<string>} room ID
*/ */
async function joinRoom(roomIDOrAlias, mxid) { async function joinRoom(roomIDOrAlias, mxid) {
/** @type {Ty.R.RoomJoined} */ /** @type {Ty.R.RoomJoined} */
const root = await mreq.mreq("POST", path(`/client/v3/join/${roomIDOrAlias}`, mxid)) const root = await mreq.mreq("POST", path(`/client/v3/join/${roomIDOrAlias}`, mxid))
return root.room_id return root.room_id
} }
async function inviteToRoom(roomID, mxidToInvite, mxid) { async function inviteToRoom(roomID, mxidToInvite, mxid) {
await mreq.mreq("POST", path(`/client/v3/rooms/${roomID}/invite`, mxid), { await mreq.mreq("POST", path(`/client/v3/rooms/${roomID}/invite`, mxid), {
user_id: mxidToInvite user_id: mxidToInvite
}) })
} }
async function leaveRoom(roomID, mxid) { async function leaveRoom(roomID, mxid) {
await mreq.mreq("POST", path(`/client/v3/rooms/${roomID}/leave`, mxid), {}) await mreq.mreq("POST", path(`/client/v3/rooms/${roomID}/leave`, mxid), {})
} }
/** /**
@ -77,9 +77,9 @@ async function leaveRoom(roomID, mxid) {
* @template T * @template T
*/ */
async function getEvent(roomID, eventID) { async function getEvent(roomID, eventID) {
/** @type {Ty.Event.Outer<T>} */ /** @type {Ty.Event.Outer<T>} */
const root = await mreq.mreq("GET", `/client/v3/rooms/${roomID}/event/${eventID}`) const root = await mreq.mreq("GET", `/client/v3/rooms/${roomID}/event/${eventID}`)
return root return root
} }
/** /**
@ -87,7 +87,17 @@ async function getEvent(roomID, eventID) {
* @returns {Promise<Ty.Event.BaseStateEvent[]>} * @returns {Promise<Ty.Event.BaseStateEvent[]>}
*/ */
function getAllState(roomID) { function getAllState(roomID) {
return mreq.mreq("GET", `/client/v3/rooms/${roomID}/state`) return mreq.mreq("GET", `/client/v3/rooms/${roomID}/state`)
}
/**
* @param {string} roomID
* @param {string} type
* @param {string} key
* @returns the *content* of the state event
*/
function getStateEvent(roomID, type, key) {
return mreq.mreq("GET", `/client/v3/rooms/${roomID}/state/${type}/${key}`)
} }
/** /**
@ -96,7 +106,7 @@ function getAllState(roomID) {
* @returns {Promise<{joined: {[mxid: string]: Ty.R.RoomMember}}>} * @returns {Promise<{joined: {[mxid: string]: Ty.R.RoomMember}}>}
*/ */
function getJoinedMembers(roomID) { function getJoinedMembers(roomID) {
return mreq.mreq("GET", `/client/v3/rooms/${roomID}/joined_members`) return mreq.mreq("GET", `/client/v3/rooms/${roomID}/joined_members`)
} }
/** /**
@ -107,12 +117,12 @@ function getJoinedMembers(roomID) {
* @returns {Promise<string>} event ID * @returns {Promise<string>} event ID
*/ */
async function sendState(roomID, type, stateKey, content, mxid) { async function sendState(roomID, type, stateKey, content, mxid) {
console.log(`[api] state: ${roomID}: ${type}/${stateKey}`) console.log(`[api] state: ${roomID}: ${type}/${stateKey}`)
assert.ok(type) assert.ok(type)
assert.ok(typeof stateKey === "string") assert.ok(typeof stateKey === "string")
/** @type {Ty.R.EventSent} */ /** @type {Ty.R.EventSent} */
const root = await mreq.mreq("PUT", path(`/client/v3/rooms/${roomID}/state/${type}/${stateKey}`, mxid), content) const root = await mreq.mreq("PUT", path(`/client/v3/rooms/${roomID}/state/${type}/${stateKey}`, mxid), content)
return root.event_id return root.event_id
} }
/** /**
@ -123,31 +133,51 @@ async function sendState(roomID, type, stateKey, content, mxid) {
* @param {number} [timestamp] timestamp of the newly created event, in unix milliseconds * @param {number} [timestamp] timestamp of the newly created event, in unix milliseconds
*/ */
async function sendEvent(roomID, type, content, mxid, timestamp) { async function sendEvent(roomID, type, content, mxid, timestamp) {
console.log(`[api] event ${type} to ${roomID} as ${mxid || "default sim"}`) console.log(`[api] event ${type} to ${roomID} as ${mxid || "default sim"}`)
/** @type {Ty.R.EventSent} */ /** @type {Ty.R.EventSent} */
const root = await mreq.mreq("PUT", path(`/client/v3/rooms/${roomID}/send/${type}/${makeTxnId.makeTxnId()}`, mxid, {ts: timestamp}), content) const root = await mreq.mreq("PUT", path(`/client/v3/rooms/${roomID}/send/${type}/${makeTxnId.makeTxnId()}`, mxid, {ts: timestamp}), content)
return root.event_id return root.event_id
} }
/** /**
* @returns {Promise<string>} room ID * @returns {Promise<string>} room ID
*/ */
async function redactEvent(roomID, eventID, mxid) { async function redactEvent(roomID, eventID, mxid) {
/** @type {Ty.R.EventRedacted} */ /** @type {Ty.R.EventRedacted} */
const root = await mreq.mreq("PUT", path(`/client/v3/rooms/${roomID}/redact/${eventID}/${makeTxnId.makeTxnId()}`, mxid), {}) const root = await mreq.mreq("PUT", path(`/client/v3/rooms/${roomID}/redact/${eventID}/${makeTxnId.makeTxnId()}`, mxid), {})
return root.event_id return root.event_id
} }
async function profileSetDisplayname(mxid, displayname) { async function profileSetDisplayname(mxid, displayname) {
await mreq.mreq("PUT", path(`/client/v3/profile/${mxid}/displayname`, mxid), { await mreq.mreq("PUT", path(`/client/v3/profile/${mxid}/displayname`, mxid), {
displayname displayname
}) })
} }
async function profileSetAvatarUrl(mxid, avatar_url) { async function profileSetAvatarUrl(mxid, avatar_url) {
await mreq.mreq("PUT", path(`/client/v3/profile/${mxid}/avatar_url`, mxid), { await mreq.mreq("PUT", path(`/client/v3/profile/${mxid}/avatar_url`, mxid), {
avatar_url avatar_url
}) })
}
/**
* Set a user's power level within a room.
* @param {string} roomID
* @param {string} mxid
* @param {number} power
*/
async function setUserPower(roomID, mxid, power) {
// Yes it's this hard https://github.com/matrix-org/matrix-appservice-bridge/blob/2334b0bae28a285a767fe7244dad59f5a5963037/src/components/intent.ts#L352
const powerLevels = await getStateEvent(roomID, "m.room.power_levels", "")
const users = powerLevels.users || {}
if (power != null) {
users[mxid] = power
} else {
delete users[mxid]
}
powerLevels.users = users
await sendState(roomID, "m.room.power_levels", "", powerLevels)
return powerLevels
} }
module.exports.path = path module.exports.path = path
@ -164,3 +194,4 @@ module.exports.sendEvent = sendEvent
module.exports.redactEvent = redactEvent module.exports.redactEvent = redactEvent
module.exports.profileSetDisplayname = profileSetDisplayname module.exports.profileSetDisplayname = profileSetDisplayname
module.exports.profileSetAvatarUrl = profileSetAvatarUrl module.exports.profileSetAvatarUrl = profileSetAvatarUrl
module.exports.setUserPower = setUserPower

View file

@ -2,22 +2,22 @@ const {kstateToState, stateToKState, diffKState, kstateStripConditionals} = requ
const {test} = require("supertape") const {test} = require("supertape")
test("kstate strip: strips false conditions", t => { test("kstate strip: strips false conditions", t => {
t.deepEqual(kstateStripConditionals({ t.deepEqual(kstateStripConditionals({
a: {$if: false, value: 2}, a: {$if: false, value: 2},
b: {value: 4} b: {value: 4}
}), { }), {
b: {value: 4} b: {value: 4}
}) })
}) })
test("kstate strip: keeps true conditions while removing $if", t => { test("kstate strip: keeps true conditions while removing $if", t => {
t.deepEqual(kstateStripConditionals({ t.deepEqual(kstateStripConditionals({
a: {$if: true, value: 2}, a: {$if: true, value: 2},
b: {value: 4} b: {value: 4}
}), { }), {
a: {value: 2}, a: {value: 2},
b: {value: 4} b: {value: 4}
}) })
}) })
test("kstate2state: general", t => { test("kstate2state: general", t => {

View file

@ -2,9 +2,9 @@ const {test} = require("supertape")
const reg = require("./read-registration") const reg = require("./read-registration")
test("reg: has necessary parameters", t => { test("reg: has necessary parameters", t => {
const propertiesToCheck = ["sender_localpart", "id", "as_token", "ooye"] const propertiesToCheck = ["sender_localpart", "id", "as_token", "ooye"]
t.deepEqual( t.deepEqual(
propertiesToCheck.filter(p => p in reg), propertiesToCheck.filter(p => p in reg),
propertiesToCheck propertiesToCheck
) )
}) })

View file

@ -18,10 +18,10 @@ passthrough.discord = discord
;(async () => { ;(async () => {
await discord.cloud.connect() await discord.cloud.connect()
console.log("Discord gateway started") console.log("Discord gateway started")
const f = event => onPacket(discord, event, () => discord.cloud.off("event", f)) const f = event => onPacket(discord, event, () => discord.cloud.off("event", f))
discord.cloud.on("event", f) discord.cloud.on("event", f)
})() })()
const expectedGuilds = new Set() const expectedGuilds = new Set()
@ -30,29 +30,29 @@ const prepared = db.prepare("UPDATE channel_room SET name = ? WHERE channel_id =
/** @param {DiscordClient} discord */ /** @param {DiscordClient} discord */
function onPacket(discord, event, unsubscribe) { function onPacket(discord, event, unsubscribe) {
if (event.t === "READY") { if (event.t === "READY") {
for (const obj of event.d.guilds) { for (const obj of event.d.guilds) {
expectedGuilds.add(obj.id) expectedGuilds.add(obj.id)
} }
} else if (event.t === "GUILD_CREATE") { } else if (event.t === "GUILD_CREATE") {
expectedGuilds.delete(event.d.id) expectedGuilds.delete(event.d.id)
// Store the channel. // Store the channel.
for (const channel of event.d.channels || []) { for (const channel of event.d.channels || []) {
prepared.run(channel.name, channel.id) prepared.run(channel.name, channel.id)
} }
// Checked them all? // Checked them all?
if (expectedGuilds.size === 0) { if (expectedGuilds.size === 0) {
discord.cloud.disconnect() discord.cloud.disconnect()
unsubscribe() unsubscribe()
// I don't know why node keeps running. // I don't know why node keeps running.
setTimeout(() => { setTimeout(() => {
console.log("Stopping now.") console.log("Stopping now.")
process.exit() process.exit()
}, 1500).unref() }, 1500).unref()
} }
} }
} }

View file

@ -18,13 +18,13 @@ const rows = db.prepare("SELECT event_id, room_id, event_type FROM event_message
const preparedUpdate = db.prepare("UPDATE event_message SET event_type = ?, event_subtype = ? WHERE event_id = ?") const preparedUpdate = db.prepare("UPDATE event_message SET event_type = ?, event_subtype = ? WHERE event_id = ?")
;(async () => { ;(async () => {
for (const row of rows) { for (const row of rows) {
if (row.event_type == null) { if (row.event_type == null) {
const event = await api.getEvent(row.room_id, row.event_id) const event = await api.getEvent(row.room_id, row.event_id)
const type = event.type const type = event.type
const subtype = event.content.msgtype || null const subtype = event.content.msgtype || null
preparedUpdate.run(type, subtype, row.event_id) preparedUpdate.run(type, subtype, row.event_id)
console.log(`Updated ${row.event_id} -> ${type} + ${subtype}`) console.log(`Updated ${row.event_id} -> ${type} + ${subtype}`)
} }
} }
})() })()

View file

@ -1375,6 +1375,63 @@ module.exports = {
flags: 0, flags: 0,
components: [], components: [],
position: 12 position: 12
},
updated_to_start_thread_from_here: {
t: "MESSAGE_UPDATE",
s: 19,
op: 0,
d: {
id: "1143121514925928541",
flags: 32,
channel_id: "1100319550446252084",
guild_id: "1100319549670301727"
},
shard_id: 0
},
thread_start_context: {
type: 21,
tts: false,
timestamp: "2023-08-21T09:57:12.558000+00:00",
position: 0,
pinned: false,
message_reference: {
message_id: "1143121514925928541",
guild_id: "1100319549670301727",
channel_id: "1100319550446252084"
},
mentions: [],
mention_roles: [],
mention_everyone: false,
member: {
roles: [],
premium_since: null,
pending: false,
nick: "worm",
mute: false,
joined_at: "2023-04-25T07:17:03.696000+00:00",
flags: 0,
deaf: false,
communication_disabled_until: null,
avatar: null
},
id: "1143121620744032327",
flags: 0,
embeds: [],
edited_timestamp: null,
content: "",
components: [],
channel_id: "1143121514925928541",
author: {
username: "cadence.worm",
public_flags: 0,
id: "772659086046658620",
global_name: "cadence",
discriminator: "0",
avatar_decoration_data: null,
avatar: "4b5c4b28051144e4c111f0113a0f1cf1"
},
attachments: [],
guild_id: "1100319549670301727"
} }
} }
} }

View file

@ -23,6 +23,7 @@ require("../matrix/read-registration.test")
require("../d2m/converters/message-to-event.test") require("../d2m/converters/message-to-event.test")
require("../d2m/converters/message-to-event.embeds.test") require("../d2m/converters/message-to-event.embeds.test")
require("../d2m/converters/edit-to-changes.test") require("../d2m/converters/edit-to-changes.test")
require("../d2m/converters/thread-to-announcement.test")
require("../d2m/actions/create-room.test") require("../d2m/actions/create-room.test")
require("../d2m/converters/user-to-mxid.test") require("../d2m/converters/user-to-mxid.test")
require("../d2m/actions/register-user.test") require("../d2m/actions/register-user.test")