Refine sync messages.

pull/1/head
Matthew Chen 8 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,8 +107,11 @@ 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.
self.cachedImageWidth = nil; @synchronized(self)
self.cachedImageHeight = nil; {
self.cachedImageWidth = nil;
self.cachedImageHeight = nil;
}
} }
} }
@ -390,35 +395,46 @@ NS_ASSUME_NONNULL_BEGIN
} }
} }
- (BOOL)shouldHaveImageSize
{
return ([self isVideo] || [self isImage] || [self isAnimated]);
}
- (CGSize)imageSize - (CGSize)imageSize
{ {
OWSAssertIsOnMainThread(); OWSAssert(self.shouldHaveImageSize);
if (self.cachedImageWidth && self.cachedImageHeight) { @synchronized(self)
return CGSizeMake(self.cachedImageWidth.floatValue, self.cachedImageHeight.floatValue); {
} if (self.cachedImageWidth && self.cachedImageHeight) {
return CGSizeMake(self.cachedImageWidth.floatValue, self.cachedImageHeight.floatValue);
}
CGSize imageSize = [self calculateImageSize]; CGSize imageSize = [self calculateImageSize];
self.cachedImageWidth = @(imageSize.width); if (imageSize.width <= 0 || imageSize.height <= 0) {
self.cachedImageHeight = @(imageSize.height); return CGSizeZero;
}
self.cachedImageWidth = @(imageSize.width);
self.cachedImageHeight = @(imageSize.height);
[self.dbReadWriteConnection asyncReadWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { [self.dbReadWriteConnection asyncReadWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
NSString *collection = [[self class] collection]; NSString *collection = [[self class] collection];
TSAttachmentStream *latestInstance = [transaction objectForKey:self.uniqueId inCollection:collection]; TSAttachmentStream *latestInstance = [transaction objectForKey:self.uniqueId inCollection:collection];
if (latestInstance) { if (latestInstance) {
latestInstance.cachedImageWidth = @(imageSize.width); latestInstance.cachedImageWidth = @(imageSize.width);
latestInstance.cachedImageHeight = @(imageSize.height); latestInstance.cachedImageHeight = @(imageSize.height);
[latestInstance saveWithTransaction:transaction]; [latestInstance saveWithTransaction:transaction];
} else { } else {
// This message has not yet been saved or has been deleted; do nothing. // This message has not yet been saved or has been deleted; do nothing.
// This isn't an error per se, but these race conditions should be // This isn't an error per se, but these race conditions should be
// _very_ rare. // _very_ rare.
OWSFail(@"%@ Attachment not yet saved.", self.logTag); OWSFail(@"%@ Attachment not yet saved.", self.logTag);
} }
}]; }];
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,13 +535,15 @@ NSString *const kTSOutgoingMessageSentRecipientAll = @"kTSOutgoingMessageSentRec
[builder setDigest:attachmentStream.digest]; [builder setDigest:attachmentStream.digest];
[builder setFlags:(self.isVoiceMessage ? OWSSignalServiceProtosAttachmentPointerFlagsVoiceMessage : 0)]; [builder setFlags:(self.isVoiceMessage ? OWSSignalServiceProtosAttachmentPointerFlagsVoiceMessage : 0)];
CGSize imageSize = [attachmentStream imageSize]; if ([attachmentStream shouldHaveImageSize]) {
if (imageSize.width < NSIntegerMax && imageSize.height < NSIntegerMax) { CGSize imageSize = [attachmentStream imageSize];
NSInteger imageWidth = (NSInteger)round(imageSize.width); if (imageSize.width < NSIntegerMax && imageSize.height < NSIntegerMax) {
NSInteger imageHeight = (NSInteger)round(imageSize.height); NSInteger imageWidth = (NSInteger)round(imageSize.width);
if (imageWidth > 0 && imageHeight > 0) { NSInteger imageHeight = (NSInteger)round(imageSize.height);
[builder setWidth:(UInt32)imageWidth]; if (imageWidth > 0 && imageHeight > 0) {
[builder setHeight:(UInt32)imageHeight]; [builder setWidth:(UInt32)imageWidth];
[builder setHeight:(UInt32)imageHeight];
}
} }
} }

@ -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