Add "is uploaded" property to attachment streams.

// FREEBIE
pull/1/head
Matthew Chen 8 years ago
parent 27fb0dd34c
commit 865d9d7b96

@ -1,5 +1,6 @@
// Created by Frederic Jacobs on 12/11/14. //
// Copyright (c) 2014 Open Whisper Systems. All rights reserved. // Copyright (c) 2017 Open Whisper Systems. All rights reserved.
//
#import "TSYapDatabaseObject.h" #import "TSYapDatabaseObject.h"
@ -19,6 +20,8 @@ NS_ASSUME_NONNULL_BEGIN
encryptionKey:(NSData *)encryptionKey encryptionKey:(NSData *)encryptionKey
contentType:(NSString *)contentType; contentType:(NSString *)contentType;
- (void)upgradeFromAttachmentSchemaVersion:(NSUInteger)attachmentSchemaVersion;
@end @end
NS_ASSUME_NONNULL_END NS_ASSUME_NONNULL_END

@ -1,12 +1,13 @@
// Created by Frederic Jacobs on 12/11/14. //
// Copyright (c) 2014 Open Whisper Systems. All rights reserved. // Copyright (c) 2017 Open Whisper Systems. All rights reserved.
//
#import "TSAttachment.h" #import "TSAttachment.h"
#import "MIMETypeUtil.h" #import "MIMETypeUtil.h"
NS_ASSUME_NONNULL_BEGIN NS_ASSUME_NONNULL_BEGIN
NSUInteger const TSAttachmentSchemaVersion = 2; NSUInteger const TSAttachmentSchemaVersion = 3;
@interface TSAttachment () @interface TSAttachment ()
@ -40,7 +41,17 @@ NSUInteger const TSAttachmentSchemaVersion = 2;
return self; return self;
} }
if (_attachmentSchemaVersion < 2) { if (_attachmentSchemaVersion < TSAttachmentSchemaVersion) {
[self upgradeFromAttachmentSchemaVersion:_attachmentSchemaVersion];
_attachmentSchemaVersion = TSAttachmentSchemaVersion;
}
return self;
}
- (void)upgradeFromAttachmentSchemaVersion:(NSUInteger)attachmentSchemaVersion
{
if (attachmentSchemaVersion < 2) {
if (!_serverId) { if (!_serverId) {
_serverId = [self.uniqueId integerValue]; _serverId = [self.uniqueId integerValue];
if (!_serverId) { if (!_serverId) {
@ -48,10 +59,6 @@ NSUInteger const TSAttachmentSchemaVersion = 2;
} }
} }
} }
_attachmentSchemaVersion = TSAttachmentSchemaVersion;
return self;
} }
+ (NSString *)collection { + (NSString *)collection {

@ -23,6 +23,9 @@ NS_ASSUME_NONNULL_BEGIN
// messages received from other clients // messages received from other clients
@property (nullable, nonatomic) NSData *digest; @property (nullable, nonatomic) NSData *digest;
// This only applies for attachments being uploaded.
@property (atomic) BOOL isUploaded;
#if TARGET_OS_IPHONE #if TARGET_OS_IPHONE
- (nullable UIImage *)image; - (nullable UIImage *)image;
#endif #endif

@ -1,5 +1,6 @@
// Created by Frederic Jacobs on 17/12/14. //
// Copyright (c) 2014 Open Whisper Systems. All rights reserved. // Copyright (c) 2017 Open Whisper Systems. All rights reserved.
//
#import "TSAttachmentStream.h" #import "TSAttachmentStream.h"
#import "MIMETypeUtil.h" #import "MIMETypeUtil.h"
@ -20,6 +21,10 @@ NS_ASSUME_NONNULL_BEGIN
_contentType = contentType; _contentType = contentType;
_isDownloaded = YES; _isDownloaded = YES;
// TSAttachmentStream doesn't have any "incoming vs. outgoing"
// state, but this constructor is used only for new outgoing
// attachments which haven't been uploaded yet.
_isUploaded = NO;
return self; return self;
} }
@ -34,10 +39,27 @@ NS_ASSUME_NONNULL_BEGIN
_contentType = pointer.contentType; _contentType = pointer.contentType;
_isDownloaded = YES; _isDownloaded = YES;
// TSAttachmentStream doesn't have any "incoming vs. outgoing"
// state, but this constructor is used only for new incoming
// attachments which don't need to be uploaded.
_isUploaded = YES;
return self; return self;
} }
- (void)upgradeFromAttachmentSchemaVersion:(NSUInteger)attachmentSchemaVersion
{
[super upgradeFromAttachmentSchemaVersion:attachmentSchemaVersion];
if (attachmentSchemaVersion < 3) {
// We want to treat any legacy TSAttachmentStream as though
// they have already been uploaded. If it needs to be reuploaded,
// the OWSUploadingService will update this progress when the
// upload begins.
self.isUploaded = YES;
}
}
#pragma mark - TSYapDatabaseModel overrides #pragma mark - TSYapDatabaseModel overrides
- (void)removeWithTransaction:(YapDatabaseReadWriteTransaction *)transaction - (void)removeWithTransaction:(YapDatabaseReadWriteTransaction *)transaction

@ -12,9 +12,17 @@ NS_ASSUME_NONNULL_BEGIN
@interface TSOutgoingMessage : TSMessage @interface TSOutgoingMessage : TSMessage
typedef NS_ENUM(NSInteger, TSOutgoingMessageState) { typedef NS_ENUM(NSInteger, TSOutgoingMessageState) {
// The message is either:
// a) Enqueued for sending.
// b) Waiting on attachment upload(s).
// c) Being sent to the service.
TSOutgoingMessageStateAttemptingOut, TSOutgoingMessageStateAttemptingOut,
// The failure state.
TSOutgoingMessageStateUnsent, TSOutgoingMessageStateUnsent,
// The message has been sent to the service, but not received by any recipient client.
TSOutgoingMessageStateSent, TSOutgoingMessageStateSent,
// The message has been sent to the service, but not received by at least one recipient client.
// A recipient may have more than one client, and group message may have more than one recipient.
TSOutgoingMessageStateDelivered TSOutgoingMessageStateDelivered
}; };

@ -1,5 +1,6 @@
// Created by Michael Kirk on 10/18/16. //
// Copyright © 2016 Open Whisper Systems. All rights reserved. // Copyright (c) 2017 Open Whisper Systems. All rights reserved.
//
NS_ASSUME_NONNULL_BEGIN NS_ASSUME_NONNULL_BEGIN
@ -7,6 +8,10 @@ NS_ASSUME_NONNULL_BEGIN
@class TSNetworkManager; @class TSNetworkManager;
@class TSAttachmentStream; @class TSAttachmentStream;
extern NSString *const kAttachmentUploadProgressNotification;
extern NSString *const kAttachmentUploadProgressKey;
extern NSString *const kAttachmentUploadAttachmentIDKey;
@interface OWSUploadingService : NSObject @interface OWSUploadingService : NSObject
- (instancetype)init NS_UNAVAILABLE; - (instancetype)init NS_UNAVAILABLE;

@ -12,6 +12,10 @@
NS_ASSUME_NONNULL_BEGIN NS_ASSUME_NONNULL_BEGIN
NSString *const kAttachmentUploadProgressNotification = @"kAttachmentUploadProgressNotification";
NSString *const kAttachmentUploadProgressKey = @"kAttachmentUploadProgressKey";
NSString *const kAttachmentUploadAttachmentIDKey = @"kAttachmentUploadAttachmentIDKey";
@interface OWSUploadingService () @interface OWSUploadingService ()
@property (nonatomic, readonly) TSNetworkManager *networkManager; @property (nonatomic, readonly) TSNetworkManager *networkManager;
@ -79,9 +83,11 @@ NS_ASSUME_NONNULL_BEGIN
location:location location:location
attachmentId:attachmentStream.uniqueId attachmentId:attachmentStream.uniqueId
success:^{ success:^{
DDLogInfo(@"%@ Uploaded attachment.", self.tag); OWSAssert([NSThread isMainThread]);
DDLogInfo(@"%@ Uploaded attachment: %p.", self.tag, attachmentStream);
attachmentStream.serverId = serverId; attachmentStream.serverId = serverId;
attachmentStream.isDownloaded = YES; attachmentStream.isUploaded = YES;
[attachmentStream save]; [attachmentStream save];
successHandler(); successHandler();
@ -111,20 +117,18 @@ NS_ASSUME_NONNULL_BEGIN
AFURLSessionManager *manager = [[AFURLSessionManager alloc] AFURLSessionManager *manager = [[AFURLSessionManager alloc]
initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]]; initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
[self fireProgressNotification:0 attachmentId:attachmentId];
NSURLSessionUploadTask *uploadTask; NSURLSessionUploadTask *uploadTask;
uploadTask = [manager uploadTaskWithRequest:request uploadTask = [manager uploadTaskWithRequest:request
fromData:cipherText fromData:cipherText
progress:^(NSProgress *_Nonnull uploadProgress) { progress:^(NSProgress *_Nonnull uploadProgress) {
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter]; [self fireProgressNotification:uploadProgress.fractionCompleted attachmentId:attachmentId];
[notificationCenter postNotificationName:@"attachmentUploadProgress"
object:nil
userInfo:@{
@"progress" : @(uploadProgress.fractionCompleted),
@"attachmentId" : attachmentId
}];
} }
completionHandler:^(NSURLResponse *_Nonnull response, id _Nullable responseObject, NSError *_Nullable error) { completionHandler:^(NSURLResponse *_Nonnull response, id _Nullable responseObject, NSError *_Nullable error) {
OWSAssert([NSThread isMainThread]);
if (error) { if (error) {
[self fireProgressNotification:0 attachmentId:attachmentId];
return failureHandler(error); return failureHandler(error);
} }
@ -137,11 +141,26 @@ NS_ASSUME_NONNULL_BEGIN
} }
successHandler(); successHandler();
[self fireProgressNotification:1 attachmentId:attachmentId];
}]; }];
[uploadTask resume]; [uploadTask resume];
} }
- (void)fireProgressNotification:(CGFloat)progress attachmentId:(NSString *)attachmentId
{
dispatch_async(dispatch_get_main_queue(), ^{
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
[notificationCenter postNotificationName:kAttachmentUploadProgressNotification
object:nil
userInfo:@{
kAttachmentUploadProgressKey : @(progress),
kAttachmentUploadAttachmentIDKey : attachmentId
}];
});
}
#pragma mark - Logging #pragma mark - Logging
+ (NSString *)tag + (NSString *)tag

Loading…
Cancel
Save