mirror of
https://github.com/oxen-io/session-ios.git
synced 2023-12-13 21:30:14 +01:00
176 lines
5.4 KiB
Objective-C
176 lines
5.4 KiB
Objective-C
//
|
|
// Copyright (c) 2017 Open Whisper Systems. All rights reserved.
|
|
//
|
|
|
|
#import "TSIncomingMessage.h"
|
|
#import "NSNotificationCenter+OWS.h"
|
|
#import "OWSDisappearingMessagesConfiguration.h"
|
|
#import "OWSDisappearingMessagesJob.h"
|
|
#import "OWSReadReceiptManager.h"
|
|
#import "TSAttachmentPointer.h"
|
|
#import "TSContactThread.h"
|
|
#import "TSDatabaseSecondaryIndexes.h"
|
|
#import "TSGroupThread.h"
|
|
#import <YapDatabase/YapDatabaseConnection.h>
|
|
|
|
NS_ASSUME_NONNULL_BEGIN
|
|
|
|
@interface TSIncomingMessage ()
|
|
|
|
@property (nonatomic, getter=wasRead) BOOL read;
|
|
|
|
@end
|
|
|
|
#pragma mark -
|
|
|
|
@implementation TSIncomingMessage
|
|
|
|
- (instancetype)initWithCoder:(NSCoder *)coder
|
|
{
|
|
self = [super initWithCoder:coder];
|
|
if (!self) {
|
|
return self;
|
|
}
|
|
|
|
return self;
|
|
}
|
|
|
|
- (instancetype)initWithTimestamp:(uint64_t)timestamp
|
|
inThread:(TSThread *)thread
|
|
authorId:(NSString *)authorId
|
|
sourceDeviceId:(uint32_t)sourceDeviceId
|
|
messageBody:(nullable NSString *)body
|
|
{
|
|
return [self initWithTimestamp:timestamp
|
|
inThread:thread
|
|
authorId:authorId
|
|
sourceDeviceId:sourceDeviceId
|
|
messageBody:body
|
|
attachmentIds:@[]
|
|
expiresInSeconds:0];
|
|
}
|
|
|
|
- (instancetype)initWithTimestamp:(uint64_t)timestamp
|
|
inThread:(TSThread *)thread
|
|
authorId:(NSString *)authorId
|
|
sourceDeviceId:(uint32_t)sourceDeviceId
|
|
messageBody:(nullable NSString *)body
|
|
attachmentIds:(NSArray<NSString *> *)attachmentIds
|
|
expiresInSeconds:(uint32_t)expiresInSeconds
|
|
{
|
|
self = [super initWithTimestamp:timestamp
|
|
inThread:thread
|
|
messageBody:body
|
|
attachmentIds:attachmentIds
|
|
expiresInSeconds:expiresInSeconds
|
|
expireStartedAt:0];
|
|
|
|
if (!self) {
|
|
return self;
|
|
}
|
|
|
|
_authorId = authorId;
|
|
_sourceDeviceId = sourceDeviceId;
|
|
_read = NO;
|
|
|
|
return self;
|
|
}
|
|
|
|
+ (nullable instancetype)findMessageWithAuthorId:(NSString *)authorId
|
|
timestamp:(uint64_t)timestamp
|
|
transaction:(YapDatabaseReadWriteTransaction *)transaction
|
|
{
|
|
OWSAssert(transaction);
|
|
|
|
__block TSIncomingMessage *foundMessage;
|
|
// In theory we could build a new secondaryIndex for (authorId,timestamp), but in practice there should
|
|
// be *very* few (millisecond) timestamps with multiple authors.
|
|
[TSDatabaseSecondaryIndexes
|
|
enumerateMessagesWithTimestamp:timestamp
|
|
withBlock:^(NSString *collection, NSString *key, BOOL *stop) {
|
|
TSInteraction *interaction =
|
|
[TSInteraction fetchObjectWithUniqueID:key transaction:transaction];
|
|
if ([interaction isKindOfClass:[TSIncomingMessage class]]) {
|
|
TSIncomingMessage *message = (TSIncomingMessage *)interaction;
|
|
|
|
NSString *messageAuthorId = message.messageAuthorId;
|
|
OWSAssert(messageAuthorId.length > 0);
|
|
|
|
if ([messageAuthorId isEqualToString:authorId]) {
|
|
foundMessage = message;
|
|
}
|
|
}
|
|
}
|
|
usingTransaction:transaction];
|
|
|
|
return foundMessage;
|
|
}
|
|
|
|
- (NSString *)messageAuthorId
|
|
{
|
|
// authorId isn't set on all legacy messages, so we take
|
|
// extra measures to ensure we obtain a valid value.
|
|
NSString *messageAuthorId;
|
|
if (self.authorId) {
|
|
// Group Thread
|
|
messageAuthorId = self.authorId;
|
|
} else {
|
|
// Contact Thread
|
|
messageAuthorId = [TSContactThread contactIdFromThreadId:self.uniqueThreadId];
|
|
}
|
|
OWSAssert(messageAuthorId.length > 0);
|
|
return messageAuthorId;
|
|
}
|
|
|
|
- (OWSInteractionType)interactionType
|
|
{
|
|
return OWSInteractionType_IncomingMessage;
|
|
}
|
|
|
|
- (BOOL)shouldStartExpireTimer:(YapDatabaseReadTransaction *)transaction
|
|
{
|
|
for (NSString *attachmentId in self.attachmentIds) {
|
|
TSAttachment *_Nullable attachment =
|
|
[TSAttachment fetchObjectWithUniqueID:attachmentId transaction:transaction];
|
|
if ([attachment isKindOfClass:[TSAttachmentPointer class]]) {
|
|
return NO;
|
|
}
|
|
}
|
|
return self.isExpiringMessage;
|
|
}
|
|
|
|
#pragma mark - OWSReadTracking
|
|
|
|
- (BOOL)shouldAffectUnreadCounts
|
|
{
|
|
return YES;
|
|
}
|
|
|
|
- (void)markAsReadWithTransaction:(YapDatabaseReadWriteTransaction *)transaction
|
|
sendReadReceipt:(BOOL)sendReadReceipt
|
|
updateExpiration:(BOOL)updateExpiration
|
|
{
|
|
OWSAssert(transaction);
|
|
|
|
if (_read) {
|
|
return;
|
|
}
|
|
|
|
DDLogDebug(
|
|
@"%@ marking as read uniqueId: %@ which has timestamp: %llu", self.logTag, self.uniqueId, self.timestamp);
|
|
_read = YES;
|
|
[self saveWithTransaction:transaction];
|
|
[self touchThreadWithTransaction:transaction];
|
|
|
|
if (updateExpiration) {
|
|
[OWSDisappearingMessagesJob setExpirationForMessage:self];
|
|
}
|
|
|
|
if (sendReadReceipt) {
|
|
[OWSReadReceiptManager.sharedManager messageWasReadLocally:self];
|
|
}
|
|
}
|
|
|
|
@end
|
|
|
|
NS_ASSUME_NONNULL_END
|