mirror of https://github.com/oxen-io/session-ios
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
125 lines
5.3 KiB
Swift
125 lines
5.3 KiB
Swift
4 years ago
|
import SessionUtilitiesKit
|
||
5 years ago
|
|
||
5 years ago
|
extension Storage {
|
||
5 years ago
|
|
||
|
// MARK: - Snode Pool
|
||
5 years ago
|
|
||
|
private static let snodePoolCollection = "LokiSnodePoolCollection"
|
||
5 years ago
|
|
||
5 years ago
|
public func getSnodePool() -> Set<Snode> {
|
||
|
var result: Set<Snode> = []
|
||
|
Storage.read { transaction in
|
||
|
transaction.enumerateKeysAndObjects(inCollection: Storage.snodePoolCollection) { _, object, _ in
|
||
|
guard let snode = object as? Snode else { return }
|
||
|
result.insert(snode)
|
||
|
}
|
||
|
}
|
||
|
return result
|
||
|
}
|
||
|
|
||
|
public func setSnodePool(to snodePool: Set<Snode>, using transaction: Any) {
|
||
|
clearSnodePool(in: transaction)
|
||
|
snodePool.forEach { snode in
|
||
|
(transaction as! YapDatabaseReadWriteTransaction).setObject(snode, forKey: snode.description, inCollection: Storage.snodePoolCollection)
|
||
|
}
|
||
|
}
|
||
|
|
||
5 years ago
|
public func clearSnodePool(in transaction: Any) {
|
||
5 years ago
|
(transaction as! YapDatabaseReadWriteTransaction).removeAllObjects(inCollection: Storage.snodePoolCollection)
|
||
|
}
|
||
|
|
||
5 years ago
|
|
||
|
|
||
|
// MARK: - Swarm
|
||
5 years ago
|
|
||
|
private static func getSwarmCollection(for publicKey: String) -> String {
|
||
|
return "LokiSwarmCollection-\(publicKey)"
|
||
|
}
|
||
5 years ago
|
|
||
5 years ago
|
public func getSwarm(for publicKey: String) -> Set<Snode> {
|
||
|
var result: Set<Snode> = []
|
||
|
let collection = Storage.getSwarmCollection(for: publicKey)
|
||
|
Storage.read { transaction in
|
||
|
transaction.enumerateKeysAndObjects(inCollection: collection) { _, object, _ in
|
||
|
guard let snode = object as? Snode else { return }
|
||
|
result.insert(snode)
|
||
|
}
|
||
|
}
|
||
|
return result
|
||
|
}
|
||
|
|
||
|
public func setSwarm(to swarm: Set<Snode>, for publicKey: String, using transaction: Any) {
|
||
|
clearSwarm(for: publicKey, in: transaction)
|
||
|
let collection = Storage.getSwarmCollection(for: publicKey)
|
||
|
swarm.forEach { snode in
|
||
|
(transaction as! YapDatabaseReadWriteTransaction).setObject(snode, forKey: snode.description, inCollection: collection)
|
||
|
}
|
||
|
}
|
||
|
|
||
5 years ago
|
public func clearSwarm(for publicKey: String, in transaction: Any) {
|
||
5 years ago
|
let collection = Storage.getSwarmCollection(for: publicKey)
|
||
|
(transaction as! YapDatabaseReadWriteTransaction).removeAllObjects(inCollection: collection)
|
||
|
}
|
||
|
|
||
5 years ago
|
|
||
|
|
||
|
// MARK: - Last Message Hash
|
||
|
|
||
5 years ago
|
private static let lastMessageHashCollection = "LokiLastMessageHashCollection"
|
||
|
|
||
5 years ago
|
public func getLastMessageHashInfo(for snode: Snode, associatedWith publicKey: String) -> JSON? {
|
||
5 years ago
|
let key = "\(snode.address):\(snode.port).\(publicKey)"
|
||
|
var result: JSON?
|
||
|
Storage.read { transaction in
|
||
|
result = transaction.object(forKey: key, inCollection: Storage.lastMessageHashCollection) as? JSON
|
||
|
}
|
||
|
if let result = result {
|
||
|
guard result["hash"] as? String != nil else { return nil }
|
||
|
guard result["expirationDate"] as? NSNumber != nil else { return nil }
|
||
|
}
|
||
|
return result
|
||
|
}
|
||
|
|
||
|
public func getLastMessageHash(for snode: Snode, associatedWith publicKey: String) -> String? {
|
||
|
return getLastMessageHashInfo(for: snode, associatedWith: publicKey)?["hash"] as? String
|
||
|
}
|
||
|
|
||
|
public func setLastMessageHashInfo(for snode: Snode, associatedWith publicKey: String, to lastMessageHashInfo: JSON, using transaction: Any) {
|
||
|
let key = "\(snode.address):\(snode.port).\(publicKey)"
|
||
|
guard lastMessageHashInfo.count == 2 && lastMessageHashInfo["hash"] as? String != nil && lastMessageHashInfo["expirationDate"] as? NSNumber != nil else { return }
|
||
|
(transaction as! YapDatabaseReadWriteTransaction).setObject(lastMessageHashInfo, forKey: key, inCollection: Storage.lastMessageHashCollection)
|
||
|
}
|
||
|
|
||
|
public func pruneLastMessageHashInfoIfExpired(for snode: Snode, associatedWith publicKey: String, using transaction: Any) {
|
||
|
guard let lastMessageHashInfo = getLastMessageHashInfo(for: snode, associatedWith: publicKey),
|
||
|
(lastMessageHashInfo["hash"] as? String) != nil, let expirationDate = (lastMessageHashInfo["expirationDate"] as? NSNumber)?.uint64Value else { return }
|
||
|
let now = NSDate.millisecondTimestamp()
|
||
|
if now >= expirationDate {
|
||
|
removeLastMessageHashInfo(for: snode, associatedWith: publicKey, using: transaction)
|
||
|
}
|
||
|
}
|
||
|
|
||
5 years ago
|
public func removeLastMessageHashInfo(for snode: Snode, associatedWith publicKey: String, using transaction: Any) {
|
||
5 years ago
|
let key = "\(snode.address):\(snode.port).\(publicKey)"
|
||
|
(transaction as! YapDatabaseReadWriteTransaction).removeObject(forKey: key, inCollection: Storage.lastMessageHashCollection)
|
||
|
}
|
||
|
|
||
5 years ago
|
|
||
|
|
||
|
// MARK: - Received Messages
|
||
|
|
||
5 years ago
|
private static let receivedMessagesCollection = "LokiReceivedMessagesCollection"
|
||
|
|
||
|
public func getReceivedMessages(for publicKey: String) -> Set<String> {
|
||
|
var result: Set<String>?
|
||
|
Storage.read { transaction in
|
||
|
result = transaction.object(forKey: publicKey, inCollection: Storage.receivedMessagesCollection) as? Set<String>
|
||
|
}
|
||
|
return result ?? []
|
||
|
}
|
||
|
|
||
|
public func setReceivedMessages(to receivedMessages: Set<String>, for publicKey: String, using transaction: Any) {
|
||
|
(transaction as! YapDatabaseReadWriteTransaction).setObject(receivedMessages, forKey: publicKey, inCollection: Storage.receivedMessagesCollection)
|
||
|
}
|
||
|
}
|