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