annotate src/lib/User.luan @ 2:ee1f91e67509

add Db
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 24 Oct 2024 15:37:35 -0600
parents
children 2c63b10781e1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2
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 ipairs = Luan.ipairs or error()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
4 local to_string = Luan.to_string or error()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
5 local get_local_only = Luan.get_local_only or error()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
6 local set_local_only = Luan.set_local_only or error()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
7 local Lucene = require "luan:lucene/Lucene.luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
8 local lucene_quote = Lucene.quote or error()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
9 local Http = require "luan:http/Http.luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
10 local Db = require "site:/lib/Db.luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
11 local Logging = require "luan:logging/Logging.luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
12 local logger = Logging.logger "User"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
13
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
14
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
15 local User = {}
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
16
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
17 local function from_doc(doc)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
18 doc.type == "user" or error "wrong type"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
19 return User.new {
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
20 id = doc.id
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
21 email = doc.user_email
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
22 password = doc.password
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
23 }
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
24 end
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
25
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
26 local function to_doc(user)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
27 return {
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
28 type = "user"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
29 id = user.id
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
30 user_email = user.email
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
31 password = user.password
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
32 }
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
33 end
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
34
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
35 function User.new(user)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
36
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
37 function user.save()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
38 local doc = to_doc(user)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
39 Db.save(doc)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
40 user.id = doc.id
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
41 end
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
42
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
43 function user.reload()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
44 return User.get_by_id(user.id) or error(user.id)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
45 end
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
46
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
47 function user.login()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
48 local id = to_string(user.id)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
49 Http.response.set_persistent_cookie("user",id)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
50 Http.response.set_persistent_cookie("password",user.password)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
51 Http.request.cookies.user = id
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
52 Http.request.cookies.password = user.password or error()
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 return user
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
56 end
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
57
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
58 local function get_by_id(id)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
59 local doc = Db.get_document("id:"..id)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
60 return doc and doc.type=="user" and from_doc(doc) or nil
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
61 end
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
62 User.get_by_id = get_by_id
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
63
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
64 function User.get_by_email(email)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
65 local doc = Db.get_document("user_email:"..lucene_quote(email))
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
66 return doc and from_doc(doc)
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 function User.search(query,sort,rows)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
70 rows = rows or 1000000
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
71 local users = {}
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
72 local docs = Db.search(query,1,rows,{sort=sort})
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
73 for _, doc in ipairs(docs) do
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
74 users[#users+1] = from_doc(doc)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
75 end
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
76 return users
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
77 end
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
78
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
79 function User.current()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
80 local user = get_local_only(User,"current")
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
81 if user == nil then
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
82 local id = Http.request.cookies.user
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
83 local password = Http.request.cookies.password
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
84 if id == nil or password == nil then
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
85 user = "nil"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
86 else
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
87 user = get_by_id(id)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
88 if user == nil or user.registered == nil or user.password ~= password then
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
89 user = "nil"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
90 end
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
91 end
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
92 set_local_only(User,"current",user)
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
93 end
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
94 return user ~= "nil" and user or nil
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
95 end
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
96
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
97 function User.current_required()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
98 local user = User.current()
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
99 user or Http.response.send_redirect "/login.html"
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
100 return user
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
101 end
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
102
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
103 return User