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.
126 lines
3.5 KiB
Objective-C
126 lines
3.5 KiB
Objective-C
//
|
|
// Copyright (c) 2019 Open Whisper Systems. All rights reserved.
|
|
//
|
|
|
|
#import "OWSOutgoingSyncMessage.h"
|
|
#import "ProtoUtils.h"
|
|
#import <SessionProtocolKit/Cryptography.h>
|
|
#import <SessionProtocolKit/NSDate+OWS.h>
|
|
#import <SignalUtilitiesKit/SignalUtilitiesKit-Swift.h>
|
|
|
|
NS_ASSUME_NONNULL_BEGIN
|
|
|
|
@implementation OWSOutgoingSyncMessage
|
|
|
|
- (nullable instancetype)initWithCoder:(NSCoder *)coder
|
|
{
|
|
return [super initWithCoder:coder];
|
|
}
|
|
|
|
- (instancetype)init
|
|
{
|
|
// MJK TODO - remove SenderTimestamp
|
|
self = [super initOutgoingMessageWithTimestamp:[NSDate ows_millisecondTimeStamp]
|
|
inThread:nil
|
|
messageBody:nil
|
|
attachmentIds:[NSMutableArray new]
|
|
expiresInSeconds:0
|
|
expireStartedAt:0
|
|
isVoiceMessage:NO
|
|
groupMetaMessage:TSGroupMetaMessageUnspecified
|
|
quotedMessage:nil
|
|
contactShare:nil
|
|
linkPreview:nil];
|
|
|
|
if (!self) {
|
|
return self;
|
|
}
|
|
|
|
return self;
|
|
}
|
|
|
|
- (instancetype)initWithTimestamp:(uint64_t)timestamp
|
|
{
|
|
self = [super initOutgoingMessageWithTimestamp:timestamp
|
|
inThread:nil
|
|
messageBody:nil
|
|
attachmentIds:[NSMutableArray new]
|
|
expiresInSeconds:0
|
|
expireStartedAt:0
|
|
isVoiceMessage:NO
|
|
groupMetaMessage:TSGroupMetaMessageUnspecified
|
|
quotedMessage:nil
|
|
contactShare:nil
|
|
linkPreview:nil];
|
|
|
|
if (!self) {
|
|
return self;
|
|
}
|
|
|
|
return self;
|
|
}
|
|
|
|
- (uint)ttl { return (uint)[LKTTLUtilities getTTLFor:LKMessageTypeSync]; }
|
|
|
|
- (BOOL)shouldBeSaved
|
|
{
|
|
return NO;
|
|
}
|
|
|
|
- (BOOL)shouldSyncTranscript
|
|
{
|
|
return NO;
|
|
}
|
|
|
|
// This method should not be overridden, since we want to add random padding to *every* sync message
|
|
- (nullable SSKProtoSyncMessage *)buildSyncMessage
|
|
{
|
|
SSKProtoSyncMessageBuilder *_Nullable builder = [self syncMessageBuilder];
|
|
if (!builder) {
|
|
return nil;
|
|
}
|
|
|
|
// Add a random 1-512 bytes to obscure sync message type
|
|
size_t paddingBytesLength = arc4random_uniform(512) + 1;
|
|
builder.padding = [Cryptography generateRandomBytes:paddingBytesLength];
|
|
|
|
NSError *error;
|
|
SSKProtoSyncMessage *_Nullable proto = [builder buildAndReturnError:&error];
|
|
if (error || !proto) {
|
|
OWSFailDebug(@"could not build protobuf: %@", error);
|
|
return nil;
|
|
}
|
|
return proto;
|
|
}
|
|
|
|
- (nullable SSKProtoSyncMessageBuilder *)syncMessageBuilder
|
|
{
|
|
OWSAbstractMethod();
|
|
|
|
return [SSKProtoSyncMessage builder];
|
|
}
|
|
|
|
- (nullable NSData *)buildPlainTextData:(SignalRecipient *)recipient
|
|
{
|
|
SSKProtoSyncMessage *_Nullable syncMessage = [self buildSyncMessage];
|
|
if (!syncMessage) {
|
|
return nil;
|
|
}
|
|
|
|
SSKProtoContentBuilder *contentBuilder = [SSKProtoContent builder];
|
|
[contentBuilder setSyncMessage:syncMessage];
|
|
|
|
NSError *error;
|
|
NSData *_Nullable data = [contentBuilder buildSerializedDataAndReturnError:&error];
|
|
if (error || !data) {
|
|
OWSFailDebug(@"could not serialize protobuf: %@", error);
|
|
return nil;
|
|
}
|
|
|
|
return data;
|
|
}
|
|
|
|
@end
|
|
|
|
NS_ASSUME_NONNULL_END
|