annotate src/lib/Post.luan @ 42:0c1b820fff34

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