2021-11-13 02:43:16 +01:00
|
|
|
const date = require('date-and-time')
|
2022-03-16 01:28:54 +01:00
|
|
|
const { getId } = require('../utils/index')
|
|
|
|
const { PlayMethod } = require('../utils/constants')
|
|
|
|
const BookMetadata = require('./metadata/BookMetadata')
|
|
|
|
const PodcastMetadata = require('./metadata/PodcastMetadata')
|
2021-11-06 02:24:02 +01:00
|
|
|
|
2022-03-16 00:57:15 +01:00
|
|
|
class PlaybackSession {
|
2021-11-06 02:24:02 +01:00
|
|
|
constructor(session) {
|
2021-11-13 02:43:16 +01:00
|
|
|
this.id = null
|
2021-11-06 02:24:02 +01:00
|
|
|
this.userId = null
|
2022-03-16 00:57:15 +01:00
|
|
|
this.libraryItemId = null
|
2022-03-26 23:41:26 +01:00
|
|
|
this.episodeId = null
|
2022-03-18 01:10:47 +01:00
|
|
|
|
2022-03-16 00:57:15 +01:00
|
|
|
this.mediaType = null
|
|
|
|
this.mediaMetadata = null
|
2022-03-26 17:59:34 +01:00
|
|
|
this.coverPath = null
|
2022-03-18 01:10:47 +01:00
|
|
|
this.duration = null
|
2022-03-16 00:57:15 +01:00
|
|
|
|
|
|
|
this.playMethod = null
|
2021-11-13 02:43:16 +01:00
|
|
|
|
|
|
|
this.date = null
|
|
|
|
this.dayOfWeek = null
|
2021-11-06 02:24:02 +01:00
|
|
|
|
|
|
|
this.timeListening = null
|
|
|
|
this.startedAt = null
|
2022-03-16 00:57:15 +01:00
|
|
|
this.updatedAt = null
|
2021-11-06 02:24:02 +01:00
|
|
|
|
2022-03-18 01:10:47 +01:00
|
|
|
// Not saved in DB
|
|
|
|
this.lastSave = 0
|
|
|
|
this.audioTracks = []
|
|
|
|
this.currentTime = 0
|
|
|
|
this.stream = null
|
|
|
|
|
2021-11-06 02:24:02 +01:00
|
|
|
if (session) {
|
|
|
|
this.construct(session)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
toJSON() {
|
|
|
|
return {
|
2021-11-13 02:43:16 +01:00
|
|
|
id: this.id,
|
|
|
|
sessionType: this.sessionType,
|
2021-11-06 02:24:02 +01:00
|
|
|
userId: this.userId,
|
2022-03-16 00:57:15 +01:00
|
|
|
libraryItemId: this.libraryItemId,
|
2022-03-26 23:41:26 +01:00
|
|
|
episodeId: this.episodeId,
|
2022-03-16 00:57:15 +01:00
|
|
|
mediaType: this.mediaType,
|
|
|
|
mediaMetadata: this.mediaMetadata ? this.mediaMetadata.toJSON() : null,
|
2022-03-26 17:59:34 +01:00
|
|
|
coverPath: this.coverPath,
|
2022-03-18 01:10:47 +01:00
|
|
|
duration: this.duration,
|
2022-03-16 00:57:15 +01:00
|
|
|
playMethod: this.playMethod,
|
2021-11-13 02:43:16 +01:00
|
|
|
date: this.date,
|
|
|
|
dayOfWeek: this.dayOfWeek,
|
2021-11-06 02:24:02 +01:00
|
|
|
timeListening: this.timeListening,
|
|
|
|
lastUpdate: this.lastUpdate,
|
2022-03-16 00:57:15 +01:00
|
|
|
updatedAt: this.updatedAt
|
2021-11-06 02:24:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-26 17:59:34 +01:00
|
|
|
toJSONForClient(libraryItem) {
|
2022-03-18 01:10:47 +01:00
|
|
|
return {
|
|
|
|
id: this.id,
|
|
|
|
sessionType: this.sessionType,
|
|
|
|
userId: this.userId,
|
|
|
|
libraryItemId: this.libraryItemId,
|
2022-03-26 23:41:26 +01:00
|
|
|
episodeId: this.episodeId,
|
2022-03-18 01:10:47 +01:00
|
|
|
mediaType: this.mediaType,
|
|
|
|
mediaMetadata: this.mediaMetadata ? this.mediaMetadata.toJSON() : null,
|
2022-03-26 17:59:34 +01:00
|
|
|
coverPath: this.coverPath,
|
2022-03-18 01:10:47 +01:00
|
|
|
duration: this.duration,
|
|
|
|
playMethod: this.playMethod,
|
|
|
|
date: this.date,
|
|
|
|
dayOfWeek: this.dayOfWeek,
|
|
|
|
timeListening: this.timeListening,
|
|
|
|
lastUpdate: this.lastUpdate,
|
|
|
|
updatedAt: this.updatedAt,
|
|
|
|
audioTracks: this.audioTracks.map(at => at.toJSON()),
|
2022-03-26 17:59:34 +01:00
|
|
|
currentTime: this.currentTime,
|
|
|
|
libraryItem: libraryItem.toJSONExpanded()
|
2022-03-18 01:10:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-06 02:24:02 +01:00
|
|
|
construct(session) {
|
2021-11-13 02:43:16 +01:00
|
|
|
this.id = session.id
|
|
|
|
this.sessionType = session.sessionType
|
2021-11-06 02:24:02 +01:00
|
|
|
this.userId = session.userId
|
2022-03-16 00:57:15 +01:00
|
|
|
this.libraryItemId = session.libraryItemId
|
2022-03-26 23:41:26 +01:00
|
|
|
this.episodeId = session.episodeId,
|
|
|
|
this.mediaType = session.mediaType
|
2022-03-18 01:10:47 +01:00
|
|
|
this.duration = session.duration
|
2022-03-16 00:57:15 +01:00
|
|
|
this.playMethod = session.playMethod
|
|
|
|
|
|
|
|
this.mediaMetadata = null
|
|
|
|
if (session.mediaMetadata) {
|
|
|
|
if (this.mediaType === 'book') {
|
|
|
|
this.mediaMetadata = new BookMetadata(session.mediaMetadata)
|
|
|
|
} else if (this.mediaType === 'podcast') {
|
|
|
|
this.mediaMetadata = new PodcastMetadata(session.mediaMetadata)
|
|
|
|
}
|
|
|
|
}
|
2022-03-26 17:59:34 +01:00
|
|
|
this.coverPath = session.coverPath
|
2021-11-13 02:43:16 +01:00
|
|
|
this.date = session.date
|
|
|
|
this.dayOfWeek = session.dayOfWeek
|
2021-11-06 02:24:02 +01:00
|
|
|
|
|
|
|
this.timeListening = session.timeListening || null
|
|
|
|
this.startedAt = session.startedAt
|
2022-03-16 00:57:15 +01:00
|
|
|
this.updatedAt = session.updatedAt || null
|
2021-11-06 02:24:02 +01:00
|
|
|
}
|
|
|
|
|
2022-03-18 01:10:47 +01:00
|
|
|
get progress() { // Value between 0 and 1
|
|
|
|
if (!this.duration) return 0
|
|
|
|
return Math.max(0, Math.min(this.currentTime / this.duration, 1))
|
|
|
|
}
|
|
|
|
|
2022-03-26 23:41:26 +01:00
|
|
|
setData(libraryItem, user, episodeId = null) {
|
2022-03-18 01:10:47 +01:00
|
|
|
this.id = getId('play')
|
2021-11-06 02:24:02 +01:00
|
|
|
this.userId = user.id
|
2022-03-16 00:57:15 +01:00
|
|
|
this.libraryItemId = libraryItem.id
|
2022-03-26 23:41:26 +01:00
|
|
|
this.episodeId = episodeId
|
2022-03-16 00:57:15 +01:00
|
|
|
this.mediaType = libraryItem.mediaType
|
|
|
|
this.mediaMetadata = libraryItem.media.metadata.clone()
|
2022-03-26 17:59:34 +01:00
|
|
|
this.coverPath = libraryItem.media.coverPath
|
|
|
|
this.duration = libraryItem.media.duration
|
2021-11-06 02:24:02 +01:00
|
|
|
|
|
|
|
this.timeListening = 0
|
2022-03-18 01:10:47 +01:00
|
|
|
this.date = date.format(new Date(), 'YYYY-MM-DD')
|
|
|
|
this.dayOfWeek = date.format(new Date(), 'dddd')
|
2021-11-06 02:24:02 +01:00
|
|
|
this.startedAt = Date.now()
|
2022-03-16 00:57:15 +01:00
|
|
|
this.updatedAt = Date.now()
|
2021-11-13 02:43:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
addListeningTime(timeListened) {
|
2022-03-18 01:10:47 +01:00
|
|
|
if (!timeListened || isNaN(timeListened)) return
|
2021-11-13 02:43:16 +01:00
|
|
|
|
2022-03-18 01:10:47 +01:00
|
|
|
if (!this.date) {
|
|
|
|
// Set date info on first listening update
|
|
|
|
this.date = date.format(new Date(), 'YYYY-MM-DD')
|
|
|
|
this.dayOfWeek = date.format(new Date(), 'dddd')
|
2021-11-13 02:43:16 +01:00
|
|
|
}
|
2022-03-18 01:10:47 +01:00
|
|
|
|
|
|
|
this.timeListening += timeListened
|
|
|
|
this.updatedAt = Date.now()
|
2021-11-13 02:43:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// New date since start of listening session
|
|
|
|
checkDateRollover() {
|
|
|
|
if (!this.date) return false
|
|
|
|
return date.format(new Date(), 'YYYY-MM-DD') !== this.date
|
2021-11-06 02:24:02 +01:00
|
|
|
}
|
|
|
|
}
|
2022-03-16 00:57:15 +01:00
|
|
|
module.exports = PlaybackSession
|