]> git.somenet.org - pub/jan/mattermost-bot.git/blob - modules/DialogManagedReport.py
modules/CommandTissJoin.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 from inspect import cleandoc
9
10 import logging
11 logger = logging.getLogger(__name__)
12
13
14 # pylint: disable=wrong-import-position
15 from AbstractCommand import AbstractCommand
16 class DialogManagedReport(AbstractCommand):
17     TRIGGER = "report"
18     CONFIG = {"display_name": "somebot-command", "auto_complete": True,
19               "auto_complete_hint": "<permalink> [<reason>]",
20              }
21     CONFIG["auto_complete_desc"] = CONFIG["description"] = AbstractCommand.ICON_PRIVATE+"Report a post. Run in same channel. Opens dialog if no reason is given."
22
23
24     def __init__(self, team_id, report_chan_id):
25         super().__init__(team_id)
26         self.report_chan_id = report_chan_id
27
28
29     def on_POST(self, request, data):
30         msg_text = data['text'].strip().split(" ", 1)
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         # do actual action
45         if len(msg_text) == 2:
46             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```")
47             request.respond_cmd_temp("## :white_check_mark: Success! :)")
48
49         else:
50             dialog = {
51                 "callback_id": post["id"]+"-"+data["user_id"],
52                 "title": "Report post",
53                 "submit_label":"Report",
54                 "state": post["id"],
55                 "elements":[{
56                     "display_name": "Reason",
57                     "placeholder": "Write the reason here.",
58                     "name": "report_reason",
59                     "type": "textarea",
60                     "help_text": "The reason is submitted to channel and team admin_as.",
61                     "optional": False,
62                 }]
63             }
64
65             self.bot.api.open_dialog(data["trigger_id"], self.URL+"/dialog", dialog)
66             request.respond()
67
68
69
70     def on_POST_dialog(self, request, data):
71         u = self.bot.api.get_user(data["user_id"])
72         t = self.bot.api.get_team(data["team_id"])
73         c = self.bot.api.get_channel(data["channel_id"])
74
75         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```")