message: Fix bug that would make rooms always public
diff: @@ -24,11 +24,8 @@ export class PublicitySyncer {
  mappings: {
  [roomId: string]: string[];
  };
  - networkToRooms: {
  - [networkId: string]: string[];
  - };
  channelIsSecret: {
  - [networkId: string]: boolean;
  + [networkIdChannel: string]: boolean;
  // '$networkId $channel': true | false
  };
  roomVisibilities: {
  @@ -36,7 +33,6 @@ export class PublicitySyncer {
  };
  } = {
  mappings: {},
  - networkToRooms: {},
  channelIsSecret: {},
  roomVisibilities: {},
  };
  @@ -84,6 +80,7 @@ export class PublicitySyncer {
  }
  public updateVisibilityMap(isMode: boolean, key: string, value: boolean, channel: string, server: IrcServer) {
  + log.debug(`updateVisibilityMap: isMode:${isMode} key:${key} value:${value} channel:${channel} server:${server.domain}`);
  let hasChanged = false;
  if (isMode) {
  if (typeof value !== 'boolean') {
  @@ -125,22 +122,14 @@ export class PublicitySyncer {
  that room is allowed to be public.
  */
  private async solveVisibility (channel: string, server: IrcServer) {
  + log.debug(`Solving visibility for ${channel} ${server.domain}`);
  + const visKey = this.getIRCVisMapKey(server.getNetworkId(), channel);
  // For each room, do a big OR on all of the channels that are linked in any way
  const mappings = await this.ircBridge.getStore().getMatrixRoomsForChannel(server, channel);
  const roomIds = mappings.map((m) => m.getId());
  this.visibilityMap.mappings = {};
  - roomIds.forEach((roomId) => {
  - const key = this.getIRCVisMapKey(server.getNetworkId(), channel);
  - // also assign reverse mapping for lookup speed later
  - if (!this.visibilityMap.networkToRooms[key]) {
  - this.visibilityMap.networkToRooms[key] = [];
  - }
  - this.visibilityMap.networkToRooms[key].push(roomId);
  - return key;
  - });
  -
  const cli = this.ircBridge.getAppServiceBridge().getBot().getClient();
  // Update rooms to correct visibilities
  let currentStates: {[roomId: string]: "public"|"private"} = {};
  @@ -153,20 +142,21 @@ export class PublicitySyncer {
  ...await this.ircBridge.getStore().getRoomsVisibility(roomIds),
  };
  - const correctState = this.visibilityMap.channelIsSecret[channel] ? 'private' : 'public';
  + const correctState = this.visibilityMap.channelIsSecret[visKey] ? 'private' : 'public';
  - log.info(`Solved visibility rules for ${channel} (${server.getNetworkId()}): ${correctState}`);
  + log.info(`Solved visibility rules for ${channel} (${server.domain}): ${correctState}`);
  return Promise.all(roomIds.map(async (roomId) => {
  const currentState = currentStates[roomId];
  // Use the server network ID of the first mapping
  // 'funNetwork #channel1' => 'funNetwork'
  - const networkId = this.visibilityMap.mappings[roomId][0].split(' ')[0];
  - if (currentState !== correctState) {
  + if (currentState === correctState) {
  + return;
  + }
  try {
  - await cli.setRoomDirectoryVisibilityAppService(networkId, roomId, correctState);
  + await cli.setRoomDirectoryVisibilityAppService(server.getNetworkId(), roomId, correctState);
  await this.ircBridge.getStore().setRoomVisibility(roomId, correctState);
  // Update cache
  this.visibilityMap.roomVisibilities[roomId] = correctState;
  @@ -174,7 +164,6 @@ export class PublicitySyncer {
  catch (ex) {
  log.error(`Failed to setRoomDirectoryVisibility (${ex.message})`);
  }
  - }
  }));
  }
  }
  