comparison page-index.c @ 78:9bfe5ce3cc45

pasterd: rework themes
author David Demelier <markand@malikania.fr>
date Thu, 16 Mar 2023 20:45:59 +0100
parents fe78b16c694d
children 52029a52a385
comparison
equal deleted inserted replaced
77:fe78b16c694d 78:9bfe5ce3cc45
20 20
21 #include "database.h" 21 #include "database.h"
22 #include "json-util.h" 22 #include "json-util.h"
23 #include "page-index.h" 23 #include "page-index.h"
24 #include "page.h" 24 #include "page.h"
25 #include "paste.h"
26 #include "util.h" 25 #include "util.h"
27 26
28 #include "html/index.h" 27 #include "html/index.h"
29 28
29 #define LIMIT 16
30 #define TITLE "paster -- recent pastes"
31
30 static void 32 static void
31 get(struct kreq *r) 33 get(struct kreq *req)
32 { 34 {
33 struct paste pastes[10] = {0}; 35 json_t *pastes;
34 size_t pastesz = NELEM(pastes);
35 36
36 if (!database_recents(pastes, &pastesz)) 37 if (!(pastes = database_recents(LIMIT)))
37 page_status(r, KHTTP_500); 38 page_status(req, KHTTP_500);
38 else { 39 else
39 page_index_render(r, pastes, pastesz); 40 page_index_render(req, pastes);
40
41 for (size_t i = 0; i < pastesz; ++i)
42 paste_finish(&pastes[i]);
43 }
44 }
45
46 static inline json_t *
47 create_pastes(const struct paste *pastes, size_t pastesz)
48 {
49 json_t *array = json_array();
50 const struct paste *paste;
51
52 for (size_t i = 0; i < pastesz; ++i) {
53 paste = &pastes[i];
54
55 json_array_append_new(array, json_pack("{ss ss ss ss so so}",
56 "id", paste->id,
57 "author", paste->author,
58 "title", paste->title,
59 "date", ju_date(paste),
60 "expiration", ju_expiration(paste)
61 ));
62 }
63
64 return array;
65 }
66
67 static inline json_t *
68 create_doc(const struct paste *pastes, size_t pastesz)
69 {
70 return json_pack("{ss so}",
71 "pagetitle", "paster -- recent pastes",
72 "pastes", create_pastes(pastes, pastesz)
73 );
74 } 41 }
75 42
76 void 43 void
77 page_index_render(struct kreq *req, const struct paste *pastes, size_t pastesz) 44 page_index_render(struct kreq *req, json_t *pastes)
78 { 45 {
79 assert(req); 46 assert(req);
80 assert(pastes); 47 assert(pastes);
81 48
82 page(req, KHTTP_200, html_index, create_doc(pastes, pastesz)); 49 page(req, KHTTP_200, html_index, json_pack("{ss so}",
50 "title", TITLE,
51 "pastes", pastes
52 ));
83 } 53 }
84 54
85 void 55 void
86 page_index(struct kreq *req) 56 page_index(struct kreq *req)
87 { 57 {