mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2026-05-13 06:51:29 +00:00
feat: Add native podcast episode bookmarking
This commit is contained in:
parent
8b89b27654
commit
ca6c7d7958
7 changed files with 54 additions and 29 deletions
|
|
@ -216,7 +216,7 @@ class MeController {
|
|||
return res.sendStatus(403)
|
||||
}
|
||||
|
||||
const { time, title } = req.body
|
||||
const { time, title, episodeId } = req.body
|
||||
if (isNullOrNaN(time)) {
|
||||
Logger.error(`[MeController] createBookmark invalid time`, time)
|
||||
return res.status(400).send('Invalid time')
|
||||
|
|
@ -226,7 +226,7 @@ class MeController {
|
|||
return res.status(400).send('Invalid title')
|
||||
}
|
||||
|
||||
const bookmark = await req.user.createBookmark(req.params.id, time, title)
|
||||
const bookmark = await req.user.createBookmark(req.params.id, time, title, episodeId)
|
||||
SocketAuthority.clientEmitter(req.user.id, 'user_updated', req.user.toOldJSONForBrowser())
|
||||
res.json(bookmark)
|
||||
}
|
||||
|
|
@ -249,7 +249,7 @@ class MeController {
|
|||
return res.sendStatus(403)
|
||||
}
|
||||
|
||||
const { time, title } = req.body
|
||||
const { time, title, episodeId } = req.body
|
||||
if (isNullOrNaN(time)) {
|
||||
Logger.error(`[MeController] updateBookmark invalid time`, time)
|
||||
return res.status(400).send('Invalid time')
|
||||
|
|
@ -259,7 +259,7 @@ class MeController {
|
|||
return res.status(400).send('Invalid title')
|
||||
}
|
||||
|
||||
const bookmark = await req.user.updateBookmark(req.params.id, time, title)
|
||||
const bookmark = await req.user.updateBookmark(req.params.id, time, title, episodeId)
|
||||
if (!bookmark) {
|
||||
Logger.error(`[MeController] updateBookmark not found for library item id "${req.params.id}" and time "${time}"`)
|
||||
return res.sendStatus(404)
|
||||
|
|
@ -291,13 +291,14 @@ class MeController {
|
|||
if (isNaN(time)) {
|
||||
return res.status(400).send('Invalid time')
|
||||
}
|
||||
const episodeId = req.query.episode
|
||||
|
||||
if (!req.user.findBookmark(req.params.id, time)) {
|
||||
if (!req.user.findBookmark(req.params.id, time, episodeId)) {
|
||||
Logger.error(`[MeController] removeBookmark not found`)
|
||||
return res.sendStatus(404)
|
||||
}
|
||||
|
||||
await req.user.removeBookmark(req.params.id, time)
|
||||
await req.user.removeBookmark(req.params.id, time, episodeId)
|
||||
|
||||
SocketAuthority.clientEmitter(req.user.id, 'user_updated', req.user.toOldJSONForBrowser())
|
||||
res.sendStatus(200)
|
||||
|
|
|
|||
|
|
@ -833,13 +833,16 @@ class User extends Model {
|
|||
|
||||
/**
|
||||
* Find bookmark
|
||||
* TODO: Bookmarks should use mediaItemId instead of libraryItemId to support podcast episodes
|
||||
*
|
||||
* @param {string} libraryItemId
|
||||
* @param {number} time
|
||||
* @param {string} [episodeId]
|
||||
* @returns {AudioBookmarkObject|null}
|
||||
*/
|
||||
findBookmark(libraryItemId, time) {
|
||||
findBookmark(libraryItemId, time, episodeId = null) {
|
||||
if (episodeId) {
|
||||
return this.bookmarks.find((bm) => bm.libraryItemId === libraryItemId && bm.episodeId === episodeId && bm.time == time)
|
||||
}
|
||||
return this.bookmarks.find((bm) => bm.libraryItemId === libraryItemId && bm.time == time)
|
||||
}
|
||||
|
||||
|
|
@ -849,10 +852,11 @@ class User extends Model {
|
|||
* @param {string} libraryItemId
|
||||
* @param {number} time
|
||||
* @param {string} title
|
||||
* @param {string} [episodeId]
|
||||
* @returns {Promise<AudioBookmarkObject>}
|
||||
*/
|
||||
async createBookmark(libraryItemId, time, title) {
|
||||
const existingBookmark = this.findBookmark(libraryItemId, time)
|
||||
async createBookmark(libraryItemId, time, title, episodeId = null) {
|
||||
const existingBookmark = this.findBookmark(libraryItemId, time, episodeId)
|
||||
if (existingBookmark) {
|
||||
Logger.warn('[User] Create Bookmark already exists for this time')
|
||||
if (existingBookmark.title !== title) {
|
||||
|
|
@ -869,6 +873,7 @@ class User extends Model {
|
|||
title,
|
||||
createdAt: Date.now()
|
||||
}
|
||||
if (episodeId) newBookmark.episodeId = episodeId
|
||||
this.bookmarks.push(newBookmark)
|
||||
this.changed('bookmarks', true)
|
||||
await this.save()
|
||||
|
|
@ -881,10 +886,11 @@ class User extends Model {
|
|||
* @param {string} libraryItemId
|
||||
* @param {number} time
|
||||
* @param {string} title
|
||||
* @param {string} [episodeId]
|
||||
* @returns {Promise<AudioBookmarkObject>}
|
||||
*/
|
||||
async updateBookmark(libraryItemId, time, title) {
|
||||
const bookmark = this.findBookmark(libraryItemId, time)
|
||||
async updateBookmark(libraryItemId, time, title, episodeId = null) {
|
||||
const bookmark = this.findBookmark(libraryItemId, time, episodeId)
|
||||
if (!bookmark) {
|
||||
Logger.error(`[User] updateBookmark not found`)
|
||||
return null
|
||||
|
|
@ -900,14 +906,19 @@ class User extends Model {
|
|||
*
|
||||
* @param {string} libraryItemId
|
||||
* @param {number} time
|
||||
* @param {string} [episodeId]
|
||||
* @returns {Promise<boolean>} - true if bookmark was removed
|
||||
*/
|
||||
async removeBookmark(libraryItemId, time) {
|
||||
if (!this.findBookmark(libraryItemId, time)) {
|
||||
async removeBookmark(libraryItemId, time, episodeId = null) {
|
||||
if (!this.findBookmark(libraryItemId, time, episodeId)) {
|
||||
Logger.error(`[User] removeBookmark not found`)
|
||||
return false
|
||||
}
|
||||
this.bookmarks = this.bookmarks.filter((bm) => bm.libraryItemId !== libraryItemId || bm.time !== time)
|
||||
if (episodeId) {
|
||||
this.bookmarks = this.bookmarks.filter((bm) => bm.libraryItemId !== libraryItemId || bm.episodeId !== episodeId || bm.time !== time)
|
||||
} else {
|
||||
this.bookmarks = this.bookmarks.filter((bm) => bm.libraryItemId !== libraryItemId || bm.time !== time)
|
||||
}
|
||||
this.changed('bookmarks', true)
|
||||
await this.save()
|
||||
return true
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue