]> git.somenet.org - pub/jan/mattermost.git/blob - somebot/modules/TACommandSyncAdmins.py
[somebot] /spoiler-adv <post-permalink> <spoiler-text>
[pub/jan/mattermost.git] / somebot / modules / TACommandSyncAdmins.py
1 # Mattermost Bot.
2 #  Copyright (c) 2016-2020 by Someone <someone@somenet.org> (aka. Jan Vales <jan@jvales.net>)
3 #  published under MIT-License
4
5
6
7 from AbstractCommand import *
8 class TACommandSyncAdmins(AbstractCommand):
9     TRIGGER = "ta-sync-admins"
10     CONFIG = {"display_name": "somebot-command", "auto_complete": True,
11     }
12     CONFIG["auto_complete_desc"] = CONFIG["description"] = "Sync TAs to other managed teams. [TEAM_ADMIN]"
13
14
15     def __init__(self, team_id, managed_teams, active_admins_channel_id, additional_user_ids=[]):
16         super().__init__(team_id)
17
18         self.managed_teams = managed_teams
19         self.active_admins_channel_id = active_admins_channel_id
20         self.additional_user_ids=additional_user_ids
21
22
23     def on_POST(self, request, data):
24         user = self.bot.api.get_team_member(data["team_id"], data["user_id"])
25         if "team_admin" not in user["roles"]:
26             request.cmd_respond_text_temp("### You are not a Team Admin. :(")
27             return
28
29         all_user_ids = [u["id"] for u in self.bot.api.get_users()]
30         users_ids = [u["user_id"] for u in self.bot.api.get_channel_members(self.active_admins_channel_id)]
31         users_ids += self.additional_user_ids
32
33         for _, t in self.managed_teams.items():
34             for u in users_ids:
35                 if t[1]:
36                     self.bot.api.add_user_to_team(t[0], u)
37                 self.bot.api.update_team_members_scheme_roles(t[0], u, {"scheme_user": True, "scheme_admin": True})
38
39             diff_uids = set(all_user_ids) - set(users_ids)
40             for u in diff_uids:
41                 self.bot.api.update_team_members_scheme_roles(t[0], u, {"scheme_user": True, "scheme_admin": False})
42
43         request.cmd_respond_text_temp("### ``Done.`` :)")
44