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()
|
46
|
14 local Html = require "luan:Html.luan"
|
|
15 local html_encode = Html.encode or error()
|
2
|
16 local Lucene = require "luan:lucene/Lucene.luan"
|
|
17 local lucene_quote = Lucene.quote or error()
|
|
18 local Http = require "luan:http/Http.luan"
|
|
19 local Db = require "site:/lib/Db.luan"
|
3
|
20 local run_in_transaction = Db.run_in_transaction or error()
|
4
|
21 local Chat = require "site:/lib/Chat.luan"
|
30
|
22 local chat_search = Chat.search or error()
|
33
|
23 local Utils = require "site:/lib/Utils.luan"
|
|
24 local list_to_set = Utils.list_to_set or error()
|
|
25 local set_to_list = Utils.set_to_list or error()
|
88
|
26 local base_url = Utils.base_url or error()
|
2
|
27 local Logging = require "luan:logging/Logging.luan"
|
|
28 local logger = Logging.logger "User"
|
|
29
|
|
30
|
|
31 local User = {}
|
|
32
|
|
33 local function from_doc(doc)
|
|
34 doc.type == "user" or error "wrong type"
|
|
35 return User.new {
|
|
36 id = doc.id
|
|
37 email = doc.user_email
|
|
38 password = doc.password
|
40
|
39 was_notified = doc.was_notified=="true"
|
|
40 notify_email = doc.notify_email
|
|
41 multi_notify = doc.multi_notify=="true"
|
41
|
42 voice_url = doc.voice_url
|
46
|
43 name = doc.name
|
2
|
44 }
|
|
45 end
|
|
46
|
|
47 local function to_doc(user)
|
|
48 return {
|
|
49 type = "user"
|
|
50 id = user.id
|
|
51 user_email = user.email
|
|
52 password = user.password
|
40
|
53 was_notified = user.was_notified and "true" or nil
|
|
54 notify_email = user.notify_email
|
|
55 multi_notify = user.multi_notify and "true" or nil
|
41
|
56 voice_url = user.voice_url
|
46
|
57 name = user.name
|
2
|
58 }
|
|
59 end
|
|
60
|
|
61 function User.new(user)
|
|
62
|
|
63 function user.save()
|
|
64 local doc = to_doc(user)
|
|
65 Db.save(doc)
|
|
66 user.id = doc.id
|
|
67 end
|
|
68
|
40
|
69 function user.reload()
|
|
70 return User.get_by_id(user.id) or error(user.id)
|
|
71 end
|
|
72
|
4
|
73 function user.delete()
|
|
74 run_in_transaction( function()
|
|
75 local id = user.id
|
30
|
76 local chats = chat_search("chat_user_ids:"..id)
|
4
|
77 for _, chat in ipairs(chats) do
|
|
78 chat.delete()
|
|
79 end
|
|
80 Db.delete("id:"..id)
|
|
81 end )
|
2
|
82 end
|
|
83
|
|
84 function user.login()
|
|
85 local id = to_string(user.id)
|
|
86 Http.response.set_persistent_cookie("user",id)
|
|
87 Http.response.set_persistent_cookie("password",user.password)
|
|
88 Http.request.cookies.user = id
|
|
89 Http.request.cookies.password = user.password or error()
|
|
90 end
|
|
91
|
30
|
92 function user.last_update()
|
|
93 local chats = chat_search( "chat_user_ids:"..user.id, "chat_updated desc", 1 )
|
|
94 local n = #chats
|
|
95 if n == 0 then
|
|
96 return 0
|
|
97 elseif n == 1 then
|
|
98 return chats[1].updated
|
|
99 else error() end
|
|
100 end
|
|
101
|
33
|
102 function user.chatting_with_ids()
|
|
103 local my_id = user.id
|
|
104 local user_ids = list_to_set{}
|
|
105 local chats = chat_search( "chat_user_ids:"..my_id )
|
|
106 for _, chat in ipairs(chats) do
|
|
107 for _, user_id in ipairs(chat.user_ids) do
|
|
108 user_ids[user_id] = true
|
|
109 end
|
|
110 end
|
|
111 user_ids[my_id] = false
|
|
112 return set_to_list(user_ids)
|
|
113 end
|
|
114
|
46
|
115 function user.name_html()
|
|
116 return html_encode(user.name or user.email)
|
|
117 end
|
|
118
|
88
|
119 function user.login_url()
|
|
120 return base_url().."/do_login.html?user="..user.id.."&password="..user.password
|
|
121 end
|
|
122
|
2
|
123 return user
|
|
124 end
|
|
125
|
|
126 local function get_by_id(id)
|
|
127 local doc = Db.get_document("id:"..id)
|
|
128 return doc and doc.type=="user" and from_doc(doc) or nil
|
|
129 end
|
|
130 User.get_by_id = get_by_id
|
|
131
|
3
|
132 local password_chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
|
|
133 do
|
|
134 local t = {}
|
|
135 for i in range(1,#password_chars) do
|
|
136 t[#t+1] = sub_string(password_chars,i,i)
|
|
137 end
|
|
138 password_chars = t
|
|
139 end
|
|
140
|
|
141 local function new_password()
|
|
142 local n = #password_chars
|
|
143 local t = {}
|
|
144 for _ in range(1,10) do
|
|
145 t[#t+1] = password_chars[random(n)]
|
|
146 end
|
|
147 return concat(t)
|
|
148 end
|
|
149
|
4
|
150 local function get_by_email(email)
|
|
151 local doc = Db.get_document("user_email:"..lucene_quote(email))
|
|
152 return doc and from_doc(doc)
|
|
153 end
|
|
154 User.get_by_email = get_by_email
|
|
155
|
3
|
156 function User.get_or_create_by_email(email)
|
|
157 return run_in_transaction( function()
|
4
|
158 local user = get_by_email(email)
|
|
159 if user == nil then
|
|
160 user = User.new{
|
3
|
161 email = email
|
|
162 password = new_password()
|
40
|
163 notify_email = email
|
3
|
164 }
|
|
165 user.save()
|
|
166 end
|
4
|
167 return user
|
3
|
168 end )
|
2
|
169 end
|
|
170
|
|
171 function User.search(query,sort,rows)
|
|
172 rows = rows or 1000000
|
|
173 local users = {}
|
|
174 local docs = Db.search(query,1,rows,{sort=sort})
|
|
175 for _, doc in ipairs(docs) do
|
|
176 users[#users+1] = from_doc(doc)
|
|
177 end
|
|
178 return users
|
|
179 end
|
|
180
|
3
|
181 local function current()
|
2
|
182 local user = get_local_only(User,"current")
|
|
183 if user == nil then
|
|
184 local id = Http.request.cookies.user
|
|
185 local password = Http.request.cookies.password
|
|
186 if id == nil or password == nil then
|
|
187 user = "nil"
|
|
188 else
|
|
189 user = get_by_id(id)
|
3
|
190 if user == nil or user.password ~= password then
|
2
|
191 user = "nil"
|
|
192 end
|
|
193 end
|
|
194 set_local_only(User,"current",user)
|
|
195 end
|
|
196 return user ~= "nil" and user or nil
|
|
197 end
|
3
|
198 User.current = current
|
2
|
199
|
|
200 function User.current_required()
|
3
|
201 local user = current()
|
2
|
202 user or Http.response.send_redirect "/login.html"
|
|
203 return user
|
|
204 end
|
|
205
|
|
206 return User
|