comparison http.c @ 10:75cfe3795de3

pasterd: improve time left on index
author David Demelier <markand@malikania.fr>
date Wed, 05 Feb 2020 15:57:33 +0100
parents e8f61741aaec
children 93f0440d452e
comparison
equal deleted inserted replaced
9:e8f61741aaec 10:75cfe3795de3
105 static const char *tmpl_new_keywords[] = { 105 static const char *tmpl_new_keywords[] = {
106 "title", /* /fork only */ 106 "title", /* /fork only */
107 "author", /* /fork only */ 107 "author", /* /fork only */
108 "code", /* /fork only */ 108 "code", /* /fork only */
109 "private", /* /fork only */ 109 "private", /* /fork only */
110 "languages" 110 "languages",
111 "durations"
111 }; 112 };
112 113
113 static const char *languages[] = { 114 static const char *languages[] = {
114 "nohighlight", 115 "nohighlight",
115 "1c", 116 "1c",
289 "yaml", 290 "yaml",
290 "zephir", 291 "zephir",
291 NULL 292 NULL
292 }; 293 };
293 294
295 static const struct {
296 const char *title;
297 long long int secs;
298 } durations[] = {
299 { "month", PASTE_MONTH },
300 { "week", PASTE_WEEK },
301 { "day", PASTE_DAY },
302 { "hour", PASTE_HOUR },
303 { NULL, -1 }
304 };
305
294 static const char * 306 static const char *
295 template(const char *filename) 307 template(const char *filename)
296 { 308 {
297 /* Build path to the template file. */ 309 /* Build path to the template file. */
298 static char path[PATH_MAX]; 310 static char path[PATH_MAX];
316 328
317 /* Default to month. */ 329 /* Default to month. */
318 return PASTE_MONTH; 330 return PASTE_MONTH;
319 } 331 }
320 332
333 static const char *
334 ttl(time_t timestamp, long long int duration)
335 {
336 const time_t now = time(NULL);
337 const long long int left = duration - difftime(now, timestamp);
338
339 if (left < PASTE_HOUR)
340 return bprintf("%lld minute(s)", left / 60);
341 if (left < PASTE_DAY)
342 return bprintf("%lld hour(s)", left / 3600);
343
344 /* Other in days. */
345 return bprintf("%lld day(s)", left / 86400);
346 }
347
321 static void 348 static void
322 render_languages(struct kreq *req, const struct paste *paste) 349 render_languages(struct kreq *req, const struct paste *paste)
323 { 350 {
324 for (const char **l = languages; *l != NULL; ++l) { 351 for (const char **l = languages; *l != NULL; ++l) {
325 const char *line; 352 const char *line;
331 358
332 khttp_puts(req, line); 359 khttp_puts(req, line);
333 } 360 }
334 } 361 }
335 362
363 static void
364 render_durations(struct kreq *req, const struct paste *paste)
365 {
366 for (size_t i = 0; durations[i].title != NULL; ++i) {
367 const char *line;
368
369 if (paste->duration == durations[i].secs)
370 line = bprintf("<option value=\"%s\" selected>%s</option>",
371 durations[i].title, durations[i].title);
372 else
373 line = bprintf("<option value=\"%s\">%s</option>",
374 durations[i].title, durations[i].title);
375
376 khttp_puts(req, line);
377 }
378 }
379
336 static int 380 static int
337 tmpl_paste(size_t index, void *arg) 381 tmpl_paste(size_t index, void *arg)
338 { 382 {
339 struct tmpl_paste *data = arg; 383 struct tmpl_paste *data = arg;
340 struct paste *paste = &data->paste; 384 struct paste *paste = &data->paste;
360 break; 404 break;
361 case 6: 405 case 6:
362 khttp_puts(data->req, bprintf("%s", paste->visible ? "Yes" : "No")); 406 khttp_puts(data->req, bprintf("%s", paste->visible ? "Yes" : "No"));
363 break; 407 break;
364 case 7: 408 case 7:
365 /* TODO: convert time left. */ 409 khttp_puts(data->req, ttl(paste->timestamp, paste->duration));
366 khttp_puts(data->req, "TODO");
367 break; 410 break;
368 default: 411 default:
369 break; 412 break;
370 } 413 }
371 414
390 break; 433 break;
391 case 3: 434 case 3:
392 khttp_puts(data->req, paste->language); 435 khttp_puts(data->req, paste->language);
393 break; 436 break;
394 case 4: 437 case 4:
395 khttp_puts(data->req, bprintf("%d", paste->duration)); 438 khttp_puts(data->req, ttl(paste->timestamp, paste->duration));
396 break; 439 break;
397 case 5: 440 case 5:
398 khttp_puts(data->req, bstrftime("%c", localtime(&paste->timestamp))); 441 khttp_puts(data->req, bstrftime("%c", localtime(&paste->timestamp)));
399 break; 442 break;
400 default: 443 default:
448 if (!paste->visible) 491 if (!paste->visible)
449 khttp_puts(data->req, "checked"); 492 khttp_puts(data->req, "checked");
450 break; 493 break;
451 case 4: 494 case 4:
452 render_languages(data->req, paste); 495 render_languages(data->req, paste);
496 break;
497 case 5:
498 render_durations(data->req, paste);
453 break; 499 break;
454 default: 500 default:
455 break; 501 break;
456 }; 502 };
457 503
526 struct tmpl_paste data = { 572 struct tmpl_paste data = {
527 .req = req 573 .req = req
528 }; 574 };
529 const struct ktemplate kt = { 575 const struct ktemplate kt = {
530 .key = tmpl_new_keywords, 576 .key = tmpl_new_keywords,
531 .keysz = 5, 577 .keysz = 6,
532 .cb = tmpl_new, 578 .cb = tmpl_new,
533 .arg = &data 579 .arg = &data
534 }; 580 };
535 581
536 page(req, &kt, KHTTP_200, "new.html"); 582 page(req, &kt, KHTTP_200, "new.html");
603 if (!database_get(&data.paste, req->path)) 649 if (!database_get(&data.paste, req->path))
604 page(req, NULL, KHTTP_404, "404.html"); 650 page(req, NULL, KHTTP_404, "404.html");
605 else { 651 else {
606 const struct ktemplate kt = { 652 const struct ktemplate kt = {
607 .key = tmpl_new_keywords, 653 .key = tmpl_new_keywords,
608 .keysz = 5, 654 .keysz = 6,
609 .cb = tmpl_new, 655 .cb = tmpl_new,
610 .arg = &data 656 .arg = &data
611 }; 657 };
612 658
613 page(req, &kt, KHTTP_200, "new.html"); 659 page(req, &kt, KHTTP_200, "new.html");