-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.rb
520 lines (439 loc) · 15.9 KB
/
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
class Imasquare < Sinatra::Base
use Rack::Session::Cookie,
key: 'rack.session',
expire_after: 60 * 60 * 24 * 14,
secret: ENV.fetch('IMASQUARE_SESSION_SECRET', 'd1d_y0u_kN0w')
use OmniAuth::Builder do
provider :slack,
ENV.fetch('IMASQUARE_SLACK_CLIENT_ID'),
ENV.fetch('IMASQUARE_SLACK_CLIENT_SECRET'),
scope: 'users:read,channels:write,identify',
team: ENV.fetch('IMASQUARE_SLACK_TEAM_NAME', 'imas-hack')
end
helpers do
alias_method :h, :escape_html
def markdown
@markdown ||= Redcarpet::Markdown.new(Redcarpet::Render::HTML.new(hard_wrap: true), autolink: true, tables: true)
end
def current_user
if session[:user_id]
@current_user ||= db.xquery(
'SELECT id, nickname, is_admin, avatar_url FROM users WHERE id = ?',
session[:user_id]
).first
end
end
def logined?
!current_user.nil?
end
def login_required!
redirect('/', 303) unless logined?
end
def admin_user?
current_user&.[]('is_admin') == 1
end
def admin_required!
redirect('/', 303) unless admin_user?
end
def db
@db_client ||= Mysql2::Client.new(
host: ENV.fetch('IMASQUARE_DB_HOST', 'localhost'),
port: ENV.fetch('IMASQUARE_DB_PORT', 3306),
username: ENV.fetch('IMASQUARE_DB_USERNAME', 'root'),
password: ENV.fetch('IMASQUARE_DB_PASSWORD', ''),
database: ENV.fetch('IMASQUARE_DB_NAME', 'imasquare'),
encoding: 'utf8mb4'
)
end
end
configure do
enable :sessions
end
configure :development do
require 'sinatra/reloader'
require 'pry'
register Sinatra::Reloader
end
get '/home' do
query = <<~SQL
SELECT teams.id, teams.name, teams.is_single, users.id AS leader_id, users.nickname AS leader_name FROM teams
INNER JOIN users_teams AS ut ON teams.id = ut.team_id AND ut.role = 'leader'
INNER JOIN users ON ut.user_id = users.id
SQL
@teams = db.query(query)
query = <<~SQL
SELECT teams.id AS team_id, users.id, users.nickname, users.avatar_url FROM users
INNER JOIN users_teams AS ut ON users.id = ut.user_id
INNER JOIN teams ON ut.team_id = teams.id
WHERE teams.id IN (?)
SQL
team_member = @teams.any? ? db.xquery(query, @teams.map { |t| t['id'] }) : []
@teams.each do |team|
team['members'] = team_member.select { |tm| tm['team_id'] == team['id'] }
end
query = <<~SQL
SELECT
entries.id, title, demo_available, slide_available,
teams.id AS team_id, teams.name AS team_name,
users.id AS author_id, users.nickname AS author_name, users.avatar_url
FROM entries
INNER JOIN teams ON teams.id = entries.team_id
INNER JOIN users ON entries.author_id = users.id
ORDER BY entries.updated_at DESC
SQL
@entries = db.query(query)
erb :home
end
get '/users' do
@users = db.xquery('SELECT id, nickname, avatar_url FROM users')
erb '/users/index'.to_sym
end
get '/users/:user_id' do
@user = db.xquery('SELECT id, nickname, avatar_url FROM users WHERE id = ? LIMIT 1', params['user_id']).first
query = <<~SQL
SELECT teams.id, teams.name, ut.role FROM users
INNER JOIN users_teams AS ut ON users.id = ut.user_id
INNER JOIN teams ON ut.team_id = teams.id
WHERE users.id = ?
SQL
@user_teams = db.xquery(query, params['user_id'])
erb 'users/show'.to_sym
end
get '/teams' do
redirect '/', 303
end
get '/teams/new' do
login_required!
erb 'teams/new'.to_sym
end
post '/teams' do
login_required!
query = <<~SQL
INSERT INTO teams (name, description, is_single, created_at, updated_at)
VALUES (?, ?, ?, NOW(), NOW())
SQL
db.xquery(query, params['name'], params['description'], params['is_single'].to_i)
team_id = db.last_id
query = <<~SQL
INSERT INTO users_teams (user_id, team_id, role, created_at, updated_at)
VALUES (?, ?, "leader", NOW(), NOW())
SQL
db.xquery(query, current_user['id'], team_id)
redirect("/teams/#{team_id}", 303)
end
get '/teams/:team_id' do
query = <<~SQL
SELECT teams.id, teams.name, description, is_single, users.id AS leader_id, users.nickname AS leader_name FROM teams
INNER JOIN users_teams AS ut ON teams.id = ut.team_id AND ut.role = 'leader'
INNER JOIN users ON ut.user_id = users.id
WHERE teams.id = ?
SQL
@team = db.xquery(query, params['team_id']).first
query = <<~SQL
SELECT users.id, users.nickname, users.avatar_url, ut.role FROM users_teams AS ut
INNER JOIN users ON ut.user_id = users.id
WHERE ut.team_id = ?
SQL
@members = db.xquery(query, params['team_id'])
query = <<~SQL
SELECT entries.id, entries.title, users.id AS author_id, users.nickname AS author_name, users.avatar_url FROM entries
INNER JOIN users ON entries.author_id = users.id
WHERE entries.team_id = ? ORDER BY entries.updated_at DESC
SQL
@entries = db.xquery(query, params['team_id'])
erb 'teams/show'.to_sym
end
post '/teams/:team_id/join' do
login_required!
query = <<~SQL
SELECT users.id, users.nickname, ut.team_id, ut.role FROM users_teams AS ut
INNER JOIN users ON ut.user_id = users.id
WHERE ut.team_id = ?
SQL
members = db.xquery(query, params['team_id'])
user_id = (admin_user? && params['admin'] && params['user_id']) ? params['user_id'] : current_user['id']
unless members.map { |m| m['id'] }.include?(user_id)
query = <<~SQL
INSERT INTO users_teams (user_id, team_id, role, created_at, updated_at)
VALUES (?, ?, "member", NOW(), NOW())
SQL
db.xquery(query, user_id, params['team_id'])
end
redirect("/teams/#{members.first['team_id']}", 303)
end
get '/teams/:team_id/leave' do
login_required!
query = <<~SQL
SELECT users.id, users.nickname, ut.team_id, ut.role FROM users_teams AS ut
INNER JOIN users ON ut.user_id = users.id
WHERE ut.team_id = ?
SQL
members = db.xquery(query, params['team_id'])
user_id = (admin_user? && params['admin'] && params['user_id']) ? params['user_id'] : current_user['id']
ut = members.find { |m| m['id'] == user_id }
return 403 if ut['role'] == 'leader'
query = <<~SQL
DELETE FROM users_teams WHERE user_id = ? AND team_id = ?
SQL
db.xquery(query, user_id, params['team_id'])
redirect("/teams/#{params['team_id']}", 303)
end
get '/teams/:team_id/edit' do
login_required!
query = <<~SQL
SELECT teams.id, teams.name, description, is_single, users.id AS leader_id, users.nickname AS leader_name FROM teams
INNER JOIN users_teams AS ut ON teams.id = ut.team_id AND ut.role = 'leader'
INNER JOIN users ON ut.user_id = users.id
WHERE teams.id = ?
SQL
@team = db.xquery(query, params['team_id']).first
return 403 if @team['leader_id'] != current_user['id']
erb 'teams/edit'.to_sym
end
post '/teams/:team_id' do
login_required!
ut = db.xquery('SELECT user_id FROM users_teams WHERE team_id = ? AND role = "leader"', params['team_id']).first
return 403 if ut['user_id'] != current_user['id']
db.xquery('UPDATE teams SET name = ?, description = ?, is_single = ?, updated_at = NOW() WHERE id = ?', params['name'], params['description'], params['is_single'].to_i, params['team_id'])
redirect("/teams/#{params['team_id']}", 303)
end
get '/teams/:team_id/entries/new' do
login_required!
query = <<~SQL
SELECT teams.id, name FROM teams
INNER JOIN users_teams AS ut ON teams.id = ut.team_id
WHERE teams.id = ? AND ut.user_id = ?
SQL
@team = db.xquery(query, params['team_id'], current_user['id']).first
if @team
erb 'teams/entries/new'.to_sym
else
redirect '/', 403
end
end
get '/entries' do
query = <<~SQL
SELECT
entries.*,
teams.id AS team_id, teams.name AS team_name,
users.id AS author_id, users.nickname AS author_name, users.avatar_url
FROM entries
INNER JOIN teams ON teams.id = entries.team_id
INNER JOIN users ON entries.author_id = users.id
WHERE entries.accepted = 1
ORDER BY entries.order
SQL
@entries = db.query(query)
erb 'entries/index'.to_sym
end
get '/entries/new' do
login_required!
query = <<~SQL
SELECT teams.id, name FROM teams
INNER JOIN users_teams AS ut ON teams.id = ut.team_id
WHERE ut.user_id = ?
SQL
@teams = db.xquery(query, current_user['id'])
if @teams.any?
erb 'entries/new'.to_sym
else
redirect '/teams/new', 303
end
end
post '/entries' do
login_required!
query = <<~SQL
SELECT teams.id, name FROM teams
INNER JOIN users_teams AS ut ON teams.id = ut.team_id
WHERE teams.id = ? AND ut.user_id = ?
SQL
@team = db.xquery(query, params['team_id'], current_user['id']).first
unless @team
redirect '/', 403
return
end
query = <<~SQL
INSERT INTO entries
(author_id, team_id, title, summary, note, slide_available, demo_available, time_request, order_request, created_at, updated_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, NOW(), NOW())
SQL
db.xquery(
query,
current_user['id'], params['team_id'], params['title'], params['summary'], params['note'],
params['slide_available'].to_i, params['demo_available'],
params['time_request'], params['order_request']
)
entry_id = db.last_id
redirect("/entries/#{entry_id}", 303)
end
post '/teams/:team_id/entries' do
login_required!
query = <<~SQL
SELECT teams.id, name FROM teams
INNER JOIN users_teams AS ut ON teams.id = ut.team_id
WHERE teams.id = ? AND ut.user_id = ?
SQL
@team = db.xquery(query, params['team_id'], current_user['id']).first
unless @team
redirect '/', 403
return
end
query = <<~SQL
INSERT INTO entries
(author_id, team_id, title, summary, note, slide_available, demo_available, time_request, order_request, created_at, updated_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, NOW(), NOW())
SQL
db.xquery(
query,
current_user['id'], params['team_id'], params['title'], params['summary'], params['note'],
params['slide_available'].to_i, params['demo_available'],
params['time_request'], params['order_request']
)
entry_id = db.last_id
redirect("/entries/#{entry_id}", 303)
end
get '/entries/:entry_id' do
query = <<~SQL
SELECT entries.id AS entry_id, entries.title, entries.summary,
users.id AS author_id, users.nickname AS author_name, users.avatar_url,
teams.id AS team_id, teams.name AS team_name FROM entries
INNER JOIN users ON entries.author_id = users.id
INNER JOIN teams ON entries.team_id = teams.id
WHERE entries.id = ?
SQL
@entry = db.xquery(query, params['entry_id']).first
erb 'entries/show'.to_sym
end
get '/entries/:entry_id/edit' do
login_required!
query = <<~SQL
SELECT
entries.*,
users.id AS author_id, teams.id AS team_id, teams.name AS team_name
FROM entries
INNER JOIN users ON entries.author_id = users.id
INNER JOIN teams ON entries.team_id = teams.id
WHERE entries.id = ?
SQL
@entry = db.xquery(query, params['entry_id']).first
return redirect("/entries/#{@entry['id']}", 403) unless current_user['id'] == @entry['author_id']
erb 'entries/edit'.to_sym
end
post '/entries/:entry_id' do
login_required!
query = <<~SQL
SELECT entries.id, entries.title, entries.summary, users.id AS author_id
FROM entries INNER JOIN users ON entries.author_id = users.id WHERE entries.id = ?
SQL
entry = db.xquery(query, params['entry_id']).first
return redirect("/entries/#{entry['id']}", 403) unless current_user['id'] == entry['author_id']
query = <<~SQL
UPDATE entries
SET title = ?, summary = ?, note = ?,
slide_available = ?, demo_available = ?,
time_request = ?, order_request = ?,
updated_at = NOW() WHERE id = ?
SQL
db.xquery(
query,
params['title'], params['summary'], params['note'],
params['slide_available'].to_i, params['demo_available'].to_i,
params['time_request'], params['order_request'], params['entry_id']
)
redirect("/entries/#{entry['id']}", 303)
end
get '/admin' do
admin_required!
@users = db.xquery('SELECT * FROM users')
query = <<~SQL
SELECT entries.*, users.nickname AS author_name, teams.name AS team_name FROM entries
INNER JOIN users ON entries.author_id = users.id
INNER JOIN teams ON entries.team_id = teams.id
ORDER BY entries.order
SQL
@entries = db.xquery(query)
query = <<~SQL
SELECT teams.id, teams.name, teams.is_single, users.id AS leader_id, users.nickname AS leader_name FROM teams
INNER JOIN users_teams AS ut ON teams.id = ut.team_id AND ut.role = 'leader'
INNER JOIN users ON ut.user_id = users.id
SQL
@teams = db.xquery(query)
query = <<~SQL
SELECT teams.id AS team_id, users.id, users.nickname, users.avatar_url FROM users
INNER JOIN users_teams AS ut ON users.id = ut.user_id
INNER JOIN teams ON ut.team_id = teams.id
WHERE teams.id IN (?)
SQL
team_member = db.xquery(query, @teams.map { |t| t['id'] })
@teams.each do |team|
team['members'] = team_member.select { |tm| tm['team_id'] == team['id'] }
end
erb 'admin/index'.to_sym
end
post '/admin/entries/:entry_id' do
admin_required!
query = <<~SQL
UPDATE entries
SET author_id = ?, time = ?, entries.order = ?, accepted = ? WHERE id = ?
SQL
db.xquery(query, params[:author_id], params[:time], params[:order], params[:accepted], params[:entry_id])
redirect('/admin', 303)
end
post '/admin/teams/:team_id/destroy' do
admin_required!
db.xquery('DELETE FROM teams WHERE id = ? LIMIT 1', params[:team_id])
redirect('/admin', 303)
end
get '/admin/entries/:entry_id/edit' do
admin_required!
@users = db.xquery('SELECT * FROM users')
query = <<~SQL
SELECT entries.*, users.nickname AS author_name, teams.name AS team_name FROM entries
INNER JOIN users ON entries.author_id = users.id
INNER JOIN teams ON entries.team_id = teams.id
WHERE entries.id = ?
SQL
@entry = db.xquery(query, params[:entry_id]).first
erb 'admin/entries/edit'.to_sym
end
post '/admin/entries/:entry_id/done' do
admin_required!
entry = db.xquery('SELECT entries.id, entries.done_at FROM entries WHERE id = ? LIMIT 1', params[:entry_id]).first
if entry['done_at']
db.xquery('UPDATE entries SET done_at = NULL WHERE id = ? LIMIT 1', entry['id'])
else
db.xquery('UPDATE entries SET done_at = NOW() WHERE id = ? LIMIT 1', entry['id'])
end
redirect('/admin', 303)
end
post '/admin/entries/:entry_id/destroy' do
admin_required!
db.xquery('DELETE FROM entries WHERE id = ? LIMIT 1', params[:entry_id])
redirect('/admin', 303)
end
post '/admin/users' do
admin_required!
query = <<~SQL
INSERT INTO users (id, nickname, avatar_url, created_at, updated_at)
VALUES (?, ?, ?, NOW(), NOW())
ON DUPLICATE KEY UPDATE nickname = ?, avatar_url = ?, updated_at = NOW()
SQL
db.xquery(query, params[:id], params[:nickname], params[:avatar_url], params[:nickname], params[:avatar_url])
redirect('/admin', 303)
end
get '/auth/slack/callback' do
auth_info = request.env['omniauth.auth']['info']
query = <<~SQL
INSERT INTO users (id, nickname, avatar_url, created_at, updated_at)
VALUES (?, ?, ?, NOW(), NOW())
ON DUPLICATE KEY UPDATE nickname = ?, avatar_url = ?, updated_at = NOW()
SQL
db.xquery(query, auth_info['user_id'], auth_info['nickname'], auth_info['image_48'], auth_info['nickname'], auth_info['image_48'])
session['user_id'] = auth_info['user_id']
redirect '/', 303
end
get '/auth/slack/destroy' do
session.clear
redirect '/', 303
end
end