]> git.somenet.org - pub/jan/mattermost-bot.git/blob - modules/DialogManagedReport.py
[somebot] requirements.txt for all modules.
[pub/jan/mattermost-bot.git] / modules / DialogManagedReport.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 from inspect import cleandoc
9
10 import logging
11 logger = logging.getLogger(__name__)
12
13
14 from AbstractCommand import AbstractCommand
15 class DialogManagedReport(AbstractCommand):
16     TRIGGER = "report"
17     CONFIG = {"display_name": "somebot-command", "auto_complete": True,
18               "auto_complete_hint": "<permalink> [<reason>]",
19              }
20     CONFIG["auto_complete_desc"] = CONFIG["description"] = "Report a post. Run in same channel. Opens dialog if no reason is given."
21
22
23     def __init__(self, team_id, report_chan_id):
24         super().__init__(team_id)
25         self.report_chan_id = report_chan_id
26
27
28     def on_POST(self, request, data):
29         msg_text = data['text'].strip().split(" ", 1)
30
31         try:
32             splitpath = msg_text[0].strip().strip("/").split("/")
33             if splitpath[4] == "pl":
34                 pl_post_id = splitpath[5]
35         except:
36             request.cmd_respond_text_temp("``/"+self.TRIGGER+"`` failed: The first parameter is not a valid post-permalink. :(")
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         # do actual action
50         if len(msg_text) == 2:
51             self.bot.api.create_post(self.report_chan_id, "``AUTODELETE-MONTH`` ``/"+self.TRIGGER+"`` used by ``@"+data["user_name"]+"`` in ``"+data["team_domain"]+"::"+data["channel_name"]+"``\n#### Reported post: "+msg_text[0]+"\nReason:\n```\n"+msg_text[1].strip()+"\n```")
52             request.cmd_respond_text_temp("### ``Done.`` :)")
53         else:
54             dialog = {
55                 "callback_id": post["id"]+"-"+data["user_id"],
56                 "title": "Report post",
57                 "submit_label":"Report",
58                                 "state": post["id"],
59                 "elements":[{
60                     "display_name": "Reason",
61                     "placeholder": "Write the reason here.",
62                     "name": "report_reason",
63                     "type": "textarea",
64                     "help_text": "The reason is submitted to channel and team admin_as.",
65                     "optional": False,
66                 }]
67             }
68
69             self.bot.api.open_dialog(data["trigger_id"], self.URL+"/dialog", dialog)
70             request.respond(200, {})
71
72
73
74     def on_POST_dialog(self, request, data):
75         u = self.bot.api.get_user(data["user_id"])
76         t = self.bot.api.get_team(data["team_id"])
77         c = self.bot.api.get_channel(data["channel_id"])
78
79         self.bot.api.create_post(self.report_chan_id, "``AUTODELETE-MONTH`` ``/"+self.TRIGGER+"`` used by ``@"+u["username"]+"`` in ``"+t["name"]+"::"+c["name"]+"``\n#### Reported post: https://mattermost.fsinf.at/"+t["name"]+"/pl/"+data["state"]+"\nReason:\n```\n"+data["submission"]["report_reason"].strip()+"\n```")