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