Skip to content
Extraits de code Groupes Projets
Valider e0f43adf rédigé par Will Hunt's avatar Will Hunt
Parcourir les fichiers

Leave users from rooms when unbridging

parent 094b2a8b
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
...@@ -17,7 +17,7 @@ limitations under the License. ...@@ -17,7 +17,7 @@ limitations under the License.
import * as Discord from "discord.js"; import * as Discord from "discord.js";
import { DiscordBot } from "./bot"; import { DiscordBot } from "./bot";
import { Util } from "./util"; import { Util } from "./util";
import { DiscordBridgeConfig } from "./config"; import { DiscordBridgeConfig, DiscordBridgeConfigChannelDeleteOptions } from "./config";
import { Bridge } from "matrix-appservice-bridge"; import { Bridge } from "matrix-appservice-bridge";
import { Log } from "./log"; import { Log } from "./log";
import { DbRoomStore, IRoomStoreEntry } from "./db/roomstore"; import { DbRoomStore, IRoomStoreEntry } from "./db/roomstore";
...@@ -105,6 +105,20 @@ export class ChannelSyncroniser { ...@@ -105,6 +105,20 @@ export class ChannelSyncroniser {
} }
} }
public async OnUnbridge(channel: Discord.Channel, roomId: string) {
try {
const entry = (await this.roomStore.getEntriesByMatrixId(roomId))[0];
const opts = new DiscordBridgeConfigChannelDeleteOptions();
opts.namePrefix = null;
opts.topicPrefix = null;
opts.ghostsLeave = true;
await this.handleChannelDeletionForRoom(channel as Discord.TextChannel, roomId, entry);
log.info(`Channel ${channel.id} has been unbridged.`);
} catch (e) {
log.error(`Failed to unbridge channel from room: ${e}`);
}
}
public async OnDelete(channel: Discord.Channel) { public async OnDelete(channel: Discord.Channel) {
if (channel.type !== "text") { if (channel.type !== "text") {
log.info(`Channel ${channel.id} was deleted but isn't a text channel, so ignoring.`); log.info(`Channel ${channel.id} was deleted but isn't a text channel, so ignoring.`);
...@@ -269,22 +283,24 @@ export class ChannelSyncroniser { ...@@ -269,22 +283,24 @@ export class ChannelSyncroniser {
private async handleChannelDeletionForRoom( private async handleChannelDeletionForRoom(
channel: Discord.TextChannel, channel: Discord.TextChannel,
roomId: string, roomId: string,
entry: IRoomStoreEntry): Promise<void> { entry: IRoomStoreEntry,
overrideOptions?: DiscordBridgeConfigChannelDeleteOptions): Promise<void> {
log.info(`Deleting ${channel.id} from ${roomId}.`); log.info(`Deleting ${channel.id} from ${roomId}.`);
const intent = await this.bridge.getIntent(); const intent = await this.bridge.getIntent();
const options = this.config.channel.deleteOptions; const options = overrideOptions || this.config.channel.deleteOptions;
const plumbed = entry.remote!.get("plumbed"); const plumbed = entry.remote!.get("plumbed");
// tslint:disable-next-line: no-any
await this.roomStore.upsertEntry(entry); await this.roomStore.upsertEntry(entry);
if (options.ghostsLeave) { if (options.ghostsLeave) {
for (const member of channel.members.array()) { for (const member of channel.members.array()) {
try {
const mIntent = await this.bot.GetIntentFromDiscordMember(member); const mIntent = await this.bot.GetIntentFromDiscordMember(member);
mIntent.leave(roomId); // Not awaiting this because we want to do this in the background.
log.info(`${member.id} left ${roomId}.`); mIntent.leave(roomId).then(() => {
} catch (e) { log.verbose(`${member.id} left ${roomId}.`);
log.warn(`Failed to make ${member.id} leave `); }).catch(() => {
} log.warn(`Failed to make ${member.id} leave.`);
});
} }
} }
if (options.namePrefix) { if (options.namePrefix) {
...@@ -347,7 +363,6 @@ export class ChannelSyncroniser { ...@@ -347,7 +363,6 @@ export class ChannelSyncroniser {
} }
} }
} }
// Unlist
// Remove entry // Remove entry
await this.roomStore.removeEntriesByMatrixRoomId(roomId); await this.roomStore.removeEntriesByMatrixRoomId(roomId);
......
...@@ -86,7 +86,7 @@ class DiscordBridgeConfigChannel { ...@@ -86,7 +86,7 @@ class DiscordBridgeConfigChannel {
public deleteOptions = new DiscordBridgeConfigChannelDeleteOptions(); public deleteOptions = new DiscordBridgeConfigChannelDeleteOptions();
} }
class DiscordBridgeConfigChannelDeleteOptions { export class DiscordBridgeConfigChannelDeleteOptions {
public namePrefix: string | null = null; public namePrefix: string | null = null;
public topicPrefix: string | null = null; public topicPrefix: string | null = null;
public disableMessaging: boolean = false; public disableMessaging: boolean = false;
......
...@@ -20,7 +20,7 @@ import { Util, ICommandActions, ICommandParameters, CommandPermissonCheck } from ...@@ -20,7 +20,7 @@ import { Util, ICommandActions, ICommandParameters, CommandPermissonCheck } from
import { Bridge } from "matrix-appservice-bridge"; import { Bridge } from "matrix-appservice-bridge";
import { Log } from "./log"; import { Log } from "./log";
const log = new Log("MatrixCommandHandler"); const log = new Log("DiscordCommandHandler");
export class DiscordCommandHandler { export class DiscordCommandHandler {
constructor( constructor(
......
...@@ -134,7 +134,7 @@ export class MatrixCommandHandler { ...@@ -134,7 +134,7 @@ export class MatrixCommandHandler {
remoteRoom.data.discord_channel, remoteRoom.data.discord_channel,
); );
try { try {
await this.provisioner.UnbridgeChannel(res.channel); await this.provisioner.UnbridgeChannel(res.channel, event.room_id);
return "This room has been unbridged"; return "This room has been unbridged";
} catch (err) { } catch (err) {
log.error("Error while unbridging room " + event.room_id); log.error("Error while unbridging room " + event.room_id);
......
...@@ -17,9 +17,12 @@ limitations under the License. ...@@ -17,9 +17,12 @@ limitations under the License.
import * as Discord from "discord.js"; import * as Discord from "discord.js";
import { DbRoomStore, RemoteStoreRoom, MatrixStoreRoom } from "./db/roomstore"; import { DbRoomStore, RemoteStoreRoom, MatrixStoreRoom } from "./db/roomstore";
import { ChannelSyncroniser } from "./channelsyncroniser"; import { ChannelSyncroniser } from "./channelsyncroniser";
import { Log } from "./log";
const PERMISSION_REQUEST_TIMEOUT = 300000; // 5 minutes const PERMISSION_REQUEST_TIMEOUT = 300000; // 5 minutes
const log = new Log("Provisioner");
export class Provisioner { export class Provisioner {
private pendingRequests: Map<string, (approved: boolean) => void> = new Map(); // [channelId]: resolver fn private pendingRequests: Map<string, (approved: boolean) => void> = new Map(); // [channelId]: resolver fn
...@@ -38,7 +41,7 @@ export class Provisioner { ...@@ -38,7 +41,7 @@ export class Provisioner {
return this.roomStore.linkRooms(local, remote); return this.roomStore.linkRooms(local, remote);
} }
public async UnbridgeChannel(channel: Discord.TextChannel) { public async UnbridgeChannel(channel: Discord.TextChannel, rId?: string) {
const roomsRes = await this.roomStore.getEntriesByRemoteRoomData({ const roomsRes = await this.roomStore.getEntriesByRemoteRoomData({
discord_channel: channel.id, discord_channel: channel.id,
discord_guild: channel.guild.id, discord_guild: channel.guild.id,
...@@ -48,6 +51,18 @@ export class Provisioner { ...@@ -48,6 +51,18 @@ export class Provisioner {
throw Error("Channel is not bridged"); throw Error("Channel is not bridged");
} }
const remoteRoom = roomsRes[0].remote as RemoteStoreRoom; const remoteRoom = roomsRes[0].remote as RemoteStoreRoom;
let roomsToUnbridge: string[] = [];
if (rId) {
roomsToUnbridge = [rId];
} else {
// Kill em all.
roomsToUnbridge = roomsRes.map((entry) => entry.matrix!.roomId);
}
await Promise.all(roomsToUnbridge.map( async (roomId) => {
return this.channelSync.OnUnbridge(channel, roomId).catch((err) => {
log.error(`Failed to cleanly unbridge ${channel.id} ${channel.guild} from ${roomId}`);
});
}));
await this.roomStore.removeEntriesByRemoteRoomId(remoteRoom.getId()); await this.roomStore.removeEntriesByRemoteRoomId(remoteRoom.getId());
} }
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Veuillez vous inscrire ou vous pour commenter