|
|
|
@ -4,74 +4,171 @@
|
|
|
|
|
|
|
|
|
|
#import "TSQuotedMessage.h"
|
|
|
|
|
#import "TSAttachment.h"
|
|
|
|
|
#import "TSAttachmentStream.h"
|
|
|
|
|
|
|
|
|
|
NS_ASSUME_NONNULL_BEGIN
|
|
|
|
|
|
|
|
|
|
@implementation TSQuotedMessage
|
|
|
|
|
@implementation OWSAttachmentInfo
|
|
|
|
|
|
|
|
|
|
- (instancetype)initWithTimestamp:(uint64_t)timestamp
|
|
|
|
|
authorId:(NSString *)authorId
|
|
|
|
|
body:(NSString *_Nullable)body
|
|
|
|
|
sourceFilename:(NSString *_Nullable)sourceFilename
|
|
|
|
|
thumbnailData:(NSData *_Nullable)thumbnailData
|
|
|
|
|
contentType:(NSString *_Nullable)contentType
|
|
|
|
|
- (instancetype)initWithAttachment:(TSAttachment *)attachment
|
|
|
|
|
{
|
|
|
|
|
self = [super initWithUniqueId:[NSUUID UUID].UUIDString];
|
|
|
|
|
self = [super init];
|
|
|
|
|
if (!self) {
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
OWSAssert(attachment.uniqueId);
|
|
|
|
|
OWSAssert(attachment.contentType);
|
|
|
|
|
|
|
|
|
|
_attachmentId = attachment.uniqueId;
|
|
|
|
|
_contentType = attachment.contentType;
|
|
|
|
|
|
|
|
|
|
// maybe nil
|
|
|
|
|
_sourceFilename = attachment.sourceFilename;
|
|
|
|
|
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
@interface TSQuotedMessage ()
|
|
|
|
|
|
|
|
|
|
@property (atomic) NSArray<OWSAttachmentInfo *> *thumbnailAttachments;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
@implementation TSQuotedMessage
|
|
|
|
|
|
|
|
|
|
- (instancetype)initOutgoingWithTimestamp:(uint64_t)timestamp
|
|
|
|
|
authorId:(NSString *)authorId
|
|
|
|
|
body:(NSString *_Nullable)body
|
|
|
|
|
attachment:(TSAttachmentStream *_Nullable)attachmentStream
|
|
|
|
|
{
|
|
|
|
|
return [self initWithTimestamp:timestamp authorId:authorId body:body attachments:@[ attachmentStream ]];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (instancetype)initIncomingWithTimestamp:(uint64_t)timestamp
|
|
|
|
|
authorId:(NSString *)authorId
|
|
|
|
|
body:(NSString *_Nullable)body
|
|
|
|
|
attachments:(NSArray<TSAttachment *> *)attachments
|
|
|
|
|
{
|
|
|
|
|
return [self initWithTimestamp:timestamp authorId:authorId body:body attachments:attachments];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (instancetype)initWithTimestamp:(uint64_t)timestamp
|
|
|
|
|
authorId:(NSString *)authorId
|
|
|
|
|
body:(NSString *_Nullable)body
|
|
|
|
|
attachments:(NSArray<TSAttachment *> *)attachments
|
|
|
|
|
{
|
|
|
|
|
OWSAssert(timestamp > 0);
|
|
|
|
|
OWSAssert(authorId.length > 0);
|
|
|
|
|
|
|
|
|
|
self = [super init];
|
|
|
|
|
if (!self) {
|
|
|
|
|
return nil;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_timestamp = timestamp;
|
|
|
|
|
_authorId = authorId;
|
|
|
|
|
_body = body;
|
|
|
|
|
// TODO get source filename from attachment
|
|
|
|
|
// _sourceFilename = sourceFilename;
|
|
|
|
|
// _thumbnailData = thumbnailData;
|
|
|
|
|
_contentType = contentType;
|
|
|
|
|
|
|
|
|
|
NSMutableArray *attachmentInfos = [NSMutableArray new];
|
|
|
|
|
for (TSAttachment *attachment in attachments) {
|
|
|
|
|
[attachmentInfos addObject:[[OWSAttachmentInfo alloc] initWithAttachment:attachment]];
|
|
|
|
|
}
|
|
|
|
|
_thumbnailAttachments = [attachmentInfos copy];
|
|
|
|
|
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO maybe this should live closer to the view
|
|
|
|
|
- (nullable UIImage *)thumbnailImage
|
|
|
|
|
- (nullable TSAttachment *)firstAttachmentWithTransaction:(YapDatabaseReadTransaction *)transaction
|
|
|
|
|
{
|
|
|
|
|
// if (self.thumbnailData.length == 0) {
|
|
|
|
|
// return nil;
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// // PERF TODO cache
|
|
|
|
|
// return [UIImage imageWithData:self.thumbnailData];
|
|
|
|
|
return nil;
|
|
|
|
|
OWSAttachmentInfo *attachmentInfo = self.firstAttachmentInfo;
|
|
|
|
|
if (!attachmentInfo) {
|
|
|
|
|
return nil;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return [TSAttachment fetchObjectWithUniqueID:attachmentInfo.attachmentId];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//- (void)setThumbnailAttachmentId:(NSString *)thumbnailAttachmentId
|
|
|
|
|
//{
|
|
|
|
|
// _thumbnailAttachmentId = thumbnailAttachmentId;
|
|
|
|
|
//}
|
|
|
|
|
//
|
|
|
|
|
//- (BOOL)hasThumbnailAttachment
|
|
|
|
|
//{
|
|
|
|
|
// return self.thumbnailAttachmentId.length > 0;
|
|
|
|
|
//}
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
- (BOOL)hasThumbnailAttachments
|
|
|
|
|
- (nullable OWSAttachmentInfo *)firstAttachmentInfo
|
|
|
|
|
{
|
|
|
|
|
return self.thumbnailAttachmentIds.count > 0;
|
|
|
|
|
return self.attachmentInfos.firstObject;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (nullable TSAttachment *)firstThumbnailAttachmentWithTransaction:(YapDatabaseReadTransaction *)transaction;
|
|
|
|
|
- (nullable UIImage *)thumbnailImageWithTransaction:(YapDatabaseReadTransaction *)transaction
|
|
|
|
|
{
|
|
|
|
|
if (!self.hasThumbnailAttachments) {
|
|
|
|
|
TSAttachmentStream *firstAttachment = (TSAttachmentStream *)self.firstThumbnailAttachment;
|
|
|
|
|
if (![firstAttachment isKindOfClass:[TSAttachmentStream class]]) {
|
|
|
|
|
return nil;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return firstAttachment.thumbnailImage;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (nullable NSString *)contentType
|
|
|
|
|
{
|
|
|
|
|
OWSAttachmentInfo *firstAttachment = self.firstThumbnailAttachment;
|
|
|
|
|
|
|
|
|
|
return firstAttachment.contentType;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (BOOL)hasThumbnailAttachments
|
|
|
|
|
{
|
|
|
|
|
return self.thumbnailAttachments.count > 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)addThumbnailAttachment:(TSAttachmentStream *)attachment
|
|
|
|
|
{
|
|
|
|
|
NSMutableArray<OWSAttachmentInfo *> *existingAttachments = [self.thumbnailAttachments mutableCopy];
|
|
|
|
|
|
|
|
|
|
OWSAttachmentInfo *attachmentInfo = [[OWSAttachmentInfo alloc] initWithAttachment:attachment];
|
|
|
|
|
[existingAttachments addObject:attachmentInfo];
|
|
|
|
|
|
|
|
|
|
self.thumbnailAttachments = [existingAttachments copy];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (nullable OWSAttachmentInfo *)firstThumbnailAttachment
|
|
|
|
|
{
|
|
|
|
|
return self.thumbnailAttachments.firstObject;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (TSAttachmentStream *)thumbnailAttachmentWithTransaction:(YapDatabaseReadTransaction *)transaction
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
return [TSAttachment fetchObjectWithUniqueID:self.thumbnailAttachmentIds.firstObject transaction:transaction];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)createThumbnailAttachmentIfNecessaryWithTransaction:(YapDatabaseReadWriteTransaction *)transaction
|
|
|
|
|
{
|
|
|
|
|
// OWSAssert([attachment isKindOfClass:[TSAttachmentStream class]]);
|
|
|
|
|
// UIImage *thumbnailImage = attachment.thumbnailImage;
|
|
|
|
|
// // Only some media types have thumbnails
|
|
|
|
|
// if (thumbnailImage) {
|
|
|
|
|
// // Copy the thumbnail to a new attachment.
|
|
|
|
|
// TSAttachmentStream *thumbnailAttachment =
|
|
|
|
|
// [[TSAttachmentStream alloc] initWithContentType:attachment.contentType
|
|
|
|
|
// byteCount:attachment.byteCount
|
|
|
|
|
// sourceFilename:attachment.sourceFilename];
|
|
|
|
|
//
|
|
|
|
|
// NSError *error;
|
|
|
|
|
// NSData *_Nullable data = [attachment readDataFromFileWithError:&error];
|
|
|
|
|
// if (!data || error) {
|
|
|
|
|
// DDLogError(@"%@ Couldn't load attachment data for message sent to self: %@.", self.logTag, error);
|
|
|
|
|
// } else {
|
|
|
|
|
// [thumbnailAttachment writeData:data error:&error];
|
|
|
|
|
// if (error) {
|
|
|
|
|
// DDLogError(
|
|
|
|
|
// @"%@ Couldn't copy attachment data for message sent to self: %@.", self.logTag, error);
|
|
|
|
|
// } else {
|
|
|
|
|
// [self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *_Nonnull transaction) {
|
|
|
|
|
// [thumbnailAttachment saveWithTransaction:transaction];
|
|
|
|
|
// quotedMessage.attachments =
|
|
|
|
|
// [message saveWithTransaction:transaction];
|
|
|
|
|
// }];
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
}
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
NS_ASSUME_NONNULL_END
|
|
|
|
|