Chore: refactor power_level sync
This commit is contained in:
parent
4a3f97ef2a
commit
f9bd2a46a1
1 changed files with 32 additions and 26 deletions
58
src/main.py
58
src/main.py
|
@ -36,23 +36,18 @@ class MlmMatrixBot:
|
|||
async def run(self):
|
||||
|
||||
logging.info("==> Sync started <==")
|
||||
|
||||
|
||||
await self._create_default_rooms()
|
||||
|
||||
current_users, all_projects = await self._load_users_and_groups()
|
||||
|
||||
await self._create_project_rooms(all_projects)
|
||||
|
||||
|
||||
logging.info("= (4/5) Loading current rooms =")
|
||||
matrix_rooms = await self._get_managed_rooms(with_power_levels=True)
|
||||
matrix_room_ids = list(map(lambda room: room['id'], matrix_rooms))
|
||||
room_name_id_map = {}
|
||||
room_id_map = {
|
||||
self._config['MATRIX_SPACE_ID']: {
|
||||
'name': "default space",
|
||||
}
|
||||
}
|
||||
room_id_map = {}
|
||||
|
||||
for room in matrix_rooms:
|
||||
room_name_id_map[room['name']] = room['id']
|
||||
|
@ -64,37 +59,43 @@ class MlmMatrixBot:
|
|||
continue
|
||||
|
||||
# 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
|
||||
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
|
||||
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))
|
||||
|
||||
|
||||
logging.info(f" \t* Syncing user {user['username']}")
|
||||
for room_id in rooms_to_join:
|
||||
# set power level
|
||||
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
|
||||
|
||||
logging.info(f"\t* joining {room_id_map[room_id]['name']}")
|
||||
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
|
||||
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:
|
||||
logging.info(f"\t* leaving {room_id_map[room_id]['name']}")
|
||||
await self._matrixHelper.remove_user_from_room(
|
||||
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 <==")
|
||||
|
||||
|
@ -134,15 +135,17 @@ class MlmMatrixBot:
|
|||
continue
|
||||
|
||||
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:
|
||||
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
|
||||
|
||||
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(
|
||||
lambda group: 'sophomorixType' in group and group['sophomorixType'] == 'project', groups))))
|
||||
|
@ -164,7 +167,8 @@ class MlmMatrixBot:
|
|||
|
||||
async def _create_project_rooms(self, projects):
|
||||
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)
|
||||
|
||||
async def _create_rooms(self, rooms):
|
||||
|
@ -184,7 +188,8 @@ class MlmMatrixBot:
|
|||
|
||||
if with_power_levels:
|
||||
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'])
|
||||
|
||||
return matrix_rooms
|
||||
|
@ -216,7 +221,8 @@ class MlmMatrixBot:
|
|||
|
||||
for configKey in requiredConfigKeys:
|
||||
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()
|
||||
config[configKey.replace('MATRIX_BOT_', '')
|
||||
] = os.environ[configKey]
|
||||
|
|
Loading…
Reference in a new issue