2021-09-01 20:07:11 -05:00
< template >
2023-03-04 17:15:48 -06:00
< div class = "w-full layout-wrapper bg-bg text-white" >
2021-09-01 20:07:11 -05:00
< app-appbar / >
2023-06-19 12:37:44 -05:00
< div id = "content" class = "overflow-hidden relative" : class = "isPlayerOpen ? 'playerOpen' : ''" >
2021-09-01 20:07:11 -05:00
< Nuxt / >
< / div >
2021-11-01 21:06:51 -05:00
< app-audio-player-container ref = "streamContainer" / >
2021-10-06 07:24:15 -05:00
< modals-libraries-modal / >
2022-12-03 17:05:43 -06:00
< modals-playlists-add-create-modal / >
2023-06-04 14:59:55 -05:00
< modals-select-local-folder-modal / >
2023-06-24 14:45:25 -05:00
< modals-rssfeeds-rss-feed-modal / >
2021-11-14 19:59:34 -06:00
< app-side-drawer / >
2021-10-17 20:20:00 -05:00
< readers-reader / >
2021-09-01 20:07:11 -05:00
< / div >
< / template >
< script >
export default {
data ( ) {
2022-04-07 18:46:58 -05:00
return {
2022-04-09 18:36:32 -05:00
inittingLibraries : false ,
hasMounted : false ,
disconnectTime : 0
2022-04-07 18:46:58 -05:00
}
2021-09-01 20:07:11 -05:00
} ,
2021-11-14 19:59:34 -06:00
watch : {
networkConnected : {
2022-04-07 18:46:58 -05:00
handler ( newVal , oldVal ) {
2022-04-09 18:36:32 -05:00
if ( ! this . hasMounted ) {
// watcher runs before mount, handling libraries/connection should be handled in mount
return
}
2021-11-14 19:59:34 -06:00
if ( newVal ) {
2022-04-07 18:46:58 -05:00
console . log ( ` [default] network connected changed ${ oldVal } -> ${ newVal } ` )
if ( ! this . user ) {
this . attemptConnection ( )
} else if ( ! this . currentLibraryId ) {
this . initLibraries ( )
2022-04-09 18:36:32 -05:00
} else {
var timeSinceDisconnect = Date . now ( ) - this . disconnectTime
if ( timeSinceDisconnect > 5000 ) {
console . log ( 'Time since disconnect was' , timeSinceDisconnect , 'sync with server' )
setTimeout ( ( ) => {
2023-09-10 12:46:57 -05:00
if ( this . $platform === 'ios' ) {
// TODO: Update ios to not use this
this . syncLocalMediaProgress ( )
} else {
this . syncLocalSessions ( )
}
2022-04-09 18:36:32 -05:00
} , 4000 )
}
2022-04-07 18:46:58 -05:00
}
2022-04-09 18:36:32 -05:00
} else {
console . log ( ` [default] lost network connection ` )
this . disconnectTime = Date . now ( )
2021-11-14 19:59:34 -06:00
}
}
}
} ,
2021-09-01 20:07:11 -05:00
computed : {
2023-06-19 12:37:44 -05:00
isPlayerOpen ( ) {
return this . $store . getters [ 'getIsPlayerOpen' ]
2023-06-17 17:34:08 -05:00
} ,
2021-09-01 20:07:11 -05:00
routeName ( ) {
return this . $route . name
2021-11-14 19:59:34 -06:00
} ,
networkConnected ( ) {
return this . $store . state . networkConnected
2021-12-04 19:56:29 -06:00
} ,
2022-04-03 14:24:17 -05:00
user ( ) {
return this . $store . state . user . user
} ,
2021-12-04 19:56:29 -06:00
currentLibraryId ( ) {
return this . $store . state . libraries . currentLibraryId
2023-06-04 17:14:26 -05:00
} ,
attemptingConnection : {
get ( ) {
return this . $store . state . attemptingConnection
} ,
set ( val ) {
this . $store . commit ( 'setAttemptingConnection' , val )
}
2021-09-01 20:07:11 -05:00
}
} ,
methods : {
initialStream ( stream ) {
2023-06-19 12:37:44 -05:00
if ( this . $refs . streamContainer ? . audioPlayerReady ) {
2021-09-01 20:07:11 -05:00
this . $refs . streamContainer . streamOpen ( stream )
}
2021-09-02 12:19:26 -05:00
} ,
2021-12-05 18:31:47 -06:00
async loadSavedSettings ( ) {
2022-12-17 14:48:56 -06:00
const userSavedServerSettings = await this . $localStore . getServerSettings ( )
2021-12-05 18:31:47 -06:00
if ( userSavedServerSettings ) {
this . $store . commit ( 'setServerSettings' , userSavedServerSettings )
}
2022-12-17 14:48:56 -06:00
await this . $store . dispatch ( 'user/loadUserSettings' )
2021-12-05 18:31:47 -06:00
} ,
2021-11-14 19:59:34 -06:00
async attemptConnection ( ) {
2022-04-09 18:36:32 -05:00
console . warn ( '[default] attemptConnection' )
2021-11-14 19:59:34 -06:00
if ( ! this . networkConnected ) {
2022-04-09 18:36:32 -05:00
console . warn ( '[default] No network connection' )
2021-11-14 19:59:34 -06:00
return
}
2022-04-07 18:46:58 -05:00
if ( this . attemptingConnection ) {
return
}
this . attemptingConnection = true
2021-11-14 19:59:34 -06:00
2023-01-08 15:32:15 -06:00
const deviceData = await this . $db . getDeviceData ( )
let serverConfig = null
if ( deviceData ) {
2023-02-26 12:54:56 -06:00
this . $store . commit ( 'globals/setHapticFeedback' , deviceData . deviceSettings ? . hapticFeedback )
2023-01-08 15:32:15 -06:00
if ( deviceData . lastServerConnectionConfigId && deviceData . serverConnectionConfigs . length ) {
serverConfig = deviceData . serverConnectionConfigs . find ( ( scc ) => scc . id == deviceData . lastServerConnectionConfigId )
}
2021-11-14 19:59:34 -06:00
}
2023-01-08 15:32:15 -06:00
2022-04-03 14:24:17 -05:00
if ( ! serverConfig ) {
// No last server config set
2022-04-07 18:46:58 -05:00
this . attemptingConnection = false
2022-04-03 14:24:17 -05:00
return
}
2022-04-07 18:46:58 -05:00
console . log ( ` [default] Got server config, attempt authorize ${ serverConfig . address } ` )
2023-01-08 15:32:15 -06:00
const authRes = await this . $axios . $post ( ` ${ serverConfig . address } /api/authorize ` , null , { headers : { Authorization : ` Bearer ${ serverConfig . token } ` } , timeout : 3000 } ) . catch ( ( error ) => {
2022-04-03 14:24:17 -05:00
console . error ( '[Server] Server auth failed' , error )
2023-01-08 15:32:15 -06:00
const errorMsg = error . response ? error . response . data || 'Unknown Error' : 'Unknown Error'
2022-04-03 14:24:17 -05:00
this . error = errorMsg
return false
} )
2022-04-07 18:46:58 -05:00
if ( ! authRes ) {
this . attemptingConnection = false
return
}
2022-04-03 14:24:17 -05:00
2022-07-28 18:36:43 -05:00
const { user , userDefaultLibraryId , serverSettings } = authRes
this . $store . commit ( 'setServerSettings' , serverSettings )
2022-04-10 20:31:47 -05:00
// Set library - Use last library if set and available fallback to default user library
2023-01-08 15:32:15 -06:00
const lastLibraryId = await this . $localStore . getLastLibraryId ( )
2022-04-10 20:31:47 -05:00
if ( lastLibraryId && ( ! user . librariesAccessible . length || user . librariesAccessible . includes ( lastLibraryId ) ) ) {
this . $store . commit ( 'libraries/setCurrentLibrary' , lastLibraryId )
} else if ( userDefaultLibraryId ) {
2022-04-03 14:24:17 -05:00
this . $store . commit ( 'libraries/setCurrentLibrary' , userDefaultLibraryId )
}
2023-01-08 15:32:15 -06:00
const serverConnectionConfig = await this . $db . setServerConnectionConfig ( serverConfig )
2022-04-03 14:24:17 -05:00
this . $store . commit ( 'user/setUser' , user )
this . $store . commit ( 'user/setServerConnectionConfig' , serverConnectionConfig )
this . $socket . connect ( serverConnectionConfig . address , serverConnectionConfig . token )
2022-04-09 18:36:32 -05:00
console . log ( '[default] Successful connection on last saved connection config' , JSON . stringify ( serverConnectionConfig ) )
2022-04-03 14:24:17 -05:00
await this . initLibraries ( )
2022-04-07 18:46:58 -05:00
this . attemptingConnection = false
2021-12-05 18:31:47 -06:00
} ,
2022-03-23 17:59:14 -05:00
itemRemoved ( libraryItem ) {
if ( this . $route . name . startsWith ( 'item' ) ) {
if ( this . $route . params . id === libraryItem . id ) {
2021-12-05 18:31:47 -06:00
this . $router . replace ( ` /bookshelf ` )
}
}
} ,
userLoggedOut ( ) {
// Only cancels stream if streamining not playing downloaded
2022-04-02 12:12:00 -05:00
this . $eventBus . $emit ( 'close-stream' )
2021-12-05 18:31:47 -06:00
} ,
2022-04-03 14:24:17 -05:00
socketConnectionFailed ( err ) {
this . $toast . error ( 'Socket connection error: ' + err . message )
} ,
async initLibraries ( ) {
2022-04-07 18:46:58 -05:00
if ( this . inittingLibraries ) {
return
}
this . inittingLibraries = true
2022-04-03 14:24:17 -05:00
await this . $store . dispatch ( 'libraries/load' )
2022-04-10 20:31:47 -05:00
console . log ( ` [default] initLibraries loaded ${ this . currentLibraryId } ` )
2022-06-05 16:32:28 -05:00
await this . $store . dispatch ( 'libraries/fetch' , this . currentLibraryId )
2022-04-03 14:24:17 -05:00
this . $eventBus . $emit ( 'library-changed' )
2022-04-07 18:46:58 -05:00
this . inittingLibraries = false
2022-04-09 18:36:32 -05:00
} ,
2023-02-05 16:54:46 -06:00
async syncLocalSessions ( ) {
if ( ! this . user ) {
console . log ( '[default] No need to sync local sessions - not connected to server' )
return
}
console . log ( '[default] Calling syncLocalSessions' )
const response = await this . $db . syncLocalSessionsWithServer ( )
if ( response && response . error ) {
console . error ( '[default] Failed to sync local sessions' , response . error )
} else {
console . log ( '[default] Successfully synced local sessions' )
}
} ,
2022-04-09 18:36:32 -05:00
async syncLocalMediaProgress ( ) {
if ( ! this . user ) {
console . log ( '[default] No need to sync local media progress - not connected to server' )
return
}
console . log ( '[default] Calling syncLocalMediaProgress' )
2022-12-11 12:01:54 -06:00
const response = await this . $db . syncLocalMediaProgressWithServer ( )
2022-04-09 18:36:32 -05:00
if ( ! response ) {
2022-04-10 20:31:47 -05:00
if ( this . $platform != 'web' ) this . $toast . error ( 'Failed to sync local media with server' )
2022-04-09 18:36:32 -05:00
return
}
2023-01-28 14:20:00 -06:00
const { numLocalMediaProgressForServer , numServerProgressUpdates , numLocalProgressUpdates , serverProgressUpdates } = response
2022-04-09 18:36:32 -05:00
if ( numLocalMediaProgressForServer > 0 ) {
2023-01-28 14:20:00 -06:00
if ( serverProgressUpdates && serverProgressUpdates . length ) {
serverProgressUpdates . forEach ( ( progress ) => {
console . log ( ` [default] Server progress was updated ${ progress . id } ` )
this . $store . commit ( 'user/updateUserMediaProgress' , progress )
} )
}
2022-04-09 18:36:32 -05:00
if ( numServerProgressUpdates > 0 || numLocalProgressUpdates > 0 ) {
console . log ( ` [default] ${ numServerProgressUpdates } Server progress updates | ${ numLocalProgressUpdates } Local progress updates ` )
} else {
console . log ( '[default] syncLocalMediaProgress No updates were necessary' )
}
} else {
console . log ( '[default] syncLocalMediaProgress No local media progress to sync' )
}
2022-04-09 20:29:59 -05:00
} ,
2023-01-28 14:20:00 -06:00
userUpdated ( user ) {
console . log ( '[default] userUpdated:' , JSON . stringify ( user ) )
2022-04-09 20:29:59 -05:00
if ( this . user && this . user . id == user . id ) {
this . $store . commit ( 'user/setUser' , user )
}
2022-06-01 19:38:26 -05:00
} ,
2023-06-19 12:37:44 -05:00
async userMediaProgressUpdated ( payload ) {
const prog = payload . data // MediaProgress
console . log ( ` [default] userMediaProgressUpdate checking for local media progress ${ payload . id } ` )
// Check if this media item is currently open in the player, paused, and this progress update is coming from a different session
const isMediaOpenInPlayer = this . $store . getters [ 'getIsMediaStreaming' ] ( prog . libraryItemId , prog . episodeId )
if ( isMediaOpenInPlayer && this . $store . getters [ 'getCurrentPlaybackSessionId' ] !== payload . sessionId && ! this . $store . state . playerIsPlaying ) {
console . log ( '[default] userMediaProgressUpdated for current open media item' , payload . data . currentTime )
this . $eventBus . $emit ( 'playback-time-update' , payload . data . currentTime )
}
2022-06-01 19:38:26 -05:00
// Update local media progress if exists
2023-01-15 14:58:26 -06:00
const localProg = await this . $db . getLocalMediaProgressForServerItem ( { libraryItemId : prog . libraryItemId , episodeId : prog . episodeId } )
2023-01-28 14:20:00 -06:00
let newLocalMediaProgress = null
2022-06-01 19:38:26 -05:00
if ( localProg && localProg . lastUpdate < prog . lastUpdate ) {
2023-01-15 14:58:26 -06:00
if ( localProg . currentTime == prog . currentTime && localProg . isFinished == prog . isFinished ) {
console . log ( '[default] syncing progress server lastUpdate > local lastUpdate but currentTime and isFinished is equal' )
return
} else {
console . log ( ` [default] syncing progress server lastUpdate > local lastUpdate. server currentTime= ${ prog . currentTime } local currentTime= ${ localProg . currentTime } | server/local isFinished= ${ prog . isFinished } / ${ localProg . isFinished } ` )
}
2022-06-01 19:38:26 -05:00
// Server progress is more up-to-date
2023-01-15 14:58:26 -06:00
console . log ( ` [default] syncing progress from server with local item for " ${ prog . libraryItemId } " ${ prog . episodeId ? ` episode ${ prog . episodeId } ` : '' } | server lastUpdate= ${ prog . lastUpdate } > local lastUpdate= ${ localProg . lastUpdate } ` )
2022-06-01 19:38:26 -05:00
const payload = {
localMediaProgressId : localProg . id ,
mediaProgress : prog
}
newLocalMediaProgress = await this . $db . syncServerMediaProgressWithLocalMediaProgress ( payload )
2022-07-13 17:10:47 -05:00
} else if ( ! localProg ) {
2022-06-01 19:38:26 -05:00
// Check if local library item exists
2022-07-13 17:10:47 -05:00
// local media progress may not exist yet if it hasn't been played
2023-01-28 14:20:00 -06:00
const localLibraryItem = await this . $db . getLocalLibraryItemByLId ( prog . libraryItemId )
2022-06-01 19:38:26 -05:00
if ( localLibraryItem ) {
if ( prog . episodeId ) {
// If episode check if local episode exists
2023-01-28 14:20:00 -06:00
const lliEpisodes = localLibraryItem . media . episodes || [ ]
const localEpisode = lliEpisodes . find ( ( ep ) => ep . serverEpisodeId === prog . episodeId )
2022-06-01 19:38:26 -05:00
if ( localEpisode ) {
// Add new local media progress
const payload = {
localLibraryItemId : localLibraryItem . id ,
localEpisodeId : localEpisode . id ,
mediaProgress : prog
}
newLocalMediaProgress = await this . $db . syncServerMediaProgressWithLocalMediaProgress ( payload )
}
} else {
// Add new local media progress
const payload = {
localLibraryItemId : localLibraryItem . id ,
mediaProgress : prog
}
newLocalMediaProgress = await this . $db . syncServerMediaProgressWithLocalMediaProgress ( payload )
}
} else {
console . log ( ` [default] userMediaProgressUpdate no local media progress or lli found for this server item ${ prog . id } ` )
}
}
2023-06-19 12:37:44 -05:00
if ( newLocalMediaProgress ? . id ) {
2022-06-01 19:38:26 -05:00
console . log ( ` [default] local media progress updated for ${ newLocalMediaProgress . id } ` )
this . $store . commit ( 'globals/updateLocalMediaProgress' , newLocalMediaProgress )
}
2021-09-12 18:37:08 -05:00
}
2021-09-01 20:07:11 -05:00
} ,
2021-11-14 19:59:34 -06:00
async mounted ( ) {
2022-04-09 20:29:59 -05:00
this . $socket . on ( 'user_updated' , this . userUpdated )
2022-06-01 19:38:26 -05:00
this . $socket . on ( 'user_media_progress_updated' , this . userMediaProgressUpdated )
2021-09-01 20:07:11 -05:00
2021-09-19 18:44:10 -05:00
if ( this . $store . state . isFirstLoad ) {
this . $store . commit ( 'setIsFirstLoad' , false )
2022-07-01 20:05:11 -05:00
const deviceData = await this . $db . getDeviceData ( )
this . $store . commit ( 'setDeviceData' , deviceData )
2023-06-20 16:29:56 -05:00
2022-12-04 10:41:09 -06:00
this . $setOrientationLock ( this . $store . getters [ 'getOrientationLockSetting' ] )
2022-07-01 20:05:11 -05:00
2021-11-21 06:54:10 -06:00
await this . $store . dispatch ( 'setupNetworkListener' )
2022-04-03 14:24:17 -05:00
2022-07-22 17:00:49 -05:00
await this . $store . dispatch ( 'globals/loadLocalMediaProgress' )
2022-04-03 14:24:17 -05:00
if ( this . $store . state . user . serverConnectionConfig ) {
await this . initLibraries ( )
} else {
await this . attemptConnection ( )
}
2022-04-09 18:36:32 -05:00
console . log ( ` [default] finished connection attempt or already connected ${ ! ! this . user } ` )
2023-02-05 16:54:46 -06:00
if ( this . $platform === 'ios' ) {
// TODO: Update ios to not use this
await this . syncLocalMediaProgress ( )
} else {
await this . syncLocalSessions ( )
}
2022-07-22 17:00:49 -05:00
2021-12-05 18:31:47 -06:00
this . loadSavedSettings ( )
2022-04-09 18:36:32 -05:00
this . hasMounted = true
2022-08-19 16:36:56 -04:00
console . log ( '[default] fully initialized' )
this . $eventBus . $emit ( 'abs-ui-ready' )
2021-09-19 18:44:10 -05:00
}
} ,
beforeDestroy ( ) {
2022-04-09 20:29:59 -05:00
this . $socket . off ( 'user_updated' , this . userUpdated )
2022-06-01 19:38:26 -05:00
this . $socket . off ( 'user_media_progress_updated' , this . userMediaProgressUpdated )
2021-09-01 20:07:11 -05:00
}
}
< / script >