player api and minor changes

This commit is contained in:
Mateusz Pstrucha committed 2020-09-21 20:49:02 +02:00
1 parent a06c812c20
commit bcf4839eb4
8 files changed
+228 -22

No files matched your search

+30
View File
@@ -90,6 +90,15 @@ module.exports = {
resolve(user)
})
})
},
search(query) {
return new Promise((resolve, reject) => {
User.find({
$text: { $search: query }
}).then(users => {
resolve(users)
})
})
}
},
votes: {
@@ -134,6 +143,18 @@ module.exports = {
})
})
},
get(uid, tid){
return new Promise((resolve, reject) => {
Vote.find({
$or: [
{ uid: uid },
{ tid: tid }
]
}).then(votes => {
resolve(votes)
})
})
},
report(user, tid) {
return new Promise((resolve, reject) => {
if (typeof user == 'undefined') return reject(new Error("Not authorized"))
@@ -246,6 +267,15 @@ module.exports = {
resolve(track)
})
})
},
search(query) {
return new Promise((resolve, reject) => {
Track.find({
$text: { $search: query }
}).then(tracks => {
resolve(tracks)
})
})
}
},
history: {
+94 -9
View File
@@ -85,7 +85,7 @@ class User {
function apiSearch(id) {
return new Promise((resolve, reject) => {
if (typeof id != 'string') return reject(new Error('Incorrect id'))
if (typeof id != 'string') return reject(new Error('Incorrect id, must by type String'))
spotify.getTrack(id).then(data => {
resolve(data.body)
}).catch(err => {
@@ -129,12 +129,13 @@ function refreshToken() {
}
const credentials = {
spotify: require('./spotify-cred.json')
spotify: require('./spotify-cred.json'),
api: require('./api-cred.json')
}
const Previous = {
tracks: [],
new(tid, played) {
new(tid, played = new Date) {
pretty.log(`Previous: new track`, 2)
var previous = { tid: tid, played: played.getTime() }
@@ -162,8 +163,8 @@ const Previous = {
now: {},
status: 'paused',
set(track) {
Previous.new(this.now.id, new Date)
this.now = track
Previous.new(this.now.id)
server.emit('player', {
tid: this.now.id,
status: this.status
@@ -216,7 +217,6 @@ const Previous = {
this._changed = true
if (this._serial - this._sentAt > this.threshold) { // force update after 'thershold' of votes
this.update()
this._changed = false
}
},
_changed: true,
@@ -225,7 +225,6 @@ const Previous = {
interval() {
if (this._changed) {
this.update()
this._changed = false
}
setTimeout(function () { Chart.interval() }, this.delay)
},
@@ -345,6 +344,44 @@ www.on('listening', function () {
pretty.log(`www: listening on ${port}`)
});
app.use('/api/*', (req, res, next) => {
// parse login and password from headers
const b64auth = (req.headers.authorization || '').split(' ')[1] || ''
const [login, password] = Buffer.from(b64auth, 'base64').toString().split(':')
if (login && password && login === credentials.api.user && password === credentials.api.password) {
return next()
}
res.set('WWW-Authenticate', 'Basic realm="401"') // change this
res.status(401).send('Authentication required.') // custom message
})
app.get('/api/player/set', function(req, res) {
if(req.query.tid){
Tracks.get(req.query.tid).then(track => {
Player.set(track)
res.send('ok')
res.status(200)
}).catch(err => {
res.send(err.message)
res.status(400)
})
}
})
app.get('/api/player/status', function(req, res) {
let states = ['playing', 'paused', 'stopped']
if(states.includes(req.query.set)){
Player.update(req.query.set)
res.send('ok')
res.status(200)
} else {
res.send('Incorrect status')
res.status(400)
}
})
Chart.interval()
Previous.update()
@@ -480,7 +517,7 @@ server.on('connection', socket => {
socket.emit('meta', {
type: 'error',
action: 'flags',
message: `Flags: api error for '${data.query}'`
message: `Flags: api error '${err.message}'`
})
})
})
@@ -502,8 +539,56 @@ server.on('connection', socket => {
})
socket.on('admin', function (data) {
if (!(socket.user.admin <= 0)) return
console.log(data)
if ((socket.user.admin <= 0)) return
switch (data.action) {
case 'search':
if (data.search.tags.includes('track')) {
db.tracks.search(data.search.query).then(tracks => {
socket.emit('admin', {
search: {
tracks: tracks
}
})
})
}
if(data.search.tags.includes('user')) {
db.users.search(data.search.query).then(users => {
socket.emit('admin', {
search: {
users: users
}
})
})
}
break;
case 'track':
db.tracks.get(data.track.id).then(tdata => {
socket.emit('admin', {
track: {
tdata: tdata
}
})
})
break;
case 'user':
db.users.get(data.user.id).then(udata => {
socket.emit('admin', {
user: {
udata: udata
}
})
})
break;
case 'vote':
db.votes.get(data.vote.uid, data.vote.tid).then(votes => {
socket.emit('admin', {
vote: {
votes: votes
}
})
})
break;
}
})
View File
Whitespace-only changes.
+40
View File
@@ -0,0 +1,40 @@
<html lang="pl">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="theme-color" content="#ca3e47">
<meta name="description" content="Radio Rolnik Nysa">
<title>radio-rolnik</title>
<link rel="stylesheet" href="/admin/index.css">
<link rel="stylesheet" href="/fontello.css">
<script src="/admin/index.js"></script>
<script src="/socket.io/socket.io.js"></script>
<link rel="shortcut icon" href="/favicon.ico" type="image/x-icon">
</head>
<body>
<div id="search">
<div class="controls">
<input type="text" id="text">
<input type="date" id="datein">
<input type="date" id="dateout">
<input type="submit" value="search">
<br>
<label for="user">user</label>
<input type="checkbox" id="user">
<label for="track">track</label>
<input type="checkbox" id="track">
<label for="vote">vote</label>
<input type="checkbox" id="vote">
</div>
<div class="list">
<div class="element">
<div class="image"></div>
<div class="title"></div>
</div>
</div>
</div>
<div id="preview"></div>
</body>
</html>
+17
View File
@@ -0,0 +1,17 @@
class Element{
constructor() {
}
el(){
}
}
var Socket
window.onload = function () {
Socket = io(`/admin`, {
autoConnect: true
})
}
+24 -3
View File
@@ -60,10 +60,12 @@ body {
#player {
position: relative;
overflow: hidden;
}
#player>.current {
width: 100%;
height: 3.7rem;
background: var(--color-primary);
transition: background .3s ease-in-out;
box-sizing: border-box;
@@ -71,10 +73,13 @@ body {
white-space: nowrap;
text-overflow: ellipsis;
border-bottom: solid white 2px;
transition-duration: 1s;
transition-property: height,background;
}
#player>.current.off {
#player.off>.current {
background: var(--color-3grey);
height: 0;
}
#player>.status {
@@ -90,9 +95,14 @@ body {
#nav {
display: flex;
position: absolute;
position: fixed;
bottom: 0;
left: 0;
right: 0;
max-width: 900px;
width: 100%;
margin: auto;
border-top: 1px solid white;
}
#nav>.button {
@@ -152,6 +162,11 @@ a:hover {
position: relative;
cursor: pointer;
overflow: hidden;
transition: border .1s ease-in-out;
}
.list>.track:hover {
border-left: 4px solid white;
}
.list>.track:nth-child(odd) {
@@ -189,9 +204,12 @@ a:hover {
/* Panes */
#panes {
height: calc(100% - (3.6rem + 3.4rem));
/* height: calc(100% - (3.6rem + 3.4rem)); */
height: 100%;
overflow-x: hidden;
background: inherit;
padding-bottom: 7rem;
box-sizing: border-box;
}
.pane {
@@ -328,6 +346,9 @@ a:hover {
.pane.preview>.params>tbody>tr.highlight {
font-weight: bold;
height: 2rem;
vertical-align: bottom;
text-decoration: underline;
}
.pane.preview>.params>tbody>tr.highlight::before {
+3 -3
View File
@@ -56,8 +56,8 @@
<div id="modals">
</div>
<div id="wrapper">
<div id="player">
<div class="current off">
<div id="player" class="off">
<div class="current">
<div class="track">
<div class="albumart"></div>
<span class="title">-</span>
@@ -104,7 +104,7 @@
<div class="buttons disabled">
<i class="icon-thumbs-up"></i>
<i class="icon-thumbs-down"></i>
<i class="icon-link"></i>
<i class="icon-export"></i>
<i class="icon-music"></i>
<i class="icon-flag"></i>
</div>
+20 -7
View File
@@ -129,8 +129,8 @@ class Track {
return new Table([
['Informacje', true],
['Tytuł', this.title],
['Autorzy', this.artists.join(', ')],
['Album', this.album],
['Autorzy', this.artists.join(', ')],
['Czas', `${minutes}:${seconds < 10 ? '0' : ''}${seconds}`],
['Top', true],
['Miejsce', this.placement ? `#${this.placement}` : '~'],
@@ -382,7 +382,10 @@ Player = {
current: null,
status: 'stopped',
set(tid, status) {
if (!tid) return
if (!tid && status) {
this._status(status)
return
}
Tracks.get(tid).then(track => {
this.current = track
Elements.player.current.innerHTML = ''
@@ -403,15 +406,15 @@ Player = {
}
},
_off() {
Elements.player.current.classList.add('off')
Elements.player.container.classList.add('off')
Elements.player.volume.change('stop-circle')
},
_pause() {
Elements.player.current.classList.add('off')
Elements.player.container.classList.add('off')
Elements.player.volume.change('pause-circle')
},
_on() {
Elements.player.current.classList.remove('off')
Elements.player.container.classList.remove('off')
Elements.player.volume.change('play-circled')
}
}
@@ -432,7 +435,7 @@ Panes = {
Previous = {
_list: [],
add(previous) {
pretty.info('Previous', `new: ${tid}`)
pretty.info('Previous', `new: ${previous.tid}`)
Tracks.get(previous.tid).then(track => {
Elements.previous.prepend(track.el({ played: previous.played }))
})
@@ -486,6 +489,7 @@ var Elements;
window.onload = function () {
Elements = {
player: {
container: document.querySelector('#player'),
current: document.querySelector('#player>.current'),
volume: new Icon(document.querySelector('#player>.status>.volume'))
},
@@ -666,6 +670,7 @@ window.onload = function () {
this.successful = true
Tracks.expireFlags()
if(Preview.track.id) Tracks.getFlag(Preview.track.id)
Elements.auth.buttons.classList.remove('disabled')
Elements.auth.prof.style.backgroundImage = `url('https://graph.facebook.com/${this.info.id}/picture?type=large')`
@@ -780,13 +785,19 @@ window.onload = function () {
if (e.target.classList.value.startsWith('icon-')) {
let target = e.target.classList.value.split(' ')[0].slice(5)
switch (target) {
case 'link':
case 'export':
if (navigator.share) {
navigator.share({ // returns a promise
title: 'radio-rolnik',
text: `Sprawdź "${Preview.track.title}" na radio-rolnik`,
url: location.href,
})
} else if(navigator.clipboard) {
navigator.clipboard.writeText(location.href).then(function() {
new Modal('Skopiowano do schowka', `Link do "${Preview.track.title}" został skopiowany do schowka`)
}, function(err) {
new Modal('Nie można skopiować do schowka', `Wystąpił błąd podczas kopiowania linku do schowka`)
});
}
break;
case 'music':
@@ -940,4 +951,6 @@ window.onload = function () {
console.log('meta', data)
});
Socket.on('admin', data => console.log(data))
}