annotate src/lib/Post.luan @ 9:9674275019bb

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