[somebot] DialogManagedReport + /report
authorSomeone <someone@somenet.org>
Wed, 8 Dec 2021 18:38:38 +0000 (19:38 +0100)
committerSomeone <someone@somenet.org>
Wed, 8 Dec 2021 18:38:38 +0000 (19:38 +0100)
modules/DialogManagedReport.py [new file with mode: 0644]

diff --git a/modules/DialogManagedReport.py b/modules/DialogManagedReport.py
new file mode 100644 (file)
index 0000000..7e71dd4
--- /dev/null
@@ -0,0 +1,79 @@
+# Mattermost Bot module.
+#  Copyright (c) 2016-2021 by Someone <someone@somenet.org> (aka. Jan Vales <jan@jvales.net>)
+#  published under MIT-License
+#
+# This command relies on the priviledged DB-Cleaner maint script.
+
+
+from inspect import cleandoc
+
+import logging
+logger = logging.getLogger(__name__)
+
+
+from AbstractCommand import AbstractCommand
+class DialogManagedReport(AbstractCommand):
+    TRIGGER = "report"
+    CONFIG = {"display_name": "somebot-command", "auto_complete": True,
+              "auto_complete_hint": "<permalink> [<reason>]",
+             }
+    CONFIG["auto_complete_desc"] = CONFIG["description"] = "Report a post. Run in same channel. Opens dialog if no reason is given."
+
+
+    def __init__(self, team_id, report_chan_id):
+        super().__init__(team_id)
+        self.report_chan_id = report_chan_id
+
+
+    def on_POST(self, request, data):
+        msg_text = data['text'].strip().split(" ", 1)
+
+        try:
+            splitpath = msg_text[0].strip().strip("/").split("/")
+            if splitpath[4] == "pl":
+                pl_post_id = splitpath[5]
+        except:
+            request.cmd_respond_text_temp("``/"+self.TRIGGER+"`` failed: The first parameter is not a valid post-permalink. :(")
+            return
+
+        post = self.bot.api.get_post(pl_post_id, exc=False)
+
+        if "status_code" in post and post["status_code"] == 404:
+            request.cmd_respond_text_temp("``/"+self.TRIGGER+"`` failed: The perma-linked post doesnt seem to exist. Was it deleted?")
+            return
+
+        if post["channel_id"] != data["channel_id"]:
+            request.cmd_respond_text_temp("``/"+self.TRIGGER+"`` failed: Must be executed in the same channel as the post. :(")
+            return
+
+        # do actual action
+        if len(msg_text) == 2:
+            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```")
+            request.cmd_respond_text_temp("### ``Done.`` :)")
+        else:
+            dialog = {
+                "callback_id": post["id"]+"-"+data["user_id"],
+                "title": "Report post",
+                "submit_label":"Report",
+                               "state": post["id"],
+                "elements":[{
+                    "display_name": "Reason",
+                    "placeholder": "Write the reason here.",
+                    "name": "report_reason",
+                    "type": "textarea",
+                    "help_text": "The reason is submitted to channel and team admin_as.",
+                    "optional": False,
+                }]
+            }
+
+            self.bot.api.open_dialog(data["trigger_id"], self.URL+"/dialog", dialog)
+            request.respond(200, {})
+
+
+
+    def on_POST_dialog(self, request, data):
+        u = self.bot.api.get_user(data["user_id"])
+        t = self.bot.api.get_team(data["team_id"])
+        c = self.bot.api.get_channel(data["channel_id"])
+
+        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```")