Change: audio player shows time remaining and percent #122, Add: experimental bookmarks start #115

This commit is contained in:
advplyr 2021-10-24 18:25:44 -05:00
parent f9bf846b30
commit c5eafdfa8a
9 changed files with 284 additions and 21 deletions

View file

@ -259,6 +259,9 @@ class Server {
socket.on('create_backup', () => this.backupManager.requestCreateBackup(socket))
socket.on('apply_backup', (id) => this.backupManager.requestApplyBackup(socket, id))
// Bookmarks
socket.on('create_bookmark', (payload) => this.createBookmark(socket, payload))
socket.on('test', () => {
socket.emit('test_received', socket.id)
})
@ -466,6 +469,23 @@ class Server {
}
}
async createBookmark(socket, payload) {
var client = socket.sheepClient
if (!client || !client.user) {
Logger.error('[Server] createBookmark invalid socket client')
return
}
var userAudiobook = client.user.createBookmark(payload)
if (userAudiobook) {
await this.db.updateEntity('user', client.user)
socket.emit('bookmark_created', payload.time)
socket.emit('current_user_audiobook_update', {
id: userAudiobook.audiobookId,
data: userAudiobook || null
})
}
}
async authenticateSocket(socket, token) {
var user = await this.auth.verifyToken(token)
if (!user) {

View file

@ -0,0 +1,32 @@
class AudioBookmark {
constructor(bookmark) {
this.title = null
this.time = null
this.createdAt = null
if (bookmark) {
this.construct(bookmark)
}
}
toJSON() {
return {
title: this.title || '',
time: this.time,
createdAt: this.createdAt
}
}
construct(bookmark) {
this.title = bookmark.title || ''
this.time = bookmark.time || 0
this.createdAt = bookmark.createdAt
}
setData(time, title) {
this.title = title
this.time = time
this.createdAt = Date.now()
}
}
module.exports = AudioBookmark

View file

@ -1,3 +1,5 @@
const AudioBookmark = require('./AudioBookmark')
class AudiobookProgress {
constructor(progress) {
this.audiobookId = null
@ -10,12 +12,18 @@ class AudiobookProgress {
this.lastUpdate = null
this.startedAt = null
this.finishedAt = null
this.bookmarks = []
if (progress) {
this.construct(progress)
}
}
bookmarksToJSON() {
if (!this.bookmarks) return []
return this.bookmarks.map(b => b.toJSON())
}
toJSON() {
return {
audiobookId: this.audiobookId,
@ -25,7 +33,8 @@ class AudiobookProgress {
isRead: this.isRead,
lastUpdate: this.lastUpdate,
startedAt: this.startedAt,
finishedAt: this.finishedAt
finishedAt: this.finishedAt,
bookmarks: this.bookmarksToJSON()
}
}
@ -38,6 +47,11 @@ class AudiobookProgress {
this.lastUpdate = progress.lastUpdate
this.startedAt = progress.startedAt
this.finishedAt = progress.finishedAt || null
if (progress.bookmarks) {
this.bookmarks = progress.bookmarks.map(b => new AudioBookmark(b))
} else {
this.bookmarks = []
}
}
updateFromStream(stream) {
@ -90,5 +104,12 @@ class AudiobookProgress {
}
return hasUpdates
}
createBookmark(time, title) {
var newBookmark = new AudioBookmark()
newBookmark.setData(time, title)
this.bookmarks.push(newBookmark)
return newBookmark
}
}
module.exports = AudiobookProgress

View file

@ -272,5 +272,14 @@ class User {
getAudiobookJSON(audiobookId) {
return this.audiobooks[audiobookId] ? this.audiobooks[audiobookId].toJSON() : null
}
createBookmark({ audiobookId, time, title }) {
if (!this.audiobooks || !this.audiobooks[audiobookId]) {
return false
}
var success = this.audiobooks[audiobookId].createBookmark(time, title)
if (success) return this.audiobooks[audiobookId]
return null
}
}
module.exports = User