96 lines
3.3 KiB
Objective-C
96 lines
3.3 KiB
Objective-C
//
|
|
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
|
|
//
|
|
|
|
#import "OWSSyncGroupsMessage.h"
|
|
#import "OWSGroupsOutputStream.h"
|
|
#import "TSAttachment.h"
|
|
#import "TSAttachmentStream.h"
|
|
#import "TSContactThread.h"
|
|
#import "TSGroupModel.h"
|
|
#import "TSGroupThread.h"
|
|
#import <SignalCoreKit/NSDate+OWS.h>
|
|
#import <SignalServiceKit/SignalServiceKit-Swift.h>
|
|
|
|
NS_ASSUME_NONNULL_BEGIN
|
|
|
|
@implementation OWSSyncGroupsMessage
|
|
|
|
- (instancetype)init
|
|
{
|
|
return [super init];
|
|
}
|
|
|
|
- (nullable instancetype)initWithCoder:(NSCoder *)coder
|
|
{
|
|
return [super initWithCoder:coder];
|
|
}
|
|
|
|
- (nullable SSKProtoSyncMessageBuilder *)syncMessageBuilder
|
|
{
|
|
if (self.attachmentIds.count != 1) {
|
|
OWSLogError(@"expected sync groups message to have exactly one attachment, but found %lu",
|
|
(unsigned long)self.attachmentIds.count);
|
|
}
|
|
|
|
SSKProtoAttachmentPointer *_Nullable attachmentProto =
|
|
[TSAttachmentStream buildProtoForAttachmentId:self.attachmentIds.firstObject];
|
|
if (!attachmentProto) {
|
|
OWSFailDebug(@"could not build protobuf.");
|
|
return nil;
|
|
}
|
|
|
|
SSKProtoSyncMessageGroupsBuilder *groupsBuilder =
|
|
[SSKProtoSyncMessageGroupsBuilder new];
|
|
[groupsBuilder setBlob:attachmentProto];
|
|
|
|
NSError *error;
|
|
SSKProtoSyncMessageGroups *_Nullable groupsProto = [groupsBuilder buildAndReturnError:&error];
|
|
if (error || !groupsProto) {
|
|
OWSFailDebug(@"could not build protobuf: %@", error);
|
|
return nil;
|
|
}
|
|
|
|
SSKProtoSyncMessageBuilder *syncMessageBuilder = [SSKProtoSyncMessageBuilder new];
|
|
[syncMessageBuilder setGroups:groupsProto];
|
|
|
|
return syncMessageBuilder;
|
|
}
|
|
|
|
- (nullable 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 alloc] initWithOutputStream:dataOutputStream];
|
|
|
|
[TSGroupThread
|
|
enumerateCollectionObjectsWithTransaction:transaction
|
|
usingBlock:^(id obj, BOOL *stop) {
|
|
if (![obj isKindOfClass:[TSGroupThread class]]) {
|
|
if (![obj isKindOfClass:[TSContactThread class]]) {
|
|
OWSLogWarn(
|
|
@"Ignoring non group thread in thread collection: %@", obj);
|
|
}
|
|
return;
|
|
}
|
|
TSGroupThread *groupThread = (TSGroupThread *)obj;
|
|
[groupsOutputStream writeGroup:groupThread transaction:transaction];
|
|
}];
|
|
|
|
[dataOutputStream close];
|
|
|
|
if (groupsOutputStream.hasError) {
|
|
OWSFailDebug(@"Could not write groups sync stream.");
|
|
return nil;
|
|
}
|
|
|
|
return [dataOutputStream propertyForKey:NSStreamDataWrittenToMemoryStreamKey];
|
|
}
|
|
|
|
@end
|
|
|
|
NS_ASSUME_NONNULL_END
|