]> git.somenet.org - pub/jan/mattermost.git/blob - somebot/modules/TACommandModDel.py
[somebot] /ta-mod-del <post-permalink> <reason>
[pub/jan/mattermost.git] / somebot / modules / TACommandModDel.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 # This command relies on the priviledged DB-Cleaner maint script.
6
7
8
9 from AbstractCommand import *
10 class TACommandModDel(AbstractCommand):
11     TRIGGER = "ta-mod-del"
12     CONFIG = {"display_name": "somebot-command", "auto_complete": True,
13               "auto_complete_hint": "<permalink> <reason>",
14              }
15     CONFIG["auto_complete_desc"] = CONFIG["description"] = "Delete permalinked post in 24 hours. Use <reason> to explain why. Must be executed in the same channel. [TEAM_ADMIN]"
16
17
18     def on_POST(self, request, data):
19         user = self.bot.api.get_team_member(data["team_id"], data["user_id"])
20         if "team_admin" not in user["roles"]:
21             request.cmd_respond_text_temp("### You are not a Team Admin. :(")
22             return
23
24         msg_text = data['text'].strip().split(" ", 1)
25
26         try:
27             splitpath = msg_text[0].strip().strip("/").split("/")
28             if splitpath[4] == "pl":
29                 pl_post_id = splitpath[5]
30         except:
31             request.cmd_respond_text_temp("``/ta-mod-del`` failed: The first parameter is not a valid post-permalink. :(")
32             return
33
34         if len(msg_text) == 1:
35             request.cmd_respond_text_temp("``/ta-mod-del`` failed: It seems like you did not supply any reason-text. (use --remove to disable auto-deletion) :(")
36             return
37
38         if len(msg_text[1]) > 7000:
39             request.cmd_respond_text_temp("``/ta-mod-del`` failed: reason-text must be smaller than 7000 characters :(\nYour text is: "+str(len(msg_text[1]))+" characters long.")
40             return
41
42         post = self.bot.api.get_post(pl_post_id)
43         if post["channel_id"] != data["channel_id"]:
44             request.cmd_respond_text_temp("``/ta-mod-del`` failed: must be executd in the same channel as the post. :(")
45             return
46
47         att = [{"color":"#f35912", "text": "**This post was marked for deletion in 24 hours by @"+str(data["user_name"])+". Reason:**\n"+msg_text[1]}]
48         post["props"].update(dict({"attachments":att, "cleaner_auto_delete":"day"}))
49
50         if msg_text[1] == "--remove":
51             del post["props"]["attachments"]
52             del post["props"]["cleaner_auto_delete"]
53
54         self.bot.api.patch_post(splitpath[5], post["message"], post["props"])
55         request.cmd_respond_text_temp("### ``Done.`` :)")