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.
54 lines
2.4 KiB
Swift
54 lines
2.4 KiB
Swift
|
|
@objc(LKDatabaseUtilities)
|
|
public final class LokiDatabaseUtilities : NSObject {
|
|
|
|
private override init() { }
|
|
|
|
// MARK: - Quotes
|
|
@objc(getServerIDForQuoteWithID:quoteeHexEncodedPublicKey:threadID:transaction:)
|
|
public static func getServerID(quoteID: UInt64, quoteeHexEncodedPublicKey: String, threadID: String, transaction: YapDatabaseReadTransaction) -> UInt64 {
|
|
guard let message = TSInteraction.interactions(withTimestamp: quoteID, filter: { interaction in
|
|
let senderHexEncodedPublicKey: String
|
|
if let message = interaction as? TSIncomingMessage {
|
|
senderHexEncodedPublicKey = message.authorId
|
|
} else if let message = interaction as? TSOutgoingMessage {
|
|
senderHexEncodedPublicKey = getUserHexEncodedPublicKey()
|
|
} else {
|
|
return false
|
|
}
|
|
return (senderHexEncodedPublicKey == quoteeHexEncodedPublicKey) && (interaction.uniqueThreadId == threadID)
|
|
}, with: transaction).first as! TSMessage? else { return 0 }
|
|
return message.openGroupServerMessageID
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Open Groups
|
|
private static let publicChatCollection = "LokiPublicChatCollection"
|
|
|
|
@objc(getAllPublicChats:)
|
|
public static func getAllPublicChats(in transaction: YapDatabaseReadTransaction) -> [String:OpenGroup] {
|
|
var result = [String:OpenGroup]()
|
|
transaction.enumerateKeysAndObjects(inCollection: publicChatCollection) { threadID, object, _ in
|
|
guard let publicChat = object as? OpenGroup else { return }
|
|
result[threadID] = publicChat
|
|
}
|
|
return result
|
|
}
|
|
|
|
@objc(getPublicChatForThreadID:transaction:)
|
|
public static func getPublicChat(for threadID: String, in transaction: YapDatabaseReadTransaction) -> OpenGroup? {
|
|
return transaction.object(forKey: threadID, inCollection: publicChatCollection) as? OpenGroup
|
|
}
|
|
|
|
@objc(setPublicChat:threadID:transaction:)
|
|
public static func setPublicChat(_ publicChat: OpenGroup, for threadID: String, in transaction: YapDatabaseReadWriteTransaction) {
|
|
transaction.setObject(publicChat, forKey: threadID, inCollection: publicChatCollection)
|
|
}
|
|
|
|
@objc(removePublicChatForThreadID:transaction:)
|
|
public static func removePublicChat(for threadID: String, in transaction: YapDatabaseReadWriteTransaction) {
|
|
transaction.removeObject(forKey: threadID, inCollection: publicChatCollection)
|
|
}
|
|
}
|