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