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
|
|
14 local function from_doc(doc)
|
|
15 doc.type == "post" or error "wrong type"
|
|
16 return Post.new {
|
|
17 id = doc.id
|
|
18 content = doc.content
|
|
19 date = doc.date
|
|
20 author_name = doc.post_author_name
|
9
|
21 root_id = doc.post_root_id
|
|
22
|
|
23 -- root only
|
|
24 subject = doc.subject
|
8
|
25 is_root = doc.post_is_root == "true"
|
9
|
26
|
|
27 -- replies only
|
|
28 parent_id = doc.parent_id
|
8
|
29 }
|
|
30 end
|
|
31
|
|
32 local function to_doc(post)
|
|
33 return {
|
|
34 type = "post"
|
|
35 id = post.id
|
|
36 content = post.content or error()
|
|
37 date = post.date or time_now()
|
|
38 post_author_name = post.author_name or error()
|
10
|
39 post_root_id = post.root_id
|
9
|
40
|
|
41 -- root only
|
|
42 subject = post.subject
|
8
|
43 post_is_root = post.is_root and "true" or nil
|
9
|
44
|
|
45 -- replies only
|
|
46 parent_id = post.parent_id
|
8
|
47 }
|
|
48 end
|
|
49
|
|
50 local metatable = {}
|
|
51 function metatable.__index(post,key)
|
|
52 if key == "author" then
|
|
53 local User = require "site:/lib/User.luan"
|
10
|
54 return User.get_by_name(post.author_name)
|
8
|
55 end
|
9
|
56 if key == "subject_html" then
|
|
57 return post.subject and html_encode(post.subject)
|
|
58 end
|
|
59 if key == "root" then
|
|
60 return post.is_root and post or Post.get_by_id(post.root_id)
|
|
61 end
|
8
|
62 return nil
|
|
63 end
|
|
64
|
|
65 function Post.new(post)
|
|
66
|
|
67 function post.save()
|
|
68 local doc = to_doc(post)
|
|
69 Db.save(doc)
|
|
70 post.id = doc.id
|
|
71 end
|
|
72
|
9
|
73 function post.reply(author,content)
|
|
74 return Db.run_in_transaction( function()
|
|
75 local post = Post.new{
|
|
76 root_id = post.root_id
|
|
77 parent_id = post.id
|
|
78 content = content
|
|
79 author_name = author.name
|
|
80 }
|
|
81 post.save()
|
|
82 return post
|
|
83 end )
|
|
84 end
|
|
85
|
8
|
86 set_metatable(post,metatable)
|
|
87 return post
|
|
88 end
|
|
89
|
|
90 function Post.get_by_id(id)
|
|
91 local doc = Db.get_document("id:"..id)
|
9
|
92 return doc and from_doc(doc)
|
8
|
93 end
|
|
94
|
|
95 function Post.new_thread(author,subject,content)
|
|
96 return Db.run_in_transaction( function()
|
|
97 local post = Post.new{
|
9
|
98 subject = subject or error()
|
8
|
99 content = content
|
|
100 author_name = author.name
|
|
101 is_root = true
|
|
102 }
|
|
103 post.save()
|
|
104 post.root_id = post.id or error()
|
|
105 post.save()
|
|
106 return post
|
|
107 end )
|
|
108 end
|
|
109
|
9
|
110 function Post.from_docs(docs)
|
|
111 local posts = {}
|
|
112 for _, doc in ipairs(docs) do
|
|
113 local post = from_doc(doc)
|
|
114 posts[#posts+1] = post
|
|
115 end
|
|
116 return posts
|
|
117 end
|
|
118
|
8
|
119 return Post
|