feat: add admin routes,
fixed: top updates
This commit is contained in:
1 parent
2caf7d2e9b
commit
21ed1e3c20
7 files changed
+127
-50
No files matched your search
@@ -3,6 +3,7 @@ import Jwt from 'jsonwebtoken'
|
||||
import {
|
||||
getTrack,
|
||||
putTrack,
|
||||
updateTrack,
|
||||
countTrackVotes,
|
||||
getUserVote,
|
||||
updateUserVote,
|
||||
@@ -52,7 +53,8 @@ const current = {
|
||||
const maxPrevious = 20
|
||||
|
||||
var topList = [],
|
||||
lastTop = -1
|
||||
lastTop = -1,
|
||||
votesSince = 0
|
||||
|
||||
const db = {
|
||||
/**
|
||||
@@ -82,8 +84,7 @@ const db = {
|
||||
// track not found in DB, fetch online
|
||||
byId(id)
|
||||
.then((track) => {
|
||||
redis.set(id, JSON.stringify(track))
|
||||
putTrack(track)
|
||||
putTrack(track).then((tdata) => redis.set(id, tdata))
|
||||
resolve(track)
|
||||
})
|
||||
.catch((spotifyErr) => {
|
||||
@@ -113,7 +114,12 @@ const db = {
|
||||
})
|
||||
},
|
||||
getUserTrack: getUserVote,
|
||||
putUserTrack: updateUserVote,
|
||||
putUserTrack: (...params) => {
|
||||
return updateUserVote(...params).then((ok) => {
|
||||
if (ok) votesSince++
|
||||
return ok
|
||||
})
|
||||
},
|
||||
},
|
||||
users: {
|
||||
getUser: getUser,
|
||||
@@ -147,23 +153,35 @@ const db = {
|
||||
get lastCount() {
|
||||
return lastTop
|
||||
},
|
||||
|
||||
get changed() {
|
||||
return
|
||||
},
|
||||
/**
|
||||
* Get current Top tracks
|
||||
* @returns {Promise<[]>} Array of Track ids
|
||||
* @returns {Promise<{}>} Array of Track ids and Fresh flag
|
||||
*/
|
||||
get: () => {
|
||||
return new Promise((resolve, reject) => {
|
||||
let now = new Date().getTime()
|
||||
|
||||
if (now - lastTop > 1e3) {
|
||||
if (now - lastTop > 60e3 || votesSince >= 2) {
|
||||
countAllVotes()
|
||||
.then((top) => {
|
||||
topList = top
|
||||
lastTop = now
|
||||
resolve(top)
|
||||
return Promise.all(top.map((tid) => getTrack(tid)))
|
||||
})
|
||||
.then((top) => {
|
||||
return top.filter(({ banned }) => !banned).map(({ id }) => id)
|
||||
})
|
||||
.then((top) => {
|
||||
topList = top // update the list
|
||||
lastTop = now // update list timestamp
|
||||
votesSince = 0 // reset number of votes since recount
|
||||
|
||||
resolve({ top, fresh: true })
|
||||
})
|
||||
.catch((err) => reject(err))
|
||||
} else resolve(topList)
|
||||
} else resolve({ top: topList, fresh: false })
|
||||
})
|
||||
},
|
||||
getTrackRank: getTrackRank,
|
||||
@@ -209,20 +227,25 @@ const db = {
|
||||
},
|
||||
|
||||
set status(chunk) {
|
||||
console.log(
|
||||
current.status.tid,
|
||||
chunk?.tid,
|
||||
current.status.tid != chunk?.tid && current.status.tid !== null
|
||||
)
|
||||
if (current.status.tid != chunk?.tid && current.status.tid !== null) {
|
||||
// add new [tid, date]
|
||||
current.previous.combo.unshift([
|
||||
current.status.tid,
|
||||
current.status.timestamp,
|
||||
])
|
||||
// pop on overfill
|
||||
if (current.previous.combo.length > maxPrevious)
|
||||
current.previous.combo.pop()
|
||||
// console.log(current.status.tid, chunk?.tid)
|
||||
if (current.status.tid != chunk?.tid && current.status.tid) {
|
||||
// skip duplicates
|
||||
let last = current.previous.combo[0] ?? [null]
|
||||
|
||||
if (last[0] !== current.status.tid) {
|
||||
// add new [tid, date]
|
||||
current.previous.combo.unshift([
|
||||
current.status.tid,
|
||||
current.status.timestamp,
|
||||
])
|
||||
|
||||
// pop on overfill
|
||||
if (current.previous.combo.length > maxPrevious)
|
||||
current.previous.combo.pop()
|
||||
|
||||
// broadcast change
|
||||
broadcast(['previous'])
|
||||
}
|
||||
}
|
||||
|
||||
current.status = { ...current.status, ...chunk, timestamp: new Date() }
|
||||
@@ -250,6 +273,10 @@ const db = {
|
||||
return current.previous
|
||||
},
|
||||
|
||||
set top(chunk) {
|
||||
current.top = chunk
|
||||
},
|
||||
|
||||
update(data) {
|
||||
if (!Array.isArray(data)) data = [data]
|
||||
|
||||
@@ -260,6 +287,19 @@ const db = {
|
||||
broadcast(data.map((chunk) => chunk.cat))
|
||||
},
|
||||
},
|
||||
admin: {
|
||||
ban: (tid) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
updateTrack(tid, { banned: true })
|
||||
.then((status) => {
|
||||
getTrack(tid).then((tdata) => redis.set(tid, tdata))
|
||||
|
||||
resolve(status.nModified === 1)
|
||||
})
|
||||
.catch((err) => resolve(false))
|
||||
})
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
export default db
|
||||
@@ -11,6 +11,7 @@ import search from './routes/search.js'
|
||||
import vote from './routes/vote.js'
|
||||
import login from './routes/login.js'
|
||||
import player from './routes/player.js'
|
||||
import admin from './routes/admin.js'
|
||||
|
||||
dotenv.config()
|
||||
|
||||
@@ -27,6 +28,7 @@ app.use('/search', search)
|
||||
app.use('/vote', vote)
|
||||
app.use('/login', login)
|
||||
app.use('/player', player)
|
||||
app.use('/admin', admin)
|
||||
|
||||
http.listen(process.env.HTTP_PORT)
|
||||
|
||||
@@ -42,24 +44,16 @@ http.on('listening', function () {
|
||||
console.info(`http: listening on ${process.env.HTTP_PORT}`)
|
||||
})
|
||||
|
||||
var i = 0
|
||||
setInterval(async () => {
|
||||
let { top, fresh } = await db.top.get()
|
||||
|
||||
// setInterval(async () => {
|
||||
// db.current.update([
|
||||
// {
|
||||
// cat: 'status',
|
||||
// content: {
|
||||
// tid: db.top.list[i],
|
||||
// },
|
||||
// },
|
||||
// {
|
||||
// cat: 'top',
|
||||
// content: {
|
||||
// tids: await db.top.get(),
|
||||
// timestamp: db.top.lastCount,
|
||||
// },
|
||||
// },
|
||||
// ])
|
||||
// i++
|
||||
// if (i >= db.top.list.length) i = 0
|
||||
// }, 10e3)
|
||||
if (fresh) {
|
||||
db.current.update({
|
||||
cat: 'top',
|
||||
content: {
|
||||
tids: top,
|
||||
timestamp: db.top.lastCount,
|
||||
},
|
||||
})
|
||||
}
|
||||
}, 2e3)
|
||||
@@ -57,7 +57,7 @@ function getTrack(id) {
|
||||
/**
|
||||
* Add track to database
|
||||
* @param {Track} track new track
|
||||
* @returns {Object} tdata
|
||||
* @returns {Promise<{}>} track data
|
||||
*/
|
||||
function putTrack(track) {
|
||||
return new Promise((resolve, reject) => {
|
||||
@@ -69,7 +69,17 @@ function putTrack(track) {
|
||||
})
|
||||
}
|
||||
|
||||
export { getTrack, putTrack }
|
||||
/**
|
||||
* Update track metadata
|
||||
* @param {String} tid track id
|
||||
* @param {*} newTdata values to update
|
||||
* @returns {Promise<{}>} updated track
|
||||
*/
|
||||
function updateTrack(id, newTdata) {
|
||||
return TrackModel.updateOne({ id }, newTdata)
|
||||
}
|
||||
|
||||
export { getTrack, putTrack, updateTrack }
|
||||
|
||||
// #### Votes #### #### #### ######## #### #### ####
|
||||
|
||||
@@ -81,6 +91,9 @@ export { getTrack, putTrack }
|
||||
const timeValid = (() => {
|
||||
switch (process.env.TOP_TIME_VALID) {
|
||||
default:
|
||||
case 'period':
|
||||
let now = new Date()
|
||||
return new Date(now.getTime() - process.env.TOP_TIME_VALUE * 1e3)
|
||||
case 'monday':
|
||||
var monday = new Date()
|
||||
monday.setHours(0, 0, 0)
|
||||
@@ -88,7 +101,7 @@ const timeValid = (() => {
|
||||
|
||||
return monday
|
||||
case 'date':
|
||||
let date = new Date(...JSON.parse(process.env.valid_date))
|
||||
let date = new Date(...JSON.parse(process.env.TOP_TIME_VALUE))
|
||||
return date
|
||||
}
|
||||
})()
|
||||
|
||||
@@ -24,6 +24,9 @@ const cache = {
|
||||
})
|
||||
},
|
||||
set: (key, value) => {
|
||||
if (typeof value !== 'string') {
|
||||
value = JSON.stringify(value)
|
||||
}
|
||||
client.set(key, value)
|
||||
},
|
||||
}
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
import express from 'express'
|
||||
import db from '../db.js'
|
||||
|
||||
const router = express.Router()
|
||||
|
||||
router.get('/ban', ({ query: { tid }, headers: { authorization } }, res) => {
|
||||
if (authorization === undefined)
|
||||
return res.status(401).end('Access token required')
|
||||
|
||||
if (authorization.length < 1)
|
||||
return res.status(400).end('Access token invalid')
|
||||
|
||||
db.users
|
||||
.verifyJwt(authorization)
|
||||
.then(({ perms }) => {
|
||||
if (perms > 0) {
|
||||
db.admin.ban(tid).then((ok) => {
|
||||
res.json(ok.toString())
|
||||
})
|
||||
}
|
||||
})
|
||||
.catch((err) => {
|
||||
res.json('false')
|
||||
console.error(err)
|
||||
})
|
||||
})
|
||||
|
||||
export default router
|
||||
+1
-2
@@ -90,13 +90,12 @@ router.get('/get/top/', ({ headers: { authorization } }, res) => {
|
||||
|
||||
if (authorization != playerSecret) return res.status(403).end('Token invalid')
|
||||
|
||||
db.top.get().then((top) => {
|
||||
db.top.get().then(({ top }) => {
|
||||
Promise.all(
|
||||
top.map((tid) => spotifyToYt.trackGet(`spotify:track:${tid}`))
|
||||
).then((yts) => {
|
||||
top.map((tid, i) => {
|
||||
yts[i].tid = tid
|
||||
yts[i].info[0].duration = 10e3
|
||||
})
|
||||
res.json(yts)
|
||||
})
|
||||
|
||||
Reference in new issue
Block a user