]> git.somenet.org - pub/jan/mattermost.git/blob - somebot/modules/TACommandWipeChannel.py
[somebot] /ta-wipe-channel "--really-all"|<username>
[pub/jan/mattermost.git] / somebot / modules / TACommandWipeChannel.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 import logging
6
7
8 from AbstractCommand import *
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"] = "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         u = self.bot.api.get_user_by_username(data["text"].strip())
21         if data["text"].strip() != "--really-all" and len(u) == 0:
22             request.cmd_respond_text_temp("### parameter must be '--really-all' or a valid username. :(")
23             return
24
25         # needs indirection. see iterators.
26         posts = [p for p in self.bot.api.get_posts_for_channel(data["channel_id"])]
27         for p in posts:
28             if p["is_pinned"]:
29                 continue
30
31             if (data["text"].strip() == "--really-all") or (data["text"].strip() != "" and u[0]["id"] == p["user_id"]) or (data["text"].strip() == ""):
32                 self.bot.api.delete_post(p["id"])
33
34         request.cmd_respond_text_temp("### ``Done.`` :)")
35         self.bot.debug_chan("``/ta-wipe-channel "+data["text"].strip()+"`` used by ``@"+data["user_name"]+"`` in ``"+data["team_domain"]+"::"+data["channel_name"]+"``")