]> git.somenet.org - pub/jan/mattermost-bot.git/blob - modules/TACommandSyncFSInf.py
modules/TACommandSyncFSInf.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
8
9 # pylint: disable=wrong-import-position
10 from AbstractCommand import AbstractCommand
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"] = AbstractCommand.ICON_PUBLIC+"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
31         fsinf_intern_userids = set([u["user_id"] for u in self.bot.api.get_channel_members(self.fsinf_intern_channelid)])
32         fsinf_altnasen_userids = set([u["user_id"] for u in self.bot.api.get_channel_members(self.ignore_accounts_channelid)])
33         all_users = {u["id"]:u["username"] for u in self.bot.api.get_users()}
34
35         fsinf_aktive_userids = fsinf_intern_userids-fsinf_altnasen_userids
36         request.respond_cmd_chan(cleandoc("""
37             ## ``/sync-fsinf``
38             These accounts were detected to be fsinf-intern:
39             ``"""+str(sorted([all_users[u] for u in fsinf_intern_userids]))+"""``
40
41             These accounts were detected to be fsinf-altnasen (not "active" -> excluded):
42             ``"""+str(sorted([all_users[u] for u in fsinf_altnasen_userids]))+"""``
43
44             Adding active fsinf accounts to these teams (and granting TEAM_ADMIN permissions) and channels:
45             ```
46             """)+"\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("""
47             ```
48             Applying now ...
49             """))
50
51         # join into teams + make team admins
52         for user_id in fsinf_aktive_userids:
53             for _, t in self.managed_teams.items():
54                 self.bot.api.add_user_to_team(t[0], user_id, exc=False)
55                 self.bot.api.update_team_members_scheme_roles(t[0], user_id, {"scheme_user": True, "scheme_admin": True}, exc=False)
56
57             # join into special channels
58             for team_id, chan_ids in self.fsinf_teams_and_channels.items():
59                 for chan_id, excluded_user_ids in chan_ids.items():
60                     if not user_id in excluded_user_ids:
61                         self.bot.api.add_user_to_channel(chan_id, user_id, exc=False)
62
63         request.respond_cmd_chan("## :white_check_mark: Success! :)", http_code=000)