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.
64 lines
3.2 KiB
Swift
64 lines
3.2 KiB
Swift
4 years ago
|
import PromiseKit
|
||
|
|
||
|
extension Storage {
|
||
|
|
||
|
public func getOrGenerateRegistrationID(using transaction: Any) -> UInt32 {
|
||
|
SSKEnvironment.shared.tsAccountManager.getOrGenerateRegistrationId(transaction as! YapDatabaseReadWriteTransaction)
|
||
|
}
|
||
|
|
||
|
public func getSenderCertificate(for publicKey: String) -> SMKSenderCertificate {
|
||
|
let (promise, seal) = Promise<SMKSenderCertificate>.pending()
|
||
|
SSKEnvironment.shared.udManager.ensureSenderCertificate { senderCertificate in
|
||
|
seal.fulfill(senderCertificate)
|
||
|
} failure: { error in
|
||
|
// Should never fail
|
||
|
}
|
||
|
return try! promise.wait()
|
||
|
}
|
||
4 years ago
|
|
||
4 years ago
|
/// Returns the ID of the thread the message was stored under along with the ID of the `TSIncomingMessage` that was constructed.
|
||
|
public func persist(_ message: VisibleMessage, groupPublicKey: String?, using transaction: Any) -> (String, String)? {
|
||
4 years ago
|
let transaction = transaction as! YapDatabaseReadWriteTransaction
|
||
|
var threadOrNil: TSThread?
|
||
|
if let groupPublicKey = groupPublicKey {
|
||
|
guard Storage.shared.isClosedGroup(groupPublicKey) else { return nil }
|
||
|
let groupID = LKGroupUtilities.getEncodedClosedGroupIDAsData(groupPublicKey)
|
||
|
threadOrNil = TSGroupThread.fetch(uniqueId: TSGroupThread.threadId(fromGroupId: groupID), transaction: transaction)
|
||
|
} else {
|
||
|
threadOrNil = TSContactThread.getOrCreateThread(withContactId: message.sender!, transaction: transaction)
|
||
|
}
|
||
|
guard let thread = threadOrNil else { return nil }
|
||
4 years ago
|
let message = TSIncomingMessage.from(message, associatedWith: thread)
|
||
4 years ago
|
message.save(with: transaction)
|
||
4 years ago
|
DispatchQueue.main.async { message.touch() } // FIXME: Hack for a thread updating issue
|
||
4 years ago
|
return (thread.uniqueId!, message.uniqueId!)
|
||
4 years ago
|
}
|
||
4 years ago
|
|
||
4 years ago
|
/// Returns the IDs of the saved attachments.
|
||
|
public func persist(_ attachments: [VisibleMessage.Attachment], using transaction: Any) -> [String] {
|
||
4 years ago
|
return attachments.map { attachment in
|
||
|
let tsAttachment = TSAttachmentPointer.from(attachment)
|
||
|
tsAttachment.save(with: transaction as! YapDatabaseReadWriteTransaction)
|
||
|
return tsAttachment.uniqueId!
|
||
|
}
|
||
|
}
|
||
4 years ago
|
|
||
|
/// Also touches the associated message.
|
||
|
public func setAttachmentState(to state: TSAttachmentPointerState, for pointer: TSAttachmentPointer, associatedWith tsIncomingMessageID: String, using transaction: Any) {
|
||
|
let transaction = transaction as! YapDatabaseReadWriteTransaction
|
||
|
pointer.state = state
|
||
|
pointer.save(with: transaction)
|
||
|
guard let tsIncomingMessage = TSIncomingMessage.fetch(uniqueId: tsIncomingMessageID, transaction: transaction) else { return }
|
||
|
tsIncomingMessage.touch(with: transaction)
|
||
|
}
|
||
|
|
||
|
/// Also touches the associated message.
|
||
|
public func persist(_ stream: TSAttachmentStream, associatedWith tsIncomingMessageID: String, using transaction: Any) {
|
||
|
let transaction = transaction as! YapDatabaseReadWriteTransaction
|
||
|
stream.save(with: transaction)
|
||
|
guard let tsIncomingMessage = TSIncomingMessage.fetch(uniqueId: tsIncomingMessageID, transaction: transaction) else { return }
|
||
|
tsIncomingMessage.touch(with: transaction)
|
||
|
}
|
||
4 years ago
|
}
|
||
4 years ago
|
|