Remove old unused sessions

This commit is contained in:
Ivan Grachyov 2021-07-11 17:38:24 +03:00
parent c7c60a5d1e
commit 758a38faeb

View File

@ -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 {
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)
}