annotate src/lib/Post.luan @ 24:fccbe5554fcc

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