|
|
|
@ -26,6 +26,22 @@
|
|
|
|
|
|
|
|
|
|
NS_ASSUME_NONNULL_BEGIN
|
|
|
|
|
|
|
|
|
|
@interface ConversationProfileState : NSObject
|
|
|
|
|
|
|
|
|
|
@property (nonatomic) BOOL hasLocalProfile;
|
|
|
|
|
@property (nonatomic) BOOL isThreadInProfileWhitelist;
|
|
|
|
|
@property (nonatomic) BOOL hasUnwhitelistedMember;
|
|
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
#pragma mark -
|
|
|
|
|
|
|
|
|
|
@implementation ConversationProfileState
|
|
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
#pragma mark -
|
|
|
|
|
|
|
|
|
|
@implementation ConversationUpdateItem
|
|
|
|
|
|
|
|
|
|
- (instancetype)initWithUpdateItemType:(ConversationUpdateItemType)updateItemType
|
|
|
|
@ -146,6 +162,9 @@ static const int kYapDatabaseRangeMinLength = 0;
|
|
|
|
|
@property (nonatomic, nullable) NSDate *collapseCutoffDate;
|
|
|
|
|
@property (nonatomic, nullable) NSString *typingIndicatorsSender;
|
|
|
|
|
|
|
|
|
|
@property (nonatomic, nullable) ConversationProfileState *conversationProfileState;
|
|
|
|
|
@property (nonatomic) BOOL hasTooManyOutgoingMessagesToBlockCached;
|
|
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
#pragma mark -
|
|
|
|
@ -243,6 +262,10 @@ static const int kYapDatabaseRangeMinLength = 0;
|
|
|
|
|
selector:@selector(blockListDidChange:)
|
|
|
|
|
name:kNSNotificationName_BlockListDidChange
|
|
|
|
|
object:nil];
|
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
|
|
|
|
selector:@selector(localProfileDidChange:)
|
|
|
|
|
name:kNSNotificationName_LocalProfileDidChange
|
|
|
|
|
object:nil];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)signalAccountsDidChange:(NSNotification *)notification
|
|
|
|
@ -256,17 +279,16 @@ static const int kYapDatabaseRangeMinLength = 0;
|
|
|
|
|
{
|
|
|
|
|
OWSAssertIsOnMainThread();
|
|
|
|
|
|
|
|
|
|
// If profile whitelist just changed, we may want to hide a profile whitelist offer.
|
|
|
|
|
NSString *_Nullable recipientId = notification.userInfo[kNSNotificationKey_ProfileRecipientId];
|
|
|
|
|
NSData *_Nullable groupId = notification.userInfo[kNSNotificationKey_ProfileGroupId];
|
|
|
|
|
if (recipientId.length > 0 && [self.thread.recipientIdentifiers containsObject:recipientId]) {
|
|
|
|
|
self.conversationProfileState = nil;
|
|
|
|
|
[self updateForTransientItems];
|
|
|
|
|
} else if (groupId.length > 0 && self.thread.isGroupThread) {
|
|
|
|
|
TSGroupThread *groupThread = (TSGroupThread *)self.thread;
|
|
|
|
|
if ([groupThread.groupModel.groupId isEqualToData:groupId]) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)localProfileDidChange:(NSNotification *)notification
|
|
|
|
|
{
|
|
|
|
|
OWSAssertIsOnMainThread();
|
|
|
|
|
|
|
|
|
|
self.conversationProfileState = nil;
|
|
|
|
|
[self updateForTransientItems];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)blockListDidChange:(id)notification
|
|
|
|
@ -929,8 +951,11 @@ static const int kYapDatabaseRangeMinLength = 0;
|
|
|
|
|
|
|
|
|
|
#pragma mark - View Items
|
|
|
|
|
|
|
|
|
|
- (nullable OWSContactOffersInteraction *)tryToBuildContactOffersInteraction
|
|
|
|
|
- (void)ensureConversationProfileState
|
|
|
|
|
{
|
|
|
|
|
if (self.conversationProfileState) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Many OWSProfileManager methods aren't safe to call from inside a database
|
|
|
|
|
// transaction, so do this work now.
|
|
|
|
@ -946,23 +971,46 @@ static const int kYapDatabaseRangeMinLength = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
__block OWSContactOffersInteraction *_Nullable offers = nil;
|
|
|
|
|
[self.uiDatabaseConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) {
|
|
|
|
|
offers = [self tryToBuildContactOffersInteractionWithTransaction:transaction
|
|
|
|
|
hasLocalProfile:hasLocalProfile
|
|
|
|
|
isThreadInProfileWhitelist:isThreadInProfileWhitelist
|
|
|
|
|
hasUnwhitelistedMember:hasUnwhitelistedMember];
|
|
|
|
|
}];
|
|
|
|
|
return offers;
|
|
|
|
|
ConversationProfileState *conversationProfileState = [ConversationProfileState new];
|
|
|
|
|
conversationProfileState.hasLocalProfile = hasLocalProfile;
|
|
|
|
|
conversationProfileState.isThreadInProfileWhitelist = isThreadInProfileWhitelist;
|
|
|
|
|
conversationProfileState.hasUnwhitelistedMember = hasUnwhitelistedMember;
|
|
|
|
|
self.conversationProfileState = conversationProfileState;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (nullable TSInteraction *)firstCallOrMessageForLoadedInteractions:(NSArray<TSInteraction *> *)loadedInteractions
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
for (TSInteraction *interaction in loadedInteractions) {
|
|
|
|
|
switch (interaction.interactionType) {
|
|
|
|
|
case OWSInteractionType_Unknown:
|
|
|
|
|
OWSFailDebug(@"Unknown interaction type.");
|
|
|
|
|
return nil;
|
|
|
|
|
case OWSInteractionType_IncomingMessage:
|
|
|
|
|
case OWSInteractionType_OutgoingMessage:
|
|
|
|
|
return interaction;
|
|
|
|
|
case OWSInteractionType_Error:
|
|
|
|
|
case OWSInteractionType_Info:
|
|
|
|
|
break;
|
|
|
|
|
case OWSInteractionType_Call:
|
|
|
|
|
case OWSInteractionType_Offer:
|
|
|
|
|
case OWSInteractionType_TypingIndicator:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (nullable OWSContactOffersInteraction *)
|
|
|
|
|
tryToBuildContactOffersInteractionWithTransaction:(YapDatabaseReadTransaction *)transaction
|
|
|
|
|
hasLocalProfile:(BOOL)hasLocalProfile
|
|
|
|
|
isThreadInProfileWhitelist:(BOOL)isThreadInProfileWhitelist
|
|
|
|
|
hasUnwhitelistedMember:(BOOL)hasUnwhitelistedMember
|
|
|
|
|
loadedInteractions:(NSArray<TSInteraction *> *)loadedInteractions
|
|
|
|
|
{
|
|
|
|
|
OWSAssertDebug(transaction);
|
|
|
|
|
OWSAssertDebug(self.conversationProfileState);
|
|
|
|
|
|
|
|
|
|
BOOL hasLocalProfile = self.conversationProfileState.hasLocalProfile;
|
|
|
|
|
BOOL isThreadInProfileWhitelist = self.conversationProfileState.isThreadInProfileWhitelist;
|
|
|
|
|
BOOL hasUnwhitelistedMember = self.conversationProfileState.hasUnwhitelistedMember;
|
|
|
|
|
|
|
|
|
|
TSThread *thread = self.thread;
|
|
|
|
|
BOOL isContactThread = [thread isKindOfClass:[TSContactThread class]];
|
|
|
|
@ -977,28 +1025,23 @@ static const int kYapDatabaseRangeMinLength = 0;
|
|
|
|
|
NSString *localNumber = [self.tsAccountManager localNumber];
|
|
|
|
|
OWSAssertDebug(localNumber.length > 0);
|
|
|
|
|
|
|
|
|
|
const int kMaxBlockOfferOutgoingMessageCount = 10;
|
|
|
|
|
|
|
|
|
|
__block TSInteraction *firstCallOrMessage = nil;
|
|
|
|
|
[[transaction ext:TSMessageDatabaseViewExtensionName]
|
|
|
|
|
enumerateRowsInGroup:thread.uniqueId
|
|
|
|
|
usingBlock:^(
|
|
|
|
|
NSString *collection, NSString *key, id object, id metadata, NSUInteger index, BOOL *stop) {
|
|
|
|
|
OWSAssertDebug([object isKindOfClass:[TSInteraction class]]);
|
|
|
|
|
|
|
|
|
|
if ([object isKindOfClass:[TSIncomingMessage class]] ||
|
|
|
|
|
[object isKindOfClass:[TSOutgoingMessage class]] || [object isKindOfClass:[TSCall class]]) {
|
|
|
|
|
firstCallOrMessage = object;
|
|
|
|
|
*stop = YES;
|
|
|
|
|
}
|
|
|
|
|
}];
|
|
|
|
|
TSInteraction *firstCallOrMessage = [self firstCallOrMessageForLoadedInteractions:loadedInteractions];
|
|
|
|
|
if (!firstCallOrMessage) {
|
|
|
|
|
return nil;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BOOL hasTooManyOutgoingMessagesToBlock;
|
|
|
|
|
if (self.hasTooManyOutgoingMessagesToBlockCached) {
|
|
|
|
|
hasTooManyOutgoingMessagesToBlock = YES;
|
|
|
|
|
} else {
|
|
|
|
|
NSUInteger outgoingMessageCount =
|
|
|
|
|
[[TSDatabaseView threadOutgoingMessageDatabaseView:transaction] numberOfItemsInGroup:thread.uniqueId];
|
|
|
|
|
|
|
|
|
|
const int kMaxBlockOfferOutgoingMessageCount = 10;
|
|
|
|
|
hasTooManyOutgoingMessagesToBlock = (outgoingMessageCount > kMaxBlockOfferOutgoingMessageCount);
|
|
|
|
|
self.hasTooManyOutgoingMessagesToBlockCached = hasTooManyOutgoingMessagesToBlock;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BOOL shouldHaveBlockOffer = YES;
|
|
|
|
|
BOOL shouldHaveAddToContactsOffer = YES;
|
|
|
|
|
BOOL shouldHaveAddToProfileWhitelistOffer = YES;
|
|
|
|
@ -1032,7 +1075,7 @@ static const int kYapDatabaseRangeMinLength = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (outgoingMessageCount > kMaxBlockOfferOutgoingMessageCount) {
|
|
|
|
|
if (hasTooManyOutgoingMessagesToBlock) {
|
|
|
|
|
// If the user has sent more than N messages, don't show a block offer.
|
|
|
|
|
shouldHaveBlockOffer = NO;
|
|
|
|
|
}
|
|
|
|
@ -1101,7 +1144,7 @@ static const int kYapDatabaseRangeMinLength = 0;
|
|
|
|
|
BOOL isGroupThread = self.thread.isGroupThread;
|
|
|
|
|
ConversationStyle *conversationStyle = self.delegate.conversationStyle;
|
|
|
|
|
|
|
|
|
|
OWSContactOffersInteraction *_Nullable offers = [self tryToBuildContactOffersInteraction];
|
|
|
|
|
[self ensureConversationProfileState];
|
|
|
|
|
|
|
|
|
|
__block BOOL hasError = NO;
|
|
|
|
|
[self.uiDatabaseConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) {
|
|
|
|
@ -1149,6 +1192,8 @@ static const int kYapDatabaseRangeMinLength = 0;
|
|
|
|
|
[interactionIds addObject:interaction.uniqueId];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
OWSContactOffersInteraction *_Nullable offers =
|
|
|
|
|
[self tryToBuildContactOffersInteractionWithTransaction:transaction loadedInteractions:interactions];
|
|
|
|
|
if (offers && [interactionIds containsObject:offers.beforeInteractionId]) {
|
|
|
|
|
id<ConversationViewItem> offersItem = tryToAddViewItem(offers);
|
|
|
|
|
if ([offersItem.interaction isKindOfClass:[OWSContactOffersInteraction class]]) {
|
|
|
|
|