-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.rb
103 lines (80 loc) · 2.56 KB
/
server.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
require 'sinatra'
require 'sinatra/json'
require 'json'
require 'haml'
require 'auth'
require 'mac'
class Server < Sinatra::Base
ZULIP_TOKEN = ENV.fetch("ZULIP_SECRET_TOKEN") # used to ensure we're getting requests from Zulip
enable :sessions
set :session_secret, ENV.fetch('SESSION_SECRET')
def initialize(auth_handler = Auth.new)
@auth = auth_handler
super app
end
post "/" do
body = JSON.parse(request.body.read)
msg = body.fetch("message")
validate_token(body.fetch("token"))
content = msg.fetch("content")
handle_message(
msg.fetch("sender_full_name"),
msg.fetch("sender_id"),
msg.fetch("sender_email"),
content
)
end
post "/macs_seen" do
body = JSON.parse(request.body.read)
macs = body.fetch("seen")
macs.each do |mac|
u = User.where(mac: mac)
if u.any?
u.first.seen!
end
end
end
get "/oauth/redirect" do
redirect @auth.authorize
end
get "/oauth/callback" do
token = @auth.callback(params[:code])
session[:access_token] = token
redirect "/"
end
get "/" do
redirect "/oauth/redirect" unless @auth.token_valid?(session[:access_token])
haml :dash
end
get "/users" do
return status 401 unless session[:access_token] && @auth.token_valid?(session[:access_token])
@users = User.seen_recently.sort_by { |u| u.name }.map do |u|
image_url, profile_url = @auth.get_user_details(u.email, session[:access_token])
{name: u.name, image_url: image_url, profile_url: profile_url}
end
json :users => @users, :registered_users => User.all.count
end
def validate_token(token)
raise if token != Server::ZULIP_TOKEN
end
def handle_message(name, zulip_id, email, content)
if content == "forget"
return forget_user(zulip_id)
end
if !Mac.valid?(content)
return json :response_string => "Hi. Please send me your MAC address and I'll show you on the RC Dashboard when you're in the space. Or say 'forget' and I'll remove any trace of you from my database."
end
mac = Mac.normalize(content)
User.new_from_params(name, zulip_id, email, mac)
json :response_string => "Your MAC address has been stored."
end
def forget_user(zulip_id)
u = User.where(zulip_id: zulip_id.to_s)
if u.any?
u.destroy
return json :response_string => "Okay - I've removed all your devices from the database. You'll stop showing on the dashboard in a few minutes."
else
return json :response_string => "Nothing to delete. Looks like you never signed up."
end
end
end