Mercurial Hosting > freedit
diff src/lib/Post.luan @ 9:9674275019bb
reply and edit
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Thu, 30 Jun 2022 00:02:28 -0600 |
parents | be36282b556a |
children | de0cbf515ef5 |
line wrap: on
line diff
--- a/src/lib/Post.luan Wed Jun 29 00:04:09 2022 -0600 +++ b/src/lib/Post.luan Thu Jun 30 00:02:28 2022 -0600 @@ -1,10 +1,13 @@ local Luan = require "luan:Luan.luan" local error = Luan.error +local ipairs = Luan.ipairs or error() local set_metatable = Luan.set_metatable or error() local Number = require "luan:Number.luan" local long = Number.long or error() local Time = require "luan:Time.luan" local time_now = Time.now or error() +local Html = require "luan:Html.luan" +local html_encode = Html.encode or error() local Db = require "site:/lib/Db.luan" @@ -14,13 +17,18 @@ doc.type == "post" or error "wrong type" return Post.new { id = doc.id - subject = doc.subject content = doc.content date = doc.date author_name = doc.post_author_name author_id = doc.post_author_id + root_id = doc.post_root_id + + -- root only + subject = doc.subject is_root = doc.post_is_root == "true" - root_id = doc.post_root_id + + -- replies only + parent_id = doc.parent_id } end @@ -28,13 +36,18 @@ return { type = "post" id = post.id - subject = post.subject or error() content = post.content or error() date = post.date or time_now() post_author_name = post.author_name or error() post_author_id = long(post.author_id) + post_root_id = post.root_id and long(post.root_id) + + -- root only + subject = post.subject post_is_root = post.is_root and "true" or nil - post_root_id = post.root_id and long(post.root_id) + + -- replies only + parent_id = post.parent_id } end @@ -44,6 +57,12 @@ local User = require "site:/lib/User.luan" return User.get_by_id(post.author_id) end + if key == "subject_html" then + return post.subject and html_encode(post.subject) + end + if key == "root" then + return post.is_root and post or Post.get_by_id(post.root_id) + end return nil end @@ -55,19 +74,33 @@ 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 + author_id = author.id + } + post.save() + return post + end ) + end + set_metatable(post,metatable) return post end function Post.get_by_id(id) local doc = Db.get_document("id:"..id) - return doc and Post.from_doc(doc) + return doc and from_doc(doc) end function Post.new_thread(author,subject,content) return Db.run_in_transaction( function() local post = Post.new{ - subject = subject + subject = subject or error() content = content author_name = author.name author_id = author.id @@ -80,4 +113,13 @@ end ) end +function Post.from_docs(docs) + local posts = {} + for _, doc in ipairs(docs) do + local post = from_doc(doc) + posts[#posts+1] = post + end + return posts +end + return Post