2018-07-04 04:27:51 +02:00
|
|
|
//
|
|
|
|
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
|
|
|
@objc
|
|
|
|
class ConversationConfigurationSyncOperation: OWSOperation {
|
|
|
|
|
|
|
|
enum ColorSyncOperationError: Error {
|
|
|
|
case assertionError(description: String)
|
|
|
|
}
|
|
|
|
|
2018-07-05 21:55:28 +02:00
|
|
|
private var dbConnection: YapDatabaseConnection {
|
2018-07-04 04:27:51 +02:00
|
|
|
return OWSPrimaryStorage.shared().dbReadConnection
|
|
|
|
}
|
|
|
|
|
2018-10-20 19:51:48 +02:00
|
|
|
private var messageSenderJobQueue: MessageSenderJobQueue {
|
|
|
|
return SSKEnvironment.shared.messageSenderJobQueue
|
2018-07-04 04:27:51 +02:00
|
|
|
}
|
|
|
|
|
2018-07-05 21:55:28 +02:00
|
|
|
private var contactsManager: OWSContactsManager {
|
2018-08-31 19:22:19 +02:00
|
|
|
return Environment.shared.contactsManager
|
2018-07-04 04:27:51 +02:00
|
|
|
}
|
|
|
|
|
2018-10-18 21:59:43 +02:00
|
|
|
private var syncManager: OWSSyncManagerProtocol {
|
|
|
|
return SSKEnvironment.shared.syncManager
|
2018-07-04 04:27:51 +02:00
|
|
|
}
|
|
|
|
|
2018-07-05 21:55:28 +02:00
|
|
|
private let thread: TSThread
|
2018-07-04 04:27:51 +02:00
|
|
|
|
|
|
|
@objc
|
2018-07-05 21:55:28 +02:00
|
|
|
public init(thread: TSThread) {
|
2018-07-04 04:27:51 +02:00
|
|
|
self.thread = thread
|
|
|
|
super.init()
|
|
|
|
}
|
|
|
|
|
2018-07-05 21:55:28 +02:00
|
|
|
override public func run() {
|
2018-07-04 04:27:51 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2018-07-05 21:55:28 +02:00
|
|
|
private func sync(contactThread: TSContactThread) {
|
2018-07-27 20:19:11 +02:00
|
|
|
guard let signalAccount: SignalAccount = self.contactsManager.fetchSignalAccount(forRecipientId: contactThread.contactIdentifier()) else {
|
2018-07-04 04:27:51 +02:00
|
|
|
reportAssertionError(description: "unable to find signalAccount")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-12-11 16:24:06 +01:00
|
|
|
syncManager.syncContacts(for: [signalAccount]).retainUntilComplete()
|
2018-07-04 04:27:51 +02:00
|
|
|
}
|
|
|
|
|
2018-07-05 21:55:28 +02:00
|
|
|
private func sync(groupThread: TSGroupThread) {
|
2018-07-04 04:27:51 +02:00
|
|
|
// TODO sync only the affected group
|
|
|
|
// The current implementation works, but seems wasteful.
|
|
|
|
// Does desktop handle single group sync correctly?
|
|
|
|
// What does Android do?
|
2020-02-21 05:11:07 +01:00
|
|
|
let syncMessage: OWSSyncGroupsMessage = OWSSyncGroupsMessage(groupThread: groupThread)
|
2018-07-04 04:27:51 +02:00
|
|
|
|
2018-08-07 18:57:48 +02:00
|
|
|
var dataSource: DataSource?
|
2018-07-04 04:27:51 +02:00
|
|
|
self.dbConnection.read { transaction in
|
2018-08-07 18:57:48 +02:00
|
|
|
guard let messageData: Data = syncMessage.buildPlainTextAttachmentData(with: transaction) else {
|
2018-08-27 16:27:48 +02:00
|
|
|
owsFailDebug("could not serialize sync groups data")
|
2018-08-07 18:57:48 +02:00
|
|
|
return
|
|
|
|
}
|
2018-07-04 04:27:51 +02:00
|
|
|
dataSource = DataSourceValue.dataSource(withSyncMessageData: messageData)
|
|
|
|
}
|
|
|
|
|
|
|
|
guard let attachmentDataSource = dataSource else {
|
|
|
|
self.reportAssertionError(description: "unable to build attachment data source")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-07-05 21:55:28 +02:00
|
|
|
self.sendConfiguration(attachmentDataSource: attachmentDataSource, syncMessage: syncMessage)
|
|
|
|
}
|
|
|
|
|
|
|
|
private func sendConfiguration(attachmentDataSource: DataSource, syncMessage: OWSOutgoingSyncMessage) {
|
2018-10-20 19:51:48 +02:00
|
|
|
self.messageSenderJobQueue.add(mediaMessage: syncMessage,
|
|
|
|
dataSource: attachmentDataSource,
|
|
|
|
contentType: OWSMimeTypeApplicationOctetStream,
|
|
|
|
sourceFilename: nil,
|
2018-11-06 23:30:09 +01:00
|
|
|
caption: nil,
|
2018-11-07 17:42:28 +01:00
|
|
|
albumMessageId: nil,
|
2018-10-20 19:51:48 +02:00
|
|
|
isTemporaryAttachment: true)
|
|
|
|
self.reportSuccess()
|
2018-07-04 04:27:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|