bridge both ways and prevent reflections

This commit is contained in:
Cadence Ember 2023-07-04 00:39:42 +12:00
parent 3578ca28b5
commit 39cdba9f90
15 changed files with 67 additions and 51 deletions

View file

@ -49,7 +49,7 @@ async function withWebhook(channelID, callback) {
/**
* @param {string} channelID
* @param {DiscordTypes.RESTPostAPIWebhookWithTokenJSONBody & {files?: {name: string, file: Buffer}[]}[]} data
* @param {DiscordTypes.RESTPostAPIWebhookWithTokenJSONBody & {files?: {name: string, file: Buffer}[]}} data
*/
async function sendMessageWithWebhook(channelID, data) {
const result = await withWebhook(channelID, async webhook => {

View file

@ -12,28 +12,22 @@ const eventToMessage = sync.require("../converters/event-to-message")
/** @param {import("../../types").Event.Outer<any>} event */
async function sendEvent(event) {
// TODO: matrix equivalents...
const roomID = await createRoom.ensureRoom(message.channel_id)
// TODO: no need to sync the member to the other side... right?
let senderMxid = null
if (!message.webhook_id) {
assert(message.member)
senderMxid = await registerUser.ensureSimJoined(message.author, roomID)
await registerUser.syncUser(message.author, message.member, message.guild_id, roomID)
}
// TODO: we just assume the bridge has already been created
const channelID = db.prepare("SELECT channel_id FROM channel_room WHERE room_id = ?").pluck().get(event.room_id)
// 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)
assert(Array.isArray(messages))
assert(Array.isArray(messages)) // sanity
/** @type {DiscordTypes.APIMessage[]} */
const messageResponses = []
let eventPart = 0 // 0 is primary, 1 is supporting
for (const message of messages) {
const messageResponse = await channelWebhook.sendMessageWithWebhook(channelID, message)
// TODO: are you sure about that? many to many? and we don't need to store which side it originated from?
db.prepare("INSERT INTO event_message (event_id, message_id, part) VALUES (?, ?, ?)").run(event.event_id, messageResponse.id, eventPart)
db.prepare("INSERT INTO event_message (event_id, message_id, part, source) VALUES (?, ?, ?, 0)").run(event.event_id, messageResponse.id, 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?
messageResponses.push(messageResponse)
}

View file

@ -1,6 +1,5 @@
// @ts-check
const assert = require("assert").strict
const DiscordTypes = require("discord-api-types/v10")
const markdown = require("discord-markdown")

View file

@ -1,14 +1,12 @@
// @ts-check
const {test} = require("supertape")
const assert = require("assert")
const {eventToMessage} = require("./event-to-message")
const data = require("../../test/data")
test("event2message: janky test", t => {
t.deepEqual(
eventToMessage({
age: 405299,
content: {
body: "test",
msgtype: "m.text"
@ -20,8 +18,7 @@ test("event2message: janky test", t => {
type: "m.room.message",
unsigned: {
age: 405299
},
user_id: "@cadence:cadence.moe"
}
}),
[{
username: "cadence:cadence.moe",

21
m2d/converters/utils.js Normal file
View file

@ -0,0 +1,21 @@
// @ts-check
const reg = require("../../matrix/read-registration")
const userRegex = reg.namespaces.users.map(u => new RegExp(u.regex))
/**
* Determine whether an event is the bridged representation of a discord message.
* Such messages shouldn't be bridged again.
* @param {string} sender
*/
function eventSenderIsFromDiscord(sender) {
// If it's from a user in the bridge's namespace, then it originated from discord
// This includes messages sent by the appservice's bot user, because that is what's used for webhooks
// TODO: It would be nice if bridge system messages wouldn't trigger this check and could be bridged from matrix to discord, while webhook reflections would remain ignored...
if (userRegex.some(x => sender.match(x))) {
return true
}
return false
}
module.exports.eventSenderIsFromDiscord = eventSenderIsFromDiscord

View file

@ -0,0 +1,16 @@
// @ts-check
const {test} = require("supertape")
const {eventSenderIsFromDiscord} = require("./utils")
test("sender type: matrix user", t => {
t.notOk(eventSenderIsFromDiscord("@cadence:cadence.moe"))
})
test("sender type: ooye bot", t => {
t.ok(eventSenderIsFromDiscord("@_ooye_bot:cadence.moe"))
})
test("sender type: ooye puppet", t => {
t.ok(eventSenderIsFromDiscord("@_ooye_sheep:cadence.moe"))
})

View file

@ -4,34 +4,19 @@
* Grab Matrix events we care about, check them, and bridge them.
*/
const assert = require("assert").strict
const {sync, as} = require("../passthrough")
const reg = require("../matrix/read-registration")
/** @type {import("./actions/send-event")} */
const sendEvent = sync.require("./actions/send-event")
/** @type {import("./converters/utils")} */
const utils = sync.require("./converters/utils")
const userRegex = reg.namespaces.users.map(u => new RegExp(u.regex))
sync.addTemporaryListener(as, "type:m.room.message",
/**
* Determine whether an event is the bridged representation of a discord message.
* Such messages shouldn't be bridged again.
* @param {import("../types").Event.Outer<any>} event
* @param {import("../types").Event.Outer<import("../types").Event.M_Room_Message>} event it is a m.room.message because that's what this listener is filtering for
*/
function eventOriginatedFromDiscord(event) {
if (
// If it's from a user in the bridge's namespace...
userRegex.some(x => event.sender.match(x))
// ...not counting the appservice's own user...
&& !event.sender.startsWith(`@${reg.sender_localpart}:`)
) {
// ...then it originated from discord
return true
}
return false
}
sync.addTemporaryListener(as, "type:m.room.message", event => {
console.log(event)
if (eventOriginatedFromDiscord(event)) return
const messageResponses = sendEvent.sendEvent(event)
async event => {
if (utils.eventSenderIsFromDiscord(event.sender)) return
const messageResponses = await sendEvent.sendEvent(event)
})