]> git.somenet.org - pub/jan/mattermost-bot.git/blob - modules/CommandCurriculaSave.py
modules/CommandCurriculaSave.py
[pub/jan/mattermost-bot.git] / modules / CommandCurriculaSave.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 from inspect import cleandoc
6
7
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": "",
15              }
16     USEINFO = CONFIG["auto_complete_desc"] = CONFIG["description"] = "Generate save-code for curricula.fsinf.at."
17
18
19     def on_POST(self, request, data):
20         att = [{
21             "text": "Delete this post.",
22             "color": "#ff0000",
23             "actions": [{
24                 "name": ":wastebasket: Delete this post and all associated data. :wastebasket:",
25                 "integration": {"url": self.URL+"/interactive", "context": {"action": "delete_self"}}
26                 }]
27             }]
28
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":{}}})
31
32         post["props"].update(dict({"attachments":att}))
33
34         post["message"] = cleandoc("""
35             ## ** https://curricula.fsinf.at storage**
36             Please copy the following code into the https://curricula.fsinf.at storage section.
37             # ``"""+post["id"]+"""``
38             There are **``"""+str(len(str(post["props"]["context"]["curricula_fsinf"])))+""" bytes``** of additional data stored in this message.
39             """)
40         self.bot.api.update_post(post["id"], message=post["message"], is_pinned=post["is_pinned"], has_reactions=None, props=post["props"])
41         request.respond(200, {})
42
43
44     def on_POST_interactive(self, request, data):
45         if data["context"]["action"] == "delete_self":
46             self.bot.api.delete_post(data["post_id"])
47
48
49     def on_public_GET(self, request, data):
50         splitpath = request.path.strip("/").split("/")
51
52         if len(splitpath) != 2:
53             request.respond_public(400, {"error":"no code given"})
54             return
55
56         post = self.bot.api.get_post(splitpath[1], exc=False)
57         if "props" in post and "context" in post["props"] and "curricula_fsinf" in post["props"]["context"]:
58             request.respond_public(200, post["props"]["context"]["curricula_fsinf"])
59             return
60
61         request.respond_public(400, {"error":"invalid code given"})
62
63
64     def on_public_POST(self, request, data):
65         splitpath = request.path.strip("/").split("/")
66
67         if len(splitpath) != 2:
68             request.respond_public(400, {"error":"no code given"})
69             return
70
71         post = self.bot.api.get_post(splitpath[1], exc=False)
72         if "props" in post and "context" in post["props"] and "curricula_fsinf" in post["props"]["context"]:
73             if post["props"]["context"]["curricula_fsinf"] == data:
74                 request.respond_public(200, {"status":"unchanged"})
75                 return
76
77             post["props"]["context"]["curricula_fsinf"] = data
78             post["message"] = cleandoc("""
79                 ## ** https://curricula.fsinf.at storage**
80                 Please copy the following code into the https://curricula.fsinf.at storage section.
81                 # ``"""+post["id"]+"""``
82                 There are **``"""+str(len(str(post["props"]["context"]["curricula_fsinf"])))+""" bytes``** of additional data stored in this message.
83                 """)
84
85             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)
86
87             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':
88                 request.respond_public(500, {"status":"post too old to update. TODO: write new post?"})
89                 return
90
91             request.respond_public(200, {"status":"stored"})
92             return
93
94         request.respond_public(400, {"error":"invalid code given/something failed"})
95