Avoid potential app side NSE UserSession races and let the SDK handle them instead

This commit is contained in:
Stefan Ceriu
2024-04-15 14:48:14 +03:00
parent 493763b17e
commit 5faa4910b1
+4 -10
View File
@@ -44,7 +44,6 @@ private let settings: NSESettingsProtocol = AppSettings()
private let notificationContentBuilder = NotificationContentBuilder(messageEventStringBuilder: RoomMessageEventStringBuilder(attributedStringBuilder: AttributedStringBuilder(mentionBuilder: PlainMentionBuilder()))) private let notificationContentBuilder = NotificationContentBuilder(messageEventStringBuilder: RoomMessageEventStringBuilder(attributedStringBuilder: AttributedStringBuilder(mentionBuilder: PlainMentionBuilder())))
private let keychainController = KeychainController(service: .sessions, private let keychainController = KeychainController(service: .sessions,
accessGroup: InfoPlistReader.main.keychainAccessGroupIdentifier) accessGroup: InfoPlistReader.main.keychainAccessGroupIdentifier)
private var userSessions = [String: NSEUserSession]()
class NotificationServiceExtension: UNNotificationServiceExtension { class NotificationServiceExtension: UNNotificationServiceExtension {
private var handler: ((UNNotificationContent) -> Void)? private var handler: ((UNNotificationContent) -> Void)?
@@ -70,9 +69,8 @@ class NotificationServiceExtension: UNNotificationServiceExtension {
NSELogger.configure(logLevel: settings.logLevel) NSELogger.configure(logLevel: settings.logLevel)
NSELogger.logMemory(with: tag)
MXLog.info("\(tag) #########################################") MXLog.info("\(tag) #########################################")
NSELogger.logMemory(with: tag)
MXLog.info("\(tag) Payload came: \(request.content.userInfo)") MXLog.info("\(tag) Payload came: \(request.content.userInfo)")
Task { Task {
@@ -97,13 +95,9 @@ class NotificationServiceExtension: UNNotificationServiceExtension {
MXLog.info("\(tag) run with roomId: \(roomId), eventId: \(eventId)") MXLog.info("\(tag) run with roomId: \(roomId), eventId: \(eventId)")
do { do {
let userSession: NSEUserSession // This function might be run concurrently and from different processes, let the SDK handle race conditions
if let existingSession = userSessions[credentials.userID] { // on fetching user sessions
userSession = existingSession let userSession = try await NSEUserSession(credentials: credentials, clientSessionDelegate: keychainController)
} else {
userSession = try await NSEUserSession(credentials: credentials, clientSessionDelegate: keychainController)
userSessions[credentials.userID] = userSession
}
guard let itemProxy = await userSession.notificationItemProxy(roomID: roomId, eventID: eventId) else { guard let itemProxy = await userSession.notificationItemProxy(roomID: roomId, eventID: eventId) else {
MXLog.info("\(tag) no notification for the event, discard") MXLog.info("\(tag) no notification for the event, discard")