-
Notifications
You must be signed in to change notification settings - Fork 0
/
au_boo_d.rb
executable file
·227 lines (214 loc) · 5.48 KB
/
au_boo_d.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
require 'rubygems'
require 'sinatra'
require 'mongo'
require 'json/ext'
require 'json'
require 'haml'
include Mongo
#
# configuration for setting
#
configure do
enable :sessions
# set for external connection
set :bind, '0.0.0.0'
# this place must implement a random secret key generation
set :session_secret, 'booit'
conn = MongoClient.new("localhost",27017)
set :mongo_connection, conn
set :mongo_db, conn.db('Boo')
end
#
# helpers for methods
#
helpers do
# lookup for id
def mongo_id val
BSON::ObjectId.from_string(val)
end
# find password from id
def password_by_id id
id = mongo_id(id) if String === id
@obj = settings.mongo_db['accounts'].find_one(:_id => id).to_json
parsed = JSON.parse(@obj)
@password = parsed["password"]
end
# find username from id
def username_by_id id
id = mongo_id(id) if String === id
@obj = settings.mongo_db['accounts'].find_one(:_id => id).to_json
parsed = JSON.parse(@obj)
return parsed["username"].to_s
end
# find friends from id
def friends_by_id id
id = mongo_id(id) if String === id
@obj = settings.mongo_db['accounts'].find_one(:_id => id).to_json
parsed = JSON.parse(@obj)
friends = parsed["friends"]
if friends.nil?
return nil.to_a.to_json
else
return friends.to_json
end
end
# find history msg from history_id
def history_by_id id
@history_obj = settings.mongo_db['dialog'].find_one(:did => id).to_json
#p "`````#{@history_obj.to_s}`````"
if @history_obj.to_s == "null"
return nil.to_a.to_json
else
his_parsed = JSON.parse(@history_obj)
ch_log = his_parsed["dialog"]
return ch_log.to_json
end
end
# check username exists ?
def check_username (name,pwd)
@lookup_result = settings.mongo_db['accounts'].find_one(:username => name).to_json
if @lookup_result.to_s == "null"
session.clear
haml :notfound
else
parsed = JSON.parse(@lookup_result)
@password_l = parsed["password"]
@session_id = parsed["_id"]["$oid"]
if @password_l.eql?(pwd)
# need to make a ticket sender server
#"show password: #{@password_l}"
session[:id] = @session_id
redirect 'friends'
#"#{parsed["_id"]["$oid"]}"
else
# need to make a simeple error redirect page
session.clear
redirect 'hello'
end
end
end
end
#
# action to direct to client to boo !
#
get '/friends' do
if session[:id] == nil
session.clear
redirect 'hello'
else
# MUST to design a random key to hash the session
if session[:id].to_s.eql?(mongo_id(session[:id]).to_s)
# this part needs a algorithm to send ticket mechanism
login_usr = username_by_id(session[:id])
friends_json = friends_by_id(session[:id])
haml :friends_list , :locals => {:user_name => "#{login_usr}",:friends_list => friends_json}
else
# this part need to some soliutions
# 1: clean session(comparison)
# 2: query DB info. again
# 3: RANDOM KET SETTING
#"====#{@session_id}==="
#session[:id]
"You are not permissioned to login!"
end
end
end
#
# client for Boo-it chating!
#
post '/chat' do
if session[:id] == nil
session.clear
redirect 'hello'
else
if session[:id].to_s.eql?(mongo_id(session[:id]).to_s)
# this part needs a algorithm to send ticket mechanism
login_usr = username_by_id(session[:id])
# history send!
historyid = []
historyid.push(login_usr.to_s)
historyid.push(params[:chat_f].to_s)
sort_his_id = historyid.sort
q_history_id = sort_his_id[0].to_s + sort_his_id[1].to_s
history_json = history_by_id(q_history_id)
haml :chat , :locals => {:user_name => "#{login_usr}",:friend_name => params[:chat_f],:history_msg => history_json}
else
"You are not permissioned to login!"
end
end
end
#
# if use incorrect http request to client
#
get '/test_client' do
session.clear
redirect 'hello'
end
#
# redirect page
#
get '/redirect' do
haml :redirect
end
#
# logout and clean the session
#
get '/logout' do
session.clear
redirect 'hello'
end
#
# action to lookup database
#
get '/documents/?' do
content_type :json
#settings.mongo_db['accounts'].find.to_a.to_json
settings.mongo_db['dialog'].find.to_a.to_json
end
#
# action to lookup by id
#
get '/documents/:id' do
content_type :json
password_by_id(params[:id])
end
#
# show index
#
get '/hello' do
haml :hello
end
#
# login
#
post '/login' do
@username = params[:username]
@password = params[:password]
check_username(params[:username],params[:password])
#erb :result
end
#
# if Page NOT FOUND
#
# 404 Error!
not_found do
status 404
haml :page_404
end
#
# mongoDB test
#
get '/collections/?' do
content_type :json
settings.mongo_db.collection_names.to_json
end
#
# friends_test
#
get '/test' do
if session[:tmp] == nil
"nil"
else
haml :test , :locals =>{:tmp => "123"}
end
end