]> git.somenet.org - pub/jan/mattermost-bot.git/blob - modules/DialogManagedReport.py
core/AbstractPublicWS.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 given 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": "What's going on?",
55                     "name": "report_reason",
56                     "type": "radio",
57                     "options": [{
58                             "text": "It's annoying or not interesting",
59                             "value": "not_interesting"
60                         },{
61                             "text": "I'm in this post and I don't like it",
62                             "value": "im_in_it_and_i_dont_like_it"
63                         },{
64                             "text": "I think it shouldn't be on Mattermost",
65                             "value": "should_not_be_on_mm"
66                         },{
67                             "text": "It's spam",
68                             "value": "spam"
69                         },{
70                             "text": "Other reason (Fill in below)",
71                             "value": "other_reason"
72                         }
73                     ],
74                     "optional": False,
75                     "default": "other_reason"
76                 },{
77                     "display_name": "Other reason or additional details",
78                     "placeholder": "Write a custom reason or additional details here.",
79                     "name": "report_other_reason",
80                     "type": "textarea",
81                     "help_text": "The report is submitted to channel and team admin_as.",
82                     "optional": True,
83                 }]
84             }
85
86             self.bot.api.open_dialog(data["trigger_id"], self.URL+"/dialog", dialog)
87             request.respond()
88
89
90
91     def on_POST_dialog(self, request, data):
92         user = self.bot.api.get_user(data["user_id"])
93         team = self.bot.api.get_team(data["team_id"])
94         chan = self.bot.api.get_channel(data["channel_id"])
95
96         if "report_other_reason" in data["submission"] and data["submission"]["report_other_reason"]:
97             report_other_reason = "\nAdditional details/other reason:\n```\n"+data["submission"]["report_other_reason"].strip()+"\n```"
98         else:
99             report_other_reason = ""
100             if "report_reason" in data["submission"] and data["submission"]["report_reason"] == "other_reason":
101                 request.respond(200, {"errors": {"report_reason": "Please write some details below, if selecting 'other reason'"}})
102                 return
103
104         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 selected: **``"+data["submission"]["report_reason"]+"``**"+report_other_reason)