Skip to content

Commit

Permalink
Warn when multiple config items resolve to the same room IDs
Browse files Browse the repository at this point in the history
Insted of aborting, keep a list of resolution clash warnings, and
log the warnings instead.

Fixes #18
  • Loading branch information
aperezdc committed May 30, 2018
1 parent 4b4e506 commit 2b49302
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
6 changes: 5 additions & 1 deletion synpurge/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,14 @@ def find_event_id(room_id, upto, token=None):

log.info("Resolving room aliases")
try:
purges = purger.resolve_room_ids(c, pgdb or api)
purges, warnings = purger.resolve_room_ids(c, pgdb or api, True)
except purger.DuplicateRoomId as e:
raise SystemExit("Two configuration items resolve to the same room: {}".format(e))

if warnings:
for ex in warnings:
log.info("During alias resolution: {}".format(e))

import delorean
import itertools
now = delorean.utcnow()
Expand Down
29 changes: 17 additions & 12 deletions synpurge/purger.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,18 @@ def room_display_name(self):
@attr.s
class RoomIdsResolver(object):
_api = attr.ib()
_rooms = attr.ib(default=attr.Factory(dict),
init=False, hash=False)
_rooms = attr.ib(default=attr.Factory(dict), init=False)
_warnings = attr.ib(default=attr.Factory(list), init=False)

def __add(self, room_id, room_conf, matched_alias=None, replace=False):
old_info = self._rooms.get(room_id, None)
if not (replace or old_info is None):
raise DuplicateRoomId(room_id, old_info.config, room_conf,
old_info.matched_alias, matched_alias)
if old_info is not None:
ex = DuplicateRoomId(room_id, old_info.config, room_conf,
old_info.matched_alias, matched_alias)
if replace:
self._warnings.append(ex)
else:
raise ex
if matched_alias:
log.debug("Resolved %s -> %s (%s)",
room_conf.name, room_id, matched_alias)
Expand All @@ -78,31 +82,32 @@ def __add(self, room_id, room_conf, matched_alias=None, replace=False):
self._rooms[room_id] = PurgeInfo(room_id, room_conf,
matched_alias=matched_alias)

def resolve(self, room_conf: config.Room):
def resolve(self, room_conf: config.Room, replace: bool = False):
params = dict(access_token=room_conf.token)
if room_conf.pattern:
log.debug("Expanding room pattern: %s", room_conf.name)
room_alias_matches = room_conf.build_alias_matcher()
for room_id, room_aliases in self._api.all_rooms.items():
for room_alias in room_aliases:
if room_alias_matches(room_alias):
self.__add(room_id, room_conf, room_alias)
self.__add(room_id, room_conf, room_alias, replace)
elif room_conf.name.startswith("!"):
self.__add(room_conf.name, room_conf)
self.__add(room_conf.name, room_conf, replace)
else:
log.debug("Expanding room alias: %s", room_conf.name)
self.__add(self._api.get_room_id(room_conf.name,
params=params),
room_conf, room_conf.name)
room_conf, room_conf.name, replace)

def get_purge_info(self):
return frozenset(tuple(info for room_id, info in self._rooms.items()))
return (frozenset(tuple(info for room_id, info
in self._rooms.items())), self._warnings)


def resolve_room_ids(conf, api):
def resolve_room_ids(conf, api, replace=False):
resolver = RoomIdsResolver(api)
for room_conf in conf.rooms:
resolver.resolve(room_conf)
resolver.resolve(room_conf, replace)
return resolver.get_purge_info()


Expand Down

0 comments on commit 2b49302

Please sign in to comment.