|
|
|
@ -7,6 +7,7 @@
|
|
|
|
|
#import "Signal-Swift.h"
|
|
|
|
|
#import "TSUnreadIndicatorInteraction.h"
|
|
|
|
|
#import <SignalServiceKit/NSDate+millisecondTimeStamp.h>
|
|
|
|
|
#import <SignalServiceKit/OWSAddToContactsOfferMessage.h>
|
|
|
|
|
#import <SignalServiceKit/OWSBlockingManager.h>
|
|
|
|
|
#import <SignalServiceKit/OWSDisappearingMessagesConfiguration.h>
|
|
|
|
|
#import <SignalServiceKit/OWSMessageSender.h>
|
|
|
|
@ -17,6 +18,12 @@
|
|
|
|
|
|
|
|
|
|
NS_ASSUME_NONNULL_BEGIN
|
|
|
|
|
|
|
|
|
|
@implementation ThreadOffersAndIndicators
|
|
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
#pragma mark -
|
|
|
|
|
|
|
|
|
|
@implementation ThreadUtil
|
|
|
|
|
|
|
|
|
|
+ (void)sendMessageWithText:(NSString *)text inThread:(TSThread *)thread messageSender:(OWSMessageSender *)messageSender
|
|
|
|
@ -74,44 +81,45 @@ NS_ASSUME_NONNULL_BEGIN
|
|
|
|
|
}];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
+ (void)createBlockOfferIfNecessary:(TSContactThread *)contactThread
|
|
|
|
|
storageManager:(TSStorageManager *)storageManager
|
|
|
|
|
contactsManager:(OWSContactsManager *)contactsManager
|
|
|
|
|
blockingManager:(OWSBlockingManager *)blockingManager
|
|
|
|
|
+ (ThreadOffersAndIndicators *)ensureThreadOffersAndIndicators:(TSThread *)thread
|
|
|
|
|
storageManager:(TSStorageManager *)storageManager
|
|
|
|
|
contactsManager:(OWSContactsManager *)contactsManager
|
|
|
|
|
blockingManager:(OWSBlockingManager *)blockingManager
|
|
|
|
|
hideUnreadMessagesIndicator:(BOOL)hideUnreadMessagesIndicator
|
|
|
|
|
fixedUnreadIndicatorTimestamp:(NSNumber *_Nullable)fixedUnreadIndicatorTimestamp
|
|
|
|
|
{
|
|
|
|
|
OWSAssert(contactThread);
|
|
|
|
|
OWSAssert(thread);
|
|
|
|
|
OWSAssert(storageManager);
|
|
|
|
|
OWSAssert(contactsManager);
|
|
|
|
|
OWSAssert(blockingManager);
|
|
|
|
|
|
|
|
|
|
if ([[blockingManager blockedPhoneNumbers] containsObject:contactThread.contactIdentifier]) {
|
|
|
|
|
// Only create block offers for users which are not already blocked.
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SignalAccount *signalAccount = contactsManager.signalAccountMap[contactThread.contactIdentifier];
|
|
|
|
|
if (signalAccount) {
|
|
|
|
|
// Only create block offers for non-contacts.
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
ThreadOffersAndIndicators *result = [ThreadOffersAndIndicators new];
|
|
|
|
|
|
|
|
|
|
[storageManager.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
|
|
|
|
|
const int kMaxOutgoingMessageCount = 10;
|
|
|
|
|
const int kMaxBlockOfferOutgoingMessageCount = 10;
|
|
|
|
|
|
|
|
|
|
__block OWSAddToContactsOfferMessage *existingAddToContactsOffer = nil;
|
|
|
|
|
__block OWSUnknownContactBlockOfferMessage *existingBlockOffer = nil;
|
|
|
|
|
__block TSUnreadIndicatorInteraction *existingUnreadIndicator = nil;
|
|
|
|
|
__block TSIncomingMessage *firstIncomingMessage = nil;
|
|
|
|
|
__block TSOutgoingMessage *firstOutgoingMessage = nil;
|
|
|
|
|
__block TSIncomingMessage *firstUnreadMessage = nil;
|
|
|
|
|
__block long outgoingMessageCount = 0;
|
|
|
|
|
__block BOOL hasUnknownContactBlockOffer = NO;
|
|
|
|
|
|
|
|
|
|
[[transaction ext:TSMessageDatabaseViewExtensionName]
|
|
|
|
|
enumerateRowsInGroup:contactThread.uniqueId
|
|
|
|
|
enumerateRowsInGroup:thread.uniqueId
|
|
|
|
|
usingBlock:^(
|
|
|
|
|
NSString *collection, NSString *key, id object, id metadata, NSUInteger index, BOOL *stop) {
|
|
|
|
|
|
|
|
|
|
if ([object isKindOfClass:[OWSUnknownContactBlockOfferMessage class]]) {
|
|
|
|
|
hasUnknownContactBlockOffer = YES;
|
|
|
|
|
// If there already is a block offer, abort.
|
|
|
|
|
*stop = YES;
|
|
|
|
|
OWSAssert(!existingBlockOffer);
|
|
|
|
|
existingBlockOffer = (OWSUnknownContactBlockOfferMessage *)object;
|
|
|
|
|
} else if ([object isKindOfClass:[OWSAddToContactsOfferMessage class]]) {
|
|
|
|
|
OWSAssert(!existingAddToContactsOffer);
|
|
|
|
|
existingAddToContactsOffer = (OWSAddToContactsOfferMessage *)object;
|
|
|
|
|
} else if ([object isKindOfClass:[TSUnreadIndicatorInteraction class]]) {
|
|
|
|
|
OWSAssert(!existingUnreadIndicator);
|
|
|
|
|
existingUnreadIndicator = (TSUnreadIndicatorInteraction *)object;
|
|
|
|
|
} else if ([object isKindOfClass:[TSIncomingMessage class]]) {
|
|
|
|
|
TSIncomingMessage *incomingMessage = (TSIncomingMessage *)object;
|
|
|
|
|
if (!firstIncomingMessage) {
|
|
|
|
@ -121,6 +129,16 @@ NS_ASSUME_NONNULL_BEGIN
|
|
|
|
|
compare:[incomingMessage receiptDateForSorting]]
|
|
|
|
|
== NSOrderedAscending);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!incomingMessage.wasRead) {
|
|
|
|
|
if (!firstUnreadMessage) {
|
|
|
|
|
firstUnreadMessage = incomingMessage;
|
|
|
|
|
} else {
|
|
|
|
|
OWSAssert([[firstUnreadMessage receiptDateForSorting]
|
|
|
|
|
compare:[incomingMessage receiptDateForSorting]]
|
|
|
|
|
== NSOrderedAscending);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else if ([object isKindOfClass:[TSOutgoingMessage class]]) {
|
|
|
|
|
TSOutgoingMessage *outgoingMessage = (TSOutgoingMessage *)object;
|
|
|
|
|
if (!firstOutgoingMessage) {
|
|
|
|
@ -131,26 +149,53 @@ NS_ASSUME_NONNULL_BEGIN
|
|
|
|
|
== NSOrderedAscending);
|
|
|
|
|
}
|
|
|
|
|
outgoingMessageCount++;
|
|
|
|
|
if (outgoingMessageCount > kMaxOutgoingMessageCount) {
|
|
|
|
|
// If the user has sent more than N interactions, abort.
|
|
|
|
|
*stop = YES;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}];
|
|
|
|
|
|
|
|
|
|
if (!firstIncomingMessage && !firstOutgoingMessage) {
|
|
|
|
|
// If the thread has no interactions, abort.
|
|
|
|
|
return;
|
|
|
|
|
TSMessage *firstMessage = firstIncomingMessage;
|
|
|
|
|
if (!firstMessage
|
|
|
|
|
|| (firstOutgoingMessage &&
|
|
|
|
|
[[firstOutgoingMessage receiptDateForSorting] compare:[firstMessage receiptDateForSorting]]
|
|
|
|
|
== NSOrderedAscending)) {
|
|
|
|
|
firstMessage = firstOutgoingMessage;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BOOL shouldHaveBlockOffer = YES;
|
|
|
|
|
BOOL shouldHaveAddToContactsOffer = YES;
|
|
|
|
|
|
|
|
|
|
BOOL isContactThread = [thread isKindOfClass:[TSContactThread class]];
|
|
|
|
|
if (!isContactThread) {
|
|
|
|
|
// Only create "add to contacts" offers in 1:1 conversations.
|
|
|
|
|
shouldHaveAddToContactsOffer = NO;
|
|
|
|
|
// Only create block offers in 1:1 conversations.
|
|
|
|
|
shouldHaveBlockOffer = NO;
|
|
|
|
|
} else {
|
|
|
|
|
NSString *recipientId = ((TSContactThread *)thread).contactIdentifier;
|
|
|
|
|
|
|
|
|
|
if ([[blockingManager blockedPhoneNumbers] containsObject:recipientId]) {
|
|
|
|
|
// Only create "add to contacts" offers for users which are not already blocked.
|
|
|
|
|
shouldHaveAddToContactsOffer = NO;
|
|
|
|
|
// Only create block offers for users which are not already blocked.
|
|
|
|
|
shouldHaveBlockOffer = NO;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SignalAccount *signalAccount = contactsManager.signalAccountMap[recipientId];
|
|
|
|
|
if (signalAccount) {
|
|
|
|
|
// Only create "add to contacts" offers for non-contacts.
|
|
|
|
|
shouldHaveAddToContactsOffer = NO;
|
|
|
|
|
// Only create block offers for non-contacts.
|
|
|
|
|
shouldHaveBlockOffer = NO;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (outgoingMessageCount > kMaxOutgoingMessageCount) {
|
|
|
|
|
// If the user has sent more than N messages, abort.
|
|
|
|
|
return;
|
|
|
|
|
if (!firstMessage) {
|
|
|
|
|
shouldHaveAddToContactsOffer = NO;
|
|
|
|
|
shouldHaveBlockOffer = NO;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (hasUnknownContactBlockOffer) {
|
|
|
|
|
// If there already is a block offer, abort.
|
|
|
|
|
return;
|
|
|
|
|
if (outgoingMessageCount > kMaxBlockOfferOutgoingMessageCount) {
|
|
|
|
|
// If the user has sent more than N messages, don't show a block offer.
|
|
|
|
|
shouldHaveBlockOffer = NO;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BOOL hasOutgoingBeforeIncomingInteraction = (firstOutgoingMessage
|
|
|
|
@ -159,99 +204,87 @@ NS_ASSUME_NONNULL_BEGIN
|
|
|
|
|
== NSOrderedAscending));
|
|
|
|
|
if (hasOutgoingBeforeIncomingInteraction) {
|
|
|
|
|
// If there is an outgoing message before an incoming message
|
|
|
|
|
// the local user initiated this conversation, abort.
|
|
|
|
|
return;
|
|
|
|
|
// the local user initiated this conversation, don't show a block offer.
|
|
|
|
|
shouldHaveBlockOffer = NO;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DDLogInfo(@"Creating block offer for unknown contact");
|
|
|
|
|
// We use these offset to control the ordering of the offers and indicators.
|
|
|
|
|
const int kBlockOfferOffset = -3;
|
|
|
|
|
const int kAddToContactsOfferOffset = -2;
|
|
|
|
|
const int kUnreadIndicatorOfferOffset = -1;
|
|
|
|
|
|
|
|
|
|
if (existingBlockOffer && !shouldHaveBlockOffer) {
|
|
|
|
|
[existingBlockOffer removeWithTransaction:transaction];
|
|
|
|
|
} else if (!existingBlockOffer && shouldHaveBlockOffer) {
|
|
|
|
|
DDLogInfo(@"Creating block offer for unknown contact");
|
|
|
|
|
|
|
|
|
|
// We want the block offer to be the first interaction in their
|
|
|
|
|
// conversation's timeline, so we back-date it to slightly before
|
|
|
|
|
// the first incoming message (which we know is the first message).
|
|
|
|
|
uint64_t blockOfferTimestamp = (uint64_t)((long long)firstMessage.timestamp + kBlockOfferOffset);
|
|
|
|
|
NSString *recipientId = ((TSContactThread *)thread).contactIdentifier;
|
|
|
|
|
|
|
|
|
|
TSMessage *offerMessage =
|
|
|
|
|
[OWSUnknownContactBlockOfferMessage unknownContactBlockOfferMessage:blockOfferTimestamp
|
|
|
|
|
thread:thread
|
|
|
|
|
contactId:recipientId];
|
|
|
|
|
[offerMessage saveWithTransaction:transaction];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// We want the block offer to be the first interaction in their
|
|
|
|
|
// conversation's timeline, so we back-date it to slightly before
|
|
|
|
|
// the first incoming message (which we know is the first message).
|
|
|
|
|
TSIncomingMessage *firstMessage = firstIncomingMessage;
|
|
|
|
|
uint64_t blockOfferTimestamp = firstMessage.timestamp - 1;
|
|
|
|
|
if (existingAddToContactsOffer && !shouldHaveAddToContactsOffer) {
|
|
|
|
|
[existingAddToContactsOffer removeWithTransaction:transaction];
|
|
|
|
|
} else if (!existingAddToContactsOffer && shouldHaveAddToContactsOffer) {
|
|
|
|
|
|
|
|
|
|
TSErrorMessage *errorMessage =
|
|
|
|
|
[OWSUnknownContactBlockOfferMessage unknownContactBlockOfferMessage:blockOfferTimestamp
|
|
|
|
|
thread:contactThread
|
|
|
|
|
contactId:contactThread.contactIdentifier];
|
|
|
|
|
[errorMessage saveWithTransaction:transaction];
|
|
|
|
|
}];
|
|
|
|
|
}
|
|
|
|
|
DDLogInfo(@"Creating 'add to contacts' offer for unknown contact");
|
|
|
|
|
|
|
|
|
|
+ (void)createUnreadMessagesIndicatorIfNecessary:(TSThread *)thread storageManager:(TSStorageManager *)storageManager
|
|
|
|
|
{
|
|
|
|
|
OWSAssert(thread);
|
|
|
|
|
OWSAssert(storageManager);
|
|
|
|
|
// We want the offer to be the first interaction in their
|
|
|
|
|
// conversation's timeline, so we back-date it to slightly before
|
|
|
|
|
// the first incoming message (which we know is the first message).
|
|
|
|
|
uint64_t offerTimestamp = (uint64_t)((long long)firstMessage.timestamp + kAddToContactsOfferOffset);
|
|
|
|
|
NSString *recipientId = ((TSContactThread *)thread).contactIdentifier;
|
|
|
|
|
|
|
|
|
|
[storageManager.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
|
|
|
|
|
|
|
|
|
|
NSMutableArray *indicators = [NSMutableArray new];
|
|
|
|
|
__block TSMessage *firstUnreadMessage = nil;
|
|
|
|
|
[[transaction ext:TSMessageDatabaseViewExtensionName]
|
|
|
|
|
enumerateRowsInGroup:thread.uniqueId
|
|
|
|
|
usingBlock:^(
|
|
|
|
|
NSString *collection, NSString *key, id object, id metadata, NSUInteger index, BOOL *stop) {
|
|
|
|
|
|
|
|
|
|
if ([object isKindOfClass:[TSUnreadIndicatorInteraction class]]) {
|
|
|
|
|
[indicators addObject:object];
|
|
|
|
|
} else if ([object isKindOfClass:[TSIncomingMessage class]]) {
|
|
|
|
|
TSIncomingMessage *incomingMessage = (TSIncomingMessage *)object;
|
|
|
|
|
if (!incomingMessage.wasRead) {
|
|
|
|
|
if (!firstUnreadMessage) {
|
|
|
|
|
firstUnreadMessage = incomingMessage;
|
|
|
|
|
} else {
|
|
|
|
|
OWSAssert([[firstUnreadMessage receiptDateForSorting]
|
|
|
|
|
compare:[incomingMessage receiptDateForSorting]]
|
|
|
|
|
== NSOrderedAscending);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}];
|
|
|
|
|
|
|
|
|
|
for (TSUnreadIndicatorInteraction *indicator in indicators) {
|
|
|
|
|
[indicator removeWithTransaction:transaction];
|
|
|
|
|
TSMessage *offerMessage = [OWSAddToContactsOfferMessage addToContactsOfferMessage:offerTimestamp
|
|
|
|
|
thread:thread
|
|
|
|
|
contactId:recipientId];
|
|
|
|
|
[offerMessage saveWithTransaction:transaction];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BOOL shouldHaveIndicator = firstUnreadMessage != nil;
|
|
|
|
|
if (!shouldHaveIndicator) {
|
|
|
|
|
return;
|
|
|
|
|
BOOL shouldHaveUnreadIndicator
|
|
|
|
|
= ((firstUnreadMessage != nil || fixedUnreadIndicatorTimestamp != nil) && !hideUnreadMessagesIndicator);
|
|
|
|
|
if (!shouldHaveUnreadIndicator) {
|
|
|
|
|
if (existingUnreadIndicator) {
|
|
|
|
|
[existingUnreadIndicator removeWithTransaction:transaction];
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// We want the block offer to appear just before the first unread incoming
|
|
|
|
|
// message in the conversation timeline...
|
|
|
|
|
//
|
|
|
|
|
// ...unless we have a fixed timestamp for the unread indicator.
|
|
|
|
|
uint64_t indicatorTimestamp = (uint64_t)(fixedUnreadIndicatorTimestamp
|
|
|
|
|
? [fixedUnreadIndicatorTimestamp longLongValue]
|
|
|
|
|
: ((long long)firstUnreadMessage.timestamp + kUnreadIndicatorOfferOffset));
|
|
|
|
|
|
|
|
|
|
if (indicatorTimestamp && existingUnreadIndicator.timestamp == indicatorTimestamp) {
|
|
|
|
|
// Keep the existing indicator; it is in the correct position.
|
|
|
|
|
|
|
|
|
|
result.unreadIndicator = existingUnreadIndicator;
|
|
|
|
|
} else {
|
|
|
|
|
if (existingUnreadIndicator) {
|
|
|
|
|
[existingUnreadIndicator removeWithTransaction:transaction];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DDLogInfo(@"%@ Creating TSUnreadIndicatorInteraction", self.tag);
|
|
|
|
|
|
|
|
|
|
TSUnreadIndicatorInteraction *indicator =
|
|
|
|
|
[[TSUnreadIndicatorInteraction alloc] initWithTimestamp:indicatorTimestamp thread:thread];
|
|
|
|
|
[indicator saveWithTransaction:transaction];
|
|
|
|
|
|
|
|
|
|
result.unreadIndicator = indicator;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DDLogInfo(@"%@ Creating TSUnreadIndicatorInteraction", self.tag);
|
|
|
|
|
|
|
|
|
|
// We want the block offer to appear just before the first unread incoming
|
|
|
|
|
// message in the conversation timeline.
|
|
|
|
|
uint64_t indicatorTimestamp = firstUnreadMessage.timestamp - 1;
|
|
|
|
|
|
|
|
|
|
TSUnreadIndicatorInteraction *indicator =
|
|
|
|
|
[[TSUnreadIndicatorInteraction alloc] initWithTimestamp:indicatorTimestamp thread:thread];
|
|
|
|
|
[indicator saveWithTransaction:transaction];
|
|
|
|
|
}];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
+ (void)clearUnreadMessagesIndicator:(TSThread *)thread storageManager:(TSStorageManager *)storageManager
|
|
|
|
|
{
|
|
|
|
|
OWSAssert(thread);
|
|
|
|
|
OWSAssert(storageManager);
|
|
|
|
|
|
|
|
|
|
[storageManager.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
|
|
|
|
|
|
|
|
|
|
NSMutableArray *indicators = [NSMutableArray new];
|
|
|
|
|
[[transaction ext:TSMessageDatabaseViewExtensionName]
|
|
|
|
|
enumerateRowsInGroup:thread.uniqueId
|
|
|
|
|
usingBlock:^(
|
|
|
|
|
NSString *collection, NSString *key, id object, id metadata, NSUInteger index, BOOL *stop) {
|
|
|
|
|
|
|
|
|
|
if ([object isKindOfClass:[TSUnreadIndicatorInteraction class]]) {
|
|
|
|
|
[indicators addObject:object];
|
|
|
|
|
}
|
|
|
|
|
}];
|
|
|
|
|
|
|
|
|
|
for (TSUnreadIndicatorInteraction *indicator in indicators) {
|
|
|
|
|
[indicator removeWithTransaction:transaction];
|
|
|
|
|
}
|
|
|
|
|
}];
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#pragma mark - Logging
|
|
|
|
|