From 758a38faeb28b870eee2c6762c589c0b92e3dd66 Mon Sep 17 00:00:00 2001 From: Ivan Grachyov Date: Sun, 11 Jul 2021 17:38:24 +0300 Subject: [PATCH] Remove old unused sessions --- Encrypted Ink/Services/SessionStorage.swift | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) 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) }