[somebot] /ta-sync-fsinf
authorSomeone <someone@somenet.org>
Wed, 8 Dec 2021 18:38:38 +0000 (19:38 +0100)
committerSomeone <someone@somenet.org>
Wed, 8 Dec 2021 18:38:38 +0000 (19:38 +0100)
modules/TACommandSyncFSInf.py [new file with mode: 0644]

diff --git a/modules/TACommandSyncFSInf.py b/modules/TACommandSyncFSInf.py
new file mode 100644 (file)
index 0000000..bd3d3e7
--- /dev/null
@@ -0,0 +1,62 @@
+# Mattermost Bot module.
+#  Copyright (c) 2016-2021 by Someone <someone@somenet.org> (aka. Jan Vales <jan@jvales.net>)
+#  published under MIT-License
+
+import pprint
+from inspect import cleandoc
+import mattermost
+
+
+from AbstractCommand import *
+class TACommandSyncFSInf(AbstractCommand):
+    TRIGGER = "sync-fsinf"
+    CONFIG = {"display_name": "somebot-command", "auto_complete": False,
+             }
+    CONFIG["auto_complete_desc"] = CONFIG["description"] = "Sync TAs to other managed teams. [BOT_ADMIN]"
+
+
+    def __init__(self, team_id, managed_teams, fsinf_intern_channelid, ignore_accounts_channelid, fsinf_teams_and_channels):
+        super().__init__(team_id)
+
+        self.managed_teams = managed_teams
+        self.fsinf_intern_channelid = fsinf_intern_channelid
+        self.ignore_accounts_channelid = ignore_accounts_channelid
+        self.fsinf_teams_and_channels = fsinf_teams_and_channels
+
+
+    def on_POST(self, request, data):
+        self._require_bot_admin(data) # will throw an exception if not. (Dont try-except: Its handled up the stack.)
+
+        fsinf_intern_userids = set([u["user_id"] for u in self.bot.api.get_channel_members(self.fsinf_intern_channelid)])
+        fsinf_altnasen_userids = set([u["user_id"] for u in self.bot.api.get_channel_members(self.ignore_accounts_channelid)])
+        all_users = {u["id"]:u["username"] for u in self.bot.api.get_users()}
+
+        fsinf_aktive_userids = fsinf_intern_userids-fsinf_altnasen_userids
+        self.bot.debug_chan(cleandoc("""
+            ## ``/sync-fsinf``
+            These accounts were detected to be fsinf-intern:
+            ``"""+str(sorted([all_users[u] for u in fsinf_intern_userids]))+"""``
+
+            These accounts were detected to be fsinf-altnasen (not "active" -> excluded):
+            ``"""+str(sorted([all_users[u] for u in fsinf_altnasen_userids]))+"""``
+
+            Adding active fsinf accounts to these teams (and granting TEAM_ADMIN permissions) and channels:
+            ```
+            """)+"\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("""
+            ```
+            Applying now ...
+            """))
+
+        # join into teams + make team admins
+        for user_id in fsinf_aktive_userids:
+            for _, t in self.managed_teams.items():
+                self.bot.api.add_user_to_team(t[0], user_id, exc=False)
+                self.bot.api.update_team_members_scheme_roles(t[0], user_id, {"scheme_user": True, "scheme_admin": True}, exc=False)
+
+            # join into special channels
+            for team_id, chan_ids in self.guest_teams_and_channels.items():
+                for chan_id, excluded_user_ids in chan_ids.items():
+                    if not user_id in excluded_user_ids:
+                        self.bot.api.add_user_to_channel(chan_id, user_id, exc=False)
+
+        request.cmd_respond_text_temp("### ``Done.`` :)")