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