]> git.somenet.org - pub/jan/mattermost-bot.git/blob - modules/CACommandModGetPostText.py
core/AbstractCommand.py
[pub/jan/mattermost-bot.git] / modules / CACommandModGetPostText.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 CACommandModGetPostText(AbstractCommand):
11     TRIGGER = "mod-get-post-text"
12     CONFIG = {"display_name": "somebot-command", "auto_complete": True,
13               "auto_complete_hint": "<permalink>",
14              }
15     CONFIG["auto_complete_desc"] = CONFIG["description"] = "Get post-text removed by /mod-remove-post-text. Run in same channel. [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()
22
23         try:
24             splitpath = msg_text.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 parameter is not a valid post-permalink. :(")
29             return
30
31         post = self.bot.api.get_post(pl_post_id, exc=False)
32
33         if "status_code" in post and post["status_code"] == 404:
34             request.cmd_respond_text_temp("``/"+self.TRIGGER+"`` failed: The perma-linked post doesnt seem to exist. Was it deleted?")
35             return
36
37         if post["channel_id"] != data["channel_id"]:
38             request.cmd_respond_text_temp("``/"+self.TRIGGER+"`` failed: Must be executed in the same channel as the post. :(")
39             return
40
41         if "somebot_removed_post_text" not in post["props"]:
42             request.cmd_respond_text_temp("``/"+self.TRIGGER+"`` failed: The post has no known removed text. :(")
43             return
44
45         request.cmd_respond_text_temp("### ``Stored removed post-text``\n\n----\n\n"+post["props"]["somebot_removed_post_text"])