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