comparison page-index.c @ 77:fe78b16c694d

pasterd: refactor json utilities
author David Demelier <markand@malikania.fr>
date Thu, 16 Mar 2023 15:05:26 +0100
parents b12491ceabfd
children 9bfe5ce3cc45
comparison
equal deleted inserted replaced
76:9643962908ab 77:fe78b16c694d
17 */ 17 */
18 18
19 #include <assert.h> 19 #include <assert.h>
20 20
21 #include "database.h" 21 #include "database.h"
22 #include "json-util.h"
22 #include "page-index.h" 23 #include "page-index.h"
23 #include "page.h" 24 #include "page.h"
24 #include "paste.h" 25 #include "paste.h"
25 #include "util.h" 26 #include "util.h"
26 27
41 paste_finish(&pastes[i]); 42 paste_finish(&pastes[i]);
42 } 43 }
43 } 44 }
44 45
45 static inline json_t * 46 static inline json_t *
46 create_date(const struct paste *paste)
47 {
48 return json_string(bstrftime("%c", localtime(&paste->timestamp)));
49 }
50
51 static inline json_t *
52 create_expiration(const struct paste *paste)
53 {
54 return json_string(ttl(paste->timestamp, paste->duration));
55 }
56
57 static inline json_t *
58 create_pastes(const struct paste *pastes, size_t pastesz) 47 create_pastes(const struct paste *pastes, size_t pastesz)
59 { 48 {
60 json_t *array = json_array(); 49 json_t *array = json_array();
61 const struct paste *paste; 50 const struct paste *paste;
62 51
65 54
66 json_array_append_new(array, json_pack("{ss ss ss ss so so}", 55 json_array_append_new(array, json_pack("{ss ss ss ss so so}",
67 "id", paste->id, 56 "id", paste->id,
68 "author", paste->author, 57 "author", paste->author,
69 "title", paste->title, 58 "title", paste->title,
70 "date", create_date(paste), 59 "date", ju_date(paste),
71 "expiration", create_expiration(paste) 60 "expiration", ju_expiration(paste)
72 )); 61 ));
73 } 62 }
74 63
75 return array; 64 return array;
76 } 65 }