diff --git a/SessionMessagingKit/Messages/Control Messages/ClosedGroupUpdate.swift b/SessionMessagingKit/Messages/Control Messages/ClosedGroupUpdate.swift index 7b55c0c32..2e2bc18f9 100644 --- a/SessionMessagingKit/Messages/Control Messages/ClosedGroupUpdate.swift +++ b/SessionMessagingKit/Messages/Control Messages/ClosedGroupUpdate.swift @@ -126,7 +126,7 @@ public final class ClosedGroupUpdate : ControlMessage { return ClosedGroupUpdate(kind: kind) } - public override func toProto() -> SNProtoContent? { + public override func toProto(using transaction: YapDatabaseReadWriteTransaction) -> SNProtoContent? { guard let kind = kind else { SNLog("Couldn't construct closed group update proto from: \(self).") return nil @@ -156,6 +156,8 @@ public final class ClosedGroupUpdate : ControlMessage { let contentProto = SNProtoContent.builder() let dataMessageProto = SNProtoDataMessage.builder() dataMessageProto.setClosedGroupUpdate(try closedGroupUpdate.build()) + // Group context + try setGroupContextIfNeeded(on: dataMessageProto, using: transaction) contentProto.setDataMessage(try dataMessageProto.build()) return try contentProto.build() } catch { diff --git a/SessionMessagingKit/Messages/Control Messages/ExpirationTimerUpdate.swift b/SessionMessagingKit/Messages/Control Messages/ExpirationTimerUpdate.swift index 7c2f221dc..473259c32 100644 --- a/SessionMessagingKit/Messages/Control Messages/ExpirationTimerUpdate.swift +++ b/SessionMessagingKit/Messages/Control Messages/ExpirationTimerUpdate.swift @@ -38,7 +38,7 @@ public final class ExpirationTimerUpdate : ControlMessage { return ExpirationTimerUpdate(duration: duration) } - public override func toProto() -> SNProtoContent? { + public override func toProto(using transaction: YapDatabaseReadWriteTransaction) -> SNProtoContent? { guard let duration = duration else { SNLog("Couldn't construct expiration timer update proto from: \(self).") return nil @@ -46,6 +46,13 @@ public final class ExpirationTimerUpdate : ControlMessage { let dataMessageProto = SNProtoDataMessage.builder() dataMessageProto.setFlags(UInt32(SNProtoDataMessage.SNProtoDataMessageFlags.expirationTimerUpdate.rawValue)) dataMessageProto.setExpireTimer(duration) + // Group context + do { + try setGroupContextIfNeeded(on: dataMessageProto, using: transaction) + } catch { + SNLog("Couldn't construct expiration timer update proto from: \(self).") + return nil + } let contentProto = SNProtoContent.builder() do { contentProto.setDataMessage(try dataMessageProto.build()) diff --git a/SessionMessagingKit/Messages/Control Messages/ReadReceipt.swift b/SessionMessagingKit/Messages/Control Messages/ReadReceipt.swift index 5199d8980..cdce7ae1e 100644 --- a/SessionMessagingKit/Messages/Control Messages/ReadReceipt.swift +++ b/SessionMessagingKit/Messages/Control Messages/ReadReceipt.swift @@ -38,7 +38,7 @@ public final class ReadReceipt : ControlMessage { return ReadReceipt(timestamps: timestamps) } - public override func toProto() -> SNProtoContent? { + public override func toProto(using transaction: YapDatabaseReadWriteTransaction) -> SNProtoContent? { guard let timestamps = timestamps else { SNLog("Couldn't construct read receipt proto from: \(self).") return nil diff --git a/SessionMessagingKit/Messages/Control Messages/TypingIndicator.swift b/SessionMessagingKit/Messages/Control Messages/TypingIndicator.swift index 6d094cd62..e09de0d5d 100644 --- a/SessionMessagingKit/Messages/Control Messages/TypingIndicator.swift +++ b/SessionMessagingKit/Messages/Control Messages/TypingIndicator.swift @@ -64,7 +64,7 @@ public final class TypingIndicator : ControlMessage { return TypingIndicator(kind: kind) } - public override func toProto() -> SNProtoContent? { + public override func toProto(using transaction: YapDatabaseReadWriteTransaction) -> SNProtoContent? { guard let timestamp = sentTimestamp, let kind = kind else { SNLog("Couldn't construct typing indicator proto from: \(self).") return nil diff --git a/SessionMessagingKit/Messages/Control Messages/Unused/NullMessage.swift b/SessionMessagingKit/Messages/Control Messages/Unused/NullMessage.swift index a66b93192..488956883 100644 --- a/SessionMessagingKit/Messages/Control Messages/Unused/NullMessage.swift +++ b/SessionMessagingKit/Messages/Control Messages/Unused/NullMessage.swift @@ -22,7 +22,7 @@ public final class NullMessage : ControlMessage { return NullMessage() } - public override func toProto() -> SNProtoContent? { + public override func toProto(using transaction: YapDatabaseReadWriteTransaction) -> SNProtoContent? { let nullMessageProto = SNProtoNullMessage.builder() let paddingSize = UInt.random(in: 0..<512) // random(in:) uses the system's default random generator, which is cryptographically secure let padding = Data.getSecureRandomData(ofSize: paddingSize)! diff --git a/SessionMessagingKit/Messages/Control Messages/Unused/SessionRequest.swift b/SessionMessagingKit/Messages/Control Messages/Unused/SessionRequest.swift index 1d57a891d..52f6e0293 100644 --- a/SessionMessagingKit/Messages/Control Messages/Unused/SessionRequest.swift +++ b/SessionMessagingKit/Messages/Control Messages/Unused/SessionRequest.swift @@ -50,7 +50,7 @@ public final class SessionRequest : ControlMessage { return SessionRequest(preKeyBundle: preKeyBundle) } - public override func toProto() -> SNProtoContent? { + public override func toProto(using transaction: YapDatabaseReadWriteTransaction) -> SNProtoContent? { guard let preKeyBundle = preKeyBundle else { SNLog("Couldn't construct session request proto from: \(self).") return nil diff --git a/SessionMessagingKit/Messages/Message.swift b/SessionMessagingKit/Messages/Message.swift index 0f04c20db..985f9ba41 100644 --- a/SessionMessagingKit/Messages/Message.swift +++ b/SessionMessagingKit/Messages/Message.swift @@ -44,8 +44,15 @@ public class Message : NSObject, NSCoding { // NSObject/NSCoding conformance is preconditionFailure("fromProto(_:) is abstract and must be overridden.") } - public func toProto() -> SNProtoContent? { - preconditionFailure("toProto() is abstract and must be overridden.") + public func toProto(using transaction: YapDatabaseReadWriteTransaction) -> SNProtoContent? { + preconditionFailure("toProto(using:) is abstract and must be overridden.") + } + + public func setGroupContextIfNeeded(on dataMessage: SNProtoDataMessage.SNProtoDataMessageBuilder, using transaction: YapDatabaseReadTransaction) throws { + guard let thread = TSThread.fetch(uniqueId: threadID!, transaction: transaction) as? TSGroupThread, thread.usesSharedSenderKeys else { return } + // Android needs a group context or it'll interpret the message as a one-to-one message + let groupProto = SNProtoGroupContext.builder(id: thread.groupModel.groupId, type: .deliver) + dataMessage.setGroup(try groupProto.build()) } // MARK: General diff --git a/SessionMessagingKit/Messages/Visible Messages/VisibleMessage.swift b/SessionMessagingKit/Messages/Visible Messages/VisibleMessage.swift index a25735ce2..522bd5169 100644 --- a/SessionMessagingKit/Messages/Visible Messages/VisibleMessage.swift +++ b/SessionMessagingKit/Messages/Visible Messages/VisibleMessage.swift @@ -54,11 +54,7 @@ public final class VisibleMessage : Message { return result } - public override func toProto() -> SNProtoContent? { - preconditionFailure("Use toProto(using:) instead.") - } - - public func toProto(using transaction: YapDatabaseReadWriteTransaction) -> SNProtoContent? { + public override func toProto(using transaction: YapDatabaseReadWriteTransaction) -> SNProtoContent? { let proto = SNProtoContent.builder() var attachmentIDs = self.attachmentIDs let dataMessage: SNProtoDataMessage.SNProtoDataMessageBuilder @@ -90,6 +86,13 @@ public final class VisibleMessage : Message { let attachmentProtos = attachments.compactMap { $0.buildProto() } dataMessage.setAttachments(attachmentProtos) // TODO: Contact + // Group context + do { + try setGroupContextIfNeeded(on: dataMessage, using: transaction) + } catch { + SNLog("Couldn't construct visible message proto from: \(self).") + return nil + } // Build do { proto.setDataMessage(try dataMessage.build()) diff --git a/SessionMessagingKit/Sending & Receiving/MessageSender.swift b/SessionMessagingKit/Sending & Receiving/MessageSender.swift index 612c06ff2..078b19c47 100644 --- a/SessionMessagingKit/Sending & Receiving/MessageSender.swift +++ b/SessionMessagingKit/Sending & Receiving/MessageSender.swift @@ -149,13 +149,7 @@ public final class MessageSender : NSObject { } } // Convert it to protobuf - let protoOrNil: SNProtoContent? - if let message = message as? VisibleMessage { - protoOrNil = message.toProto(using: transaction) // Needed because of how TSAttachmentStream works - } else { - protoOrNil = message.toProto() - } - guard let proto = protoOrNil else { handleFailure(with: Error.protoConversionFailed, using: transaction); return promise } + guard let proto = message.toProto(using: transaction) else { handleFailure(with: Error.protoConversionFailed, using: transaction); return promise } // Serialize the protobuf let plaintext: Data do {