]> git.somenet.org - pub/jan/mattermost-bot.git/blob - modules/CACommandModDelDelayed.py
modules/DialogManagedLVAFeedback.py
[pub/jan/mattermost-bot.git] / modules / CACommandModDelDelayed.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 # This command relies on the priviledged DB-Cleaner maint script.
6
7
8
9 # pylint: disable=wrong-import-position
10 from AbstractCommand import AbstractCommand
11 class CACommandModDelDelayed(AbstractCommand):
12     TRIGGER = "mod-del-delayed"
13     CONFIG = {"display_name": "somebot-command", "auto_complete": True,
14               "auto_complete_hint": "<permalink> <reason>",
15              }
16     CONFIG["auto_complete_desc"] = CONFIG["description"] = AbstractCommand.ICON_PUBLIC+"Delete permalinked post in 24 hours. Run in same channel. Use <reason> = '--abort' to abort. [CHANNEL_ADMIN]"
17
18
19     def on_POST(self, request, data):
20         self._require_channel_admin(data) # will throw an exception if not. (Dont try-except: Its handled up the stack.)
21
22         msg_text = data['text'].strip().split(" ", 1)
23
24         if len(msg_text) == 1:
25             request.respond_cmd_err("``/"+self.TRIGGER+"`` It seems like you did not supply any reason-text. (use --abort to disable auto-deletion)")
26             return
27
28         if len(msg_text[1]) > 15000:
29             request.respond_cmd_err("``/"+self.TRIGGER+"`` reason-text must be smaller than 15000 characters :(\nYour text is: "+str(len(msg_text[1]))+" characters long.")
30             return
31
32         try:
33             splitpath = msg_text[0].strip().strip("/").split("/")
34             if splitpath[4] == "pl":
35                 post = self.bot.api.get_post(splitpath[5])
36         except:
37             request.respond_cmd_err("``/"+self.TRIGGER+"`` The first parameter is not a valid post-permalink or the permalinked post has been deleted.")
38             return
39
40         if post["channel_id"] != data["channel_id"]:
41             request.respond_cmd_err("``/"+self.TRIGGER+"`` Must be executed in the same channel as the permalinked post.")
42             return
43
44         if "cmd" in post["props"] and post["props"]["cmd"] != self.TRIGGER:
45             request.respond_cmd_err("``/"+self.TRIGGER+"`` The command **``/"+post["props"]["cmd"]+"``** already affects this post. Undo said command before applying this one.")
46             return
47
48         # do actual action
49         att = [{"color":"#f35912", "text": "**This post was marked for deletion in 24 hours. Reason:**\n"+msg_text[1]}]
50         post["props"].update(dict({"attachments":att, "somecleaner_autodelete":"day", "cmd":self.TRIGGER}))
51
52         if msg_text[1] in ["--abort", "--undo", "--remove", "--clear"]:
53             del post["props"]["cmd"]
54             del post["props"]["attachments"]
55             del post["props"]["somecleaner_autodelete"]
56
57         self.bot.api.update_post(splitpath[5], message=post["message"], is_pinned=post["is_pinned"], has_reactions=post["has_reactions"], props=post["props"])
58         self.bot.debug_chan("``/"+self.TRIGGER+" "+data["text"].strip()+"`` used by ``@"+data["user_name"]+"`` in ``"+data["team_domain"]+"::"+data["channel_name"]+"``")
59         request.respond_cmd_temp("## :white_check_mark: Success! :)")