diff --git a/Encrypted Ink/Services/SessionStorage.swift b/Encrypted Ink/Services/SessionStorage.swift index 6256b368..1b2c4ae4 100644 --- a/Encrypted Ink/Services/SessionStorage.swift +++ b/Encrypted Ink/Services/SessionStorage.swift @@ -17,11 +17,25 @@ class SessionStorage { private init() {} func loadAll() -> [Item] { - let items = Array(Defaults.storedSessions.values) + var items = Array(Defaults.storedSessions.values) let wcItems = WCSessionStore.allSessions - for item in items where wcItems[item.session.topic] == nil { - WCSessionStore.store(item.session, peerId: item.sessionDetails.peerId, peerMeta: item.sessionDetails.peerMeta) + let latestInteractionDates = Defaults.latestInteractionDates + let now = Date() + let oldnessThreshold: Double = 60 * 60 * 24 * 60 // 60 days + + items = items.filter { item -> Bool in + guard let date = latestInteractionDates[item.clientId], now.timeIntervalSince(date) < oldnessThreshold else { + remove(clientId: item.clientId) + return false + } + + if wcItems[item.session.topic] == nil { + WCSessionStore.store(item.session, peerId: item.sessionDetails.peerId, peerMeta: item.sessionDetails.peerMeta) + } + + return true } + return items } @@ -30,6 +44,7 @@ class SessionStorage { } func remove(clientId: String) { + Defaults.latestInteractionDates.removeValue(forKey: clientId) if let item = Defaults.storedSessions.removeValue(forKey: clientId) { WCSessionStore.clear(item.session.topic) }