annotate src/lib/User.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
3
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1 local Luan = require "luan:Luan.luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
2 local error = Luan.error
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
3 local set_metatable = Luan.set_metatable or error()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
4 local range = Luan.range or error()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
5 local String = require "luan:String.luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
6 local sub_string = String.sub or error()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
7 local Table = require "luan:Table.luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
8 local concat = Table.concat or error()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
9 local Math = require "luan:Math.luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
10 local random = Math.random or error()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
11 local Time = require "luan:Time.luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
12 local time_now = Time.now or error()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
13 local Html = require "luan:Html.luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
14 local html_encode = Html.encode or error()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
15 local Lucene = require "luan:lucene/Lucene.luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
16 local lucene_quote = Lucene.quote or error()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
17 local Http = require "luan:http/Http.luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
18 local Db = require "site:/lib/Db.luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
19
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
20
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
21 local User = {}
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
22
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
23 local function from_doc(doc)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
24 doc.type == "user" or error "wrong type"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
25 return User.new {
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
26 id = doc.id
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
27 email = doc.user_email
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
28 password = doc.password
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
29 name = doc.user_name
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
30 created = doc.created
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
31 }
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
32 end
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
33
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
34 local function to_doc(user)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
35 local email = user.email
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
36 return {
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
37 type = "user"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
38 id = user.id
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
39 user_email = email
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
40 password = user.password
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
41 user_name = user.name
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
42 created = user.created or time_now()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
43 }
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
44 end
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
45
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
46 local metatable = {}
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
47 function metatable.__index(user,key)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
48 if key == "name_html" then
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
49 user.name_html = html_encode(user.name)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
50 return user.name_html
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
51 end
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
52 return nil
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
53 end
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
54
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
55 function User.new(user)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
56
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
57 function user.save()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
58 local doc = to_doc(user)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
59 Db.save(doc)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
60 user.id = doc.id
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
61 end
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
62
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
63 set_metatable(user,metatable)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
64 return user
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
65 end
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
66
8
be36282b556a add new_thread
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
67 function User.get_by_id(id)
be36282b556a add new_thread
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
68 local doc = Db.get_document("id:"..id)
be36282b556a add new_thread
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
69 return doc and User.from_doc(doc)
be36282b556a add new_thread
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
70 end
be36282b556a add new_thread
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
71
3
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
72 function User.get_by_email(email)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
73 local doc = Db.get_document("user_email:"..lucene_quote(email))
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
74 return doc and from_doc(doc)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
75 end
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
76
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
77 local function get_by_name(name)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
78 local doc = Db.get_document("user_name:"..lucene_quote(name))
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
79 return doc and from_doc(doc)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
80 end
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
81 User.get_by_name = get_by_name
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
82
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
83 function User.current()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
84 local name = Http.request.cookies.user
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
85 local password = Http.request.cookies.password
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
86 if name == nil or password == nil then
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
87 return nil
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
88 end
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
89 local user = get_by_name(name)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
90 if user == nil or user.password ~= password then
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
91 return nil
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
92 end
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
93 return user
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
94 end
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
95
8
be36282b556a add new_thread
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
96 function User.current_required()
be36282b556a add new_thread
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
97 local user = User.current()
be36282b556a add new_thread
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
98 user or Http.response.send_redirect "/login.html"
be36282b556a add new_thread
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
99 return user
be36282b556a add new_thread
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
100 end
be36282b556a add new_thread
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
101
3
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
102 local password_chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
103 do
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
104 local t = {}
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
105 for i in range(1,#password_chars) do
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
106 t[#t+1] = sub_string(password_chars,i,i)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
107 end
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
108 password_chars = t
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
109 end
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
110
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
111 local function new_password()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
112 local n = #password_chars
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
113 local t = {}
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
114 for _ in range(1,10) do
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
115 t[#t+1] = password_chars[random(n)]
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
116 end
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
117 return concat(t)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
118 end
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
119
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
120 function User.get_or_create_by_email(email)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
121 local user = User.get_by_email(email)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
122 if user == nil then
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
123 user = User.new{ email=email, password=new_password() }
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
124 user.save()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
125 end
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
126 return user
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
127 end
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
128
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
129 return User