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