8
|
1 local Luan = require "luan:Luan.luan"
|
|
2 local error = Luan.error
|
9
|
3 local ipairs = Luan.ipairs or error()
|
8
|
4 local set_metatable = Luan.set_metatable or error()
|
43
|
5 local set_local_only = Luan.set_local_only or error()
|
|
6 local get_local_only = Luan.get_local_only or error()
|
8
|
7 local Time = require "luan:Time.luan"
|
|
8 local time_now = Time.now or error()
|
9
|
9 local Html = require "luan:Html.luan"
|
|
10 local html_encode = Html.encode or error()
|
42
|
11 local Number = require "luan:Number.luan"
|
|
12 local long = Number.long or error()
|
8
|
13 local Db = require "site:/lib/Db.luan"
|
|
14
|
|
15
|
|
16 local Post = {}
|
|
17
|
18
|
18 local times = {
|
|
19 {
|
|
20 time = 1000*60*60*24*365
|
|
21 unit = "year"
|
|
22 }
|
|
23 {
|
|
24 time = 1000*60*60*24*7
|
|
25 unit = "week"
|
|
26 }
|
|
27 {
|
|
28 time = 1000*60*60*24
|
|
29 unit = "day"
|
|
30 }
|
|
31 {
|
|
32 time = 1000*60*60
|
|
33 unit = "hour"
|
|
34 }
|
|
35 {
|
|
36 time = 1000*60
|
|
37 unit = "minute"
|
|
38 }
|
|
39 }
|
|
40
|
43
|
41 local posts_by_id = {}
|
|
42
|
8
|
43 local function from_doc(doc)
|
|
44 doc.type == "post" or error "wrong type"
|
43
|
45 local post = Post.new {
|
8
|
46 id = doc.id
|
|
47 content = doc.content
|
|
48 date = doc.date
|
|
49 author_name = doc.post_author_name
|
9
|
50 root_id = doc.post_root_id
|
42
|
51 is_root = doc.post_is_root == "true"
|
9
|
52
|
|
53 -- root only
|
|
54 subject = doc.subject
|
8
|
55 }
|
43
|
56 set_local_only(posts_by_id,post.id,post)
|
|
57 return post
|
8
|
58 end
|
|
59
|
|
60 local function to_doc(post)
|
|
61 return {
|
|
62 type = "post"
|
|
63 id = post.id
|
|
64 content = post.content or error()
|
42
|
65 date = long(post.date)
|
8
|
66 post_author_name = post.author_name or error()
|
42
|
67 post_root_id = long(post.root_id)
|
|
68 post_is_root = post.is_root and "true" or nil
|
9
|
69
|
|
70 -- root only
|
|
71 subject = post.subject
|
8
|
72 }
|
|
73 end
|
|
74
|
|
75 local metatable = {}
|
|
76 function metatable.__index(post,key)
|
|
77 if key == "author" then
|
|
78 local User = require "site:/lib/User.luan"
|
10
|
79 return User.get_by_name(post.author_name)
|
8
|
80 end
|
9
|
81 if key == "subject_html" then
|
|
82 return post.subject and html_encode(post.subject)
|
|
83 end
|
|
84 if key == "root" then
|
|
85 return post.is_root and post or Post.get_by_id(post.root_id)
|
|
86 end
|
8
|
87 return nil
|
|
88 end
|
|
89
|
|
90 function Post.new(post)
|
42
|
91 post.date = post.date or time_now()
|
8
|
92
|
|
93 function post.save()
|
|
94 local doc = to_doc(post)
|
|
95 Db.save(doc)
|
|
96 post.id = doc.id
|
|
97 end
|
|
98
|
42
|
99 function post.delete()
|
|
100 Db.run_in_transaction( function()
|
|
101 not post.is_root or Post.thread_size(post.root_id) == 1 or error "can't delete root post with replies"
|
|
102 Db.delete("id:"..post.id)
|
9
|
103 end )
|
|
104 end
|
|
105
|
16
|
106 function post.author_is_current()
|
|
107 local User = require "site:/lib/User.luan"
|
|
108 local user = User.current()
|
|
109 return user ~= nil and user.name == post.author_name
|
|
110 end
|
|
111
|
18
|
112 function post.ago(now)
|
|
113 local diff = now - post.date
|
|
114 for _, t in ipairs(times) do
|
|
115 local n = diff // t.time
|
|
116 if n > 0 then
|
|
117 %><%=n%> <%=t.unit%><%
|
|
118 if n > 1 then
|
|
119 %>s<%
|
|
120 end
|
|
121 return
|
|
122 end
|
|
123 end
|
|
124 %>0 minutes<%
|
|
125 end
|
|
126
|
8
|
127 set_metatable(post,metatable)
|
|
128 return post
|
|
129 end
|
|
130
|
|
131 function Post.get_by_id(id)
|
43
|
132 local post = get_local_only(posts_by_id,id)
|
|
133 if post ~= nil then return post end
|
8
|
134 local doc = Db.get_document("id:"..id)
|
9
|
135 return doc and from_doc(doc)
|
8
|
136 end
|
|
137
|
|
138 function Post.new_thread(author,subject,content)
|
|
139 return Db.run_in_transaction( function()
|
|
140 local post = Post.new{
|
9
|
141 subject = subject or error()
|
8
|
142 content = content
|
|
143 author_name = author.name
|
|
144 is_root = true
|
|
145 }
|
46
|
146 post.root_id = -1
|
8
|
147 post.save()
|
|
148 post.root_id = post.id or error()
|
|
149 post.save()
|
|
150 return post
|
|
151 end )
|
|
152 end
|
|
153
|
9
|
154 function Post.from_docs(docs)
|
|
155 local posts = {}
|
|
156 for _, doc in ipairs(docs) do
|
|
157 local post = from_doc(doc)
|
|
158 posts[#posts+1] = post
|
|
159 end
|
|
160 return posts
|
|
161 end
|
|
162
|
42
|
163 function Post.thread_size(root_id)
|
|
164 return Db.count("post_root_id:"..root_id)
|
|
165 end
|
|
166
|
8
|
167 return Post
|