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