2
|
1 local Luan = require "luan:Luan.luan"
|
|
2 local error = Luan.error
|
|
3 local ipairs = Luan.ipairs or error()
|
3
|
4 local range = Luan.range or error()
|
2
|
5 local to_string = Luan.to_string or error()
|
|
6 local get_local_only = Luan.get_local_only or error()
|
|
7 local set_local_only = Luan.set_local_only or error()
|
3
|
8 local String = require "luan:String.luan"
|
|
9 local sub_string = String.sub or error()
|
|
10 local Math = require "luan:Math.luan"
|
|
11 local random = Math.random or error()
|
|
12 local Table = require "luan:Table.luan"
|
|
13 local concat = Table.concat or error()
|
2
|
14 local Lucene = require "luan:lucene/Lucene.luan"
|
|
15 local lucene_quote = Lucene.quote or error()
|
|
16 local Http = require "luan:http/Http.luan"
|
|
17 local Db = require "site:/lib/Db.luan"
|
3
|
18 local run_in_transaction = Db.run_in_transaction or error()
|
4
|
19 local Chat = require "site:/lib/Chat.luan"
|
30
|
20 local chat_search = Chat.search or error()
|
2
|
21 local Logging = require "luan:logging/Logging.luan"
|
|
22 local logger = Logging.logger "User"
|
|
23
|
|
24
|
|
25 local User = {}
|
|
26
|
|
27 local function from_doc(doc)
|
|
28 doc.type == "user" or error "wrong type"
|
|
29 return User.new {
|
|
30 id = doc.id
|
|
31 email = doc.user_email
|
|
32 password = doc.password
|
|
33 }
|
|
34 end
|
|
35
|
|
36 local function to_doc(user)
|
|
37 return {
|
|
38 type = "user"
|
|
39 id = user.id
|
|
40 user_email = user.email
|
|
41 password = user.password
|
|
42 }
|
|
43 end
|
|
44
|
|
45 function User.new(user)
|
|
46
|
|
47 function user.save()
|
|
48 local doc = to_doc(user)
|
|
49 Db.save(doc)
|
|
50 user.id = doc.id
|
|
51 end
|
|
52
|
4
|
53 function user.delete()
|
|
54 run_in_transaction( function()
|
|
55 local id = user.id
|
30
|
56 local chats = chat_search("chat_user_ids:"..id)
|
4
|
57 for _, chat in ipairs(chats) do
|
|
58 chat.delete()
|
|
59 end
|
|
60 Db.delete("id:"..id)
|
|
61 end )
|
2
|
62 end
|
|
63
|
|
64 function user.login()
|
|
65 local id = to_string(user.id)
|
|
66 Http.response.set_persistent_cookie("user",id)
|
|
67 Http.response.set_persistent_cookie("password",user.password)
|
|
68 Http.request.cookies.user = id
|
|
69 Http.request.cookies.password = user.password or error()
|
|
70 end
|
|
71
|
30
|
72 function user.last_update()
|
|
73 local chats = chat_search( "chat_user_ids:"..user.id, "chat_updated desc", 1 )
|
|
74 local n = #chats
|
|
75 if n == 0 then
|
|
76 return 0
|
|
77 elseif n == 1 then
|
|
78 return chats[1].updated
|
|
79 else error() end
|
|
80 end
|
|
81
|
2
|
82 return user
|
|
83 end
|
|
84
|
|
85 local function get_by_id(id)
|
|
86 local doc = Db.get_document("id:"..id)
|
|
87 return doc and doc.type=="user" and from_doc(doc) or nil
|
|
88 end
|
|
89 User.get_by_id = get_by_id
|
|
90
|
3
|
91 local password_chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
|
|
92 do
|
|
93 local t = {}
|
|
94 for i in range(1,#password_chars) do
|
|
95 t[#t+1] = sub_string(password_chars,i,i)
|
|
96 end
|
|
97 password_chars = t
|
|
98 end
|
|
99
|
|
100 local function new_password()
|
|
101 local n = #password_chars
|
|
102 local t = {}
|
|
103 for _ in range(1,10) do
|
|
104 t[#t+1] = password_chars[random(n)]
|
|
105 end
|
|
106 return concat(t)
|
|
107 end
|
|
108
|
4
|
109 local function get_by_email(email)
|
|
110 local doc = Db.get_document("user_email:"..lucene_quote(email))
|
|
111 return doc and from_doc(doc)
|
|
112 end
|
|
113 User.get_by_email = get_by_email
|
|
114
|
3
|
115 function User.get_or_create_by_email(email)
|
|
116 return run_in_transaction( function()
|
4
|
117 local user = get_by_email(email)
|
|
118 if user == nil then
|
|
119 user = User.new{
|
3
|
120 email = email
|
|
121 password = new_password()
|
|
122 }
|
|
123 user.save()
|
|
124 end
|
4
|
125 return user
|
3
|
126 end )
|
2
|
127 end
|
|
128
|
|
129 function User.search(query,sort,rows)
|
|
130 rows = rows or 1000000
|
|
131 local users = {}
|
|
132 local docs = Db.search(query,1,rows,{sort=sort})
|
|
133 for _, doc in ipairs(docs) do
|
|
134 users[#users+1] = from_doc(doc)
|
|
135 end
|
|
136 return users
|
|
137 end
|
|
138
|
3
|
139 local function current()
|
2
|
140 local user = get_local_only(User,"current")
|
|
141 if user == nil then
|
|
142 local id = Http.request.cookies.user
|
|
143 local password = Http.request.cookies.password
|
|
144 if id == nil or password == nil then
|
|
145 user = "nil"
|
|
146 else
|
|
147 user = get_by_id(id)
|
3
|
148 if user == nil or user.password ~= password then
|
2
|
149 user = "nil"
|
|
150 end
|
|
151 end
|
|
152 set_local_only(User,"current",user)
|
|
153 end
|
|
154 return user ~= "nil" and user or nil
|
|
155 end
|
3
|
156 User.current = current
|
2
|
157
|
|
158 function User.current_required()
|
3
|
159 local user = current()
|
2
|
160 user or Http.response.send_redirect "/login.html"
|
|
161 return user
|
|
162 end
|
|
163
|
|
164 return User
|