2022-03-16 00:57:15 +01:00
|
|
|
const Logger = require('../../Logger')
|
2021-10-25 01:25:44 +02:00
|
|
|
|
2022-03-26 17:59:34 +01:00
|
|
|
class MediaProgress {
|
2021-09-06 21:13:01 +02:00
|
|
|
constructor(progress) {
|
2022-04-26 02:03:26 +02:00
|
|
|
this.id = null
|
2022-03-17 18:25:12 +01:00
|
|
|
this.libraryItemId = null
|
2022-03-26 17:59:34 +01:00
|
|
|
this.episodeId = null // For podcasts
|
2021-10-23 03:08:02 +02:00
|
|
|
|
2022-03-18 21:31:46 +01:00
|
|
|
this.duration = null
|
2021-09-06 21:13:01 +02:00
|
|
|
this.progress = null // 0 to 1
|
|
|
|
this.currentTime = null // seconds
|
2022-03-17 18:25:12 +01:00
|
|
|
this.isFinished = false
|
2022-03-16 00:57:15 +01:00
|
|
|
|
2021-09-06 21:13:01 +02:00
|
|
|
this.lastUpdate = null
|
|
|
|
this.startedAt = null
|
|
|
|
this.finishedAt = null
|
|
|
|
|
|
|
|
if (progress) {
|
|
|
|
this.construct(progress)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
toJSON() {
|
|
|
|
return {
|
2022-03-16 00:57:15 +01:00
|
|
|
id: this.id,
|
2022-03-17 18:25:12 +01:00
|
|
|
libraryItemId: this.libraryItemId,
|
2022-03-26 17:59:34 +01:00
|
|
|
episodeId: this.episodeId,
|
2022-03-18 21:31:46 +01:00
|
|
|
duration: this.duration,
|
2021-09-06 21:13:01 +02:00
|
|
|
progress: this.progress,
|
|
|
|
currentTime: this.currentTime,
|
2022-03-17 18:25:12 +01:00
|
|
|
isFinished: this.isFinished,
|
2021-09-06 21:13:01 +02:00
|
|
|
lastUpdate: this.lastUpdate,
|
|
|
|
startedAt: this.startedAt,
|
2022-03-16 00:57:15 +01:00
|
|
|
finishedAt: this.finishedAt
|
2021-09-06 21:13:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
construct(progress) {
|
2022-03-16 00:57:15 +01:00
|
|
|
this.id = progress.id
|
2022-03-17 18:25:12 +01:00
|
|
|
this.libraryItemId = progress.libraryItemId
|
2022-03-26 17:59:34 +01:00
|
|
|
this.episodeId = progress.episodeId
|
2022-03-18 21:31:46 +01:00
|
|
|
this.duration = progress.duration || 0
|
2021-09-06 21:13:01 +02:00
|
|
|
this.progress = progress.progress
|
|
|
|
this.currentTime = progress.currentTime
|
2022-03-17 18:25:12 +01:00
|
|
|
this.isFinished = !!progress.isFinished
|
2021-09-06 21:13:01 +02:00
|
|
|
this.lastUpdate = progress.lastUpdate
|
|
|
|
this.startedAt = progress.startedAt
|
|
|
|
this.finishedAt = progress.finishedAt || null
|
|
|
|
}
|
|
|
|
|
2022-03-18 01:10:47 +01:00
|
|
|
get inProgress() {
|
|
|
|
return !this.isFinished && this.progress > 0
|
2022-03-17 19:33:22 +01:00
|
|
|
}
|
|
|
|
|
2022-03-26 23:41:26 +01:00
|
|
|
setData(libraryItemId, progress, episodeId = null) {
|
2022-04-09 02:19:47 +02:00
|
|
|
this.id = episodeId ? `${libraryItemId}-${episodeId}` : libraryItemId
|
2022-03-17 19:33:22 +01:00
|
|
|
this.libraryItemId = libraryItemId
|
2022-03-26 23:41:26 +01:00
|
|
|
this.episodeId = episodeId
|
2022-03-18 21:31:46 +01:00
|
|
|
this.duration = progress.duration || 0
|
2022-03-17 19:33:22 +01:00
|
|
|
this.progress = Math.min(1, (progress.progress || 0))
|
|
|
|
this.currentTime = progress.currentTime || 0
|
|
|
|
this.isFinished = !!progress.isFinished || this.progress == 1
|
|
|
|
this.lastUpdate = Date.now()
|
|
|
|
this.startedAt = Date.now()
|
|
|
|
this.finishedAt = null
|
|
|
|
if (this.isFinished) {
|
2021-10-24 21:02:49 +02:00
|
|
|
this.finishedAt = Date.now()
|
2022-03-17 19:33:22 +01:00
|
|
|
this.progress = 1
|
2021-09-06 21:13:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
update(payload) {
|
|
|
|
var hasUpdates = false
|
|
|
|
for (const key in payload) {
|
2022-01-06 02:07:32 +01:00
|
|
|
if (this[key] !== undefined && payload[key] !== this[key]) {
|
2022-03-17 18:25:12 +01:00
|
|
|
if (key === 'isFinished') {
|
2022-03-26 23:41:26 +01:00
|
|
|
if (!payload[key]) { // Updating to Not Finished - Reset progress and current time
|
2021-09-06 21:13:01 +02:00
|
|
|
this.finishedAt = null
|
|
|
|
this.progress = 0
|
|
|
|
this.currentTime = 0
|
2022-03-26 23:41:26 +01:00
|
|
|
} else { // Updating to Finished
|
2021-09-06 21:13:01 +02:00
|
|
|
if (!this.finishedAt) this.finishedAt = Date.now()
|
|
|
|
this.progress = 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this[key] = payload[key]
|
|
|
|
hasUpdates = true
|
|
|
|
}
|
|
|
|
}
|
2022-03-26 23:41:26 +01:00
|
|
|
|
|
|
|
if (this.progress >= 1 && !this.isFinished) {
|
|
|
|
this.isFinished = true
|
|
|
|
this.finishedAt = Date.now()
|
|
|
|
this.progress = 1
|
|
|
|
} else if (this.progress < 1 && this.isFinished) {
|
|
|
|
this.isFinished = false
|
|
|
|
this.finishedAt = null
|
|
|
|
}
|
|
|
|
|
2021-09-06 21:13:01 +02:00
|
|
|
if (!this.startedAt) {
|
|
|
|
this.startedAt = Date.now()
|
|
|
|
}
|
2021-10-23 03:08:02 +02:00
|
|
|
if (hasUpdates) {
|
|
|
|
this.lastUpdate = Date.now()
|
|
|
|
}
|
2021-09-06 21:13:01 +02:00
|
|
|
return hasUpdates
|
|
|
|
}
|
|
|
|
}
|
2022-03-26 17:59:34 +01:00
|
|
|
module.exports = MediaProgress
|