]> git.somenet.org - pub/jan/mattermost-bot.git/blob - modules/TACommandWipeChannel.py
new file: modules/CommandFSOpen.py
[pub/jan/mattermost-bot.git] / modules / TACommandWipeChannel.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
6
7 # pylint: disable=wrong-import-position
8 from AbstractCommand import AbstractCommand
9 class TACommandWipeChannel(AbstractCommand):
10     TRIGGER = "ta-wipe-channel"
11     CONFIG = {"display_name": "somebot-command", "auto_complete": True,
12               "auto_complete_hint": "'--really-all'|<username>",
13              }
14     CONFIG["auto_complete_desc"] = CONFIG["description"] = AbstractCommand.ICON_PRIVATE+"Delete all not-pinned posts in channel (optionally: only by <username>). There is no undo. [TEAM_ADMIN]"
15
16
17     def on_POST(self, request, data):
18         self._require_team_admin(data) # will throw an exception if not. (Dont try-except: Its handled up the stack.)
19
20         user = self.bot.api.get_user_by_username(data["text"].strip(), exc=False)
21         if data["text"].strip() in ["--really-all", "--system"] and not user:
22             request.respond_cmd_err("``/"+self.TRIGGER+"`` parameter must be '--really-all', '--system' or a valid username.")
23             return
24
25         # needs indirection. see iterators.
26         posts = list(self.bot.api.get_posts_for_channel(data['channel_id']))
27         for post in posts:
28             if post["is_pinned"]:
29                 continue
30
31             if (data["text"].strip() == "--really-all") or (data["text"].strip() == "--system" and post["type"].startswith("system_")) or (data["text"].strip() != "" and user["id"] == post["user_id"]) or (data["text"].strip() == ""):
32                 self.bot.api.delete_post(post["id"])
33
34         self.bot.debug_chan("``/ta-wipe-channel "+data["text"].strip()+"`` used by ``@"+data["user_name"]+"`` in ``"+data["team_domain"]+"::"+data["channel_name"]+"``")
35         request.respond_cmd_temp("## :white_check_mark: Success! :)")