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