]> git.somenet.org - pub/jan/mattermost-bot.git/blob - modules/DialogManagedReport.py
core/AbstractCommand.py
[pub/jan/mattermost-bot.git] / modules / DialogManagedReport.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 import logging
9 logger = logging.getLogger(__name__)
10
11
12 # pylint: disable=wrong-import-position
13 from AbstractCommand import AbstractCommand
14 class DialogManagedReport(AbstractCommand):
15     TRIGGER = "report"
16     CONFIG = {"display_name": "somebot-command", "auto_complete": True,
17               "auto_complete_hint": "<permalink> [<reason>]",
18              }
19     CONFIG["auto_complete_desc"] = CONFIG["description"] = AbstractCommand.ICON_PRIVATE+"Report a post. Run in same channel. Opens dialog if no reason is given."
20
21
22     def __init__(self, team_id, report_chan_id):
23         super().__init__(team_id)
24         self.report_chan_id = report_chan_id
25
26
27     def on_POST(self, request, data):
28         msg_text = data['text'].strip().split(" ", 1)
29
30         try:
31             splitpath = msg_text[0].strip().strip("/").split("/")
32             if splitpath[4] == "pl":
33                 post = self.bot.api.get_post(splitpath[5])
34         except:
35             request.respond_cmd_err("``/"+self.TRIGGER+"`` The first parameter is not a valid post-permalink or the permalinked post has been deleted.")
36             return
37
38         if post["channel_id"] != data["channel_id"]:
39             request.respond_cmd_err("``/"+self.TRIGGER+"`` Must be executed in the same channel as the permalinked post.")
40             return
41
42         # do actual action
43         if len(msg_text) == 2:
44             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```")
45             request.respond_cmd_temp("## :white_check_mark: Success! :)")
46
47         else:
48             dialog = {
49                 "callback_id": post["id"]+"-"+data["user_id"],
50                 "title": "Report post",
51                 "submit_label":"Report",
52                 "state": post["id"],
53                 "elements":[{
54                     "display_name": "Reason",
55                     "placeholder": "Write the reason here.",
56                     "name": "report_reason",
57                     "type": "textarea",
58                     "help_text": "The reason is submitted to channel and team admin_as.",
59                     "optional": False,
60                 }]
61             }
62
63             self.bot.api.open_dialog(data["trigger_id"], self.URL+"/dialog", dialog)
64             request.respond()
65
66
67
68     def on_POST_dialog(self, request, data):
69         user = self.bot.api.get_user(data["user_id"])
70         team = self.bot.api.get_team(data["team_id"])
71         chan = self.bot.api.get_channel(data["channel_id"])
72
73         self.bot.api.create_post(self.report_chan_id, "``AUTODELETE-MONTH`` ``/"+self.TRIGGER+"`` used by ``@"+user["username"]+"`` in ``"+team["name"]+"::"+chan["name"]+"``\n#### Reported post: https://mattermost.fsinf.at/"+team["name"]+"/pl/"+data["state"]+"\nReason:\n```\n"+data["submission"]["report_reason"].strip()+"\n```")