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
5 # This command relies on the priviledged DB-Cleaner maint script.
8 from inspect import cleandoc
11 logger = logging.getLogger(__name__)
14 from AbstractCommand import AbstractCommand
15 class DialogManagedReport(AbstractCommand):
17 CONFIG = {"display_name": "somebot-command", "auto_complete": True,
18 "auto_complete_hint": "<permalink> [<reason>]",
20 CONFIG["auto_complete_desc"] = CONFIG["description"] = "Report a post. Run in same channel. Opens dialog if no reason is given."
23 def __init__(self, team_id, report_chan_id):
24 super().__init__(team_id)
25 self.report_chan_id = report_chan_id
28 def on_POST(self, request, data):
29 msg_text = data['text'].strip().split(" ", 1)
31 pl_post_id = "--invalid--"
33 splitpath = msg_text[0].strip().strip("/").split("/")
34 if splitpath[4] == "pl":
35 pl_post_id = splitpath[5]
37 request.respond_err_temp("``/"+self.TRIGGER+"`` failed: The first parameter is not a valid post-permalink. :(")
41 request.respond_err_temp("``/"+self.TRIGGER+"`` failed: The first parameter is not a valid post-permalink. :(")
45 post = self.bot.api.get_post(pl_post_id, exc=False)
48 if "status_code" in post and post["status_code"] == 404:
49 request.respond_err_temp("``/"+self.TRIGGER+"`` failed: The perma-linked post doesnt seem to exist. Was it deleted?")
52 if post["channel_id"] != data["channel_id"]:
53 request.respond_err_temp("``/"+self.TRIGGER+"`` failed: Must be executed in the same channel as the post. :(")
57 if len(msg_text) == 2:
58 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```")
59 request.cmd_respond_text_temp("### ``Done.`` :)")
62 "callback_id": post["id"]+"-"+data["user_id"],
63 "title": "Report post",
64 "submit_label":"Report",
67 "display_name": "Reason",
68 "placeholder": "Write the reason here.",
69 "name": "report_reason",
71 "help_text": "The reason is submitted to channel and team admin_as.",
76 self.bot.api.open_dialog(data["trigger_id"], self.URL+"/dialog", dialog)
81 def on_POST_dialog(self, request, data):
82 u = self.bot.api.get_user(data["user_id"])
83 t = self.bot.api.get_team(data["team_id"])
84 c = self.bot.api.get_channel(data["channel_id"])
86 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```")