Compare commits

..

No commits in common. "b34932d55276d2e2ba12eb05fc136c58f0aec740" and "5810fb3955d7be41addba54149b9e27108f6e973" have entirely different histories.

10 changed files with 19 additions and 81 deletions

View file

@ -2,7 +2,6 @@
const assert = require("assert").strict
const DiscordTypes = require("discord-api-types/v10")
const reg = require("../../matrix/read-registration")
const passthrough = require("../../passthrough")
const { discord, sync, db } = passthrough
@ -62,16 +61,11 @@ async function channelToKState(channel, guild) {
const spaceID = db.prepare("SELECT space_id FROM guild_space WHERE guild_id = ?").pluck().get(guild.id)
assert.ok(typeof spaceID === "string")
const row = db.prepare("SELECT nick, custom_avatar FROM channel_room WHERE channel_id = ?").get(channel.id)
assert(row)
const customName = row.nick
const customAvatar = row.custom_avatar
const customName = db.prepare("SELECT nick FROM channel_room WHERE channel_id = ?").pluck().get(channel.id)
const [convertedName, convertedTopic] = convertNameAndTopic(channel, guild, customName)
const avatarEventContent = {}
if (customAvatar) {
avatarEventContent.url = customAvatar
} else if (guild.icon) {
if (guild.icon) {
avatarEventContent.discord_path = file.guildIcon(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
}
@ -86,7 +80,7 @@ async function channelToKState(channel, guild) {
"m.room.guest_access/": {guest_access: "can_join"},
"m.room.history_visibility/": {history_visibility},
[`m.space.parent/${spaceID}`]: {
via: [reg.ooye.server_name],
via: ["cadence.moe"], // TODO: put the proper server here
canonical: true
},
"m.room.join_rules/": {
@ -189,8 +183,6 @@ async function _syncRoom(channelID, shouldActuallySync) {
return creation // Naturally, the newly created room is already up to date, so we can always skip syncing here.
}
const roomID = existing.room_id
if (!shouldActuallySync) {
return existing.room_id // only need to ensure room exists, and it does. return the room ID
}
@ -200,16 +192,15 @@ async function _syncRoom(channelID, shouldActuallySync) {
const {spaceID, channelKState} = await channelToKState(channel, guild)
// sync channel state to room
const roomKState = await roomToKState(roomID)
const roomKState = await roomToKState(existing.room_id)
const roomDiff = ks.diffKState(roomKState, channelKState)
const roomApply = applyKStateDiffToRoom(roomID, roomDiff)
db.prepare("UPDATE channel_room SET name = ? WHERE room_id = ?").run(channel.name, roomID)
const roomApply = applyKStateDiffToRoom(existing.room_id, roomDiff)
// sync room as space member
const spaceApply = _syncSpaceMember(channel, spaceID, roomID)
const spaceApply = _syncSpaceMember(channel, spaceID, existing.room_id)
await Promise.all([roomApply, spaceApply])
return roomID
return existing.room_id
}
async function _unbridgeRoom(channelID) {
@ -253,7 +244,7 @@ async function _syncSpaceMember(channel, spaceID, roomID) {
&& !channel["thread_metadata"]?.archived // archived threads do not belong in the space (don't offer people conversations that are no longer relevant)
) {
spaceEventContent = {
via: [reg.ooye.server_name]
via: ["cadence.moe"] // TODO: use the proper server
}
}
const spaceDiff = ks.diffKState(spaceKState, {
@ -288,7 +279,5 @@ module.exports.ensureRoom = ensureRoom
module.exports.syncRoom = syncRoom
module.exports.createAllForGuild = createAllForGuild
module.exports.channelToKState = channelToKState
module.exports.roomToKState = roomToKState
module.exports.applyKStateDiffToRoom = applyKStateDiffToRoom
module.exports._convertNameAndTopic = convertNameAndTopic
module.exports._unbridgeRoom = _unbridgeRoom

View file

@ -4,15 +4,13 @@ const assert = require("assert")
const DiscordTypes = require("discord-api-types/v10")
const passthrough = require("../../passthrough")
const { discord, sync, db } = passthrough
const { sync, db } = passthrough
/** @type {import("../../matrix/api")} */
const api = sync.require("../../matrix/api")
/** @type {import("../../matrix/file")} */
const file = sync.require("../../matrix/file")
/** @type {import("./create-room")} */
const createRoom = sync.require("./create-room")
/** @type {import("../../matrix/kstate")} */
const ks = sync.require("../../matrix/kstate")
/**
* @param {import("discord-api-types/v10").RESTGetAPIGuildResult} guild
@ -60,7 +58,7 @@ async function guildToKState(guild) {
"m.room.name/": {name: guild.name},
"m.room.avatar/": avatarEventContent,
"m.room.guest_access/": {guest_access: "can_join"}, // guests can join space if other conditions are met
"m.room.history_visibility/": {history_visibility: "invited"} // any events sent after user was invited are visible
"m.room.history_visibility": {history_visibility: "invited"} // any events sent after user was invited are visible
}
return guildKState
@ -71,26 +69,17 @@ async function syncSpace(guildID) {
const guild = discord.guilds.get(guildID)
assert.ok(guild)
/** @type {string?} */
const spaceID = db.prepare("SELECT space_id from guild_space WHERE guild_id = ?").pluck().get(guildID)
/** @type {{room_id: string, thread_parent: string?}} */
const existing = db.prepare("SELECT space_id from guild_space WHERE guild_id = ?").get(guildID)
const guildKState = await guildToKState(guild)
if (!spaceID) {
if (!existing) {
const spaceID = await createSpace(guild, guildKState)
return spaceID // Naturally, the newly created space is already up to date, so we can always skip syncing here.
return spaceID
}
console.log(`[space sync] to matrix: ${guild.name}`)
// sync channel state to room
const spaceKState = await createRoom.roomToKState(spaceID)
const spaceDiff = ks.diffKState(spaceKState, guildKState)
await createRoom.applyKStateDiffToRoom(spaceID, spaceDiff)
return spaceID
}
module.exports.createSpace = createSpace
module.exports.syncSpace = syncSpace
module.exports.guildToKState = guildToKState

View file

@ -21,7 +21,7 @@ async function createSim(user) {
// Choose sim name
const simName = userToMxid.userToSimName(user)
const localpart = reg.ooye.namespace_prefix + simName
const mxid = `@${localpart}:${reg.ooye.server_name}`
const mxid = "@" + localpart + ":cadence.moe"
// Save chosen name in the database forever
// Making this database change right away so that in a concurrent registration, the 2nd registration will already have generated a different localpart because it can see this row when it generates

View file

@ -82,10 +82,7 @@ const utils = {
// Event dispatcher for OOYE bridge operations
try {
if (message.t === "GUILD_UPDATE") {
await eventDispatcher.onGuildUpdate(client, message.d)
} else if (message.t === "CHANNEL_UPDATE") {
if (message.t === "CHANNEL_UPDATE") {
await eventDispatcher.onChannelOrThreadUpdate(client, message.d, false)
} else if (message.t === "THREAD_CREATE") {

View file

@ -14,8 +14,6 @@ const addReaction = sync.require("./actions/add-reaction")
const announceThread = sync.require("./actions/announce-thread")
/** @type {import("./actions/create-room")}) */
const createRoom = sync.require("./actions/create-room")
/** @type {import("./actions/create-space")}) */
const createSpace = sync.require("./actions/create-space")
/** @type {import("../matrix/api")}) */
const api = sync.require("../matrix/api")
@ -118,16 +116,6 @@ module.exports = {
await announceThread.announceThread(parentRoomID, threadRoomID, thread)
},
/**
* @param {import("./discord-client")} client
* @param {import("discord-api-types/v10").GatewayGuildUpdateDispatchData} guild
*/
async onGuildUpdate(client, guild) {
const spaceID = db.prepare("SELECT space_id FROM guild_space WHERE guild_id = ?").pluck().get(guild.id)
if (!spaceID) return
await createSpace.syncSpace(guild.id)
},
/**
* @param {import("./discord-client")} client
* @param {import("discord-api-types/v10").GatewayChannelUpdateDispatchData} channelOrThread

View file

@ -11,7 +11,6 @@ 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...
// TODO that only applies to the above todo: But you'd have to watch out for the /icon command, where the bridge bot would set the room avatar, and that shouldn't be reflected into the room a second time.
if (userRegex.some(x => sender.match(x))) {
return true
}

View file

@ -6,7 +6,7 @@
const util = require("util")
const Ty = require("../types")
const {db, sync, as} = require("../passthrough")
const {sync, as} = require("../passthrough")
/** @type {import("./actions/send-event")} */
const sendEvent = sync.require("./actions/send-event")
@ -69,14 +69,3 @@ async event => {
if (utils.eventSenderIsFromDiscord(event.sender)) return
await addReaction.addReaction(event)
}))
sync.addTemporaryListener(as, "type:m.room.avatar", guard("m.room.avatar",
/**
* @param {Ty.Event.StateOuter<Ty.Event.M_Room_Avatar>} event
*/
async event => {
if (event.state_key !== "") return
if (utils.eventSenderIsFromDiscord(event.sender)) return
const url = event.content.url || null
db.prepare("UPDATE channel_room SET custom_avatar = ? WHERE room_id = ?").run(url, event.room_id)
}))

View file

@ -167,8 +167,6 @@ async function profileSetAvatarUrl(mxid, avatar_url) {
* @param {number} power
*/
async function setUserPower(roomID, mxid, power) {
assert(roomID[0] === "!")
assert(mxid[0] === "@")
// 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 || {}

View file

@ -42,7 +42,6 @@ function diffKState(actual, target) {
const diff = {}
// go through each key that it should have
for (const key of Object.keys(target)) {
if (!key.includes("/")) throw new Error(`target kstate's key "${key}" does not contain a slash separator; if a blank state_key was intended, add a trailing slash to the kstate key.`)
if (key in actual) {
// diff
try {

14
types.d.ts vendored
View file

@ -19,7 +19,6 @@ export type AppServiceRegistrationConfig = {
ooye: {
namespace_prefix: string
max_file_size: number
server_name: string
}
}
@ -28,7 +27,7 @@ export type WebhookCreds = {
token: string
}
export namespace Event {
namespace Event {
export type Outer<T> = {
type: string
room_id: string
@ -39,10 +38,6 @@ export namespace Event {
event_id: string
}
export type StateOuter<T> = Outer<T> & {
state_key: string
}
export type ReplacementContent<T> = T & {
"m.new_content": T
"m.relates_to": {
@ -79,11 +74,6 @@ export namespace Event {
avatar_url?: string
}
export type M_Room_Avatar = {
discord_path?: string
url?: string
}
export type M_Reaction = {
"m.relates_to": {
rel_type: "m.annotation"
@ -93,7 +83,7 @@ export namespace Event {
}
}
export namespace R {
namespace R {
export type RoomCreated = {
room_id: string
}