changeset 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 b9b046818b0c
files http.c paste.h themes/minimal/new.html
diffstat 3 files changed, 59 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/http.c	Wed Feb 05 14:26:19 2020 +0100
+++ b/http.c	Wed Feb 05 15:57:33 2020 +0100
@@ -107,7 +107,8 @@
 	"author",       /* /fork only */
 	"code",         /* /fork only */
 	"private",      /* /fork only */
-	"languages"
+	"languages",
+	"durations"
 };
 
 static const char *languages[] = {
@@ -291,6 +292,17 @@
 	NULL
 };
 
+static const struct {
+	const char *title;
+	long long int secs;
+} durations[] = {
+	{ "month",      PASTE_MONTH     },
+	{ "week",       PASTE_WEEK      },
+	{ "day",        PASTE_DAY       },
+	{ "hour",       PASTE_HOUR      },
+	{ NULL,         -1              }
+};
+
 static const char *
 template(const char *filename)
 {
@@ -318,6 +330,21 @@
 	return PASTE_MONTH;
 }
 
+static const char *
+ttl(time_t timestamp, long long int duration)
+{
+	const time_t now = time(NULL);
+	const long long int left = duration - difftime(now, timestamp);
+
+	if (left < PASTE_HOUR)
+		return bprintf("%lld minute(s)", left / 60);
+	if (left < PASTE_DAY)
+		return bprintf("%lld hour(s)", left / 3600);
+
+	/* Other in days. */
+	return bprintf("%lld day(s)", left / 86400);
+}
+
 static void
 render_languages(struct kreq *req, const struct paste *paste)
 {
@@ -333,6 +360,23 @@
 	}
 }
 
+static void
+render_durations(struct kreq *req, const struct paste *paste)
+{
+	for (size_t i = 0; durations[i].title != NULL; ++i) {
+		const char *line;
+
+		if (paste->duration == durations[i].secs)
+			line = bprintf("<option value=\"%s\" selected>%s</option>",
+			    durations[i].title, durations[i].title);
+		else
+			line = bprintf("<option value=\"%s\">%s</option>",
+			    durations[i].title, durations[i].title);
+
+		khttp_puts(req, line);
+	}
+}
+
 static int
 tmpl_paste(size_t index, void *arg)
 {
@@ -362,8 +406,7 @@
 		khttp_puts(data->req, bprintf("%s", paste->visible ? "Yes" : "No"));
 		break;
 	case 7:
-		/* TODO: convert time left. */
-		khttp_puts(data->req, "TODO");
+		khttp_puts(data->req, ttl(paste->timestamp, paste->duration));
 		break;
 	default:
 		break;
@@ -392,7 +435,7 @@
 		khttp_puts(data->req, paste->language);
 		break;
 	case 4:
-		khttp_puts(data->req, bprintf("%d", paste->duration));
+		khttp_puts(data->req, ttl(paste->timestamp, paste->duration));
 		break;
 	case 5:
 		khttp_puts(data->req, bstrftime("%c", localtime(&paste->timestamp)));
@@ -451,6 +494,9 @@
 	case 4:
 		render_languages(data->req, paste);
 		break;
+	case 5:
+		render_durations(data->req, paste);
+		break;
 	default:
 		break;
 	};
@@ -528,7 +574,7 @@
 	};
 	const struct ktemplate kt = {
 		.key    = tmpl_new_keywords,
-		.keysz  = 5,
+		.keysz  = 6,
 		.cb     = tmpl_new,
 		.arg    = &data
 	};
@@ -605,7 +651,7 @@
 	else {
 		const struct ktemplate kt = {
 			.key    = tmpl_new_keywords,
-			.keysz  = 5,
+			.keysz  = 6,
 			.cb     = tmpl_new,
 			.arg    = &data
 		};
--- a/paste.h	Wed Feb 05 14:26:19 2020 +0100
+++ b/paste.h	Wed Feb 05 15:57:33 2020 +0100
@@ -25,7 +25,7 @@
 #define PASTE_HOUR      3600          /*!< Seconds in one hour. */
 #define PASTE_DAY       86400         /*!< Seconds in one day. */
 #define PASTE_WEEK      604800        /*!< Seconds in one week. */
-#define PASTE_MONTH     18144400      /*!< Rounded to 30 days. */
+#define PASTE_MONTH     2592000       /*!< Rounded to 30 days. */
 
 /**
  * \brief Paste structure.
--- a/themes/minimal/new.html	Wed Feb 05 14:26:19 2020 +0100
+++ b/themes/minimal/new.html	Wed Feb 05 15:57:33 2020 +0100
@@ -14,17 +14,18 @@
 
 			<tr>
 				<td>Language</td>
-				<td><select name="language">@@languages@@</select></td>
+				<td>
+					<select name="language">
+						@@languages@@
+					</select>
+				</td>
 			</tr>
 
 			<tr>
 				<td>Expires in</td>
 				<td>
 					<select name="duration">
-						<option value="month">one month</option>
-						<option value="week">one week</option>
-						<option value="day">one day</option>
-						<option value="hour">one hour</option>
+						@@durations@@
 					</select>
 				</td>
 			</tr>