Chore: refactor power_level sync

This commit is contained in:
Dorian Zedler 2022-09-29 21:31:07 +02:00
parent 4a3f97ef2a
commit f9bd2a46a1
Signed by: dozedler
GPG key ID: 989DE36109AFA354

View file

@ -43,16 +43,11 @@ class MlmMatrixBot:
await self._create_project_rooms(all_projects) await self._create_project_rooms(all_projects)
logging.info("= (4/5) Loading current rooms =") logging.info("= (4/5) Loading current rooms =")
matrix_rooms = await self._get_managed_rooms(with_power_levels=True) matrix_rooms = await self._get_managed_rooms(with_power_levels=True)
matrix_room_ids = list(map(lambda room: room['id'], matrix_rooms)) matrix_room_ids = list(map(lambda room: room['id'], matrix_rooms))
room_name_id_map = {} room_name_id_map = {}
room_id_map = { room_id_map = {}
self._config['MATRIX_SPACE_ID']: {
'name': "default space",
}
}
for room in matrix_rooms: for room in matrix_rooms:
room_name_id_map[room['name']] = room['id'] room_name_id_map[room['name']] = room['id']
@ -64,37 +59,43 @@ class MlmMatrixBot:
continue continue
# default groups and space # default groups and space
rooms_to_join = self._get_default_rooms() + [self._config["MATRIX_SPACE_ID"]] rooms_to_join = self._get_default_rooms(
) + [self._config["MATRIX_SPACE_ID"]]
# projects # projects
rooms_to_join += list(map(lambda group: group.replace('p_', ''), filter(lambda project: project.startswith("p_"), user['groups']))) rooms_to_join += list(map(lambda group: group.replace('p_', ''),
filter(lambda project: project.startswith("p_"), user['groups'])))
# resolve names to ids # resolve names to ids
rooms_to_join = list(map( rooms_to_join = list(map(
lambda room: room if room.startswith("!") else room_name_id_map[room], lambda room: room if room.startswith(
"!") else room_name_id_map[room],
rooms_to_join)) rooms_to_join))
logging.info(f" \t* Syncing user {user['username']}") logging.info(f" \t* Syncing user {user['username']}")
for room_id in rooms_to_join: for room_id in rooms_to_join:
# set power level # set power level
if room_id in user['rooms']: if room_id in user['rooms']:
if ( user['matrix_username'] in room_id_map[room_id]['power_levels'] and room_id_map[room_id]['power_levels'][user['matrix_username']] != user['power_level']) or (user['matrix_username'] not in room_id_map[room_id]['power_levels'] and user['power_level'] != 0):
logging.info(f"\t\t* Setting power level in {room_id_map[room_id]['name']} to {user['power_level']}")
await self._matrixHelper.set_user_power_level_in_room(user['matrix_username'], room_id, user['power_level'])
continue continue
logging.info(f"\t* joining {room_id_map[room_id]['name']}") logging.info(f"\t* joining {room_id_map[room_id]['name']}")
await self._matrixHelper.add_user_to_room(user['matrix_username'], room_id) await self._matrixHelper.add_user_to_room(user['matrix_username'], room_id)
if user['power_level'] != 0:
logging.info(f"\t\t* Setting power level in {room_id_map[room_id]['name']} to {user['power_level']}")
await self._matrixHelper.set_user_power_level_in_room(user['matrix_username'], room_id, user['power_level'])
# remove from rooms that are not in the groups # remove from rooms that are not in the groups
rooms_to_be_left = list(filter(lambda room: room in matrix_room_ids and not room in rooms_to_join, user['rooms'])) rooms_to_be_left = list(filter(
lambda room: room in matrix_room_ids and not room in rooms_to_join, user['rooms']))
for room_id in rooms_to_be_left: for room_id in rooms_to_be_left:
logging.info(f"\t* leaving {room_id_map[room_id]['name']}") logging.info(f"\t* leaving {room_id_map[room_id]['name']}")
await self._matrixHelper.remove_user_from_room( await self._matrixHelper.remove_user_from_room(
user['matrix_username'], room_id) user['matrix_username'], room_id)
# update power levels
for room in matrix_rooms:
if not room['id'] in rooms_to_join and not room['id'] in user['rooms']:
continue
if (user['matrix_username'] in room['power_levels'] and room['power_levels'][user['matrix_username']] != user['power_level']) or (user['matrix_username'] not in room['power_levels'] and user['power_level'] != 0):
logging.info(
f"\t\t* Setting power level in {room['name']} to {user['power_level']}")
await self._matrixHelper.set_user_power_level_in_room(user['matrix_username'], room['id'], user['power_level'])
logging.info("==> Sync finished <==") logging.info("==> Sync finished <==")
@ -134,15 +135,17 @@ class MlmMatrixBot:
continue continue
rc, raw_groups = self._ldapHelper.search(f"(&(member:1.2.840.113556.1.4.1941:={ldap_user[0]['distinguishedName']})(|(sophomorixType=adminclass)(sophomorixType=project)))", [ rc, raw_groups = self._ldapHelper.search(f"(&(member:1.2.840.113556.1.4.1941:={ldap_user[0]['distinguishedName']})(|(sophomorixType=adminclass)(sophomorixType=project)))", [
'sAMAccountName', 'sophomorixType', 'sophomorixMailList']) 'sAMAccountName', 'sophomorixType', 'sophomorixMailList'])
if not rc: if not rc:
continue continue
all_groups = list(map(lambda group: group['sAMAccountName'], raw_groups)) all_groups = list(
map(lambda group: group['sAMAccountName'], raw_groups))
power_level = 100 if self._config['ADMIN_GROUP'] in all_groups else 50 if self._config['MODERATOR_GROUP'] in all_groups else 0 power_level = 100 if self._config['ADMIN_GROUP'] in all_groups else 50 if self._config['MODERATOR_GROUP'] in all_groups else 0
groups = list(filter(lambda group: group['sophomorixMailList'] == 'TRUE' or group['sophomorixType'] == "adminclass", raw_groups)) groups = list(filter(lambda group: group['sophomorixMailList'] ==
'TRUE' or group['sophomorixType'] == "adminclass", raw_groups))
all_projects.extend(list(map(lambda group: group['sAMAccountName'], filter( all_projects.extend(list(map(lambda group: group['sAMAccountName'], filter(
lambda group: 'sophomorixType' in group and group['sophomorixType'] == 'project', groups)))) lambda group: 'sophomorixType' in group and group['sophomorixType'] == 'project', groups))))
@ -164,7 +167,8 @@ class MlmMatrixBot:
async def _create_project_rooms(self, projects): async def _create_project_rooms(self, projects):
logging.info("= (3/5) Creating project rooms =") logging.info("= (3/5) Creating project rooms =")
projects = list(map(lambda project: project.replace('p_', ''), projects)) projects = list(
map(lambda project: project.replace('p_', ''), projects))
await self._create_rooms(projects) await self._create_rooms(projects)
async def _create_rooms(self, rooms): async def _create_rooms(self, rooms):
@ -184,7 +188,8 @@ class MlmMatrixBot:
if with_power_levels: if with_power_levels:
for i in range(len(matrix_rooms)): for i in range(len(matrix_rooms)):
logging.info(f"\t* Getting power levels of room {matrix_rooms[i]['name']}") logging.info(
f"\t* Getting power levels of room {matrix_rooms[i]['name']}")
matrix_rooms[i]['power_levels'] = await self._matrixHelper.get_room_power_levels(matrix_rooms[i]['id']) matrix_rooms[i]['power_levels'] = await self._matrixHelper.get_room_power_levels(matrix_rooms[i]['id'])
return matrix_rooms return matrix_rooms
@ -216,7 +221,8 @@ class MlmMatrixBot:
for configKey in requiredConfigKeys: for configKey in requiredConfigKeys:
if configKey not in os.environ: if configKey not in os.environ:
logging.error(f"Required environment value {configKey} is not set") logging.error(
f"Required environment value {configKey} is not set")
sys.exit() sys.exit()
config[configKey.replace('MATRIX_BOT_', '') config[configKey.replace('MATRIX_BOT_', '')
] = os.environ[configKey] ] = os.environ[configKey]