-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
394 lines (350 loc) · 10.7 KB
/
Copy pathserver.js
File metadata and controls
394 lines (350 loc) · 10.7 KB
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
// @ts-check
const express = require('express')
const path = require('path')
const app = express()
const mysql = require('mysql')
const bodyParser = require('body-parser')
const fetch = require('node-fetch')
const port = process.env.PORT || 5000
const puppeteer = require('puppeteer');
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true }))
app.use('/', express.static('./client/build'))
const con = mysql.createConnection(process.env.JAWSDB_URL)
let browser = null;
(async() => {
browser = await puppeteer.launch({
args: ["--no-sandbox", "--disable-setuid-sandbox", "--single-process"]
});
})()
function clean(result) {
return JSON.parse(JSON.stringify(result))
}
con.connect((err) => {
if (err) throw err
else {
console.log('db connected')
}
})
const querySql = (queryString, params = null) => {
return new Promise((resolve, reject) => {
con.query(queryString, params, (err, result) => {
if (err) reject(err)
if (result) {
result = clean(result)
}
resolve(result)
})
})
}
// LOGIN
app.post('/api/login', (req, res) => {
const { email, password } = req.body
con.query('SELECT * FROM User WHERE email=?', [email], (err, result) => {
if (err) throw err
if (result < 1) {
return res.json({ status: 404 })
}
if (result[0].password !== password) {
return res.json({ status: 401 })
}
let user = result[0]
return res.json({ ...user, follow: clean(result), status: 200 })
})
})
// REGISTER
app.post('/api/user', (req, res) => {
const { email, username, password, image } = req.body
con.query('SELECT * FROM User WHERE email=? OR username=?', [email, username], (err, result) => {
if (err) throw err
if (result == 1) {
res.json({ status: 404 })
} else {
con.query('INSERT INTO User (email, username, password, thumbnail_url) VALUES (?,?,?,?)', [email, username, password, image], (err, result) => {
res.json({ user_id: result.insertId, email: email, username: username, thumbnail_url: image, status: 200 })
})
}
})
})
app.put('/api/user/:id', async (req, res) => {
let id = req.params.id
const { image } = req.body
const result = await querySql("UPDATE User SET thumbnail_url=? WHERE user_id=?", [image, id])
res.json(result)
})
// Search user by get params
app.get('/api/user', async (req, res) => {
let query = req.query.q
let users = []
let result = await querySql(
'SELECT user_id, email, username, thumbnail_url FROM User WHERE username LIKE ? OR email LIKE ?',
[`%${query}%`, `%${query}%`]
)
res.json(result)
})
// Create New Playlist
app.post('/api/playlist', (req, res) => {
let user_id = req.body.user_id
con.query("INSERT INTO Playlist (name, is_public, user_id) VALUES ('New Playlist', 1, ?)", [user_id], (err, result) => {
})
con.query('SELECT LAST_INSERT_ID()', (err, result) => {
result = result.map((e) => Object.assign({}, e))
let id = result[0]['LAST_INSERT_ID()']
res.json({ playlist_id: id })
})
})
// Change playlist name
app.patch('/api/playlist', (req, res) => {
let playlist_id = req.body.playlist_id
let new_name = req.body.new_name
con.query('UPDATE Playlist SET name=? WHERE playlist_id=?', [new_name, playlist_id], (err, result) => {
if (err) res.sendStatus(404)
res.sendStatus(200)
})
})
// Get followers and following users
app.get('/api/user/:id/friend', async (req, res) => {
let following = []
let followers = []
let user_id = req.params.id
try {
let query1 = await querySql(
'SELECT user_id, username, thumbnail_url FROM User INNER JOIN Follow ON User.user_id = Follow.user_id2 WHERE Follow.user_id1 = ?',
[user_id]
)
if (query1.length === 0) {
} else {
following = query1
}
let query2 = await querySql(
'SELECT user_id, username, thumbnail_url FROM User INNER JOIN Follow ON User.user_id = Follow.user_id1 WHERE Follow.user_id2 = ?',
[user_id]
)
if (query2.length === 0) {
} else {
followers = query2
}
res.json({ following: following, followers: followers })
} catch (error) {
res.sendStatus(404)
}
})
// Follow playlist
app.post('/api/playlist/follow', async (req, res) => {
let { user_id, playlist_id } = req.body
try {
let query = await querySql("INSERT INTO User_follows_playlist VALUES(?, ?)",
[playlist_id, user_id]
)
res.sendStatus(200)
} catch (error) {
res.sendStatus(404)
}
})
// Unfollow playlist
app.delete('/api/playlist/follow', async (req, res) => {
let { user_id, playlist_id } = req.body
try {
let query = await querySql(
"DELETE FROM User_follows_playlist WHERE playlist_id=? AND user_id=?",
[playlist_id, user_id]
)
res.sendStatus(200)
} catch (error) {
res.sendStatus(404)
}
})
//Get user Playlists
app.get('/api/user/:id/playlist', async (req, res) => {
let user = req.params.id
try {
let query = await querySql(
`SELECT p.playlist_id, p.name, p.is_public, p.user_id FROM Playlist p
INNER JOIN User_follows_playlist USING(playlist_id)
WHERE User_follows_playlist.user_id=?
UNION
SELECT * FROM Playlist
WHERE user_id=?
`,
// SELECT p.playlist_id, p.name, p.is_public, p.user_id FROM Playlist p
// INNER JOIN User_follows_playlist USING(playlist_id)
// WHERE User_follows_playlist.user_id = 1
// UNION
// SELECT * FROM Playlist
// WHERE user_id=1;
[user, user]
)
let result
// let response = []
let response = query.map(async (playlist) => {
result = await querySql(
`SELECT * FROM Track t
INNER JOIN Playlist_has_track pt USING (track_id)
INNER JOIN Playlist p USING (playlist_id)
WHERE p.playlist_id = ?`,
[playlist.playlist_id]
)
return { ...playlist, songs: result }
})
let lastReponse = await Promise.all(response)
res.json(lastReponse)
} catch (error) {
res.sendStatus(404)
}
})
//Delete Playlist
app.delete('/api/playlist/:id', async (req, res) => {
let id = req.params.id
try {
let query = await querySql(
"DELETE FROM Playlist WHERE playlist_id = ?",
[id]
)
res.sendStatus(200)
} catch (error) {
res.sendStatus(404)
}
})
//Delete song from playlist
app.delete('/api/playlist/:playlist_id/:track_id', async (req, res) => {
let playlistId = req.params.playlist_id
let trackId = req.params.track_id
try {
let query = await querySql("DELETE FROM Playlist_has_track WHERE track_id = ? AND playlist_id = ?",
[trackId, playlistId]
)
res.sendStatus(200)
} catch (error) {
res.sendStatus(404)
}
})
//Insert new song to playlist
app.put('/api/playlist/:id', async (req, res) => {
let playlist_id = req.params.id
let song = req.body
let track_id
let q = con.query(`CALL InsertTrackToPlaylist(?, ?, ?, ?, ?, ?, ?)`,
[song.source, song.title, song.artist, song.thumbnail_url, song.url, song.duration_ms, playlist_id],
(err, result) => {
if (err) {
res.sendStatus(404)
}
result = result.map((e) => Object.assign({}, e))
res.json({ track_id: result[0]['0'].hash })
}
)
})
//Follow user
app.post('/api/user/friend', async (req, res) => {
let { user_id1, user_id2 } = req.body
try {
let q = await querySql('INSERT INTO Follow (user_id1, user_id2) VALUES (?, ?)',
[user_id1, user_id2]
)
res.sendStatus(200)
} catch (error) {
res.sendStatus(404)
}
})
//Unfollow user
app.delete('/api/user/friend', async (req, res) => {
let { user_id1, user_id2 } = req.body
try {
let q = await querySql('DELETE FROM Follow WHERE user_id1 = ? AND user_id2 = ?',
[user_id1, user_id2]
)
res.sendStatus(200)
} catch (error) {
res.sendStatus(404)
}
})
//Send Inbox
app.post('/api/user/inbox', async (req, res) => {
let { origin_user_id, destination_user_id, song } = req.body
let q = con.query("CALL checkTrackInbox (?, ?, ?, ?, ?, ?)",
[song.source, song.title, song.artist, song.thumbnail_url, song.url, song.duration_ms],
(err, result) => {
if (err) {
res.sendStatus(404)
}
let track_id = result[0]['0'].hash
con.query("INSERT INTO Inbox (origin_user_id, destination_user_id, playlist_id, track_id) VALUES (?, ?, ?, ?)", [origin_user_id, destination_user_id, null, track_id],
(err, result) => {
if (err) {
res.sendStatus(404)
} else {
res.sendStatus(200)
}
})
})
})
//Get user's inbox
app.get('/api/:id/inbox', async (req, res) => {
let user_id = req.params.id
try {
let q = await querySql(
`SELECT i.origin_user_id, i.destination_user_id, i.date_sent, i.playlist_id, i.track_id, t.source, t.title, t.artist, t.thumbnail_url, t.url, t.duration_ms, u.user_id, u.email, u.username
FROM Inbox i
INNER JOIN Track t using(track_id)
INNER JOIN User u ON(i.origin_user_id = u.user_id)
WHERE destination_user_id = ?`,
[user_id]
)
res.json(q)
} catch (error) {
res.sendStatus(404)
}
})
//Clear user's inbox
app.delete('/api/user/inbox', async (req, res) => {
let user_id = req.body.user_id
try {
let q = await querySql("DELETE FROM Inbox WHERE destination_user_id = ?",
[user_id]
)
res.sendStatus(200)
} catch (error) {
res.sendStatus(404)
}
})
app.get('/api/youtube', async (req, res) => {
res.set('Cache-control', 'public, max-age=86400')
try {
// @ts-ignore
let params = new URLSearchParams(Object.entries({
part: 'snippet',
q: req.query.q,
maxResults: 20,
type: 'video',
videoDuration: 'medium',
key: process.env.YOUTUBE_API_KEY
}))
// @ts-ignore
const yt = await fetch('https://www.googleapis.com/youtube/v3/search?' + params, { method: "GET", headers: {
referer: `https://streamaster.herokuapp.com`
} })
return res.json(await yt.json())
} catch (err) {
return res.status(400).json(JSON.stringify(err))
}
})
app.get('/api/soundcloud', async (req, res) => {
res.set('Cache-control', 'public, max-age=86400')
const page = await browser.newPage();
const dataPromise = page.waitForResponse((resp) => {
return resp.url().startsWith('https://api-v2.soundcloud.com/search')
})
await page.goto(`https://soundcloud.com/search?q=${req.query.q}`);
const data = (await (await dataPromise).json()).collection.filter(e => e.kind !== "user") || []
await page.close()
return res.json(data)
})
if (process.env.NODE_ENV === 'production') {
// Serve any static files
app.use(express.static(path.join(__dirname, 'client/build')))
// Handle React routing, return all requests to React app
app.get('*', function (req, res) {
res.sendFile(path.join(__dirname, 'client/build', 'index.html'))
})
}
app.listen(port, () => console.log(`Listening on port ${port}`))