annotate src/lib/Post.luan @ 46:289718f121e4

use bbcode editor
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 14 Nov 2022 18:43:38 -0700
parents 298c71e0c854
children 7ce54f6d93f2
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()
43
298c71e0c854 caching
Franklin Schmidt <fschmidt@gmail.com>
parents: 42
diff changeset
5 local set_local_only = Luan.set_local_only or error()
298c71e0c854 caching
Franklin Schmidt <fschmidt@gmail.com>
parents: 42
diff changeset
6 local get_local_only = Luan.get_local_only or error()
8
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()
42
0c1b820fff34 use push
Franklin Schmidt <fschmidt@gmail.com>
parents: 18
diff changeset
11 local Number = require "luan:Number.luan"
0c1b820fff34 use push
Franklin Schmidt <fschmidt@gmail.com>
parents: 18
diff changeset
12 local long = Number.long or error()
8
be36282b556a add new_thread
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
13 local Db = require "site:/lib/Db.luan"
be36282b556a add new_thread
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
14
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 Post = {}
be36282b556a add new_thread
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
17
18
94e26bffd4fb UI work
Franklin Schmidt <fschmidt@gmail.com>
parents: 16
diff changeset
18 local times = {
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*365
94e26bffd4fb UI work
Franklin Schmidt <fschmidt@gmail.com>
parents: 16
diff changeset
21 unit = "year"
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*7
94e26bffd4fb UI work
Franklin Schmidt <fschmidt@gmail.com>
parents: 16
diff changeset
25 unit = "week"
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*24
94e26bffd4fb UI work
Franklin Schmidt <fschmidt@gmail.com>
parents: 16
diff changeset
29 unit = "day"
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*60
94e26bffd4fb UI work
Franklin Schmidt <fschmidt@gmail.com>
parents: 16
diff changeset
33 unit = "hour"
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 time = 1000*60
94e26bffd4fb UI work
Franklin Schmidt <fschmidt@gmail.com>
parents: 16
diff changeset
37 unit = "minute"
94e26bffd4fb UI work
Franklin Schmidt <fschmidt@gmail.com>
parents: 16
diff changeset
38 }
94e26bffd4fb UI work
Franklin Schmidt <fschmidt@gmail.com>
parents: 16
diff changeset
39 }
94e26bffd4fb UI work
Franklin Schmidt <fschmidt@gmail.com>
parents: 16
diff changeset
40
43
298c71e0c854 caching
Franklin Schmidt <fschmidt@gmail.com>
parents: 42
diff changeset
41 local posts_by_id = {}
298c71e0c854 caching
Franklin Schmidt <fschmidt@gmail.com>
parents: 42
diff changeset
42
8
be36282b556a add new_thread
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
43 local function from_doc(doc)
be36282b556a add new_thread
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
44 doc.type == "post" or error "wrong type"
43
298c71e0c854 caching
Franklin Schmidt <fschmidt@gmail.com>
parents: 42
diff changeset
45 local post = Post.new {
8
be36282b556a add new_thread
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
46 id = doc.id
be36282b556a add new_thread
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
47 content = doc.content
be36282b556a add new_thread
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
48 date = doc.date
be36282b556a add new_thread
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
49 author_name = doc.post_author_name
9
9674275019bb reply and edit
Franklin Schmidt <fschmidt@gmail.com>
parents: 8
diff changeset
50 root_id = doc.post_root_id
42
0c1b820fff34 use push
Franklin Schmidt <fschmidt@gmail.com>
parents: 18
diff changeset
51 is_root = doc.post_is_root == "true"
9
9674275019bb reply and edit
Franklin Schmidt <fschmidt@gmail.com>
parents: 8
diff changeset
52
9674275019bb reply and edit
Franklin Schmidt <fschmidt@gmail.com>
parents: 8
diff changeset
53 -- root only
9674275019bb reply and edit
Franklin Schmidt <fschmidt@gmail.com>
parents: 8
diff changeset
54 subject = doc.subject
8
be36282b556a add new_thread
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
55 }
43
298c71e0c854 caching
Franklin Schmidt <fschmidt@gmail.com>
parents: 42
diff changeset
56 set_local_only(posts_by_id,post.id,post)
298c71e0c854 caching
Franklin Schmidt <fschmidt@gmail.com>
parents: 42
diff changeset
57 return post
8
be36282b556a add new_thread
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
58 end
be36282b556a add new_thread
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
59
be36282b556a add new_thread
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
60 local function to_doc(post)
be36282b556a add new_thread
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
61 return {
be36282b556a add new_thread
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
62 type = "post"
be36282b556a add new_thread
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
63 id = post.id
be36282b556a add new_thread
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
64 content = post.content or error()
42
0c1b820fff34 use push
Franklin Schmidt <fschmidt@gmail.com>
parents: 18
diff changeset
65 date = long(post.date)
8
be36282b556a add new_thread
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
66 post_author_name = post.author_name or error()
42
0c1b820fff34 use push
Franklin Schmidt <fschmidt@gmail.com>
parents: 18
diff changeset
67 post_root_id = long(post.root_id)
0c1b820fff34 use push
Franklin Schmidt <fschmidt@gmail.com>
parents: 18
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 -- root only
9674275019bb reply and edit
Franklin Schmidt <fschmidt@gmail.com>
parents: 8
diff changeset
71 subject = post.subject
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)
42
0c1b820fff34 use push
Franklin Schmidt <fschmidt@gmail.com>
parents: 18
diff changeset
91 post.date = post.date or time_now()
8
be36282b556a add new_thread
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
92
be36282b556a add new_thread
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
93 function post.save()
be36282b556a add new_thread
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
94 local doc = to_doc(post)
be36282b556a add new_thread
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
95 Db.save(doc)
be36282b556a add new_thread
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
96 post.id = doc.id
be36282b556a add new_thread
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
97 end
be36282b556a add new_thread
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
98
42
0c1b820fff34 use push
Franklin Schmidt <fschmidt@gmail.com>
parents: 18
diff changeset
99 function post.delete()
0c1b820fff34 use push
Franklin Schmidt <fschmidt@gmail.com>
parents: 18
diff changeset
100 Db.run_in_transaction( function()
0c1b820fff34 use push
Franklin Schmidt <fschmidt@gmail.com>
parents: 18
diff changeset
101 not post.is_root or Post.thread_size(post.root_id) == 1 or error "can't delete root post with replies"
0c1b820fff34 use push
Franklin Schmidt <fschmidt@gmail.com>
parents: 18
diff changeset
102 Db.delete("id:"..post.id)
9
9674275019bb reply and edit
Franklin Schmidt <fschmidt@gmail.com>
parents: 8
diff changeset
103 end )
9674275019bb reply and edit
Franklin Schmidt <fschmidt@gmail.com>
parents: 8
diff changeset
104 end
9674275019bb reply and edit
Franklin Schmidt <fschmidt@gmail.com>
parents: 8
diff changeset
105
16
bff178656073 delete function
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
106 function post.author_is_current()
bff178656073 delete function
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
107 local User = require "site:/lib/User.luan"
bff178656073 delete function
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
108 local user = User.current()
bff178656073 delete function
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
109 return user ~= nil and user.name == post.author_name
bff178656073 delete function
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
110 end
bff178656073 delete function
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
111
18
94e26bffd4fb UI work
Franklin Schmidt <fschmidt@gmail.com>
parents: 16
diff changeset
112 function post.ago(now)
94e26bffd4fb UI work
Franklin Schmidt <fschmidt@gmail.com>
parents: 16
diff changeset
113 local diff = now - post.date
94e26bffd4fb UI work
Franklin Schmidt <fschmidt@gmail.com>
parents: 16
diff changeset
114 for _, t in ipairs(times) do
94e26bffd4fb UI work
Franklin Schmidt <fschmidt@gmail.com>
parents: 16
diff changeset
115 local n = diff // t.time
94e26bffd4fb UI work
Franklin Schmidt <fschmidt@gmail.com>
parents: 16
diff changeset
116 if n > 0 then
94e26bffd4fb UI work
Franklin Schmidt <fschmidt@gmail.com>
parents: 16
diff changeset
117 %><%=n%> <%=t.unit%><%
94e26bffd4fb UI work
Franklin Schmidt <fschmidt@gmail.com>
parents: 16
diff changeset
118 if n > 1 then
94e26bffd4fb UI work
Franklin Schmidt <fschmidt@gmail.com>
parents: 16
diff changeset
119 %>s<%
94e26bffd4fb UI work
Franklin Schmidt <fschmidt@gmail.com>
parents: 16
diff changeset
120 end
94e26bffd4fb UI work
Franklin Schmidt <fschmidt@gmail.com>
parents: 16
diff changeset
121 return
94e26bffd4fb UI work
Franklin Schmidt <fschmidt@gmail.com>
parents: 16
diff changeset
122 end
94e26bffd4fb UI work
Franklin Schmidt <fschmidt@gmail.com>
parents: 16
diff changeset
123 end
94e26bffd4fb UI work
Franklin Schmidt <fschmidt@gmail.com>
parents: 16
diff changeset
124 %>0 minutes<%
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
8
be36282b556a add new_thread
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
127 set_metatable(post,metatable)
be36282b556a add new_thread
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
128 return post
be36282b556a add new_thread
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
129 end
be36282b556a add new_thread
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
130
be36282b556a add new_thread
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
131 function Post.get_by_id(id)
43
298c71e0c854 caching
Franklin Schmidt <fschmidt@gmail.com>
parents: 42
diff changeset
132 local post = get_local_only(posts_by_id,id)
298c71e0c854 caching
Franklin Schmidt <fschmidt@gmail.com>
parents: 42
diff changeset
133 if post ~= nil then return post end
8
be36282b556a add new_thread
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
134 local doc = Db.get_document("id:"..id)
9
9674275019bb reply and edit
Franklin Schmidt <fschmidt@gmail.com>
parents: 8
diff changeset
135 return doc and from_doc(doc)
8
be36282b556a add new_thread
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
136 end
be36282b556a add new_thread
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
137
be36282b556a add new_thread
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
138 function Post.new_thread(author,subject,content)
be36282b556a add new_thread
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
139 return Db.run_in_transaction( function()
be36282b556a add new_thread
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
140 local post = Post.new{
9
9674275019bb reply and edit
Franklin Schmidt <fschmidt@gmail.com>
parents: 8
diff changeset
141 subject = subject or error()
8
be36282b556a add new_thread
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
142 content = content
be36282b556a add new_thread
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
143 author_name = author.name
be36282b556a add new_thread
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
144 is_root = true
be36282b556a add new_thread
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
145 }
46
289718f121e4 use bbcode editor
Franklin Schmidt <fschmidt@gmail.com>
parents: 43
diff changeset
146 post.root_id = -1
8
be36282b556a add new_thread
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
147 post.save()
be36282b556a add new_thread
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
148 post.root_id = post.id or error()
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 return post
be36282b556a add new_thread
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
151 end )
be36282b556a add new_thread
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
152 end
be36282b556a add new_thread
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
153
9
9674275019bb reply and edit
Franklin Schmidt <fschmidt@gmail.com>
parents: 8
diff changeset
154 function Post.from_docs(docs)
9674275019bb reply and edit
Franklin Schmidt <fschmidt@gmail.com>
parents: 8
diff changeset
155 local posts = {}
9674275019bb reply and edit
Franklin Schmidt <fschmidt@gmail.com>
parents: 8
diff changeset
156 for _, doc in ipairs(docs) do
9674275019bb reply and edit
Franklin Schmidt <fschmidt@gmail.com>
parents: 8
diff changeset
157 local post = from_doc(doc)
9674275019bb reply and edit
Franklin Schmidt <fschmidt@gmail.com>
parents: 8
diff changeset
158 posts[#posts+1] = post
9674275019bb reply and edit
Franklin Schmidt <fschmidt@gmail.com>
parents: 8
diff changeset
159 end
9674275019bb reply and edit
Franklin Schmidt <fschmidt@gmail.com>
parents: 8
diff changeset
160 return posts
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
42
0c1b820fff34 use push
Franklin Schmidt <fschmidt@gmail.com>
parents: 18
diff changeset
163 function Post.thread_size(root_id)
0c1b820fff34 use push
Franklin Schmidt <fschmidt@gmail.com>
parents: 18
diff changeset
164 return Db.count("post_root_id:"..root_id)
0c1b820fff34 use push
Franklin Schmidt <fschmidt@gmail.com>
parents: 18
diff changeset
165 end
0c1b820fff34 use push
Franklin Schmidt <fschmidt@gmail.com>
parents: 18
diff changeset
166
8
be36282b556a add new_thread
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
167 return Post