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