diff src/lib/Post.luan @ 18:94e26bffd4fb

UI work
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 11 Jul 2022 12:14:05 -0600
parents bff178656073
children 0c1b820fff34
line wrap: on
line diff
--- a/src/lib/Post.luan	Thu Jul 07 15:30:06 2022 -0600
+++ b/src/lib/Post.luan	Mon Jul 11 12:14:05 2022 -0600
@@ -11,6 +11,29 @@
 
 local Post = {}
 
+local times = {
+	{
+		time = 1000*60*60*24*365
+		unit = "year"
+	}
+	{
+		time = 1000*60*60*24*7
+		unit = "week"
+	}
+	{
+		time = 1000*60*60*24
+		unit = "day"
+	}
+	{
+		time = 1000*60*60
+		unit = "hour"
+	}
+	{
+		time = 1000*60
+		unit = "minute"
+	}
+}
+
 local function from_doc(doc)
 	doc.type == "post" or error "wrong type"
 	return Post.new {
@@ -91,6 +114,21 @@
 		return user ~= nil and user.name == post.author_name
 	end
 
+	function post.ago(now)
+		local diff = now - post.date
+		for _, t in ipairs(times) do
+			local n = diff // t.time
+			if n > 0 then
+				%><%=n%> <%=t.unit%><%
+				if n > 1 then
+					%>s<%
+				end
+				return
+			end
+		end
+		%>0 minutes<%
+	end
+
 	set_metatable(post,metatable)
 	return post
 end