bridge both ways and prevent reflections
This commit is contained in:
parent
3578ca28b5
commit
39cdba9f90
15 changed files with 67 additions and 51 deletions
|
@ -17,7 +17,6 @@ const createRoom = sync.require("../actions/create-room")
|
|||
async function addReaction(data) {
|
||||
const user = data.member?.user
|
||||
assert.ok(user && user.username)
|
||||
// TODO: should add my own sent messages to event_message so they can be reacted to?
|
||||
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 // TODO: how to handle reactions for unbridged messages? is there anything I can do?
|
||||
assert.equal(typeof parentID, "string")
|
||||
|
|
|
@ -37,7 +37,7 @@ async function sendMessage(message, guild) {
|
|||
delete eventWithoutType.$type
|
||||
|
||||
const eventID = await api.sendEvent(roomID, eventType, event, senderMxid)
|
||||
db.prepare("INSERT INTO event_message (event_id, message_id, part) VALUES (?, ?, ?)").run(eventID, message.id, eventPart)
|
||||
db.prepare("INSERT INTO event_message (event_id, message_id, part, source) VALUES (?, ?, ?, 1)").run(eventID, message.id, eventPart) // source 1 = discord
|
||||
|
||||
eventPart = 1 // TODO: use more intelligent algorithm to determine whether primary or supporting
|
||||
eventIDs.push(eventID)
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
const {test} = require("supertape")
|
||||
const assert = require("assert")
|
||||
const {messageToEvent} = require("./message-to-event")
|
||||
const data = require("../../test/data")
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
const assert = require("assert").strict
|
||||
const {sync} = require("../passthrough")
|
||||
const {sync, db} = require("../passthrough")
|
||||
|
||||
/** @type {import("./actions/send-message")}) */
|
||||
const sendMessage = sync.require("./actions/send-message")
|
||||
|
@ -18,6 +18,13 @@ module.exports = {
|
|||
const channel = client.channels.get(message.channel_id)
|
||||
const guild = client.guilds.get(channel.guild_id)
|
||||
if (message.guild_id !== "112760669178241024" && message.guild_id !== "497159726455455754") return // TODO: activate on other servers (requires the space creation flow to be done first)
|
||||
if (message.webhook_id) {
|
||||
const row = db.prepare("SELECT webhook_id FROM webhook WHERE webhook_id = ?").pluck().get(message.webhook_id)
|
||||
if (row) {
|
||||
// The message was sent by the bridge's own webhook on discord. We don't want to reflect this back, so just drop it.
|
||||
return
|
||||
}
|
||||
}
|
||||
sendMessage.sendMessage(message, guild)
|
||||
},
|
||||
|
||||
|
|
|
@ -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 => {
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
// @ts-check
|
||||
|
||||
const assert = require("assert").strict
|
||||
const DiscordTypes = require("discord-api-types/v10")
|
||||
const markdown = require("discord-markdown")
|
||||
|
||||
|
|
|
@ -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
21
m2d/converters/utils.js
Normal 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
|
16
m2d/converters/utils.test.js
Normal file
16
m2d/converters/utils.test.js
Normal 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"))
|
||||
})
|
|
@ -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)
|
||||
})
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
const {test} = require("supertape")
|
||||
const assert = require("assert")
|
||||
const {path} = require("./api")
|
||||
|
||||
test("api path: no change for plain path", t => {
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
const {test} = require("supertape")
|
||||
const assert = require("assert")
|
||||
const reg = require("./read-registration")
|
||||
|
||||
test("reg: has necessary parameters", t => {
|
||||
|
@ -8,4 +7,4 @@ test("reg: has necessary parameters", t => {
|
|||
propertiesToCheck.filter(p => p in reg),
|
||||
propertiesToCheck
|
||||
)
|
||||
})
|
||||
})
|
||||
|
|
2
stdin.js
2
stdin.js
|
@ -11,7 +11,7 @@ const createRoom = sync.require("./d2m/actions/create-room")
|
|||
const registerUser = sync.require("./d2m/actions/register-user")
|
||||
const mreq = sync.require("./matrix/mreq")
|
||||
const api = sync.require("./matrix/api")
|
||||
const sendMessage = sync.require("./m2d/actions/send-message")
|
||||
const sendEvent = sync.require("./m2d/actions/send-event")
|
||||
const guildID = "112760669178241024"
|
||||
|
||||
const extraContext = {}
|
||||
|
|
|
@ -19,3 +19,4 @@ require("../d2m/actions/create-room.test")
|
|||
require("../d2m/converters/user-to-mxid.test")
|
||||
require("../d2m/actions/register-user.test")
|
||||
require("../m2d/converters/event-to-message.test")
|
||||
require("../m2d/converters/utils.test")
|
||||
|
|
Loading…
Reference in a new issue