From 6c8b3bcd8783d9cd1b0d89e9acd03f7ba9b23e5e Mon Sep 17 00:00:00 2001
From: Someone <someone@somenet.org>
Date: Mon, 5 Sep 2022 22:11:39 +0200
Subject: [PATCH] modules/CommandCurriculaSave.py

---
 modules/CommandCurriculaSave.py | 49 +++++++++++++++++++++++++++++++--
 1 file changed, 46 insertions(+), 3 deletions(-)

diff --git a/modules/CommandCurriculaSave.py b/modules/CommandCurriculaSave.py
index 5c4f52a..edc0364 100644
--- a/modules/CommandCurriculaSave.py
+++ b/modules/CommandCurriculaSave.py
@@ -35,7 +35,7 @@ class CommandCurriculaSave(AbstractCommand,AbstractPublicWS):
             ## ** https://curricula.fsinf.at storage**
             Please copy the following code into the https://curricula.fsinf.at storage section.
             # ``"""+post["id"]+"""``
-            There are **``"""+str(len(post["props"]["context"]["curricula_fsinf"]))+""" bytes``** of additional data stored in this message.
+            There are **``"""+str(len(str(post["props"]["context"]["curricula_fsinf"])))+""" bytes``** of additional data stored in this message.
             """)
         self.bot.api.update_post(post["id"], message=post["message"], is_pinned=post["is_pinned"], has_reactions=None, props=post["props"])
         request.respond(200, {})
@@ -45,8 +45,51 @@ class CommandCurriculaSave(AbstractCommand,AbstractPublicWS):
         if data["context"]["action"] == "delete_self":
             self.bot.api.delete_post(data["post_id"])
 
+
     def on_public_GET(self, request, data):
-        request.respond_public(200, {"asdf":"qwer"})
+        splitpath = request.path.strip("/").split("/")
+
+        if len(splitpath) != 2:
+            request.respond_public(400, {"error":"no code given"})
+            return
+
+        post = self.bot.api.get_post(splitpath[1], exc=False)
+        if "props" in post and "context" in post["props"] and "curricula_fsinf" in post["props"]["context"]:
+            request.respond_public(200, post["props"]["context"]["curricula_fsinf"])
+            return
+
+        request.respond_public(400, {"error":"invalid code given"})
+
 
     def on_public_POST(self, request, data):
-        return 'post-qwer'
+        splitpath = request.path.strip("/").split("/")
+
+        if len(splitpath) != 2:
+            request.respond_public(400, {"error":"no code given"})
+            return
+
+        post = self.bot.api.get_post(splitpath[1], exc=False)
+        if "props" in post and "context" in post["props"] and "curricula_fsinf" in post["props"]["context"]:
+            if post["props"]["context"]["curricula_fsinf"] == data:
+                request.respond_public(200, {"status":"unchanged"})
+                return
+
+            post["props"]["context"]["curricula_fsinf"] = data
+            post["message"] = cleandoc("""
+                ## ** https://curricula.fsinf.at storage**
+                Please copy the following code into the https://curricula.fsinf.at storage section.
+                # ``"""+post["id"]+"""``
+                There are **``"""+str(len(str(post["props"]["context"]["curricula_fsinf"])))+""" bytes``** of additional data stored in this message.
+                """)
+
+            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)
+
+            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':
+                request.respond_public(500, {"status":"post too old to update. TODO: write new post?"})
+                return
+
+            request.respond_public(200, {"status":"stored"})
+            return
+
+        request.respond_public(400, {"error":"invalid code given/something failed"})
+
-- 
2.43.0