comparison page-index.c @ 74:67b3d13a5035

pasterd: make own HTML code for good
author David Demelier <markand@malikania.fr>
date Wed, 15 Mar 2023 19:34:00 +0100
parents 6792975da9a0
children b12491ceabfd
comparison
equal deleted inserted replaced
73:6792975da9a0 74:67b3d13a5035
1 /* 1 /*
2 * page-index.c -- page / 2 * page-index.c -- page /
3 * 3 *
4 * Copyright (c) 2020-2023 David Demelier <markand@malikania.fr> 4 * Copyright (c) 2020-2023 David Demelier <markand@malikania.fr>
5 * 5 *
6 * Permission to use, copy, modify, and/or distribute this software for any 6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above 7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies. 8 * copyright notice and this permission notice appear in all copies.
9 * 9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 */ 17 */
18 18
19 #include <assert.h> 19 #include <assert.h>
20 20
21 #include "database.h" 21 #include "database.h"
22 #include "fmt-paste.h"
23 #include "fmt.h"
24 #include "fragment-paste.h"
25 #include "page-index.h" 22 #include "page-index.h"
26 #include "page.h" 23 #include "page.h"
27 #include "paste.h" 24 #include "paste.h"
28 #include "util.h" 25 #include "util.h"
29 26
30 #include "html/index.h" 27 #include "html/index.h"
31 28
32 static void 29 static void
33 print_paste_table(struct kreq *req, struct khtmlreq *html, const void *data)
34 {
35 (void)html;
36
37 fmt_paste_table(req, data);
38 }
39
40 static void
41 get(struct kreq *r) 30 get(struct kreq *r)
42 { 31 {
43 struct paste pastes[10] = {0}; 32 struct paste pastes[10] = {0};
44 size_t pastesz = NELEM(pastes); 33 size_t pastesz = NELEM(pastes);
45 34
46 if (!database_recents(pastes, &pastesz)) 35 if (!database_recents(pastes, &pastesz))
47 page(r, NULL, KHTTP_500, "pages/500.html", "500"); 36 page_status(r, KHTTP_500);
48 else { 37 else {
49 page_index_render(r, pastes, pastesz); 38 page_index_render(r, pastes, pastesz);
50 39
51 for (size_t i = 0; i < pastesz; ++i) 40 for (size_t i = 0; i < pastesz; ++i)
52 paste_finish(&pastes[i]); 41 paste_finish(&pastes[i]);
53 } 42 }
54 } 43 }
55 44
56 void 45 static inline json_t *
57 page_index_render(struct kreq *r, const struct paste *pastes, size_t pastesz) 46 create_date(const struct paste *paste)
58 { 47 {
59 assert(r); 48 return json_string(bstrftime("%c", localtime(&paste->timestamp)));
60 assert(pastes); 49 }
61 50
62 struct fmt_paste_vec vec = { 51 static inline json_t *
63 .pastes = pastes, 52 create_expiration(const struct paste *paste)
64 .pastesz = pastesz 53 {
65 }; 54 return json_string(ttl(paste->timestamp, paste->duration));
55 }
66 56
67 page2(r, KHTTP_200, "recent pastes", html_index, &vec, (const struct fmt_printer []) { 57 static inline json_t *
68 { "paste-table", print_paste_table }, 58 create_pastes(const struct paste *pastes, size_t pastesz)
69 { NULL, NULL } 59 {
70 }); 60 json_t *array = json_array();
61 const struct paste *paste;
62
63 for (size_t i = 0; i < pastesz; ++i) {
64 paste = &pastes[i];
65
66 json_array_append_new(array, json_pack("{ss ss ss ss so so}",
67 "id", paste->id,
68 "author", paste->author,
69 "title", paste->title,
70 "date", create_date(paste),
71 "expiration", create_expiration(paste)
72 ));
73 }
74
75 return array;
76 }
77
78 static inline json_t *
79 create_doc(const struct paste *pastes, size_t pastesz)
80 {
81 return json_pack("{ss so}",
82 "pagetitle", "sci -- recent pastes",
83 "pastes", create_pastes(pastes, pastesz)
84 );
71 } 85 }
72 86
73 void 87 void
74 page_index(struct kreq *r) 88 page_index_render(struct kreq *req, const struct paste *pastes, size_t pastesz)
75 { 89 {
76 switch (r->method) { 90 assert(req);
91 assert(pastes);
92
93 page(req, KHTTP_200, html_index, create_doc(pastes, pastesz));
94 }
95
96 void
97 page_index(struct kreq *req)
98 {
99 assert(req);
100
101 switch (req->method) {
77 case KMETHOD_GET: 102 case KMETHOD_GET:
78 get(r); 103 get(req);
79 break; 104 break;
80 default: 105 default:
81 page(r, NULL, KHTTP_400, "400.html", "400"); 106 page_status(req, KHTTP_400);
82 break; 107 break;
83 } 108 }
84 } 109 }