From 0da6fca727bcab82022d3a00977d2f1d2e7041a8 Mon Sep 17 00:00:00 2001 From: advplyr Date: Mon, 5 Feb 2024 19:04:11 -0600 Subject: [PATCH] Fix:iOS 0 second listening sessions #970 and include device info with offline sessions --- ios/App/App/plugins/AbsDatabase.swift | 5 +++-- ios/App/Shared/util/ApiClient.swift | 11 +++++++---- layouts/default.vue | 8 ++++---- plugins/capacitor/AbsDatabase.js | 2 +- plugins/db.js | 4 ++-- 5 files changed, 17 insertions(+), 13 deletions(-) diff --git a/ios/App/App/plugins/AbsDatabase.swift b/ios/App/App/plugins/AbsDatabase.swift index fcfd9c63..44f542db 100644 --- a/ios/App/App/plugins/AbsDatabase.swift +++ b/ios/App/App/plugins/AbsDatabase.swift @@ -147,14 +147,15 @@ public class AbsDatabase: CAPPlugin { } @objc func syncLocalSessionsWithServer(_ call: CAPPluginCall) { - logger.log("syncLocalSessionsWithServer: Starting") + let isFirstSync = call.getBool("isFirstSync", false) + logger.log("syncLocalSessionsWithServer: Starting (First sync: \(isFirstSync))") guard Store.serverConfig != nil else { call.reject("syncLocalSessionsWithServer not connected to server") return call.resolve() } Task { - await ApiClient.syncLocalSessionsWithServer() + await ApiClient.syncLocalSessionsWithServer(isFirstSync: isFirstSync) call.resolve() } } diff --git a/ios/App/Shared/util/ApiClient.swift b/ios/App/Shared/util/ApiClient.swift index bb18d3fa..37e831e1 100644 --- a/ios/App/Shared/util/ApiClient.swift +++ b/ios/App/Shared/util/ApiClient.swift @@ -190,10 +190,10 @@ class ApiClient { } public static func reportAllLocalPlaybackSessions(_ sessions: [PlaybackSession]) async -> Bool { - return await postResource(endpoint: "api/session/local-all", parameters: LocalPlaybackSessionSyncAllPayload(sessions: sessions)) + return await postResource(endpoint: "api/session/local-all", parameters: LocalPlaybackSessionSyncAllPayload(sessions: sessions, deviceInfo: sessions.first?.deviceInfo)) } - public static func syncLocalSessionsWithServer() async { + public static func syncLocalSessionsWithServer(isFirstSync: Bool) async { do { // Sync server progress with local media progress let localMediaProgressList = Database.shared.getAllLocalMediaProgress().filter { @@ -232,13 +232,15 @@ class ApiClient { let playbackSessions = Database.shared.getAllPlaybackSessions().filter { $0.serverConnectionConfigId == Store.serverConfig?.id }.map { $0.freeze() } - logger.log("syncLocalSessionsWithServer: Found \(playbackSessions.count) playback sessions for server") + logger.log("syncLocalSessionsWithServer: Found \(playbackSessions.count) playback sessions for server (first sync: \(isFirstSync))") if (!playbackSessions.isEmpty) { let success = await ApiClient.reportAllLocalPlaybackSessions(playbackSessions) if (success) { // Remove sessions from db try playbackSessions.forEach { session in - if (!session.isActiveSession) { + logger.log("syncLocalSessionsWithServer: Handling \(session.displayTitle ?? "") (\(session.id)) \(session.isActiveSession)") + // On first sync then remove all sessions + if (!session.isActiveSession || isFirstSync) { if let session = session.thaw() { try session.delete() } @@ -327,6 +329,7 @@ struct LocalMediaProgressSyncResultsPayload: Codable { struct LocalPlaybackSessionSyncAllPayload: Codable { var sessions: [PlaybackSession] + var deviceInfo: [String: String?]? } struct Connectivity { diff --git a/layouts/default.vue b/layouts/default.vue index fb43779b..243aac92 100644 --- a/layouts/default.vue +++ b/layouts/default.vue @@ -45,7 +45,7 @@ export default { if (timeSinceDisconnect > 5000) { console.log('Time since disconnect was', timeSinceDisconnect, 'sync with server') setTimeout(() => { - this.syncLocalSessions() + this.syncLocalSessions(false) }, 4000) } } @@ -195,14 +195,14 @@ export default { this.$eventBus.$emit('library-changed') this.inittingLibraries = false }, - async syncLocalSessions() { + async syncLocalSessions(isFirstSync) { 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() + const response = await this.$db.syncLocalSessionsWithServer(isFirstSync) if (response?.error) { console.error('[default] Failed to sync local sessions', response.error) } else { @@ -335,7 +335,7 @@ export default { } console.log(`[default] finished connection attempt or already connected ${!!this.user}`) - await this.syncLocalSessions() + await this.syncLocalSessions(true) this.hasMounted = true diff --git a/plugins/capacitor/AbsDatabase.js b/plugins/capacitor/AbsDatabase.js index 42ab8701..fc34a6fa 100644 --- a/plugins/capacitor/AbsDatabase.js +++ b/plugins/capacitor/AbsDatabase.js @@ -196,7 +196,7 @@ class AbsDatabaseWeb extends WebPlugin { return null } - async syncLocalSessionsWithServer() { + async syncLocalSessionsWithServer({ isFirstSync }) { return null } diff --git a/plugins/db.js b/plugins/db.js index 1ea7a65e..8819434f 100644 --- a/plugins/db.js +++ b/plugins/db.js @@ -70,8 +70,8 @@ class DbService { return AbsDatabase.removeLocalMediaProgress({ localMediaProgressId }) } - syncLocalSessionsWithServer() { - return AbsDatabase.syncLocalSessionsWithServer() + syncLocalSessionsWithServer(isFirstSync) { + return AbsDatabase.syncLocalSessionsWithServer({ isFirstSync }) } syncServerMediaProgressWithLocalMediaProgress(payload) {