2021-11-03 17:02:45 +01:00
|
|
|
import DictionaryCoding
|
2024-02-09 09:56:41 +01:00
|
|
|
import Foundation
|
2021-09-17 11:56:43 +02:00
|
|
|
|
2024-01-31 14:16:40 +01:00
|
|
|
private let SSE_INFO_KEY = "sseInfo"
|
|
|
|
private let ALARMS_KEY = "repeatingAlarmNotification"
|
|
|
|
private let LAST_PROCESSED_NOTIFICAION_ID_KEY = "lastProcessedNotificationId"
|
|
|
|
private let LAST_MISSED_NOTIFICATION_CHECK_TIME = "lastMissedNotificationCheckTime"
|
2024-03-11 16:25:43 +01:00
|
|
|
private let EXTENDED_NOTIFICATION_MODE = "extendedNotificationMode"
|
2021-09-17 11:56:43 +02:00
|
|
|
|
2024-03-11 16:25:43 +01:00
|
|
|
public class NotificationStorage {
|
2024-02-09 09:56:41 +01:00
|
|
|
private let userPreferencesProvider: UserPreferencesProvider
|
2024-01-31 14:16:40 +01:00
|
|
|
|
2024-03-11 16:25:43 +01:00
|
|
|
public init(userPreferencesProvider: UserPreferencesProvider) { self.userPreferencesProvider = userPreferencesProvider }
|
2024-01-31 14:16:40 +01:00
|
|
|
|
2024-03-11 16:25:43 +01:00
|
|
|
public var sseInfo: SSEInfo? {
|
2024-02-09 09:56:41 +01:00
|
|
|
get {
|
|
|
|
let dict = self.userPreferencesProvider.getObject(forKey: SSE_INFO_KEY)
|
|
|
|
return dict.map { try! DictionaryDecoder().decode(SSEInfo.self, from: $0 as! NSDictionary) }
|
|
|
|
}
|
|
|
|
}
|
2024-01-31 14:16:40 +01:00
|
|
|
|
2024-03-11 16:25:43 +01:00
|
|
|
public func store(pushIdentifier: String, userId: String, sseOrigin: String) throws {
|
2024-08-16 10:59:34 +02:00
|
|
|
// Provide right defaults for extended notification mode.
|
|
|
|
// - Start with "nothing" as a conservative default
|
|
|
|
// - If notifications were not used before, enable extended notifications
|
2024-02-09 09:56:41 +01:00
|
|
|
if var sseInfo = self.sseInfo {
|
|
|
|
sseInfo.pushIdentifier = pushIdentifier
|
|
|
|
sseInfo.sseOrigin = sseOrigin
|
|
|
|
var userIds = sseInfo.userIds
|
2024-03-11 16:25:43 +01:00
|
|
|
if !userIds.contains(userId) {
|
|
|
|
userIds.append(userId)
|
2024-08-16 10:59:34 +02:00
|
|
|
try self.setExtendedNotificationConfig(userId, .sender_and_subject)
|
2024-03-11 16:25:43 +01:00
|
|
|
}
|
2024-02-09 09:56:41 +01:00
|
|
|
sseInfo.userIds = userIds
|
|
|
|
self.put(sseInfo: sseInfo)
|
|
|
|
} else {
|
|
|
|
let sseInfo = SSEInfo(pushIdentifier: pushIdentifier, sseOrigin: sseOrigin, userIds: [userId])
|
2024-08-16 10:59:34 +02:00
|
|
|
try self.setExtendedNotificationConfig(userId, .sender_and_subject)
|
2024-02-09 09:56:41 +01:00
|
|
|
self.put(sseInfo: sseInfo)
|
|
|
|
}
|
|
|
|
}
|
2024-01-31 14:16:40 +01:00
|
|
|
|
2024-03-11 16:25:43 +01:00
|
|
|
public func store(alarms: [EncryptedAlarmNotification]) {
|
2024-02-09 09:56:41 +01:00
|
|
|
let jsonData = try! JSONEncoder().encode(alarms)
|
|
|
|
self.userPreferencesProvider.setValue(jsonData, forKey: ALARMS_KEY)
|
|
|
|
}
|
2024-01-31 14:16:40 +01:00
|
|
|
|
2024-03-11 16:25:43 +01:00
|
|
|
public var alarms: [EncryptedAlarmNotification] {
|
2024-02-09 09:56:41 +01:00
|
|
|
get {
|
|
|
|
let notificationsJsonData = self.userPreferencesProvider.getObject(forKey: ALARMS_KEY)
|
|
|
|
if let notificationsJsonData {
|
|
|
|
return try! JSONDecoder().decode(Array<EncryptedAlarmNotification>.self, from: notificationsJsonData as! Data)
|
|
|
|
} else {
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-01-31 14:16:40 +01:00
|
|
|
|
2024-03-11 16:25:43 +01:00
|
|
|
public func removeUser(_ userId: String) {
|
2024-02-09 09:56:41 +01:00
|
|
|
guard var sseInfo = self.sseInfo else {
|
|
|
|
TUTSLog("Removing userId but there's no SSEInfo stored")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var userIds = sseInfo.userIds
|
|
|
|
if let index = userIds.firstIndex(of: userId) { userIds.remove(at: index) }
|
|
|
|
sseInfo.userIds = userIds
|
|
|
|
self.put(sseInfo: sseInfo)
|
|
|
|
}
|
2024-01-31 14:16:40 +01:00
|
|
|
|
2024-03-11 16:25:43 +01:00
|
|
|
public var lastProcessedNotificationId: String? {
|
2024-02-09 09:56:41 +01:00
|
|
|
get { self.userPreferencesProvider.getObject(forKey: LAST_PROCESSED_NOTIFICAION_ID_KEY) as! String? }
|
|
|
|
set { return self.userPreferencesProvider.setValue(newValue, forKey: LAST_PROCESSED_NOTIFICAION_ID_KEY) }
|
|
|
|
}
|
2024-01-31 14:16:40 +01:00
|
|
|
|
2024-03-11 16:25:43 +01:00
|
|
|
public var lastMissedNotificationCheckTime: Date? {
|
2024-02-09 09:56:41 +01:00
|
|
|
get { self.userPreferencesProvider.getObject(forKey: LAST_MISSED_NOTIFICATION_CHECK_TIME) as! Date? }
|
|
|
|
set { return self.userPreferencesProvider.setValue(newValue, forKey: LAST_MISSED_NOTIFICATION_CHECK_TIME) }
|
|
|
|
}
|
2024-01-31 14:16:40 +01:00
|
|
|
|
2024-03-11 16:25:43 +01:00
|
|
|
public func clear() {
|
2024-02-09 09:56:41 +01:00
|
|
|
TUTSLog("UserPreference clear")
|
|
|
|
let sseInfo = self.sseInfo
|
|
|
|
if var sseInfo {
|
|
|
|
sseInfo.userIds = []
|
|
|
|
self.put(sseInfo: nil)
|
|
|
|
self.lastMissedNotificationCheckTime = nil
|
|
|
|
self.store(alarms: [])
|
|
|
|
}
|
|
|
|
}
|
2024-01-31 14:16:40 +01:00
|
|
|
|
2024-03-11 16:25:43 +01:00
|
|
|
public func setExtendedNotificationConfig(_ userId: String, _ mode: TutanotaSharedFramework.ExtendedNotificationMode) throws {
|
|
|
|
self.userPreferencesProvider.setValue(mode.rawValue, forKey: "\(EXTENDED_NOTIFICATION_MODE):\(userId)")
|
|
|
|
}
|
|
|
|
|
|
|
|
public func getExtendedNotificationConfig(_ userId: String) throws -> TutanotaSharedFramework.ExtendedNotificationMode {
|
2024-08-16 10:59:34 +02:00
|
|
|
// This default gets overwritten later when we store the pushIdentifier
|
2024-03-11 16:25:43 +01:00
|
|
|
self.userPreferencesProvider.getObject(forKey: "\(EXTENDED_NOTIFICATION_MODE):\(userId)")
|
2024-07-19 18:29:17 +02:00
|
|
|
.map { mode in ExtendedNotificationMode(rawValue: mode as! String)! } ?? .sender_and_subject
|
2024-03-11 16:25:43 +01:00
|
|
|
}
|
|
|
|
|
2024-02-09 09:56:41 +01:00
|
|
|
private func put(sseInfo: SSEInfo?) {
|
|
|
|
if let sseInfo {
|
|
|
|
let dict: NSDictionary = try! DictionaryEncoder().encode(sseInfo)
|
|
|
|
self.userPreferencesProvider.setValue(dict, forKey: SSE_INFO_KEY)
|
|
|
|
} else {
|
|
|
|
self.userPreferencesProvider.setValue(nil, forKey: SSE_INFO_KEY)
|
|
|
|
}
|
|
|
|
}
|
2021-09-17 11:56:43 +02:00
|
|
|
}
|