]> gitweb.ps.run Git - chirp/commitdiff
read/write nested subcomments
authorPatrick <patrick.schoenberger@posteo.de>
Mon, 28 Aug 2023 01:53:08 +0000 (03:53 +0200)
committerPatrick <patrick.schoenberger@posteo.de>
Mon, 28 Aug 2023 01:53:08 +0000 (03:53 +0200)
src/main.c

index 4c994d9d216a7ac83181d9b6a5bbb651813fc6c9..2747fdea3c2658059d89152a05a3a9200540e62c 100644 (file)
@@ -19,9 +19,9 @@ struct Post {
   time_t timestamp;\r
   User * user;\r
   char content[CONTENT_LEN];\r
-  int likes;\r
+  uint32_t likes;\r
   Post * comments;\r
-  Post * parent;\r
+  //Post * parent;\r
 };\r
 \r
 struct User {\r
@@ -42,6 +42,12 @@ PostWrite(Post * post, FILE * f) {
   fwrite(&post->timestamp, 8, 1, f);\r
   fwrite(post->user->name, 1, sizeof(post->user->name), f);\r
   fwrite(post->content, 1, sizeof(post->content), f);\r
+  fwrite(&post->likes, 4, 1, f);\r
+  uint32_t numComments = arrlen(post->comments);\r
+  fwrite(&numComments, 4, 1, f);\r
+  for (int i = 0; i < numComments; i++) {\r
+    PostWrite(&post->comments[i], f);\r
+  }\r
 }\r
 \r
 void\r
@@ -51,6 +57,15 @@ PostRead(Post * post, FILE * f) {
   fread(name, 1, sizeof(name), f);\r
   post->user = UsersFind(mg_str(name));\r
   fread(post->content, 1, sizeof(post->content), f);\r
+  fread(&post->likes, 4, 1, f);\r
+  uint32_t numComments;\r
+  fread(&numComments, 4, 1, f);\r
+  post->comments = NULL;\r
+  for (int i = 0; i < numComments; i++) {\r
+    Post newComment = PostNew();\r
+    PostRead(&newComment, f);\r
+    arrput(post->comments, newComment);\r
+  }\r
 }\r
 \r
 User\r
@@ -339,7 +354,6 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data)
           Post newPost;\r
           newPost.likes = 0;\r
           newPost.comments = NULL;\r
-          newPost.parent = NULL;\r
           newPost.timestamp = time(NULL);\r
           newPost.user = user;\r
           mg_http_get_var(&hm->body, "content", newPost.content, sizeof(newPost.content)-1);\r
@@ -355,7 +369,6 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data)
             Post newComment;\r
             newComment.likes = 0;\r
             newComment.comments = NULL;\r
-            newComment.parent = parent;\r
             newComment.timestamp = time(NULL);\r
             newComment.user = user;\r
             mg_http_get_var(&hm->body, "content", newComment.content, sizeof(newComment.content)-1);\r