Init sqlite take 2

This commit is contained in:
advplyr
2023-07-04 18:14:44 -05:00
parent d86a3b3dc2
commit cf7fd315b6
88 changed files with 7017 additions and 692 deletions

View File

@@ -1,10 +1,9 @@
const { getId } = require('../utils/index')
const uuidv4 = require("uuid").v4
class Collection {
constructor(collection) {
this.id = null
this.libraryId = null
this.userId = null
this.name = null
this.description = null
@@ -25,7 +24,6 @@ class Collection {
return {
id: this.id,
libraryId: this.libraryId,
userId: this.userId,
name: this.name,
description: this.description,
cover: this.cover,
@@ -60,7 +58,6 @@ class Collection {
construct(collection) {
this.id = collection.id
this.libraryId = collection.libraryId
this.userId = collection.userId
this.name = collection.name
this.description = collection.description || null
this.cover = collection.cover || null
@@ -71,11 +68,10 @@ class Collection {
}
setData(data) {
if (!data.userId || !data.libraryId || !data.name) {
if (!data.libraryId || !data.name) {
return false
}
this.id = getId('col')
this.userId = data.userId
this.id = uuidv4()
this.libraryId = data.libraryId
this.name = data.name
this.description = data.description || null

View File

@@ -1,5 +1,9 @@
const uuidv4 = require("uuid").v4
class DeviceInfo {
constructor(deviceInfo = null) {
this.id = null
this.userId = null
this.deviceId = null
this.ipAddress = null
@@ -16,7 +20,8 @@ class DeviceInfo {
this.model = null
this.sdkVersion = null // Android Only
this.serverVersion = null
this.clientName = null
this.deviceName = null
if (deviceInfo) {
this.construct(deviceInfo)
@@ -33,6 +38,8 @@ class DeviceInfo {
toJSON() {
const obj = {
id: this.id,
userId: this.userId,
deviceId: this.deviceId,
ipAddress: this.ipAddress,
browserName: this.browserName,
@@ -44,7 +51,8 @@ class DeviceInfo {
manufacturer: this.manufacturer,
model: this.model,
sdkVersion: this.sdkVersion,
serverVersion: this.serverVersion
clientName: this.clientName,
deviceName: this.deviceName
}
for (const key in obj) {
if (obj[key] === null || obj[key] === undefined) {
@@ -65,6 +73,7 @@ class DeviceInfo {
// When client doesn't send a device id
getTempDeviceId() {
const keys = [
this.userId,
this.browserName,
this.browserVersion,
this.osName,
@@ -78,7 +87,9 @@ class DeviceInfo {
return 'temp-' + Buffer.from(keys.join('-'), 'utf-8').toString('base64')
}
setData(ip, ua, clientDeviceInfo, serverVersion) {
setData(ip, ua, clientDeviceInfo, serverVersion, userId) {
this.id = uuidv4()
this.userId = userId
this.deviceId = clientDeviceInfo?.deviceId || null
this.ipAddress = ip || null
@@ -88,16 +99,54 @@ class DeviceInfo {
this.osVersion = ua?.os.version || null
this.deviceType = ua?.device.type || null
this.clientVersion = clientDeviceInfo?.clientVersion || null
this.clientVersion = clientDeviceInfo?.clientVersion || serverVersion
this.manufacturer = clientDeviceInfo?.manufacturer || null
this.model = clientDeviceInfo?.model || null
this.sdkVersion = clientDeviceInfo?.sdkVersion || null
this.serverVersion = serverVersion || null
this.clientName = clientDeviceInfo?.clientName || null
if (this.sdkVersion) {
if (!this.clientName) this.clientName = 'Abs Android'
this.deviceName = `${this.manufacturer || 'Unknown'} ${this.model || ''}`
} else if (this.model) {
if (!this.clientName) this.clientName = 'Abs iOS'
this.deviceName = `${this.manufacturer || 'Unknown'} ${this.model || ''}`
} else if (this.osName && this.browserName) {
if (!this.clientName) this.clientName = 'Abs Web'
this.deviceName = `${this.osName} ${this.osVersion || 'N/A'} ${this.browserName}`
} else if (!this.clientName) {
this.clientName = 'Unknown'
}
if (!this.deviceId) {
this.deviceId = this.getTempDeviceId()
}
}
update(deviceInfo) {
const deviceInfoJson = deviceInfo.toJSON ? deviceInfo.toJSON() : deviceInfo
const existingDeviceInfoJson = this.toJSON()
let hasUpdates = false
for (const key in deviceInfoJson) {
if (['id', 'deviceId'].includes(key)) continue
if (deviceInfoJson[key] !== existingDeviceInfoJson[key]) {
this[key] = deviceInfoJson[key]
hasUpdates = true
}
}
for (const key in existingDeviceInfoJson) {
if (['id', 'deviceId'].includes(key)) continue
if (existingDeviceInfoJson[key] && !deviceInfoJson[key]) {
this[key] = null
hasUpdates = true
}
}
return hasUpdates
}
}
module.exports = DeviceInfo

View File

@@ -1,3 +1,4 @@
const uuidv4 = require("uuid").v4
const FeedMeta = require('./FeedMeta')
const FeedEpisode = require('./FeedEpisode')
const RSS = require('../libs/rss')
@@ -90,7 +91,7 @@ class Feed {
const feedUrl = `${serverAddress}/feed/${slug}`
const author = isPodcast ? mediaMetadata.author : mediaMetadata.authorName
this.id = slug
this.id = uuidv4()
this.slug = slug
this.userId = userId
this.entityType = 'libraryItem'
@@ -179,7 +180,7 @@ class Feed {
const itemsWithTracks = collectionExpanded.books.filter(libraryItem => libraryItem.media.tracks.length)
const firstItemWithCover = itemsWithTracks.find(item => item.media.coverPath)
this.id = slug
this.id = uuidv4()
this.slug = slug
this.userId = userId
this.entityType = 'collection'
@@ -253,7 +254,7 @@ class Feed {
const libraryId = itemsWithTracks[0].libraryId
const firstItemWithCover = itemsWithTracks.find(li => li.media.coverPath)
this.id = slug
this.id = uuidv4()
this.slug = slug
this.userId = userId
this.entityType = 'series'

View File

@@ -1,4 +1,3 @@
const Path = require('path')
const date = require('../libs/dateAndTime')
const { secondsToTimestamp } = require('../utils/index')

View File

@@ -1,4 +1,4 @@
const { getId } = require("../utils")
const uuidv4 = require("uuid").v4
class Folder {
constructor(folder = null) {
@@ -29,7 +29,7 @@ class Folder {
}
setData(data) {
this.id = data.id ? data.id : getId('fol')
this.id = data.id || uuidv4()
this.fullPath = data.fullPath
this.libraryId = data.libraryId
this.addedAt = Date.now()

View File

@@ -1,6 +1,6 @@
const uuidv4 = require("uuid").v4
const Folder = require('./Folder')
const LibrarySettings = require('./settings/LibrarySettings')
const { getId } = require('../utils/index')
const { filePathToPOSIX } = require('../utils/fileUtils')
class Library {
@@ -87,7 +87,7 @@ class Library {
}
setData(data) {
this.id = data.id ? data.id : getId('lib')
this.id = data.id || uuidv4()
this.name = data.name
if (data.folder) {
this.folders = [

View File

@@ -1,3 +1,4 @@
const uuidv4 = require("uuid").v4
const fs = require('../libs/fsExtra')
const Path = require('path')
const { version } = require('../../package.json')
@@ -8,7 +9,7 @@ const Book = require('./mediaTypes/Book')
const Podcast = require('./mediaTypes/Podcast')
const Video = require('./mediaTypes/Video')
const Music = require('./mediaTypes/Music')
const { areEquivalent, copyValue, getId, cleanStringForSearch } = require('../utils/index')
const { areEquivalent, copyValue, cleanStringForSearch } = require('../utils/index')
const { filePathToPOSIX } = require('../utils/fileUtils')
class LibraryItem {
@@ -191,7 +192,7 @@ class LibraryItem {
// Data comes from scandir library item data
setData(libraryMediaType, payload) {
this.id = getId('li')
this.id = uuidv4()
this.mediaType = libraryMediaType
if (libraryMediaType === 'video') {
this.media = new Video()
@@ -202,6 +203,7 @@ class LibraryItem {
} else if (libraryMediaType === 'music') {
this.media = new Music()
}
this.media.id = uuidv4()
this.media.libraryItemId = this.id
for (const key in payload) {

View File

@@ -1,4 +1,4 @@
const { getId } = require('../utils/index')
const uuidv4 = require("uuid").v4
class Notification {
constructor(notification = null) {
@@ -57,7 +57,7 @@ class Notification {
}
setData(payload) {
this.id = getId('noti')
this.id = uuidv4()
this.libraryId = payload.libraryId || null
this.eventName = payload.eventName
this.urls = payload.urls

View File

@@ -1,5 +1,6 @@
const date = require('../libs/dateAndTime')
const { getId } = require('../utils/index')
const uuidv4 = require("uuid").v4
const serverVersion = require('../../package.json').version
const BookMetadata = require('./metadata/BookMetadata')
const PodcastMetadata = require('./metadata/PodcastMetadata')
const DeviceInfo = require('./DeviceInfo')
@@ -11,6 +12,7 @@ class PlaybackSession {
this.userId = null
this.libraryId = null
this.libraryItemId = null
this.bookId = null
this.episodeId = null
this.mediaType = null
@@ -24,6 +26,7 @@ class PlaybackSession {
this.playMethod = null
this.mediaPlayer = null
this.deviceInfo = null
this.serverVersion = null
this.date = null
this.dayOfWeek = null
@@ -52,6 +55,7 @@ class PlaybackSession {
userId: this.userId,
libraryId: this.libraryId,
libraryItemId: this.libraryItemId,
bookId: this.bookId,
episodeId: this.episodeId,
mediaType: this.mediaType,
mediaMetadata: this.mediaMetadata?.toJSON() || null,
@@ -63,6 +67,7 @@ class PlaybackSession {
playMethod: this.playMethod,
mediaPlayer: this.mediaPlayer,
deviceInfo: this.deviceInfo?.toJSON() || null,
serverVersion: this.serverVersion,
date: this.date,
dayOfWeek: this.dayOfWeek,
timeListening: this.timeListening,
@@ -79,6 +84,7 @@ class PlaybackSession {
userId: this.userId,
libraryId: this.libraryId,
libraryItemId: this.libraryItemId,
bookId: this.bookId,
episodeId: this.episodeId,
mediaType: this.mediaType,
mediaMetadata: this.mediaMetadata?.toJSON() || null,
@@ -90,6 +96,7 @@ class PlaybackSession {
playMethod: this.playMethod,
mediaPlayer: this.mediaPlayer,
deviceInfo: this.deviceInfo?.toJSON() || null,
serverVersion: this.serverVersion,
date: this.date,
dayOfWeek: this.dayOfWeek,
timeListening: this.timeListening,
@@ -108,12 +115,20 @@ class PlaybackSession {
this.userId = session.userId
this.libraryId = session.libraryId || null
this.libraryItemId = session.libraryItemId
this.bookId = session.bookId
this.episodeId = session.episodeId
this.mediaType = session.mediaType
this.duration = session.duration
this.playMethod = session.playMethod
this.mediaPlayer = session.mediaPlayer || null
this.deviceInfo = new DeviceInfo(session.deviceInfo)
if (session.deviceInfo instanceof DeviceInfo) {
this.deviceInfo = new DeviceInfo(session.deviceInfo.toJSON())
} else {
this.deviceInfo = new DeviceInfo(session.deviceInfo)
}
this.serverVersion = session.serverVersion
this.chapters = session.chapters || []
this.mediaMetadata = null
@@ -151,7 +166,7 @@ class PlaybackSession {
}
get deviceId() {
return this.deviceInfo?.deviceId
return this.deviceInfo?.id
}
get deviceDescription() {
@@ -169,10 +184,11 @@ class PlaybackSession {
}
setData(libraryItem, user, mediaPlayer, deviceInfo, startTime, episodeId = null) {
this.id = getId('play')
this.id = uuidv4()
this.userId = user.id
this.libraryId = libraryItem.libraryId
this.libraryItemId = libraryItem.id
this.bookId = episodeId ? null : libraryItem.media.id
this.episodeId = episodeId
this.mediaType = libraryItem.mediaType
this.mediaMetadata = libraryItem.media.metadata.clone()
@@ -189,6 +205,7 @@ class PlaybackSession {
this.mediaPlayer = mediaPlayer
this.deviceInfo = deviceInfo || new DeviceInfo()
this.serverVersion = serverVersion
this.timeListening = 0
this.startTime = startTime

View File

@@ -1,5 +1,4 @@
const Logger = require('../Logger')
const { getId } = require('../utils/index')
const uuidv4 = require("uuid").v4
class Playlist {
constructor(playlist) {
@@ -88,7 +87,7 @@ class Playlist {
if (!data.userId || !data.libraryId || !data.name) {
return false
}
this.id = getId('pl')
this.id = uuidv4()
this.userId = data.userId
this.libraryId = data.libraryId
this.name = data.name

View File

@@ -1,5 +1,5 @@
const Path = require('path')
const { getId } = require('../utils/index')
const uuidv4 = require("uuid").v4
const { sanitizeFilename } = require('../utils/fileUtils')
const globals = require('../utils/globals')
@@ -70,7 +70,7 @@ class PodcastEpisodeDownload {
}
setData(podcastEpisode, libraryItem, isAutoDownload, libraryId) {
this.id = getId('epdl')
this.id = uuidv4()
this.podcastEpisode = podcastEpisode
const url = podcastEpisode.enclosure.url

View File

@@ -1,4 +1,4 @@
const { getId } = require('../utils/index')
const uuidv4 = require("uuid").v4
class Task {
constructor() {
@@ -35,7 +35,7 @@ class Task {
}
setData(action, title, description, showSuccess, data = {}) {
this.id = getId(action)
this.id = uuidv4()
this.action = action
this.data = { ...data }
this.title = title

View File

@@ -1,5 +1,5 @@
const Logger = require('../../Logger')
const { getId } = require('../../utils/index')
const uuidv4 = require("uuid").v4
const { checkNamesAreEqual } = require('../../utils/parsers/parseNameString')
class Author {
@@ -53,7 +53,7 @@ class Author {
}
setData(data) {
this.id = getId('aut')
this.id = uuidv4()
this.name = data.name
this.description = data.description || null
this.asin = data.asin || null

View File

@@ -1,12 +1,14 @@
const uuidv4 = require("uuid").v4
const Path = require('path')
const Logger = require('../../Logger')
const { getId, cleanStringForSearch, areEquivalent, copyValue } = require('../../utils/index')
const { cleanStringForSearch, areEquivalent, copyValue } = require('../../utils/index')
const AudioFile = require('../files/AudioFile')
const AudioTrack = require('../files/AudioTrack')
class PodcastEpisode {
constructor(episode) {
this.libraryItemId = null
this.podcastId = null
this.id = null
this.index = null
@@ -32,6 +34,7 @@ class PodcastEpisode {
construct(episode) {
this.libraryItemId = episode.libraryItemId
this.podcastId = episode.podcastId
this.id = episode.id
this.index = episode.index
this.season = episode.season
@@ -54,6 +57,7 @@ class PodcastEpisode {
toJSON() {
return {
libraryItemId: this.libraryItemId,
podcastId: this.podcastId,
id: this.id,
index: this.index,
season: this.season,
@@ -75,6 +79,7 @@ class PodcastEpisode {
toJSONExpanded() {
return {
libraryItemId: this.libraryItemId,
podcastId: this.podcastId,
id: this.id,
index: this.index,
season: this.season,
@@ -117,7 +122,7 @@ class PodcastEpisode {
}
setData(data, index = 1) {
this.id = getId('ep')
this.id = uuidv4()
this.index = index
this.title = data.title
this.subtitle = data.subtitle || ''
@@ -133,7 +138,7 @@ class PodcastEpisode {
}
setDataFromAudioFile(audioFile, index) {
this.id = getId('ep')
this.id = uuidv4()
this.audioFile = audioFile
this.title = Path.basename(audioFile.metadata.filename, Path.extname(audioFile.metadata.filename))
this.index = index

View File

@@ -1,4 +1,4 @@
const { getId } = require('../../utils/index')
const uuidv4 = require("uuid").v4
class Series {
constructor(series) {
@@ -40,7 +40,7 @@ class Series {
}
setData(data) {
this.id = getId('ser')
this.id = uuidv4()
this.name = data.name
this.description = data.description || null
this.addedAt = Date.now()

View File

@@ -12,6 +12,7 @@ const EBookFile = require('../files/EBookFile')
class Book {
constructor(book) {
this.id = null
this.libraryItemId = null
this.metadata = null
@@ -32,6 +33,7 @@ class Book {
}
construct(book) {
this.id = book.id
this.libraryItemId = book.libraryItemId
this.metadata = new BookMetadata(book.metadata)
this.coverPath = book.coverPath
@@ -46,6 +48,7 @@ class Book {
toJSON() {
return {
id: this.id,
libraryItemId: this.libraryItemId,
metadata: this.metadata.toJSON(),
coverPath: this.coverPath,
@@ -59,6 +62,7 @@ class Book {
toJSONMinified() {
return {
id: this.id,
metadata: this.metadata.toJSONMinified(),
coverPath: this.coverPath,
tags: [...this.tags],
@@ -75,6 +79,7 @@ class Book {
toJSONExpanded() {
return {
id: this.id,
libraryItemId: this.libraryItemId,
metadata: this.metadata.toJSONExpanded(),
coverPath: this.coverPath,

View File

@@ -11,6 +11,7 @@ const naturalSort = createNewSortInstance({
class Podcast {
constructor(podcast) {
this.id = null
this.libraryItemId = null
this.metadata = null
this.coverPath = null
@@ -32,6 +33,7 @@ class Podcast {
}
construct(podcast) {
this.id = podcast.id
this.libraryItemId = podcast.libraryItemId
this.metadata = new PodcastMetadata(podcast.metadata)
this.coverPath = podcast.coverPath
@@ -50,6 +52,7 @@ class Podcast {
toJSON() {
return {
id: this.id,
libraryItemId: this.libraryItemId,
metadata: this.metadata.toJSON(),
coverPath: this.coverPath,
@@ -65,6 +68,7 @@ class Podcast {
toJSONMinified() {
return {
id: this.id,
metadata: this.metadata.toJSONMinified(),
coverPath: this.coverPath,
tags: [...this.tags],
@@ -80,6 +84,7 @@ class Podcast {
toJSONExpanded() {
return {
id: this.id,
libraryItemId: this.libraryItemId,
metadata: this.metadata.toJSONExpanded(),
coverPath: this.coverPath,
@@ -284,8 +289,9 @@ class Podcast {
}
addNewEpisodeFromAudioFile(audioFile, index) {
var pe = new PodcastEpisode()
const pe = new PodcastEpisode()
pe.libraryItemId = this.libraryItemId
pe.podcastId = this.id
audioFile.index = 1 // Only 1 audio file per episode
pe.setDataFromAudioFile(audioFile, index)
this.episodes.push(pe)

View File

@@ -218,7 +218,7 @@ class BookMetadata {
// Updates author name
updateAuthor(updatedAuthor) {
var author = this.authors.find(au => au.id === updatedAuthor.id)
const author = this.authors.find(au => au.id === updatedAuthor.id)
if (!author || author.name == updatedAuthor.name) return false
author.name = updatedAuthor.name
return true

View File

@@ -1,9 +1,15 @@
const uuidv4 = require("uuid").v4
class MediaProgress {
constructor(progress) {
this.id = null
this.userId = null
this.libraryItemId = null
this.episodeId = null // For podcasts
this.mediaItemId = null // For use in new data model
this.mediaItemType = null // For use in new data model
this.duration = null
this.progress = null // 0 to 1
this.currentTime = null // seconds
@@ -25,8 +31,11 @@ class MediaProgress {
toJSON() {
return {
id: this.id,
userId: this.userId,
libraryItemId: this.libraryItemId,
episodeId: this.episodeId,
mediaItemId: this.mediaItemId,
mediaItemType: this.mediaItemType,
duration: this.duration,
progress: this.progress,
currentTime: this.currentTime,
@@ -42,8 +51,11 @@ class MediaProgress {
construct(progress) {
this.id = progress.id
this.userId = progress.userId
this.libraryItemId = progress.libraryItemId
this.episodeId = progress.episodeId
this.mediaItemId = progress.mediaItemId
this.mediaItemType = progress.mediaItemType
this.duration = progress.duration || 0
this.progress = progress.progress
this.currentTime = progress.currentTime || 0
@@ -60,10 +72,16 @@ class MediaProgress {
return !this.isFinished && (this.progress > 0 || (this.ebookLocation != null && this.ebookProgress > 0))
}
setData(libraryItemId, progress, episodeId = null) {
this.id = episodeId ? `${libraryItemId}-${episodeId}` : libraryItemId
this.libraryItemId = libraryItemId
setData(libraryItem, progress, episodeId, userId) {
this.id = uuidv4()
this.userId = userId
this.libraryItemId = libraryItem.id
this.episodeId = episodeId
// PodcastEpisodeId or BookId
this.mediaItemId = episodeId || libraryItem.media.id
this.mediaItemType = episodeId ? 'podcastEpisode' : 'book'
this.duration = progress.duration || 0
this.progress = Math.min(1, (progress.progress || 0))
this.currentTime = progress.currentTime || 0

View File

@@ -5,6 +5,7 @@ const MediaProgress = require('./MediaProgress')
class User {
constructor(user) {
this.id = null
this.oldUserId = null // TODO: Temp for keeping old access tokens
this.username = null
this.pash = null
this.type = null
@@ -73,6 +74,7 @@ class User {
toJSON() {
return {
id: this.id,
oldUserId: this.oldUserId,
username: this.username,
pash: this.pash,
type: this.type,
@@ -93,6 +95,7 @@ class User {
toJSONForBrowser(hideRootToken = false, minimal = false) {
const json = {
id: this.id,
oldUserId: this.oldUserId,
username: this.username,
type: this.type,
token: (this.type === 'root' && hideRootToken) ? '' : this.token,
@@ -126,6 +129,7 @@ class User {
}
return {
id: this.id,
oldUserId: this.oldUserId,
username: this.username,
type: this.type,
session,
@@ -137,6 +141,7 @@ class User {
construct(user) {
this.id = user.id
this.oldUserId = user.oldUserId
this.username = user.username
this.pash = user.pash
this.type = user.type
@@ -320,7 +325,7 @@ class User {
if (!itemProgress) {
const newItemProgress = new MediaProgress()
newItemProgress.setData(libraryItem.id, updatePayload, episodeId)
newItemProgress.setData(libraryItem, updatePayload, episodeId, this.id)
this.mediaProgress.push(newItemProgress)
return true
}
@@ -336,12 +341,6 @@ class User {
return true
}
removeMediaProgressForLibraryItem(libraryItemId) {
if (!this.mediaProgress.some(lip => lip.libraryItemId == libraryItemId)) return false
this.mediaProgress = this.mediaProgress.filter(lip => lip.libraryItemId != libraryItemId)
return true
}
checkCanAccessLibrary(libraryId) {
if (this.permissions.accessAllLibraries) return true
if (!this.librariesAccessible) return false