This repository has been archived by the owner on Oct 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathmy_app.rb
248 lines (191 loc) · 5.57 KB
/
my_app.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
require 'rubygems'
require 'sinatra'
require 'json'
require 'pp' if ENV['RACK_ENV'] == 'development'
require './config/init.rb'
STATUS_OK = 0
STATUS_ERROR = 1
STATUS_UNREGISTERED = 2
STATUS_ALREADY_WATCHED = 3
MINIMUM_VERSION = 73
helpers do
def slack_webhook_uri
URI.join(ENV['SLACK_URL'], "services/hooks/incoming-webhook?token=#{ENV['SLACK_TOKEN']}")
end
def agent_version
agent = request.env['HTTP_USER_AGENT']
if agent
matches = /^\w+\/(\d+)\s/.match(agent)
if matches
version = matches[1]
else
version = 9999
end
else
version = 9999
end
end
end
get '/' do
halt 404
end
get '/status' do
erb :index
end
get '/status/table' do
# @people = Person.where{version >= MINIMUM_VERSION}.select_order_map([:name, :status])
# @count = Person.where(:status => 'In').where{version >= MINIMUM_VERSION}.count
@people = Person.select_order_map([:name, :status])
@count = Person.where(:status => 'In').count
erb :table
end
get '/status/list' do
DB.transaction do
DB.run("delete from people where now() - date > INTERVAL '2 day';")
end
# people = Person.order(:name).where{version >= MINIMUM_VERSION}
people = Person.order(:name)
if params[:name]
lowName = params[:name].downcase
requestor = Person[:name => lowName]
halt 400 unless requestor
end
content_type :json
output = []
people.each do |p|
watched_by = false
watches = false
if requestor
watched_by = p.watched_by_name(requestor.name)
watches = p.watches_name(requestor.name)
end
output << { "status" => p.status, "name" => p.name, "watched_by_requestor" => watched_by, "watches_requestor" => watches }
end
output.to_json
end
post '/status/update' do
content_type :json
require_params :name, :status
output = ""
DB.transaction do
lowName = params[:name].downcase
lowName = 'steven' if lowName == 'stevenf'
if params[:push_id] && !params[:push_id].empty?
person = Person.for_update.first(:push_id => params[:push_id])
end
if !person
person = Person.for_update.first(:name => lowName)
end
if !person
person = Person.new()
output = {"result" => STATUS_UNREGISTERED, "msg" => "Name not found"}.to_json
else
status_changed = params[:status] != person.status
output = {"result" => STATUS_OK, "status_changed" => status_changed}.to_json
if status_changed
# Send notifications
# count = Person.where(:status => 'In').where{version >= MINIMUM_VERSION}.count
count = Person.where(:status => 'In').count
recipient_ids = []
person.watchers.each do |w|
if w.push_id != ""
recipient_ids << w.push_id
puts "Queuing notification for #{w.name}"
end
end
if recipient_ids.count > 0
ZeroPush.notify({
device_tokens: recipient_ids,
alert: "#{person.name.capitalize!} is #{params[:status]}",
sound: "status.caf",
badge: "",
info: ""
})
end
end
end
person.status = params[:status]
person.name = lowName
person.push_id = params[:push_id]
person.beacon_minor = params[:beacon_minor]
person.version = agent_version
person.date = DateTime.now
person.save or {"result" => STATUS_ERROR, "reason" => "The record could not be saved"}.to_json
puts "STATUS UPDATE: #{person.name.capitalize!} is #{params[:status]}"
end
output
end
post '/message/in' do
content_type :json
require_params :name, :message
lowName = params[:name].downcase
lowName = 'steven' if lowName == 'stevenf'
sender = Person.first(:name => lowName)
in_people = Person.where(:status => 'In')
message = Message.new
message.person = sender
message.date = DateTime.now
message.message = params[:message]
message.save or { "result" => STATUS_ERROR, "reason" => "The record could not be saved" }.to_json
recipient_ids = []
in_people.each do |p|
if p.push_id != ""
recipient_ids << p.push_id
puts "Queuing message notification for #{p.name}"
end
end
if recipient_ids.count > 0
ZeroPush.notify({
device_tokens: recipient_ids,
alert: "#{sender.name.capitalize!}: #{params[:message]}",
sound: "message.caf",
badge: "",
info: ""
})
end
{"result" => STATUS_OK}.to_json
end
get '/messages' do
content_type :json
Message.reverse_order(:date).to_json(
except: [:id, :person_id],
include: { person: { only: :name } }
)
end
post '/watch/:target' do
content_type :json
require_params :name, :target
target = Person[:name => params[:target].downcase]
watcher = Person[:name => params[:name].downcase]
if target.watched_by_name(watcher.name)
{ "status" => STATUS_ALREADY_WATCHED }.to_json
else
target.add_watcher(watcher)
{ "status" => STATUS_OK }.to_json
end
end
post '/unwatch/:target' do
content_type :json
require_params :name, :target
target = Person[:name => params[:target].downcase]
watcher = Person[:name => params[:name].downcase]
if target.watched_by_name(watcher.name)
target.remove_watcher(watcher)
end
{ "status" => STATUS_OK }.to_json
end
get '/image/:name' do
require_params :name
image_name = "#{params[:name]}.png"
image_path = File.expand_path(image_name, settings.public_folder)
if File.exists?(image_path)
send_file image_path
else
send_file File.expand_path("unknown.png", settings.public_folder)
end
end
def require_params(*parameters)
parameters.each do |param|
halt 400 unless params[param]
end
end