]> git.somenet.org - pub/jan/mattermost-bot.git/blob - modules/TACommandSyncFSInf.py
modules/CommandOrder.py
[pub/jan/mattermost-bot.git] / modules / TACommandSyncFSInf.py
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
4
5 import pprint
6 from inspect import cleandoc
7 import mattermost
8
9
10 # pylint: disable=wrong-import-position
11 from AbstractCommand import *
12 class TACommandSyncFSInf(AbstractCommand):
13     TRIGGER = "sync-fsinf"
14     CONFIG = {"display_name": "somebot-command", "auto_complete": False,
15              }
16     CONFIG["auto_complete_desc"] = CONFIG["description"] = "Sync TAs to other managed teams. [BOT_ADMIN]"
17
18
19     def __init__(self, team_id, managed_teams, fsinf_intern_channelid, ignore_accounts_channelid, fsinf_teams_and_channels):
20         super().__init__(team_id)
21
22         self.managed_teams = managed_teams
23         self.fsinf_intern_channelid = fsinf_intern_channelid
24         self.ignore_accounts_channelid = ignore_accounts_channelid
25         self.fsinf_teams_and_channels = fsinf_teams_and_channels
26
27
28     def on_POST(self, request, data):
29         raise Exception ("borked. low priority todo.")
30         self._require_bot_admin(data) # will throw an exception if not. (Dont try-except: Its handled up the stack.)
31
32
33         fsinf_intern_userids = set([u["user_id"] for u in self.bot.api.get_channel_members(self.fsinf_intern_channelid)])
34         fsinf_altnasen_userids = set([u["user_id"] for u in self.bot.api.get_channel_members(self.ignore_accounts_channelid)])
35         all_users = {u["id"]:u["username"] for u in self.bot.api.get_users()}
36
37         fsinf_aktive_userids = fsinf_intern_userids-fsinf_altnasen_userids
38         self.bot.debug_chan(cleandoc("""
39             ## ``/sync-fsinf``
40             These accounts were detected to be fsinf-intern:
41             ``"""+str(sorted([all_users[u] for u in fsinf_intern_userids]))+"""``
42
43             These accounts were detected to be fsinf-altnasen (not "active" -> excluded):
44             ``"""+str(sorted([all_users[u] for u in fsinf_altnasen_userids]))+"""``
45
46             Adding active fsinf accounts to these teams (and granting TEAM_ADMIN permissions) and channels:
47             ```
48             """)+"\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("""
49             ```
50             Applying now ...
51             """))
52
53         # join into teams + make team admins
54         for user_id in fsinf_aktive_userids:
55             for _, t in self.managed_teams.items():
56                 self.bot.api.add_user_to_team(t[0], user_id, exc=False)
57                 self.bot.api.update_team_members_scheme_roles(t[0], user_id, {"scheme_user": True, "scheme_admin": True}, exc=False)
58
59             # join into special channels
60             for team_id, chan_ids in self.guest_teams_and_channels.items():
61                 for chan_id, excluded_user_ids in chan_ids.items():
62                     if not user_id in excluded_user_ids:
63                         self.bot.api.add_user_to_channel(chan_id, user_id, exc=False)
64
65         request.cmd_respond_text_temp("### ``Done.`` :)")