session-ios/SignalServiceKit/src/Messages/DeviceSyncing/OWSSyncGroupsMessage.m

87 lines
3.1 KiB
Mathematica
Raw Normal View History

2017-04-13 18:54:03 +02:00
//
2018-02-02 16:56:16 +01:00
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
2017-04-13 18:54:03 +02:00
//
#import "OWSSyncGroupsMessage.h"
#import "NSDate+OWS.h"
#import "OWSGroupsOutputStream.h"
2016-08-29 00:31:27 +02:00
#import "TSAttachment.h"
#import "TSAttachmentStream.h"
2018-06-14 18:47:47 +02:00
#import "TSContactThread.h"
2016-08-29 00:31:27 +02:00
#import "TSGroupModel.h"
#import "TSGroupThread.h"
#import <SignalServiceKit/SignalServiceKit-Swift.h>
NS_ASSUME_NONNULL_BEGIN
@implementation OWSSyncGroupsMessage
2016-08-29 00:31:27 +02:00
- (instancetype)init
{
2018-05-11 16:36:40 +02:00
return [super init];
2016-08-29 00:31:27 +02:00
}
- (nullable instancetype)initWithCoder:(NSCoder *)coder
{
return [super initWithCoder:coder];
}
- (nullable SSKProtoSyncMessageBuilder *)syncMessageBuilder
{
if (self.attachmentIds.count != 1) {
2016-08-29 00:31:27 +02:00
DDLogError(@"expected sync groups message to have exactly one attachment, but found %lu",
(unsigned long)self.attachmentIds.count);
}
SSKProtoAttachmentPointer *attachmentProto = [TSAttachmentStream buildProtoForAttachmentId:self.attachmentIds.firstObject];
2016-09-03 01:44:51 +02:00
SSKProtoSyncMessageGroupsBuilder *groupsBuilder =
[SSKProtoSyncMessageGroupsBuilder new];
2016-09-03 01:44:51 +02:00
[groupsBuilder setBlob:attachmentProto];
NSError *error;
SSKProtoSyncMessageGroups *_Nullable groupsProto = [groupsBuilder buildAndReturnError:&error];
if (error || !groupsProto) {
OWSFail(@"%@ could not build protobuf: %@", self.logTag, error);
return nil;
}
SSKProtoSyncMessageBuilder *syncMessageBuilder = [SSKProtoSyncMessageBuilder new];
[syncMessageBuilder setGroups:groupsProto];
return syncMessageBuilder;
}
- (NSData *)buildPlainTextAttachmentDataWithTransaction:(YapDatabaseReadTransaction *)transaction
{
// TODO use temp file stream to avoid loading everything into memory at once
// First though, we need to re-engineer our attachment process to accept streams (encrypting with stream,
// and uploading with streams).
NSOutputStream *dataOutputStream = [NSOutputStream outputStreamToMemory];
[dataOutputStream open];
OWSGroupsOutputStream *groupsOutputStream = [OWSGroupsOutputStream streamWithOutputStream:dataOutputStream];
2017-11-06 17:52:28 +01:00
[TSGroupThread
enumerateCollectionObjectsWithTransaction:transaction
usingBlock:^(id obj, BOOL *stop) {
if (![obj isKindOfClass:[TSGroupThread class]]) {
2018-06-14 18:47:47 +02:00
if (![obj isKindOfClass:[TSContactThread class]]) {
DDLogWarn(
@"Ignoring non group thread in thread collection: %@", obj);
}
2017-11-06 17:52:28 +01:00
return;
}
2018-03-01 19:41:15 +01:00
TSGroupThread *groupThread = (TSGroupThread *)obj;
[groupsOutputStream writeGroup:groupThread transaction:transaction];
2017-11-06 17:52:28 +01:00
}];
2016-08-29 00:31:27 +02:00
[groupsOutputStream flush];
[dataOutputStream close];
return [dataOutputStream propertyForKey:NSStreamDataWrittenToMemoryStreamKey];
}
@end
NS_ASSUME_NONNULL_END