-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb.rb
502 lines (397 loc) · 11.2 KB
/
web.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
# coding: utf-8
require 'sinatra/base'
require 'sinatra/url_for'
require 'sinatra/reloader' if development?
require 'sinatra/content_for'
require 'haml'
require 'coffee-script'
require './db'
require './constant'
Tilt::CoffeeScriptTemplate.default_bare = true
class App < Sinatra::Base
helpers Sinatra::UrlForHelper
register Sinatra::Contrib
configure do
mime_type :js, 'application/javascript'
mime_type :json, 'application/json'
end
configure :development do
register Sinatra::Reloader
end
set :root, File.dirname(__FILE__)
set :views, root + '/views'
set :public_folder, root + '/public'
set :run, false # this line tells mongrel not to run and to let passenger handle the application
set :haml, :format => :html5
set :sprockets, Sprockets::Environment.new(root){ |environment|
environment.append_path 'app/js'
environment.append_path 'app/css'
if ENV['RACK_ENV'] == 'production'
environment.js_compressor = YUI::JavaScriptCompressor.new(munge: true)
environment.css_compressor = YUI::CssCompressor.new
end
}
def next_color(staffs)
# 次のスタッフの色を決定する
# 最も使用回数が少ない色を選択。同数ならCOLORSの順で選ぶ
color_count = {}
COLORS.each do |e|
color_count[e] = 0
end
staffs.each do |e|
if e.color # @todo for compatibility. remove in the future
color_count[e.color] += 1
end
end
colors = []
color_count.each do |k, v|
colors << [k, v]
end
colors.sort! do |a, b|
if a[1] == b[1]
COLORS.index(a[0]) <=> COLORS.index(b[0])
else
a[1] <=> b[1]
end
end
colors[0][0]
end
get '/js/:filename' do
content_type :js
CoffeeScript.compile erb(:"#{params[:filename]}.coffee"), { no_wrap: true }
end
get '/' do
haml :index
end
post '/new_project' do
project = Project.new_project()
redirect url_for("/projects/#{project.key}")
end
post '/copy_project' do
from_key = params[:project]
new_project = Project.copy_project(from_key)
redirect url_for("/projects/#{new_project.key}")
end
get '/project_name' do
key = params[:project]
project = Project.where(key: key).first
project.name
end
post '/edit_project' do
key = params[:project]
name = params[:value]
project = Project.where(key: key).first
project.name = name
project.save!
name
end
get '/projects/:key' do
@project_key = params[:key]
@colors = COLORS
haml :project
end
get '/tasks' do
key = params[:project]
project = Project.where(key: key).first
# order_by([[:position, :asc], [:created_at, :desc]])を意図
# なぜか複数のキーでorder_byできないので手動で
tasks = project.tasks.where(complete: false).to_a.sort{|a, b|
if a.position && b.position
a.position <=> b.position
elsif a.position
1
elsif b.position
-1
elsif a.created_at && b.created_at
b.created_at <=> a.created_at
else
0
end
}
completes = project.tasks.where(complete: true).order_by(completed_at: :desc)
(tasks + completes).to_json
end
get '/incoming_tasks' do
key = params[:project]
project = Project.where(key: key).first
project.tasks.where(complete: false).to_json
end
post '/new_task' do
key = params[:project]
name = params[:name]
project = Project.where(key: key).first
task = project.new_task(name)
"#{task._id}"
end
post '/edit_task' do
key = params[:project]
id = params[:id]
name = params[:value]
project = Project.where(key: key).first
task = project.tasks.find(id)
task.name = name
task.save!
name
end
post '/color_task' do
key = params[:project]
id = params[:id]
color = params[:color]
project = Project.where(key: key).first
task = project.tasks.find(id)
task.color = color
task.save!
'OK'.to_json
end
post '/delete_task' do
key = params[:project]
id = params[:id]
project = Project.where(key: key).first
staff = project.staffs.where({ :task_ids.in => [ id ] }).first
if staff
# 割り当て済みなら割り当て解除
Project.collection.find({ '_id' => project._id, 'staffs._id' => staff._id }).update({
"$pull" => {
"tasks" => { '_id' => Moped::BSON::ObjectId(id) },
"staffs.$.task_ids" => id,
}
})
else
project.tasks.find(id).destroy
end
'OK'.to_json
end
post '/complete_task' do
key = params[:project]
id = params[:task_id]
project = Project.where(key: key).first
task = project.tasks.find(id)
task.complete = true
task.completed_at = Time.now
assigned_staff = project.staffs.where({ :task_ids.in => [ id ] }).first
if assigned_staff
assigned_staff.pull(:task_ids, id)
end
project.save!
'OK'.to_json
end
post '/recycle_task' do
key = params[:project]
id = params[:task_id]
project = Project.where(key: key).first
task = project.tasks.find(id)
task.complete = false
task.remove_attribute(:completed_at)
project.save!
'OK'.to_json
end
post '/task_sorted' do
key = params[:project]
order = params['task']
project = Project.where(key: key).first
order.each_with_index do |task_id, i|
next if task_id == 'template'
project.tasks.find(task_id).position = i
end
project.save!
'OK'.to_json
end
get '/staffs' do
key = params[:project]
project = Project.where(key: key).first
staffs = project.staffs
staffs.each do |e|
next unless e.task_ids
e.task_ids.sort_by! do |id|
project.tasks.find(id).position
end
end
staffs.to_json
end
post '/new_staff' do
key = params[:project]
name = params[:name]
project = Project.where(key: key).first
staff = project.staffs.create(name: name, color: next_color(project.staffs))
{ id: staff._id, color: staff.color }.to_json
end
post '/edit_staff' do
key = params[:project]
id = params[:id]
name = params[:value]
project = Project.where(key: key).first
staff = project.staffs.find(id)
staff.name = name
staff.save!
name
end
post '/color_staff' do
key = params[:project]
id = params[:id]
color = params[:color]
project = Project.where(key: key).first
staff = project.staffs.find(id)
staff.color = color
staff.save!
'OK'.to_json
end
post '/delete_staff' do
key = params[:project]
id = params[:id]
project = Project.where(key: key).first
project.staffs.find(id).destroy
end
post '/assign_task' do
key = params[:project]
task_id = params[:task_id]
staff_id = params[:staff_id]
project = Project.where(key: key).first
# 複数人で使っている場合に、他の人が完了したタスクは割り当てられないようにする
task = project.tasks.where(_id: task_id, complete: false).first
if task
# 誰かに割り当て済みならそちらを解除する
old_staff = project.staffs.where({ :task_ids.in => [ task_id ] }).first
if old_staff
old_staff.pull(:task_ids, task_id)
end
# 割り当て
staff = project.staffs.find(staff_id)
staff.push(:task_ids, task_id)
task.assigned_at = Time.now
# 以上をアトミックに保存する
project.save!
end
'OK'.to_json
end
post '/deassign_task' do
key = params[:project]
task_id = params[:task_id]
project = Project.where(key: key).first
staff = project.staffs.where({ :task_ids.in => [ task_id ] }).first
staff.pull(:task_ids, task_id)
project.tasks.find(task_id).remove_attribute(:assigned_at)
project.save!
'OK'.to_json
end
get '/lookup_followees' do
followees = params[:followees]
result = []
followees.each do |dummy, e|
project = Project.where(key: e['project']).first
staff_name = nil
task_name = nil
assigned_at = nil
# staffは削除されてるかもしれない
staff = project.staffs.where(id: e['staff']).first
if staff
staff_name = staff.name
if staff.task_ids && staff.task_ids.size >= 1
# taskは確実にあるはず
task = project.tasks.find(staff.task_ids.first)
task_name = task.name
assigned_at = task.assigned_at
end
end
result << e.update({
project_name: project.name,
staff_name: staff_name,
task_name: task_name,
assigned_at: assigned_at
})
end
result.to_json
end
get '/recent_projects' do
cookie = request.cookies['entakun-recent-project'] || '[]'
keys = JSON.parse(cookie)
@projects = keys.map do |e|
project = Project.where(key: e).first
if project
{ key: e, name: project.name }
else
nil
end
end
@projects.compact!
haml :recent_projects
end
get '/export_wekan' do
# @todo 複数のプロジェクトのマージ
key = params[:projects]
project = Project.where(key: key).first
to_label_id = {
"orange" => "color_orange",
"yellow" => "color_yellow",
"green" => "color_lime",
"cyan" => "color_sky",
"blue" => "color_purple",
"pink" => "color_pink",
"gray" => nil,
}
@board_title = project.name
@cards = project.tasks.map do |task|
title = ""
description = ""
if task.name.match(/(.+?)\n(.+)/m)
title = $1
description = $2
else
title = task.name
description = ""
end
{
"_id" => task.id,
"title" => title,
"description" => description,
"members" => [],
"labelIds" => to_label_id[task.color] ? [ to_label_id[task.color] ] : [],
"listId" => "Inbox", # staffに割り当てられてないカードはInboxへ
"sort" => task.position || 0,
"swimlaneId" => "gRArzLSPRcgag8HrC",
"archived" => false,
"createdAt" => "2018-02-04T13:22:59.022Z",
"dateLastActivity" => "2018-02-04T13:22:59.022Z",
"isOvertime" => false,
"userId" => "WbgvtzpbZgvaQKTfo"
}
end
@lists = [
{
"_id" => "Inbox",
"archived" => false,
"createdAt" => "2018-02-04T12:56:23.065Z",
"title" => "Inbox",
"wipLimit" => {
"value" => 1,
"enabled" => false,
"soft" => false
},
"updatedAt" => "2018-02-04T12:56:23.068Z"
},
]
@lists += project.staffs.map do |staff|
{
"_id" => "staff_#{staff.id}",
"archived" => false,
"createdAt" => "2018-02-04T12:56:23.065Z",
"title" => staff.name,
"wipLimit" => {
"value" => 1,
"enabled" => false,
"soft" => false
},
"updatedAt" => "2018-02-04T12:56:23.068Z"
}
end
project.staffs.each do |staff|
staff.task_ids.each do |task_id|
assigned_card = @cards.find{|e| e["_id"].to_s == task_id.to_s}
assigned_card["listId"] = "staff_#{staff.id}"
end
end
@cards = @cards.to_json
@lists = @lists.to_json
content_type :json
erb :"export_wekan.json", layout => false
end
end