-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
301 lines (279 loc) · 9.67 KB
/
app.js
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
const express = require('express')
const app = express()
const version = require('project-version')
const bodyParser = require('body-parser')
app.use(bodyParser.urlencoded({limit:'50mb', extended: true}))
// ライブラリの読み込み
const lib = require('./server/lib')
// HTTPを使用する(公開用)
const http = require('http')
app.listen(3002)
const path = require('path')
const NeDB = require('nedb')
// CORSを許可する
app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', '*')
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept')
next()
})
const surveyDB = new NeDB({
filename: path.join(__dirname, 'data/survey.db'),
autoload: true,
timestampData: true
})
const guestDB = new NeDB({
filename: path.join(__dirname, 'data/guest.db'),
autoload: true,
timestampData: true
})
const selectionDB = new NeDB({
filename: path.join(__dirname, 'selection/selection.db'),
autoload: true,
timestampData: true
})
const likeDB = new NeDB({
filename: path.join(__dirname, 'selection/like.db'),
autoload: true,
timestampData: true
})
// クライアントアプリを返す
const client = './client/build'
app.use('/', express.static(client))
app.use('/home', express.static(client))
app.use('/guest', express.static(client))
app.use('/check', express.static(client))
app.use('/close', express.static(client))
app.use('/34thconcert', express.static(client))
app.use('/34thconcert/survey', express.static(client))
// api設定
app.post('/api/login', (req, res) => {
const passwd = req.body.passwd
console.log('[' + lib.showTime() + '] api/login: (passwd)')
if (lib.getHash(passwd) === '0002a3739bc2f722677cb2d9c00450c5b3da7b5972846fef1db51963ba84229eef66baca5251931ce876cc92bda7eb7628a7eed7277d3208d06d13f5ed2acaeb') {
return res.json({status: true})
} else {
return res.json({status: false})
}
})
app.post('/api/post', (req, res) => {
const message = req.body.message
const ip = req.body.ip
const userAgent = req.body.userAgent
console.log('[' + lib.showTime() + '] api/post', message, ip, userAgent)
const reg = {data: message, ip, userAgent}
surveyDB.insert(reg, (error, newdoc) => {
if (error) {
console.log('Database Error')
} else {
console.log('Database Insert')
}
return res.json({status: true})
})
})
app.post('/api/post/guest', (req, res) => {
const answer = req.body.answer
const time = req.body.time
const ip = req.body.ip
const userAgent = req.body.userAgent
console.log('[' + lib.showTime() + '] api/post/guest', '[' + time + '] ' + ip)
guestDB.insert({data: answer, ip, userAgent, time}, (err, newdoc) => {
err ? console.log('Database Error') : console.log('Database Insert')
return res.json({status: true})
})
})
// member app
const request = require('superagent')
function auth (session, callback) {
request.post('https://auth.winds-n.com/auth').type('form').send({session})
.end((error, response) => {
if (error) return callback(false)
if (response.body.status) {
return callback(true)
}
})
}
// prepare, first, second, third, end
app.post('/api/selection/phase', (req, res) => {
const session = req.body.session
console.log('[' + lib.showTime() + '] api/selection/phase: ' + session.userid)
auth(session, (authentication) => {
if (!authentication) return res.status(403)
// return res.json({status: true, phase: 'onlyadmin'})
// return res.json({status: true, phase: 'getmusic'})
// return res.json({status: true, phase: 'showlist'})
return res.json({status: true, phase: 'hide'})
})
})
app.post('/api/selection/detail', (req, res) => {
const session = req.body.session
const id = req.body.id
console.log('[' + lib.showTime() + '] api/selection/detail: ' + session.userid)
auth(session, (authentication) => {
if (!authentication) return res.status(403)
selectionDB.findOne({_id: id}, (err, selection) => {
if (err || !selection) return res.json({status: false})
return res.json({status: true, selection})
})
})
})
app.post('/api/selection/list', (req, res) => {
const session = req.body.session
const query = req.body.query
const sort = req.body.sort
const order = req.body.order
console.log('[' + lib.showTime() + '] api/selection/list: ' + session.userid + ', ' + query, sort, order)
const sortRequest = {[sort]: parseInt(order)}
auth(session, (authentication) => {
if (!authentication) return res.status(403)
if (query === '') {
selectionDB.find({}).sort(sortRequest).exec((err, list) => {
if (err) return res.json({status: false})
return res.json({status: true, list})
})
} else {
const s = new RegExp(lib.escapeReg(query), 'i')
const searchQuery = [
{title: s},
{titleJa: s},
{titleEn: s},
{composer: s},
{arranger: s}
]
selectionDB.find({$or: searchQuery}).sort(sortRequest).exec((err, list) => {
if (err) return res.json({status: false})
return res.json({status: true, list})
})
}
})
})
app.post('/api/selection/post', (req, res) => {
const session = req.body.session
const postUserid = req.body.postUserid
const id = req.body.id
const remove = req.body.remove
const selection = fixComposerArranger(req.body.selection)
console.log('[' + lib.showTime() + '] api/selection/post: ' + session.userid)
auth(session, (authentication) => {
if (!authentication) return res.status(403)
if (id !== 'false' && remove === 'true') {
console.log('[' + lib.showTime() + '] api/selection/post: remove')
selectionDB.remove({_id: id}, {}, (err, num) => {
if (err) return res.json({status: false})
return res.json({status: true})
})
} else if (id === 'false') {
console.log('[' + lib.showTime() + '] api/selection/post: add')
console.log({...selection, postUserid, user: session.userid})
selectionDB.insert({...selection, postUserid, user: session.userid}, (err, newdoc) => {
if (err) return res.json({status: false})
return res.json({status: true})
})
} else {
console.log('[' + lib.showTime() + '] api/selection/post: edit')
console.log({...selection})
selectionDB.update({_id: id}, {$set: {...selection}}, {}, (err, num) => {
if (err) return res.json({status: false})
return res.json({status: true})
})
}
})
})
function fixComposerArranger (data) {
if (Array.isArray(data.composer)) {
const composerCount = data.composer.length
let blank = []
for (let i=0;i<composerCount;i++) {
if (data.composer[i].trim() === '') blank.push(i)
}
blank.reverse().map((j) => data.composer.splice(j,1))
if (data.composer.length === 0) data.composer = ['']
} else {
data.composer = [data.composer.trim()]
}
if (Array.isArray(data.arranger)) {
const arrangerCount = data.arranger.length
let blank = []
for (let i=0;i<arrangerCount;i++) {
if (data.arranger[i].trim() === '') blank.push(i)
}
blank.reverse().map((j) => data.arranger.splice(j,1))
if (data.arranger.length === 0) data.arranger = ['']
} else {
data.arranger = [data.arranger.trim()]
}
if (Array.isArray(data.url)) {
const urlCount = data.url.length
let blank = []
for (let i=0;i<urlCount;i++) {
if (data.url[i].trim() === '') blank.push(i)
}
blank.reverse().map((j) => data.url.splice(j,1))
if (data.url.length === 0) data.url = ['']
} else {
data.url = [data.url.trim()]
}
return data
}
app.post('/api/selection/like/get', (req, res) => {
const session = req.body.session
console.log('[' + lib.showTime() + '] api/selection/like/get: ' + session.userid)
auth(session, (authentication) => {
if (!authentication) return res.status(403)
likeDB.find({}, (err, like) => {
if (err) return res.json({status: false})
return res.json({status: true, like})
})
})
})
app.post('/api/selection/like/add', (req, res) => {
const session = req.body.session
const selectionid = req.body.selectionid
const likeUserid = req.body.likeUserid
console.log('[' + lib.showTime() + '] api/selection/like/add: ' + session.userid)
auth(session, (authentication) => {
if (!authentication) return res.status(403)
likeDB.findOne({likeUserid}, (err, doc) => {
if (err) return res.json({status: false})
if (!doc) {
// はじめてのいいね
console.log('[' + lib.showTime() + '] api/selection/like/add: add, ' + selectionid)
likeDB.insert({likeUserid, like: [selectionid]}, (insertErr, newdoc) => {
if (insertErr) return res.json({status: false})
return res.json({status: true})
})
} else {
let newdoc
if (doc.like.find(item => item === selectionid)) {
const likeArray = doc.like.find(item => item !== selectionid) ? doc.like.find(item => item !== selectionid) : []
// いいね取り消し
console.log('[' + lib.showTime() + '] api/selection/like/add: remove, ' + selectionid)
newdoc = Array.isArray(likeArray) ? {
...doc,
like: [
...likeArray
]
} : {
...doc,
like: [
likeArray
]
}
} else {
// いいね追加
console.log('[' + lib.showTime() + '] api/selection/like/add: [add] ' + selectionid)
newdoc = {
...doc,
like: [
...doc.like,
selectionid
]
}
}
likeDB.update({likeUserid}, {...newdoc}, {}, (updateErr, num) => {
if (updateErr) return res.json({status: false})
return res.json({status: true})
})
}
})
})
})