mirror of
https://github.com/oxen-io/session-ios.git
synced 2023-12-13 21:30:14 +01:00
DRY up and refine the logic to attach the local profile key to outgoing messages.
// FREEBIE
This commit is contained in:
parent
282ac4bb29
commit
aa6312b586
|
@ -41,17 +41,11 @@ NS_ASSUME_NONNULL_BEGIN
|
|||
return [OWSSignalServiceProtosSyncMessageBuilder new];
|
||||
}
|
||||
|
||||
- (NSData *)buildPlainTextData
|
||||
- (NSData *)buildPlainTextData:(SignalRecipient *)recipient
|
||||
{
|
||||
OWSSignalServiceProtosContentBuilder *contentBuilder = [OWSSignalServiceProtosContentBuilder new];
|
||||
[contentBuilder setSyncMessage:[self buildSyncMessage]];
|
||||
|
||||
#ifndef SKIP_PROFILE_KEYS
|
||||
if (OWSProfilesManager.sharedManager.localProfileKey) {
|
||||
[contentBuilder setProfileKey:OWSProfilesManager.sharedManager.localProfileKey];
|
||||
}
|
||||
#endif
|
||||
|
||||
[self addLocalProfileKeyIfNecessary:contentBuilder recipient:recipient];
|
||||
return [[contentBuilder build] data];
|
||||
}
|
||||
|
||||
|
|
|
@ -32,6 +32,8 @@ typedef NS_ENUM(NSInteger, TSGroupMetaMessage) {
|
|||
|
||||
@class OWSSignalServiceProtosAttachmentPointer;
|
||||
@class OWSSignalServiceProtosDataMessageBuilder;
|
||||
@class OWSSignalServiceProtosContentBuilder;
|
||||
@class SignalRecipient;
|
||||
|
||||
@interface TSOutgoingMessage : TSMessage
|
||||
|
||||
|
@ -107,7 +109,7 @@ typedef NS_ENUM(NSInteger, TSGroupMetaMessage) {
|
|||
/**
|
||||
* The data representation of this message, to be encrypted, before being sent.
|
||||
*/
|
||||
- (NSData *)buildPlainTextData;
|
||||
- (NSData *)buildPlainTextData:(SignalRecipient *)recipient;
|
||||
|
||||
/**
|
||||
* Intermediate protobuf representation
|
||||
|
@ -176,6 +178,11 @@ typedef NS_ENUM(NSInteger, TSGroupMetaMessage) {
|
|||
- (void)updateWithSentRecipient:(NSString *)contactId transaction:(YapDatabaseReadWriteTransaction *)transaction;
|
||||
- (void)updateWithSentRecipient:(NSString *)contactId;
|
||||
|
||||
#pragma mark -
|
||||
|
||||
- (void)addLocalProfileKeyIfNecessary:(OWSSignalServiceProtosContentBuilder *)contentBuilder
|
||||
recipient:(SignalRecipient *)recipient;
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
|
|
|
@ -4,8 +4,10 @@
|
|||
|
||||
#import "TSOutgoingMessage.h"
|
||||
#import "NSDate+millisecondTimeStamp.h"
|
||||
#import "OWSOutgoingSyncMessage.h"
|
||||
#import "OWSProfilesManager.h"
|
||||
#import "OWSSignalServiceProtos.pb.h"
|
||||
#import "SignalRecipient.h"
|
||||
#import "TSAttachmentStream.h"
|
||||
#import "TSContactThread.h"
|
||||
#import "TSGroupThread.h"
|
||||
|
@ -456,24 +458,54 @@ NSString *const kTSOutgoingMessageSentRecipientAll = @"kTSOutgoingMessageSentRec
|
|||
return [[self dataMessageBuilder] build];
|
||||
}
|
||||
|
||||
- (NSData *)buildPlainTextData
|
||||
- (void)addLocalProfileKeyIfNecessary:(OWSSignalServiceProtosContentBuilder *)contentBuilder
|
||||
recipient:(SignalRecipient *)recipient
|
||||
{
|
||||
OWSAssert(contentBuilder);
|
||||
OWSAssert(recipient);
|
||||
|
||||
#ifndef SKIP_PROFILE_KEYS
|
||||
OWSAssert(OWSProfilesManager.sharedManager.localProfileKey.length > 0);
|
||||
BOOL shouldIncludeProfileKey = NO;
|
||||
|
||||
if ([self isKindOfClass:[OWSOutgoingSyncMessage class]]) {
|
||||
// Always sync the profile key to linked devices.
|
||||
shouldIncludeProfileKey = YES;
|
||||
} else {
|
||||
OWSAssert(self.thread);
|
||||
|
||||
// For 1:1 threads, we want to include the profile key IFF the
|
||||
// contact is in the whitelist.
|
||||
//
|
||||
// For Group threads, we want to include the profile key IFF the
|
||||
// recipient OR the group is in the whitelist.
|
||||
if ([OWSProfilesManager.sharedManager isUserInProfileWhitelist:recipient.recipientId]) {
|
||||
shouldIncludeProfileKey = YES;
|
||||
} else if (self.thread.isGroupThread) {
|
||||
TSGroupThread *groupThread = (TSGroupThread *)self.thread;
|
||||
NSData *groupId = groupThread.groupModel.groupId;
|
||||
|
||||
if ([OWSProfilesManager.sharedManager isGroupIdInProfileWhitelist:groupId]) {
|
||||
[contentBuilder setProfileKey:OWSProfilesManager.sharedManager.localProfileKey];
|
||||
}
|
||||
} else {
|
||||
TSContactThread *contactThread = (TSContactThread *)self.thread;
|
||||
NSString *recipientId = contactThread.contactIdentifier;
|
||||
OWSAssert([recipientId isEqualToString:recipient.recipientId]);
|
||||
}
|
||||
}
|
||||
|
||||
if (shouldIncludeProfileKey) {
|
||||
[contentBuilder setProfileKey:OWSProfilesManager.sharedManager.localProfileKey];
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
- (NSData *)buildPlainTextData:(SignalRecipient *)recipient
|
||||
{
|
||||
OWSSignalServiceProtosContentBuilder *contentBuilder = [OWSSignalServiceProtosContentBuilder new];
|
||||
contentBuilder.dataMessage = [self buildDataMessage];
|
||||
|
||||
#ifndef SKIP_PROFILE_KEYS
|
||||
OWSAssert([self.thread isKindOfClass:[TSContactThread class]]);
|
||||
if ([self.thread isKindOfClass:[TSContactThread class]]) {
|
||||
TSContactThread *contactThread = (TSContactThread *)self.thread;
|
||||
NSString *recipientId = contactThread.contactIdentifier;
|
||||
|
||||
if (OWSProfilesManager.sharedManager.localProfileKey &&
|
||||
[OWSProfilesManager.sharedManager isUserInProfileWhitelist:recipientId]) {
|
||||
[contentBuilder setProfileKey:OWSProfilesManager.sharedManager.localProfileKey];
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
[self addLocalProfileKeyIfNecessary:contentBuilder recipient:recipient];
|
||||
return [[contentBuilder build] data];
|
||||
}
|
||||
|
||||
|
|
|
@ -1166,8 +1166,8 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException";
|
|||
inThread:(TSThread *)thread
|
||||
{
|
||||
NSMutableArray *messagesArray = [NSMutableArray arrayWithCapacity:recipient.devices.count];
|
||||
|
||||
NSData *plainText = [message buildPlainTextData];
|
||||
|
||||
NSData *plainText = [message buildPlainTextData:recipient];
|
||||
DDLogDebug(@"%@ built message: %@ plainTextData.length: %lu", self.tag, [message class], (unsigned long)plainText.length);
|
||||
|
||||
for (NSNumber *deviceNumber in recipient.devices) {
|
||||
|
|
|
@ -122,24 +122,11 @@ NS_ASSUME_NONNULL_BEGIN
|
|||
// return _thread;
|
||||
//}
|
||||
|
||||
- (NSData *)buildPlainTextData
|
||||
- (NSData *)buildPlainTextData:(SignalRecipient *)recipient
|
||||
{
|
||||
OWSSignalServiceProtosContentBuilder *contentBuilder = [OWSSignalServiceProtosContentBuilder new];
|
||||
[contentBuilder setCallMessage:[self asProtobuf]];
|
||||
|
||||
#ifndef SKIP_PROFILE_KEYS
|
||||
OWSAssert([self.thread isKindOfClass:[TSContactThread class]]);
|
||||
if ([self.thread isKindOfClass:[TSContactThread class]]) {
|
||||
TSContactThread *contactThread = (TSContactThread *)self.thread;
|
||||
NSString *recipientId = contactThread.contactIdentifier;
|
||||
|
||||
if (OWSProfilesManager.sharedManager.localProfileKey &&
|
||||
[OWSProfilesManager.sharedManager isUserInProfileWhitelist:recipientId]) {
|
||||
[contentBuilder setProfileKey:OWSProfilesManager.sharedManager.localProfileKey];
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
[self addLocalProfileKeyIfNecessary:contentBuilder recipient:recipient];
|
||||
return [[contentBuilder build] data];
|
||||
}
|
||||
|
||||
|
|
|
@ -36,7 +36,7 @@ NS_ASSUME_NONNULL_BEGIN
|
|||
|
||||
#pragma mark - override TSOutgoingMessage
|
||||
|
||||
- (NSData *)buildPlainTextData
|
||||
- (NSData *)buildPlainTextData:(SignalRecipient *)recipient
|
||||
{
|
||||
OWSSignalServiceProtosContentBuilder *contentBuilder = [OWSSignalServiceProtosContentBuilder new];
|
||||
OWSSignalServiceProtosNullMessageBuilder *nullMessageBuilder = [OWSSignalServiceProtosNullMessageBuilder new];
|
||||
|
@ -57,19 +57,8 @@ NS_ASSUME_NONNULL_BEGIN
|
|||
nullMessageBuilder.padding = [Cryptography generateRandomBytes:contentLength];
|
||||
|
||||
contentBuilder.nullMessage = [nullMessageBuilder build];
|
||||
|
||||
#ifndef SKIP_PROFILE_KEYS
|
||||
OWSAssert([self.thread isKindOfClass:[TSContactThread class]]);
|
||||
if ([self.thread isKindOfClass:[TSContactThread class]]) {
|
||||
TSContactThread *contactThread = (TSContactThread *)self.thread;
|
||||
NSString *recipientId = contactThread.contactIdentifier;
|
||||
|
||||
if (OWSProfilesManager.sharedManager.localProfileKey &&
|
||||
[OWSProfilesManager.sharedManager isUserInProfileWhitelist:recipientId]) {
|
||||
[contentBuilder setProfileKey:OWSProfilesManager.sharedManager.localProfileKey];
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
[self addLocalProfileKeyIfNecessary:contentBuilder recipient:recipient];
|
||||
|
||||
return [contentBuilder build].data;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue