Refine sync messages.

pull/1/head
Matthew Chen 7 years ago
parent 59ff1561f5
commit 799949e546

@ -22,7 +22,6 @@ NS_ASSUME_NONNULL_BEGIN
+ (instancetype)getOrCreateThreadWithGroupId:(NSData *)groupId + (instancetype)getOrCreateThreadWithGroupId:(NSData *)groupId
transaction:(YapDatabaseReadWriteTransaction *)transaction; transaction:(YapDatabaseReadWriteTransaction *)transaction;
+ (nullable instancetype)threadWithGroupId:(NSData *)groupId;
+ (nullable instancetype)threadWithGroupId:(NSData *)groupId transaction:(YapDatabaseReadTransaction *)transaction; + (nullable instancetype)threadWithGroupId:(NSData *)groupId transaction:(YapDatabaseReadTransaction *)transaction;
+ (NSString *)threadIdFromGroupId:(NSData *)groupId; + (NSString *)threadIdFromGroupId:(NSData *)groupId;

@ -58,17 +58,6 @@ NS_ASSUME_NONNULL_BEGIN
return self; return self;
} }
+ (nullable instancetype)threadWithGroupId:(NSData *)groupId;
{
OWSAssert(groupId.length > 0);
__block TSGroupThread *thread;
[[self dbReadWriteConnection] readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
thread = [self threadWithGroupId:groupId transaction:transaction];
}];
return thread;
}
+ (nullable instancetype)threadWithGroupId:(NSData *)groupId transaction:(YapDatabaseReadTransaction *)transaction + (nullable instancetype)threadWithGroupId:(NSData *)groupId transaction:(YapDatabaseReadTransaction *)transaction
{ {
OWSAssert(groupId.length > 0); OWSAssert(groupId.length > 0);

@ -1,14 +1,17 @@
// Copyright © 2016 Open Whisper Systems. All rights reserved. //
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
//
#import "OWSChunkedOutputStream.h" #import "OWSChunkedOutputStream.h"
NS_ASSUME_NONNULL_BEGIN NS_ASSUME_NONNULL_BEGIN
@class TSGroupModel; @class TSGroupModel;
@class YapDatabaseReadTransaction;
@interface OWSGroupsOutputStream : OWSChunkedOutputStream @interface OWSGroupsOutputStream : OWSChunkedOutputStream
- (void)writeGroup:(TSGroupModel *)group; - (void)writeGroup:(TSGroupModel *)group transaction:(YapDatabaseReadTransaction *)transaction;
@end @end

@ -14,8 +14,11 @@ NS_ASSUME_NONNULL_BEGIN
@implementation OWSGroupsOutputStream @implementation OWSGroupsOutputStream
- (void)writeGroup:(TSGroupModel *)group - (void)writeGroup:(TSGroupModel *)group transaction:(YapDatabaseReadTransaction *)transaction
{ {
OWSAssert(group);
OWSAssert(transaction);
OWSSignalServiceProtosGroupDetailsBuilder *groupBuilder = [OWSSignalServiceProtosGroupDetailsBuilder new]; OWSSignalServiceProtosGroupDetailsBuilder *groupBuilder = [OWSSignalServiceProtosGroupDetailsBuilder new];
[groupBuilder setId:group.groupId]; [groupBuilder setId:group.groupId];
[groupBuilder setName:group.groupName]; [groupBuilder setName:group.groupName];
@ -37,10 +40,10 @@ NS_ASSUME_NONNULL_BEGIN
[self.delegateStream writeRawVarint32:groupDataLength]; [self.delegateStream writeRawVarint32:groupDataLength];
[self.delegateStream writeRawData:groupData]; [self.delegateStream writeRawData:groupData];
TSGroupThread *_Nullable groupThread = [TSGroupThread threadWithGroupId:group.groupId]; TSGroupThread *_Nullable groupThread = [TSGroupThread threadWithGroupId:group.groupId transaction:transaction];
if (groupThread) { if (groupThread) {
OWSDisappearingMessagesConfiguration *_Nullable disappearingMessagesConfiguration = OWSDisappearingMessagesConfiguration *_Nullable disappearingMessagesConfiguration =
[OWSDisappearingMessagesConfiguration fetchObjectWithUniqueID:groupThread.uniqueId]; [OWSDisappearingMessagesConfiguration fetchObjectWithUniqueID:groupThread.uniqueId transaction:transaction];
if (disappearingMessagesConfiguration && disappearingMessagesConfiguration.isEnabled) { if (disappearingMessagesConfiguration && disappearingMessagesConfiguration.isEnabled) {
[groupBuilder setExpireTimer:disappearingMessagesConfiguration.durationSeconds]; [groupBuilder setExpireTimer:disappearingMessagesConfiguration.durationSeconds];

@ -1,5 +1,5 @@
// //
// Copyright (c) 2017 Open Whisper Systems. All rights reserved. // Copyright (c) 2018 Open Whisper Systems. All rights reserved.
// //
#import "DataSource.h" #import "DataSource.h"
@ -53,6 +53,7 @@ NS_ASSUME_NONNULL_BEGIN
+ (void)deleteAttachments; + (void)deleteAttachments;
+ (NSString *)attachmentsFolder; + (NSString *)attachmentsFolder;
- (BOOL)shouldHaveImageSize;
- (CGSize)imageSize; - (CGSize)imageSize;
- (CGFloat)audioDurationSeconds; - (CGFloat)audioDurationSeconds;

@ -19,9 +19,11 @@ NS_ASSUME_NONNULL_BEGIN
// changes in the file path generation logic don't break existing attachments. // changes in the file path generation logic don't break existing attachments.
@property (nullable, nonatomic) NSString *localRelativeFilePath; @property (nullable, nonatomic) NSString *localRelativeFilePath;
// These properties should only be accessed on the main thread. // These properties should only be accessed while synchronized on self.
@property (nullable, nonatomic) NSNumber *cachedImageWidth; @property (nullable, nonatomic) NSNumber *cachedImageWidth;
@property (nullable, nonatomic) NSNumber *cachedImageHeight; @property (nullable, nonatomic) NSNumber *cachedImageHeight;
// This property should only be accessed on the main thread.
@property (nullable, nonatomic) NSNumber *cachedAudioDurationSeconds; @property (nullable, nonatomic) NSNumber *cachedAudioDurationSeconds;
@end @end
@ -105,10 +107,13 @@ NS_ASSUME_NONNULL_BEGIN
if (attachmentSchemaVersion < 4) { if (attachmentSchemaVersion < 4) {
// Legacy image sizes don't correctly reflect image orientation. // Legacy image sizes don't correctly reflect image orientation.
@synchronized(self)
{
self.cachedImageWidth = nil; self.cachedImageWidth = nil;
self.cachedImageHeight = nil; self.cachedImageHeight = nil;
} }
} }
}
- (void)ensureFilePath - (void)ensureFilePath
{ {
@ -390,15 +395,25 @@ NS_ASSUME_NONNULL_BEGIN
} }
} }
- (BOOL)shouldHaveImageSize
{
return ([self isVideo] || [self isImage] || [self isAnimated]);
}
- (CGSize)imageSize - (CGSize)imageSize
{ {
OWSAssertIsOnMainThread(); OWSAssert(self.shouldHaveImageSize);
@synchronized(self)
{
if (self.cachedImageWidth && self.cachedImageHeight) { if (self.cachedImageWidth && self.cachedImageHeight) {
return CGSizeMake(self.cachedImageWidth.floatValue, self.cachedImageHeight.floatValue); return CGSizeMake(self.cachedImageWidth.floatValue, self.cachedImageHeight.floatValue);
} }
CGSize imageSize = [self calculateImageSize]; CGSize imageSize = [self calculateImageSize];
if (imageSize.width <= 0 || imageSize.height <= 0) {
return CGSizeZero;
}
self.cachedImageWidth = @(imageSize.width); self.cachedImageWidth = @(imageSize.width);
self.cachedImageHeight = @(imageSize.height); self.cachedImageHeight = @(imageSize.height);
@ -420,6 +435,7 @@ NS_ASSUME_NONNULL_BEGIN
return imageSize; return imageSize;
} }
}
- (CGFloat)calculateAudioDurationSeconds - (CGFloat)calculateAudioDurationSeconds
{ {

@ -1,5 +1,5 @@
// //
// Copyright (c) 2017 Open Whisper Systems. All rights reserved. // Copyright (c) 2018 Open Whisper Systems. All rights reserved.
// //
#import "OWSSyncGroupsMessage.h" #import "OWSSyncGroupsMessage.h"
@ -57,7 +57,7 @@ NS_ASSUME_NONNULL_BEGIN
return; return;
} }
TSGroupModel *group = ((TSGroupThread *)obj).groupModel; TSGroupModel *group = ((TSGroupThread *)obj).groupModel;
[groupsOutputStream writeGroup:group]; [groupsOutputStream writeGroup:group transaction:transaction];
}]; }];
[groupsOutputStream flush]; [groupsOutputStream flush];

@ -535,6 +535,7 @@ NSString *const kTSOutgoingMessageSentRecipientAll = @"kTSOutgoingMessageSentRec
[builder setDigest:attachmentStream.digest]; [builder setDigest:attachmentStream.digest];
[builder setFlags:(self.isVoiceMessage ? OWSSignalServiceProtosAttachmentPointerFlagsVoiceMessage : 0)]; [builder setFlags:(self.isVoiceMessage ? OWSSignalServiceProtosAttachmentPointerFlagsVoiceMessage : 0)];
if ([attachmentStream shouldHaveImageSize]) {
CGSize imageSize = [attachmentStream imageSize]; CGSize imageSize = [attachmentStream imageSize];
if (imageSize.width < NSIntegerMax && imageSize.height < NSIntegerMax) { if (imageSize.width < NSIntegerMax && imageSize.height < NSIntegerMax) {
NSInteger imageWidth = (NSInteger)round(imageSize.width); NSInteger imageWidth = (NSInteger)round(imageSize.width);
@ -544,6 +545,7 @@ NSString *const kTSOutgoingMessageSentRecipientAll = @"kTSOutgoingMessageSentRec
[builder setHeight:(UInt32)imageHeight]; [builder setHeight:(UInt32)imageHeight];
} }
} }
}
return [builder build]; return [builder build];
} }

@ -312,6 +312,19 @@ NS_ASSUME_NONNULL_BEGIN
OWSAssert(dataMessage); OWSAssert(dataMessage);
OWSAssert(transaction); OWSAssert(transaction);
if (dataMessage.hasTimestamp) {
if (dataMessage.timestamp <= 0) {
DDLogError(@"%@ Ignoring message with invalid data message timestamp: %@", self.logTag, envelope.source);
return;
}
// This prevents replay attacks by the service.
if (dataMessage.timestamp != envelope.timestamp) {
DDLogError(
@"%@ Ignoring message with non-matching data message timestamp: %@", self.logTag, envelope.source);
return;
}
}
if ([dataMessage hasProfileKey]) { if ([dataMessage hasProfileKey]) {
NSData *profileKey = [dataMessage profileKey]; NSData *profileKey = [dataMessage profileKey];
NSString *recipientId = envelope.source; NSString *recipientId = envelope.source;

Loading…
Cancel
Save