diff --git a/src/bot.ts b/src/bot.ts index f0223093e76ae07d2c5104cbaec1725583506f00..1661b5ccf190ed4136ab19f6205b51ced1efa1cd 100644 --- a/src/bot.ts +++ b/src/bot.ts @@ -101,7 +101,7 @@ export class DiscordBot { this.mxEventProcessor = new MatrixEventProcessor( new MatrixEventProcessorOpts(this.config, bridge, this), ); - this.channelSync = new ChannelSyncroniser(this.bridge, this.config, this); + this.channelSync = new ChannelSyncroniser(this.bridge, this.config, this, this.store.roomStore); } public setRoomHandler(roomHandler: MatrixRoomHandler) { @@ -487,7 +487,7 @@ export class DiscordBot { } public async GetChannelFromRoomId(roomId: string, client?: Discord.Client): Promise<Discord.Channel> { - const entries = await this.bridge.getRoomStore().getEntriesByMatrixId( + const entries = await this.store.roomStore.getEntriesByMatrixId( roomId, ); @@ -500,9 +500,9 @@ export class DiscordBot { throw Error("Room(s) not found."); } const entry = entries[0]; - const guild = client.guilds.get(entry.remote.get("discord_guild")); + const guild = client.guilds.get(entry.remote!.get("discord_guild") as string); if (guild) { - const channel = client.channels.get(entry.remote.get("discord_channel")); + const channel = client.channels.get(entry.remote!.get("discord_channel") as string); if (channel) { return channel; } @@ -558,14 +558,14 @@ export class DiscordBot { this.roomIdsForGuildCache.set(`${guild.id}:${guild.member}`, {roomIds: rooms, ts: Date.now()}); return rooms; } else { - const rooms = await this.bridge.getRoomStore().getEntriesByRemoteRoomData({ + const rooms = await this.store.roomStore.getEntriesByRemoteRoomData({ discord_guild: guild.id, }); if (rooms.length === 0) { log.verbose(`Couldn't find room(s) for guild id:${guild.id}.`); throw new Error("Room(s) not found."); } - const roomIds = rooms.map((room) => room.matrix.getId()); + const roomIds = rooms.map((room) => room.matrix!.getId()); this.roomIdsForGuildCache.set(`${guild.id}:`, {roomIds, ts: Date.now()}); return roomIds; } diff --git a/src/channelsyncroniser.ts b/src/channelsyncroniser.ts index 74932cf6215145a1292787055f61d4fa5422bde1..f0b3e7a2796c5ad2894f23a96ecf26b3b4848106 100644 --- a/src/channelsyncroniser.ts +++ b/src/channelsyncroniser.ts @@ -18,8 +18,9 @@ import * as Discord from "discord.js"; import { DiscordBot } from "./bot"; import { Util } from "./util"; import { DiscordBridgeConfig } from "./config"; -import { Bridge, RoomBridgeStore, Entry } from "matrix-appservice-bridge"; +import { Bridge } from "matrix-appservice-bridge"; import { Log } from "./log"; +import { DbRoomStore, IRoomStoreEntry } from "./db/roomstore"; const log = new Log("ChannelSync"); @@ -56,13 +57,13 @@ export interface IChannelState { } export class ChannelSyncroniser { - - private roomStore: RoomBridgeStore; constructor( private bridge: Bridge, private config: DiscordBridgeConfig, - private bot: DiscordBot) { - this.roomStore = this.bridge.getRoomStore(); + private bot: DiscordBot, + private roomStore: DbRoomStore, + ) { + } public async OnUpdate(channel: Discord.Channel) { @@ -111,7 +112,7 @@ export class ChannelSyncroniser { } log.info(`Channel ${channel.id} has been deleted.`); let roomids; - let entries; + let entries: IRoomStoreEntry[]; try { roomids = await this.GetRoomIdsFromChannel(channel); entries = await this.roomStore.getEntriesByMatrixIds(roomids); @@ -139,9 +140,6 @@ export class ChannelSyncroniser { } public async GetRoomIdsFromChannel(channel: Discord.Channel): Promise<string[]> { - if (!this.roomStore) { - this.roomStore = this.bridge.getRoomStore(); - } const rooms = await this.roomStore.getEntriesByRemoteRoomData({ discord_channel: channel.id, }); @@ -149,7 +147,7 @@ export class ChannelSyncroniser { log.verbose(`Couldn't find room(s) for channel ${channel.id}.`); return Promise.reject("Room(s) not found."); } - return rooms.map((room) => room.matrix.getId() as string); + return rooms.map((room) => room.matrix!.getId() as string); } public async GetChannelUpdateState(channel: Discord.TextChannel, forceUpdate = false): Promise<IChannelState> { @@ -159,9 +157,6 @@ export class ChannelSyncroniser { mxChannels: [], }); - if (!this.roomStore) { - this.roomStore = this.bridge.getRoomStore(); - } const remoteRooms = await this.roomStore.getEntriesByRemoteRoomData({discord_channel: channel.id}); if (remoteRooms.length === 0) { log.verbose(`Could not find any channels in room store.`); @@ -183,26 +178,26 @@ export class ChannelSyncroniser { iconUrl = `https://cdn.discordapp.com/icons/${channel.guild.id}/${icon}.png`; } remoteRooms.forEach((remoteRoom) => { - const mxid = remoteRoom.matrix.getId(); + const mxid = remoteRoom.matrix!.getId(); const singleChannelState: ISingleChannelState = Object.assign({}, DEFAULT_SINGLECHANNEL_STATE, { mxid, }); - const oldName = remoteRoom.remote.get("discord_name"); - if (remoteRoom.remote.get("update_name") && (forceUpdate || oldName !== name)) { + const oldName = remoteRoom.remote!.get("discord_name"); + if (remoteRoom.remote!.get("update_name") && (forceUpdate || oldName !== name)) { log.verbose(`Channel ${mxid} name should be updated`); singleChannelState.name = name; } - const oldTopic = remoteRoom.remote.get("discord_topic"); - if (remoteRoom.remote.get("update_topic") && (forceUpdate || oldTopic !== topic)) { + const oldTopic = remoteRoom.remote!.get("discord_topic"); + if (remoteRoom.remote!.get("update_topic") && (forceUpdate || oldTopic !== topic)) { log.verbose(`Channel ${mxid} topic should be updated`); singleChannelState.topic = topic; } - const oldIconUrl = remoteRoom.remote.get("discord_iconurl"); + const oldIconUrl = remoteRoom.remote!.get("discord_iconurl"); // no force on icon update as we don't want to duplicate ALL the icons - if (remoteRoom.remote.get("update_icon") && oldIconUrl !== iconUrl) { + if (remoteRoom.remote!.get("update_icon") && oldIconUrl !== iconUrl) { log.verbose(`Channel ${mxid} icon should be updated`); if (iconUrl !== null) { singleChannelState.iconUrl = iconUrl; @@ -227,6 +222,10 @@ export class ChannelSyncroniser { for (const channelState of channelsState.mxChannels) { let roomUpdated = false; const remoteRoom = (await this.roomStore.getEntriesByMatrixId(channelState.mxid))[0]; + if (!remoteRoom.remote) { + log.warn("Remote room not set for this room"); + return; + } if (channelState.name !== null) { log.verbose(`Updating channelname for ${channelState.mxid} to "${channelState.name}"`); await intent.setRoomName(channelState.mxid, channelState.name); @@ -274,11 +273,11 @@ export class ChannelSyncroniser { private async handleChannelDeletionForRoom( channel: Discord.TextChannel, roomId: string, - entry: Entry): Promise<void> { + entry: IRoomStoreEntry): Promise<void> { log.info(`Deleting ${channel.id} from ${roomId}.`); const intent = await this.bridge.getIntent(); const options = this.config.channel.deleteOptions; - const plumbed = entry.remote.get("plumbed"); + const plumbed = entry.remote!.get("plumbed"); this.roomStore.upsertEntry(entry); if (options.ghostsLeave) { @@ -314,7 +313,7 @@ export class ChannelSyncroniser { if (plumbed !== true) { if (options.unsetRoomAlias) { try { - const alias = `#_${entry.remote.roomId}:${this.config.bridge.domain}`; + const alias = `#_${entry.remote!.roomId}:${this.config.bridge.domain}`; const canonicalAlias = await intent.getClient().getStateEvent(roomId, "m.room.canonical_alias"); if (canonicalAlias.alias === alias) { await intent.getClient().sendStateEvent(roomId, "m.room.canonical_alias", {});