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
|
|
|
|
this.mediaType = null
|
|
|
|
this.mediaMetadata = null
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
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,
|
|
|
|
mediaType: this.mediaType,
|
|
|
|
mediaMetadata: this.mediaMetadata ? this.mediaMetadata.toJSON() : null,
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
this.mediaType = session.mediaType
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
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-13 02:59:35 +01:00
|
|
|
setData(libraryItem, user) {
|
2021-11-16 03:09:42 +01:00
|
|
|
this.id = getId('ls')
|
2021-11-06 02:24:02 +01:00
|
|
|
this.userId = user.id
|
2022-03-16 00:57:15 +01:00
|
|
|
this.libraryItemId = libraryItem.id
|
|
|
|
this.mediaType = libraryItem.mediaType
|
|
|
|
this.mediaMetadata = libraryItem.media.metadata.clone()
|
|
|
|
this.playMethod = PlayMethod.TRANSCODE
|
2021-11-06 02:24:02 +01:00
|
|
|
|
|
|
|
this.timeListening = 0
|
|
|
|
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) {
|
|
|
|
if (timeListened && !isNaN(timeListened)) {
|
|
|
|
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')
|
|
|
|
}
|
|
|
|
|
|
|
|
this.timeListening += timeListened
|
2022-03-16 00:57:15 +01:00
|
|
|
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
|