Self-service removal of channel links

This commit is contained in:
Travis Ralston 2018-03-31 15:48:20 -06:00
parent f33f7e5716
commit 9a8041ea38
5 changed files with 50 additions and 3 deletions

View File

@ -46,4 +46,20 @@ export class DimensionIrcService {
LogService.info("DimensionIrcService", userId + " requested #" + channelNoHash + " on " + networkId + " to be linked to " + roomId); LogService.info("DimensionIrcService", userId + " requested #" + channelNoHash + " on " + networkId + " to be linked to " + roomId);
return {}; // 200 OK return {}; // 200 OK
} }
@POST
@Path(":networkId/channel/:channel/unlink/:roomId")
public async unlink(@QueryParam("scalar_token") scalarToken: string, @PathParam("networkId") networkId: string, @PathParam("channel") channelNoHash: string, @PathParam("roomId") roomId: string): Promise<any> {
const userId = await ScalarService.getTokenOwner(scalarToken);
const parsed = IrcBridge.parseNetworkId(networkId);
const bridge = await IrcBridgeRecord.findByPrimary(parsed.bridgeId);
if (!bridge) throw new ApiError(404, "Bridge not found");
const client = new IrcBridge(userId);
await client.removeLink(bridge, parsed.bridgeNetworkId, "#" + channelNoHash, roomId);
LogService.info("DimensionIrcService", userId + " unlinked #" + channelNoHash + " on " + networkId + " from " + roomId);
return {}; // 200 OK
}
} }

View File

@ -127,6 +127,25 @@ export class IrcBridge {
} }
} }
public async removeLink(bridge: IrcBridgeRecord, networkId: string, channel: string, inRoomId: string): Promise<any> {
const network = (await this.getAllNetworks()).find(n => n.isEnabled && n.ircBridgeId === bridge.id && n.bridgeNetworkId === networkId);
if (!network) throw new Error("Network not found");
const requestBody = {
remote_room_server: network.domain,
remote_room_channel: channel,
matrix_room_id: inRoomId,
user_id: this.requestingUserId,
};
if (bridge.upstreamId) {
delete requestBody["user_id"];
await this.doUpstreamRequest(bridge, "POST", "/bridges/irc/_matrix/provision/unlink", null, requestBody);
} else {
await this.doProvisionRequest(bridge, "POST", "/_matrix/provision/unlink", null, requestBody);
}
}
private async getAllNetworks(): Promise<CachedNetwork[]> { private async getAllNetworks(): Promise<CachedNetwork[]> {
const cached = Cache.for(CACHE_IRC_BRIDGE).get("networks"); const cached = Cache.for(CACHE_IRC_BRIDGE).get("networks");
if (cached) return cached; if (cached) return cached;

View File

@ -1,5 +1,4 @@
Release checklist: Release checklist:
* IRC Bridge
* Update documentation * Update documentation
* Configuration migration (if possible) * Configuration migration (if possible)
* Final testing (widgets, bots, etc) * Final testing (widgets, bots, etc)

View File

@ -127,7 +127,16 @@ export class IrcBridgeConfigComponent extends BridgeComponent<IrcConfig> {
return channels; return channels;
} }
public removeChannel(channel: any) { public removeChannel(channel: LocalChannel) {
console.log(channel); this.isUpdating = true;
this.irc.removeLink(this.roomId, channel.networkId, channel.name.substring(1)).then(() => {
this.isUpdating = false;
const idx = this.bridge.config.links[channel.networkId].findIndex(c => c.channelName === channel.name);
if (idx !== -1) this.bridge.config.links[channel.networkId].splice(idx, 1);
this.toaster.pop("success", "Link removed");
}).catch(err => {
console.error(err);
this.toaster.pop("error", "Failed to remove link");
});
} }
} }

View File

@ -15,4 +15,8 @@ export class IrcApiService extends AuthedApi {
public requestLink(roomId: string, networkId: string, channelNoHash: string, op: string): Promise<any> { public requestLink(roomId: string, networkId: string, channelNoHash: string, op: string): Promise<any> {
return this.authedPost("/api/v1/dimension/irc/" + networkId + "/channel/" + channelNoHash + "/link/" + roomId, {op: op}).map(r => r.json()).toPromise(); return this.authedPost("/api/v1/dimension/irc/" + networkId + "/channel/" + channelNoHash + "/link/" + roomId, {op: op}).map(r => r.json()).toPromise();
} }
public removeLink(roomId: string, networkId: string, channelNoHash: string): Promise<any> {
return this.authedPost("/api/v1/dimension/irc/" + networkId + "/channel/" + channelNoHash + "/unlink/" + roomId).map(r => r.json()).toPromise();
}
} }