comparison 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
comparison
equal deleted inserted replaced
41:acb730710328 42:0c1b820fff34
4 local set_metatable = Luan.set_metatable or error() 4 local set_metatable = Luan.set_metatable or error()
5 local Time = require "luan:Time.luan" 5 local Time = require "luan:Time.luan"
6 local time_now = Time.now or error() 6 local time_now = Time.now or error()
7 local Html = require "luan:Html.luan" 7 local Html = require "luan:Html.luan"
8 local html_encode = Html.encode or error() 8 local html_encode = Html.encode or error()
9 local Number = require "luan:Number.luan"
10 local long = Number.long or error()
9 local Db = require "site:/lib/Db.luan" 11 local Db = require "site:/lib/Db.luan"
10 12
11 13
12 local Post = {} 14 local Post = {}
13 15
40 id = doc.id 42 id = doc.id
41 content = doc.content 43 content = doc.content
42 date = doc.date 44 date = doc.date
43 author_name = doc.post_author_name 45 author_name = doc.post_author_name
44 root_id = doc.post_root_id 46 root_id = doc.post_root_id
45 is_deleted = doc.is_deleted == "true" 47 is_root = doc.post_is_root == "true"
46 48
47 -- root only 49 -- root only
48 subject = doc.subject 50 subject = doc.subject
49 is_root = doc.post_is_root == "true"
50
51 -- replies only
52 parent_id = doc.parent_id
53 } 51 }
54 end 52 end
55 53
56 local function to_doc(post) 54 local function to_doc(post)
57 return { 55 return {
58 type = "post" 56 type = "post"
59 id = post.id 57 id = post.id
60 content = post.content or error() 58 content = post.content or error()
61 date = post.date or time_now() 59 date = long(post.date)
62 post_author_name = post.author_name or error() 60 post_author_name = post.author_name or error()
63 post_root_id = post.root_id 61 post_root_id = long(post.root_id)
64 is_deleted = post.is_deleted and "true" or nil 62 post_is_root = post.is_root and "true" or nil
65 63
66 -- root only 64 -- root only
67 subject = post.subject 65 subject = post.subject
68 post_is_root = post.is_root and "true" or nil
69
70 -- replies only
71 parent_id = post.parent_id
72 } 66 }
73 end 67 end
74 68
75 local metatable = {} 69 local metatable = {}
76 function metatable.__index(post,key) 70 function metatable.__index(post,key)
86 end 80 end
87 return nil 81 return nil
88 end 82 end
89 83
90 function Post.new(post) 84 function Post.new(post)
85 post.date = post.date or time_now()
91 86
92 function post.save() 87 function post.save()
93 local doc = to_doc(post) 88 local doc = to_doc(post)
94 Db.save(doc) 89 Db.save(doc)
95 post.id = doc.id 90 post.id = doc.id
96 end 91 end
97 92
98 function post.reply(author,content) 93 function post.delete()
99 return Db.run_in_transaction( function() 94 Db.run_in_transaction( function()
100 local post = Post.new{ 95 not post.is_root or Post.thread_size(post.root_id) == 1 or error "can't delete root post with replies"
101 root_id = post.root_id 96 Db.delete("id:"..post.id)
102 parent_id = post.id
103 content = content
104 author_name = author.name
105 }
106 post.save()
107 return post
108 end ) 97 end )
109 end 98 end
110 99
111 function post.author_is_current() 100 function post.author_is_current()
112 local User = require "site:/lib/User.luan" 101 local User = require "site:/lib/User.luan"
160 posts[#posts+1] = post 149 posts[#posts+1] = post
161 end 150 end
162 return posts 151 return posts
163 end 152 end
164 153
154 function Post.thread_size(root_id)
155 return Db.count("post_root_id:"..root_id)
156 end
157
165 return Post 158 return Post