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 from inspect import cleandoc
8 # pylint: disable=wrong-import-position
9 from AbstractCommand import AbstractCommand
10 from AbstractPublicWS import AbstractPublicWS
11 class CommandCurriculaSave(AbstractCommand,AbstractPublicWS):
12 TRIGGER = "curricula-save"
13 CONFIG = {"display_name": "somebot-command", "auto_complete": True,
14 "auto_complete_hint": "",
16 USEINFO = CONFIG["auto_complete_desc"] = CONFIG["description"] = "Generate save-code for curricula.fsinf.at."
19 def on_POST(self, request, data):
21 "text": "Delete this post.",
24 "name": ":wastebasket: Delete this post and all associated data. :wastebasket:",
25 "integration": {"url": self.URL+"/interactive", "context": {"action": "delete_self"}}
29 chan = self.bot.api.create_dm_channel_with(data['user_id'])
30 post = self.bot.api.create_post(chan['id'], "### Generating curricula.fsinf.at storage ...", {"attachments":att, "context":{"curricula_fsinf":{}}})
32 post["message"] = cleandoc("""
33 ## ** https://curricula.fsinf.at storage**
34 Please copy the following code into the https://curricula.fsinf.at storage section.
38 There are **``"""+str(len(str(post["props"]["context"]["curricula_fsinf"])))+""" bytes``** of additional data stored in this message.
40 post["props"].update(dict({"attachments":att}))
41 self.bot.api.update_post(post["id"], message=post["message"], is_pinned=post["is_pinned"], has_reactions=None, props=post["props"])
42 request.respond(200, {})
45 def on_POST_interactive(self, request, data):
46 if data["context"]["action"] == "delete_self":
47 self.bot.api.delete_post(data["post_id"])
50 def on_public_GET(self, request, data):
51 splitpath = request.path.strip("/").split("/")
53 if len(splitpath) != 2:
54 request.respond_public(400, {"error":"no code given"})
57 post = self.bot.api.get_post(splitpath[1], exc=False)
58 if "props" in post and "context" in post["props"] and "curricula_fsinf" in post["props"]["context"]:
59 self.bot.api.create_post(post['channel_id'], "``AUTODELETE-WEEK`` Somebody just accessed this storage.\nIf this was not you consider creating a new storage post, migrate data and then delete this storage post, also consider contacting @someone.", root_id=post["id"])
60 request.respond_public(200, post["props"]["context"]["curricula_fsinf"])
63 request.respond_public(400, {"error":"invalid code given"})
66 def on_public_POST(self, request, data):
67 splitpath = request.path.strip("/").split("/")
69 if len(splitpath) != 2:
70 request.respond_public(400, {"error":"no code given"})
73 post = self.bot.api.get_post(splitpath[1], exc=False)
74 if "props" in post and "context" in post["props"] and "curricula_fsinf" in post["props"]["context"] and self.bot.api._my_user_id == post["user_id"]:
75 if post["props"]["context"]["curricula_fsinf"] == data:
76 request.respond_public(200, {"status":"unchanged"})
79 post["props"]["context"]["curricula_fsinf"] = data
80 post["message"] = cleandoc("""
81 ## ** https://curricula.fsinf.at storage**
82 Please copy the following code into the https://curricula.fsinf.at storage section.
86 There are **``"""+str(len(str(post["props"]["context"]["curricula_fsinf"])))+""" bytes``** of additional data stored in this message.
89 "text": "Delete this post.",
92 "name": ":wastebasket: Delete this post and all associated data. :wastebasket:",
93 "integration": {"url": self.bot.modules[list(self.bot.modules.keys())[0]][self.TRIGGER].URL+"/interactive", "context": {"action": "delete_self"}}
97 post["props"].update(dict({"attachments":att}))
98 saved_post = self.bot.api.update_post(post["id"], message=post["message"], is_pinned=post["is_pinned"], has_reactions=None, props=post["props"], exc=False)
100 if "status_code" in saved_post and saved_post["status_code"] == 400 and saved_post['id'] == 'api.post.update_post.permissions_time_limit.app_error':
101 request.respond_public(500, {"status":"post too old to update. TODO: write new post?"})
104 request.respond_public(200, {"status":"stored"})
107 request.respond_public(400, {"error":"invalid code given/something failed"})