diff src/lib/Post.luan @ 42:0c1b820fff34

use push
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 08 Nov 2022 14:02:28 -0700
parents 94e26bffd4fb
children 298c71e0c854
line wrap: on
line diff
--- a/src/lib/Post.luan	Sun Nov 06 17:39:21 2022 -0700
+++ b/src/lib/Post.luan	Tue Nov 08 14:02:28 2022 -0700
@@ -6,6 +6,8 @@
 local time_now = Time.now or error()
 local Html = require "luan:Html.luan"
 local html_encode = Html.encode or error()
+local Number = require "luan:Number.luan"
+local long = Number.long or error()
 local Db = require "site:/lib/Db.luan"
 
 
@@ -42,14 +44,10 @@
 		date = doc.date
 		author_name = doc.post_author_name
 		root_id = doc.post_root_id
-		is_deleted = doc.is_deleted == "true"
+		is_root = doc.post_is_root == "true"
 
 		-- root only
 		subject = doc.subject
-		is_root = doc.post_is_root == "true"
-
-		-- replies only
-		parent_id = doc.parent_id
 	}
 end
 
@@ -58,17 +56,13 @@
 		type = "post"
 		id = post.id
 		content = post.content or error()
-		date = post.date or time_now()
+		date = long(post.date)
 		post_author_name = post.author_name or error()
-		post_root_id = post.root_id
-		is_deleted = post.is_deleted and "true" or nil
+		post_root_id = long(post.root_id)
+		post_is_root = post.is_root and "true" or nil
 
 		-- root only
 		subject = post.subject
-		post_is_root = post.is_root and "true" or nil
-
-		-- replies only
-		parent_id = post.parent_id
 	}
 end
 
@@ -88,6 +82,7 @@
 end
 
 function Post.new(post)
+	post.date = post.date or time_now()
 
 	function post.save()
 		local doc = to_doc(post)
@@ -95,16 +90,10 @@
 		post.id = doc.id
 	end
 
-	function post.reply(author,content)
-		return Db.run_in_transaction( function()
-			local post = Post.new{
-				root_id = post.root_id
-				parent_id = post.id
-				content = content
-				author_name = author.name
-			}
-			post.save()
-			return post
+	function post.delete()
+		Db.run_in_transaction( function()
+			not post.is_root or Post.thread_size(post.root_id) == 1 or error "can't delete root post with replies"
+			Db.delete("id:"..post.id)
 		end )
 	end
 
@@ -162,4 +151,8 @@
 	return posts
 end
 
+function Post.thread_size(root_id)
+	return Db.count("post_root_id:"..root_id)
+end
+
 return Post