1 # Mattermost Bot module.
2 # Copyright (c) 2016-2022 by Someone <someone@somenet.org> (aka. Jan Vales <jan@jvales.net>)
3 # published under MIT-License
6 from inspect import cleandoc
10 from AbstractCommand import *
11 class TACommandSyncFSInf(AbstractCommand):
12 TRIGGER = "sync-fsinf"
13 CONFIG = {"display_name": "somebot-command", "auto_complete": False,
15 CONFIG["auto_complete_desc"] = CONFIG["description"] = "Sync TAs to other managed teams. [BOT_ADMIN]"
18 def __init__(self, team_id, managed_teams, fsinf_intern_channelid, ignore_accounts_channelid, fsinf_teams_and_channels):
19 super().__init__(team_id)
21 self.managed_teams = managed_teams
22 self.fsinf_intern_channelid = fsinf_intern_channelid
23 self.ignore_accounts_channelid = ignore_accounts_channelid
24 self.fsinf_teams_and_channels = fsinf_teams_and_channels
27 def on_POST(self, request, data):
28 raise Exception ("borked. low priority todo.")
29 self._require_bot_admin(data) # will throw an exception if not. (Dont try-except: Its handled up the stack.)
32 fsinf_intern_userids = set([u["user_id"] for u in self.bot.api.get_channel_members(self.fsinf_intern_channelid)])
33 fsinf_altnasen_userids = set([u["user_id"] for u in self.bot.api.get_channel_members(self.ignore_accounts_channelid)])
34 all_users = {u["id"]:u["username"] for u in self.bot.api.get_users()}
36 fsinf_aktive_userids = fsinf_intern_userids-fsinf_altnasen_userids
37 self.bot.debug_chan(cleandoc("""
39 These accounts were detected to be fsinf-intern:
40 ``"""+str(sorted([all_users[u] for u in fsinf_intern_userids]))+"""``
42 These accounts were detected to be fsinf-altnasen (not "active" -> excluded):
43 ``"""+str(sorted([all_users[u] for u in fsinf_altnasen_userids]))+"""``
45 Adding active fsinf accounts to these teams (and granting TEAM_ADMIN permissions) and channels:
47 """)+"\n"+pprint.pformat({self.bot.api.get_team(tid)["name"]:{self.bot.api.get_channel(cid)["name"]:uids for cid,uids in self.fsinf_teams_and_channels[tid].items()} for tid in self.fsinf_teams_and_channels})+"\n"+cleandoc("""
52 # join into teams + make team admins
53 for user_id in fsinf_aktive_userids:
54 for _, t in self.managed_teams.items():
55 self.bot.api.add_user_to_team(t[0], user_id, exc=False)
56 self.bot.api.update_team_members_scheme_roles(t[0], user_id, {"scheme_user": True, "scheme_admin": True}, exc=False)
58 # join into special channels
59 for team_id, chan_ids in self.guest_teams_and_channels.items():
60 for chan_id, excluded_user_ids in chan_ids.items():
61 if not user_id in excluded_user_ids:
62 self.bot.api.add_user_to_channel(chan_id, user_id, exc=False)
64 request.cmd_respond_text_temp("### ``Done.`` :)")