annotate src/lib/User.luan @ 47:7ef9222474e2

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