[somebot] /ta-wipe-channel "--really-all"|<username>
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/TACommandWipeChannel.py [new file with mode: 0644]

diff --git a/modules/TACommandWipeChannel.py b/modules/TACommandWipeChannel.py
new file mode 100644 (file)
index 0000000..9938ce6
--- /dev/null
@@ -0,0 +1,34 @@
+# Mattermost Bot module.
+#  Copyright (c) 2016-2021 by Someone <someone@somenet.org> (aka. Jan Vales <jan@jvales.net>)
+#  published under MIT-License
+
+
+
+from AbstractCommand import *
+class TACommandWipeChannel(AbstractCommand):
+    TRIGGER = "ta-wipe-channel"
+    CONFIG = {"display_name": "somebot-command", "auto_complete": True,
+              "auto_complete_hint": "'--really-all'|<username>",
+             }
+    CONFIG["auto_complete_desc"] = CONFIG["description"] = "Delete all not-pinned posts in channel (optionally: only by <username>). There is no undo. [TEAM_ADMIN]"
+
+
+    def on_POST(self, request, data):
+        self._require_team_admin(data) # will throw an exception if not. (Dont try-except: Its handled up the stack.)
+
+        u = self.bot.api.get_user_by_username(data["text"].strip(), exc=False)
+        if data["text"].strip() in ["--really-all", "--system"] and not u:
+            request.cmd_respond_text_temp("### parameter must be '--really-all', '--system' or a valid username. :(")
+            return
+
+        # needs indirection. see iterators.
+        posts = [p for p in self.bot.api.get_posts_for_channel(data["channel_id"])]
+        for p in posts:
+            if p["is_pinned"]:
+                continue
+
+            if (data["text"].strip() == "--really-all") or (data["text"].strip() == "--system" and p["type"].startswith("system_")) or (data["text"].strip() != "" and u["id"] == p["user_id"]) or (data["text"].strip() == ""):
+                self.bot.api.delete_post(p["id"])
+
+        request.cmd_respond_text_temp("### ``Done.`` :)")
+        self.bot.debug_chan("``/ta-wipe-channel "+data["text"].strip()+"`` used by ``@"+data["user_name"]+"`` in ``"+data["team_domain"]+"::"+data["channel_name"]+"``")