session-ios/Session/Signal/ConversationConfigurationSyncOperation.swift

98 lines
3.4 KiB
Swift
Raw Normal View History

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
}
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 {
return Environment.shared.contactsManager
2018-07-04 04:27:51 +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) {
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
}
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?
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) {
self.messageSenderJobQueue.add(mediaMessage: syncMessage,
dataSource: attachmentDataSource,
contentType: OWSMimeTypeApplicationOctetStream,
sourceFilename: nil,
caption: nil,
albumMessageId: nil,
isTemporaryAttachment: true)
self.reportSuccess()
2018-07-04 04:27:51 +02:00
}
}