Skip to content

Commit

Permalink
dedupe code from archive/deletion channel handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
ashfame committed Oct 10, 2023
1 parent e495172 commit adea1df
Showing 1 changed file with 16 additions and 37 deletions.
53 changes: 16 additions & 37 deletions src/TeamSyncer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -375,35 +375,8 @@ export class TeamSyncer {
return;
}

// notify admins
if (this.adminRoom) {
await this.main.botIntent.sendMessage(this.adminRoom, {
msgtype: "m.notice",
body: `${teamId} removed channel ${channelId}, bridged room: ${room.MatrixRoomId}`,
});
}

try {
await this.main.botIntent.sendMessage(room.MatrixRoomId, {
msgtype: "m.notice",
body: "The Slack channel bridged to this room has been deleted.",
});
} catch (ex) {
log.warn("Failed to send deletion notice into the room:", ex);
}

// Hide deleted channels in the room directory.
try {
await this.main.botIntent.setRoomDirectoryVisibility(room.MatrixRoomId, "private");
} catch (ex) {
log.warn("Failed to hide room from the room directory:", ex);
}

try {
await this.main.actionUnlink({ matrix_room_id: room.MatrixRoomId });
} catch (ex) {
log.warn("Tried to unlink room but failed:", ex);
}
await this.notifyAdmins(`${teamId} removed channel ${channelId}, bridged room: ${room.MatrixRoomId}`);
await this.shutDownBridgedRoom("deleted", channelId);
}

public async onChannelArchived(teamId: string, channelId: string): Promise<void> {
Expand All @@ -417,32 +390,38 @@ export class TeamSyncer {
return;
}

// notify admins
await this.notifyAdmins(`${teamId} archived channel ${channelId}, bridged room: ${room.MatrixRoomId}`);
await this.shutDownBridgedRoom("archived", channelId);
}

public async notifyAdmins(message: string) {
if (this.adminRoom) {
await this.main.botIntent.sendMessage(this.adminRoom, {
msgtype: "m.notice",
body: `${teamId} archived channel ${channelId}, bridged room: ${room.MatrixRoomId}`,
body: message,
});
}
}

public async shutDownBridgedRoom(reason: string, roomId: string) {
try {
await this.main.botIntent.sendMessage(room.MatrixRoomId, {
await this.main.botIntent.sendMessage(roomId, {
msgtype: "m.notice",
body: "The Slack channel bridged to this room has been archived.",
body: `The Slack channel bridged to this room has been \`${reason}\`.`,
});
} catch (ex) {
log.warn("Failed to send archived notice into the room:", ex);
log.warn(`Failed to send \`${reason}\` notice into the room:`, ex);
}

// Hide deleted channels in the room directory.
// Hide from room directory.
try {
await this.main.botIntent.setRoomDirectoryVisibility(room.MatrixRoomId, "private");
await this.main.botIntent.setRoomDirectoryVisibility(roomId, "private");
} catch (ex) {
log.warn("Failed to hide room from the room directory:", ex);
}

try {
await this.main.actionUnlink({ matrix_room_id: room.MatrixRoomId });
await this.main.actionUnlink({ matrix_room_id: roomId });
} catch (ex) {
log.warn("Tried to unlink room but failed:", ex);
}
Expand Down

0 comments on commit adea1df

Please sign in to comment.