diff --git a/nirvana-ios/Services/AuthSessionStore.swift b/nirvana-ios/Services/AuthSessionStore.swift index 694ec95..fcb269f 100644 --- a/nirvana-ios/Services/AuthSessionStore.swift +++ b/nirvana-ios/Services/AuthSessionStore.swift @@ -276,7 +276,7 @@ extension AuthSessionStore { continue } - // create a listener for this specific user + // create a listener for this specific friend if they are an active friend let currFriendListener = self.db.collection("users").document(userFriend!.friendId) .addSnapshotListener {documentSnapshot, error in guard let document = documentSnapshot else { diff --git a/nirvana-ios/Views/Convos/ConvoViewModel.swift b/nirvana-ios/Views/Convos/ConvoViewModel.swift index f92acb3..cdd41be 100644 --- a/nirvana-ios/Views/Convos/ConvoViewModel.swift +++ b/nirvana-ios/Views/Convos/ConvoViewModel.swift @@ -389,26 +389,10 @@ class ConvoViewModel: NSObject, ObservableObject { return } - var updatedConvo = convo - //update convo in database to show that I left...if I am the last person, also close out the channel + // handle edge cases + let updatedConvo = Self.updatedConvoOnLeaving(convo: convo!, userId: userId!) - // if I am the last one in the convo, then end the convo in the db - if updatedConvo!.users.count == 1 && updatedConvo!.users[0] == userId { - updatedConvo!.state = .complete - updatedConvo!.endedTimestamp = Date() - } // if I am second to last, the last guy is a loner and activate him to get out - else if updatedConvo!.users.count == 2 && updatedConvo!.users.contains(userId!) { - updatedConvo!.secondToLastUserEndedTimestamp = Date() - } - - // remove me from the users array to omit me from the convo - let updatedUsers: [String] = updatedConvo!.users.filter{ arrUserId in - return arrUserId != userId - } - - updatedConvo!.users = updatedUsers - - self?.firestoreService.updateConvo(convo: updatedConvo!) {[weak self] res in + self?.firestoreService.updateConvo(convo: updatedConvo) {[weak self] res in self?.selectedConvoId = nil print("left convo officially in our db as well") @@ -422,6 +406,75 @@ class ConvoViewModel: NSObject, ObservableObject { } } + /** force leave for when the user quits a session and is in a call */ + static func leaveAnyConvo() { + print("leaving any convos user may be in") + + let userId = AuthSessionStore.getCurrentUserId() + + if userId == nil { + print("not authenticated...can't leave any channels") + return + } + + let firestoreService = FirestoreService() + var db = Firestore.firestore() + + // get all live convos where I am in the users array + db.collection("convos").whereField("state", isEqualTo: ConvoState.active.rawValue).whereField("users", arrayContains: userId) + .getDocuments() {(querySnapshot, err) in + if let err = err { + print("Error getting user's active friends: \(err)") + return + } + + // for each...prolly just one...remove me from the list of users + for document in querySnapshot!.documents { + var convo:Convo? = try? document.data(as: Convo.self) + + if convo == nil { + continue + } + + print(convo) + + // handle edge cases + let updatedConvo = self.updatedConvoOnLeaving(convo: convo!, userId: userId!) + + // send update back to firestore + firestoreService.updateConvo(convo: updatedConvo) {res in + print(res) + + AgoraRtcEngineKit.destroy() + } + + } + } + } + + static func updatedConvoOnLeaving(convo: Convo, userId: String) -> Convo { + var updatedConvo = convo + //update convo in database to show that I left...if I am the last person, also close out the channel + + // if I am the last one in the convo, then end the convo in the db + if updatedConvo.users.count == 1 && updatedConvo.users[0] == userId { + updatedConvo.state = .complete + updatedConvo.endedTimestamp = Date() + } // if I am second to last, the last guy is a loner and activate him to get out + else if updatedConvo.users.count == 2 && updatedConvo.users.contains(userId) { + updatedConvo.secondToLastUserEndedTimestamp = Date() + } + + // remove me from the users array to omit me from the convo + let updatedUsers: [String] = updatedConvo.users.filter{ arrUserId in + return arrUserId != userId + } + + updatedConvo.users = updatedUsers + + return updatedConvo + } + func destroyInstance() { AgoraRtcEngineKit.destroy() } diff --git a/nirvana-ios/nirvana_iosApp.swift b/nirvana-ios/nirvana_iosApp.swift index 45177c3..a6bf707 100644 --- a/nirvana-ios/nirvana_iosApp.swift +++ b/nirvana-ios/nirvana_iosApp.swift @@ -143,6 +143,15 @@ class AppDelegate: NSObject, UIApplicationDelegate { func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) { print("failed to register for remote notifications with error: \(error)") } + + /** when user kills the app process completely if it is in the background */ + func applicationWillTerminate(_ application: UIApplication) { + // TODO: get user off of all live convos + + ConvoViewModel.leaveAnyConvo() + + print("user killed app, leaving convos if in any") + } } // cloud messaging