]> git.somenet.org - pub/jan/mattermost-bot.git/blob - modules/CACommandModRemovePostText.py
[somebot] WS:ConvertWebp
[pub/jan/mattermost-bot.git] / modules / CACommandModRemovePostText.py
1 # Mattermost Bot module.
2 #  Copyright (c) 2016-2021 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 AbstractCommand
10 class CACommandModRemovePostText(AbstractCommand):
11     TRIGGER = "mod-remove-post-text"
12     CONFIG = {"display_name": "somebot-command", "auto_complete": True,
13               "auto_complete_hint": "<permalink> <reason>",
14              }
15     CONFIG["auto_complete_desc"] = CONFIG["description"] = "Remove post-text, but dont delete post. Run in same channel. Cannot be undone! [CHANNEL_ADMIN]"
16
17
18     def on_POST(self, request, data):
19         self._require_channel_admin(data) # will throw an exception if not. (Dont try-except: Its handled up the stack.)
20
21         msg_text = data['text'].strip().split(" ", 1)
22
23         try:
24             splitpath = msg_text[0].strip().strip("/").split("/")
25             if splitpath[4] == "pl":
26                 pl_post_id = splitpath[5]
27         except:
28             request.cmd_respond_text_temp("``/"+self.TRIGGER+"`` failed: The first parameter is not a valid post-permalink. :(")
29             return
30
31         if len(msg_text) == 1:
32             request.cmd_respond_text_temp("``/"+self.TRIGGER+"`` failed: It seems like you did not supply any reason-text. :(")
33             return
34
35         if len(msg_text[1]) > 15000:
36             request.cmd_respond_text_temp("``/"+self.TRIGGER+"`` failed: reason-text must be smaller than 15000 characters :(\nYour text is: "+str(len(msg_text[1]))+" characters long.")
37             return
38
39         post = self.bot.api.get_post(pl_post_id, exc=False)
40
41         if "status_code" in post and post["status_code"] == 404:
42             request.cmd_respond_text_temp("``/"+self.TRIGGER+"`` failed: The perma-linked post doesnt seem to exist. Was it deleted?")
43             return
44
45         if post["channel_id"] != data["channel_id"]:
46             request.cmd_respond_text_temp("``/"+self.TRIGGER+"`` failed: Must be executed in the same channel as the post. :(")
47             return
48
49         if "cmd" in post["props"] and post["props"]["cmd"] != self.TRIGGER:
50             request.cmd_respond_text_temp("``/"+self.TRIGGER+"`` failed: The command **``/"+post["props"]["cmd"]+"``** already affects this post. Undo said command before applying this one. :(")
51             return
52
53         if post["message"].strip() == "":
54             request.cmd_respond_text_temp("``/"+self.TRIGGER+"`` failed: This command doesnt work on empty messages and cant be undone.\nIf you are an admin_a you can use ``/mod-get-post-text`` to view the original text. :(")
55             return
56
57         # do actual action
58         att = [{"color":"#f35912", "text": "**Post-text removed. Reason:**\n"+msg_text[1]}]
59         post["props"].update(dict({"attachments":att, "somebot_removed_post_text":post["message"], "cmd":self.TRIGGER}))
60
61 #        if msg_text[1] in ["--abort", "--undo", "--remove", "--clear"]:
62 #            del post["props"]["cmd"]
63 #            del post["props"]["attachments"]
64 #            del post["props"]["somebot_removed_post_text"]
65 #
66         self.bot.api.update_post(splitpath[5], message=None, is_pinned=post["is_pinned"], has_reactions=post["has_reactions"], props=post["props"])
67         self.bot.debug_chan("``/"+self.TRIGGER+" "+data["text"].strip()+"`` used by ``@"+data["user_name"]+"`` in ``"+data["team_domain"]+"::"+data["channel_name"]+"``")
68         request.cmd_respond_text_temp("### ``Done.`` :)")