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.
98 lines
3.4 KiB
Swift
98 lines
3.4 KiB
Swift
7 years ago
|
//
|
||
|
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
|
||
|
//
|
||
|
|
||
|
import Foundation
|
||
|
|
||
|
@objc
|
||
|
class ConversationConfigurationSyncOperation: OWSOperation {
|
||
|
|
||
|
enum ColorSyncOperationError: Error {
|
||
|
case assertionError(description: String)
|
||
|
}
|
||
|
|
||
7 years ago
|
private var dbConnection: YapDatabaseConnection {
|
||
7 years ago
|
return OWSPrimaryStorage.shared().dbReadConnection
|
||
|
}
|
||
|
|
||
7 years ago
|
private var messageSenderJobQueue: MessageSenderJobQueue {
|
||
|
return SSKEnvironment.shared.messageSenderJobQueue
|
||
7 years ago
|
}
|
||
|
|
||
7 years ago
|
private var contactsManager: OWSContactsManager {
|
||
7 years ago
|
return Environment.shared.contactsManager
|
||
7 years ago
|
}
|
||
|
|
||
7 years ago
|
private var syncManager: OWSSyncManagerProtocol {
|
||
|
return SSKEnvironment.shared.syncManager
|
||
7 years ago
|
}
|
||
|
|
||
7 years ago
|
private let thread: TSThread
|
||
7 years ago
|
|
||
|
@objc
|
||
7 years ago
|
public init(thread: TSThread) {
|
||
7 years ago
|
self.thread = thread
|
||
|
super.init()
|
||
|
}
|
||
|
|
||
7 years ago
|
override public func run() {
|
||
7 years ago
|
if let contactThread = thread as? TSContactThread {
|
||
|
sync(contactThread: contactThread)
|
||
|
} else if let groupThread = thread as? TSGroupThread {
|
||
|
sync(groupThread: groupThread)
|
||
|
} else {
|
||
|
self.reportAssertionError(description: "unknown thread type")
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private func reportAssertionError(description: String) {
|
||
|
let error = ColorSyncOperationError.assertionError(description: description)
|
||
|
self.reportError(error)
|
||
|
}
|
||
|
|
||
7 years ago
|
private func sync(contactThread: TSContactThread) {
|
||
7 years ago
|
guard let signalAccount: SignalAccount = self.contactsManager.fetchSignalAccount(forRecipientId: contactThread.contactIdentifier()) else {
|
||
7 years ago
|
reportAssertionError(description: "unable to find signalAccount")
|
||
|
return
|
||
|
}
|
||
|
|
||
6 years ago
|
syncManager.syncContacts(for: [signalAccount]).retainUntilComplete()
|
||
7 years ago
|
}
|
||
|
|
||
7 years ago
|
private func sync(groupThread: TSGroupThread) {
|
||
7 years ago
|
// TODO sync only the affected group
|
||
|
// The current implementation works, but seems wasteful.
|
||
|
// Does desktop handle single group sync correctly?
|
||
|
// What does Android do?
|
||
5 years ago
|
let syncMessage: OWSSyncGroupsMessage = OWSSyncGroupsMessage(groupThread: groupThread)
|
||
7 years ago
|
|
||
7 years ago
|
var dataSource: DataSource?
|
||
7 years ago
|
self.dbConnection.read { transaction in
|
||
7 years ago
|
guard let messageData: Data = syncMessage.buildPlainTextAttachmentData(with: transaction) else {
|
||
7 years ago
|
owsFailDebug("could not serialize sync groups data")
|
||
7 years ago
|
return
|
||
|
}
|
||
7 years ago
|
dataSource = DataSourceValue.dataSource(withSyncMessageData: messageData)
|
||
|
}
|
||
|
|
||
|
guard let attachmentDataSource = dataSource else {
|
||
|
self.reportAssertionError(description: "unable to build attachment data source")
|
||
|
return
|
||
|
}
|
||
|
|
||
7 years ago
|
self.sendConfiguration(attachmentDataSource: attachmentDataSource, syncMessage: syncMessage)
|
||
|
}
|
||
|
|
||
|
private func sendConfiguration(attachmentDataSource: DataSource, syncMessage: OWSOutgoingSyncMessage) {
|
||
7 years ago
|
self.messageSenderJobQueue.add(mediaMessage: syncMessage,
|
||
|
dataSource: attachmentDataSource,
|
||
|
contentType: OWSMimeTypeApplicationOctetStream,
|
||
|
sourceFilename: nil,
|
||
7 years ago
|
caption: nil,
|
||
7 years ago
|
albumMessageId: nil,
|
||
7 years ago
|
isTemporaryAttachment: true)
|
||
|
self.reportSuccess()
|
||
7 years ago
|
}
|
||
|
|
||
|
}
|