|
|
|
@ -17,6 +17,7 @@
|
|
|
|
|
#import "TSMessage.h"
|
|
|
|
|
|
|
|
|
|
NS_ASSUME_NONNULL_BEGIN
|
|
|
|
|
|
|
|
|
|
// Can we move to Signal-iOS?
|
|
|
|
|
@interface OWSDisappearingMessagesJob ()
|
|
|
|
|
|
|
|
|
@ -24,13 +25,25 @@ NS_ASSUME_NONNULL_BEGIN
|
|
|
|
|
|
|
|
|
|
@property (nonatomic, readonly) OWSDisappearingMessagesFinder *disappearingMessagesFinder;
|
|
|
|
|
|
|
|
|
|
+ (dispatch_queue_t)serialQueue;
|
|
|
|
|
|
|
|
|
|
// These three properties should only be accessed on the main thread.
|
|
|
|
|
@property (nonatomic) BOOL hasStarted;
|
|
|
|
|
@property (nonatomic, nullable) NSTimer *timer;
|
|
|
|
|
@property (nonatomic, nullable) NSDate *timerScheduleDate;
|
|
|
|
|
@property (nonatomic, nullable) NSTimer *nextDisappearanceTimer;
|
|
|
|
|
@property (nonatomic, nullable) NSDate *nextDisappearanceDate;
|
|
|
|
|
@property (nonatomic, nullable) NSTimer *fallbackTimer;
|
|
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
void AssertIsOnDisappearingMessagesQueue()
|
|
|
|
|
{
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
|
if (@available(iOS 10.0, *)) {
|
|
|
|
|
dispatch_assert_queue(OWSDisappearingMessagesJob.serialQueue);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#pragma mark -
|
|
|
|
|
|
|
|
|
|
@implementation OWSDisappearingMessagesJob
|
|
|
|
@ -55,6 +68,18 @@ NS_ASSUME_NONNULL_BEGIN
|
|
|
|
|
_databaseConnection = primaryStorage.newDatabaseConnection;
|
|
|
|
|
_disappearingMessagesFinder = [OWSDisappearingMessagesFinder new];
|
|
|
|
|
|
|
|
|
|
// suspenders in case a deletion schedule is missed.
|
|
|
|
|
NSTimeInterval kFallBackTimerInterval = 5 * kMinuteInterval;
|
|
|
|
|
[AppReadiness runNowOrWhenAppIsReady:^{
|
|
|
|
|
if (CurrentAppContext().isMainApp) {
|
|
|
|
|
self.fallbackTimer = [NSTimer weakScheduledTimerWithTimeInterval:kFallBackTimerInterval
|
|
|
|
|
target:self
|
|
|
|
|
selector:@selector(fallbackTimerDidFire)
|
|
|
|
|
userInfo:nil
|
|
|
|
|
repeats:YES];
|
|
|
|
|
}
|
|
|
|
|
}];
|
|
|
|
|
|
|
|
|
|
OWSSingletonAssert();
|
|
|
|
|
|
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
|
|
|
@ -84,96 +109,79 @@ NS_ASSUME_NONNULL_BEGIN
|
|
|
|
|
return queue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// This method should only be called on the serialQueue.
|
|
|
|
|
- (void)run
|
|
|
|
|
- (NSUInteger)deleteExpiredMessages
|
|
|
|
|
{
|
|
|
|
|
AssertIsOnDisappearingMessagesQueue();
|
|
|
|
|
|
|
|
|
|
uint64_t now = [NSDate ows_millisecondTimeStamp];
|
|
|
|
|
|
|
|
|
|
OWSBackgroundTask *_Nullable backgroundTask = [OWSBackgroundTask backgroundTaskWithLabelStr:__PRETTY_FUNCTION__];
|
|
|
|
|
|
|
|
|
|
__block uint expirationCount = 0;
|
|
|
|
|
__block NSUInteger expirationCount = 0;
|
|
|
|
|
[self.databaseConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *_Nonnull transaction) {
|
|
|
|
|
[self.disappearingMessagesFinder enumerateExpiredMessagesWithBlock:^(TSMessage *message) {
|
|
|
|
|
// sanity check
|
|
|
|
|
if (message.expiresAt > now) {
|
|
|
|
|
DDLogError(
|
|
|
|
|
OWSFail(
|
|
|
|
|
@"%@ Refusing to remove message which doesn't expire until: %lld", self.logTag, message.expiresAt);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DDLogDebug(@"%@ Removing message which expired at: %lld", self.logTag, message.expiresAt);
|
|
|
|
|
DDLogInfo(@"%@ Removing message which expired at: %lld", self.logTag, message.expiresAt);
|
|
|
|
|
[message removeWithTransaction:transaction];
|
|
|
|
|
expirationCount++;
|
|
|
|
|
}
|
|
|
|
|
transaction:transaction];
|
|
|
|
|
}];
|
|
|
|
|
|
|
|
|
|
DDLogDebug(@"%@ Removed %u expired messages", self.logTag, expirationCount);
|
|
|
|
|
DDLogDebug(@"%@ Removed %tu expired messages", self.logTag, expirationCount);
|
|
|
|
|
|
|
|
|
|
backgroundTask = nil;
|
|
|
|
|
return expirationCount;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// This method should only be called on the serialQueue.
|
|
|
|
|
- (void)runLoop
|
|
|
|
|
// deletes any expired messages and schedules the next run.
|
|
|
|
|
- (NSUInteger)runLoop
|
|
|
|
|
{
|
|
|
|
|
DDLogVerbose(@"%@ Run", self.logTag);
|
|
|
|
|
DDLogVerbose(@"%@ in runLoop", self.logTag);
|
|
|
|
|
AssertIsOnDisappearingMessagesQueue();
|
|
|
|
|
|
|
|
|
|
[self run];
|
|
|
|
|
NSUInteger deletedCount = [self deleteExpiredMessages];
|
|
|
|
|
|
|
|
|
|
uint64_t now = [NSDate ows_millisecondTimeStamp];
|
|
|
|
|
__block NSNumber *nextExpirationTimestampNumber;
|
|
|
|
|
[self.databaseConnection readWithBlock:^(YapDatabaseReadTransaction *_Nonnull transaction) {
|
|
|
|
|
nextExpirationTimestampNumber =
|
|
|
|
|
[self.disappearingMessagesFinder nextExpirationTimestampWithTransaction:transaction];
|
|
|
|
|
}];
|
|
|
|
|
|
|
|
|
|
if (!nextExpirationTimestampNumber) {
|
|
|
|
|
// In theory we could kill the loop here. It should resume when the next expiring message is saved,
|
|
|
|
|
// But this is a safeguard for any race conditions that exist while running the job as a new message is saved.
|
|
|
|
|
DDLogDebug(@"%@ No more expiring messages.", self.logTag);
|
|
|
|
|
[self runLater];
|
|
|
|
|
return;
|
|
|
|
|
return deletedCount;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint64_t nextExpirationAt = [nextExpirationTimestampNumber unsignedLongLongValue];
|
|
|
|
|
[self runByDate:[NSDate ows_dateWithMillisecondsSince1970:MAX(nextExpirationAt, now)]];
|
|
|
|
|
}
|
|
|
|
|
uint64_t nextExpirationAt = nextExpirationTimestampNumber.unsignedLongLongValue;
|
|
|
|
|
NSDate *nextEpirationDate = [NSDate ows_dateWithMillisecondsSince1970:nextExpirationAt];
|
|
|
|
|
[self scheduleRunByDate:nextEpirationDate];
|
|
|
|
|
|
|
|
|
|
+ (void)setExpirationForMessage:(TSMessage *)message
|
|
|
|
|
{
|
|
|
|
|
dispatch_async(self.serialQueue, ^{
|
|
|
|
|
[[self sharedJob] setExpirationForMessage:message];
|
|
|
|
|
});
|
|
|
|
|
return deletedCount;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)setExpirationForMessage:(TSMessage *)message
|
|
|
|
|
- (void)startAnyExpirationForMessage:(TSMessage *)message
|
|
|
|
|
expirationStartedAt:(uint64_t)expirationStartedAt
|
|
|
|
|
transaction:(YapDatabaseReadWriteTransaction *_Nonnull)transaction
|
|
|
|
|
{
|
|
|
|
|
if (!message.isExpiringMessage) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
OWSDisappearingMessagesConfiguration *disappearingConfig =
|
|
|
|
|
[OWSDisappearingMessagesConfiguration fetchObjectWithUniqueID:message.uniqueThreadId];
|
|
|
|
|
[OWSDisappearingMessagesConfiguration fetchObjectWithUniqueID:message.uniqueThreadId transaction:transaction];
|
|
|
|
|
|
|
|
|
|
if (!disappearingConfig.isEnabled) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[self setExpirationForMessage:message expirationStartedAt:[NSDate ows_millisecondTimeStamp]];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
+ (void)setExpirationForMessage:(TSMessage *)message expirationStartedAt:(uint64_t)expirationStartedAt
|
|
|
|
|
{
|
|
|
|
|
dispatch_async(self.serialQueue, ^{
|
|
|
|
|
[[self sharedJob] setExpirationForMessage:message expirationStartedAt:expirationStartedAt];
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// This method should only be called on the serialQueue.
|
|
|
|
|
- (void)setExpirationForMessage:(TSMessage *)message expirationStartedAt:(uint64_t)expirationStartedAt
|
|
|
|
|
{
|
|
|
|
|
[self.databaseConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *_Nonnull transaction) {
|
|
|
|
|
[self setExpirationForMessage:message expirationStartedAt:expirationStartedAt transaction:transaction];
|
|
|
|
|
}];
|
|
|
|
|
[self setExpirationForMessage:message expirationStartedAt:expirationStartedAt transaction:transaction];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)setExpirationForMessage:(TSMessage *)message
|
|
|
|
@ -186,114 +194,80 @@ NS_ASSUME_NONNULL_BEGIN
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int startedSecondsAgo = [NSDate new].timeIntervalSince1970 - expirationStartedAt / 1000.0;
|
|
|
|
|
DDLogDebug(@"%@ Starting expiration for message read %d seconds ago", self.logTag, startedSecondsAgo);
|
|
|
|
|
NSTimeInterval startedSecondsAgo = ([NSDate ows_millisecondTimeStamp] - expirationStartedAt) / 1000.0;
|
|
|
|
|
DDLogDebug(@"%@ Starting expiration for message read %f seconds ago", self.logTag, startedSecondsAgo);
|
|
|
|
|
|
|
|
|
|
// Don't clobber if multiple actions simultaneously triggered expiration.
|
|
|
|
|
if (message.expireStartedAt == 0 || message.expireStartedAt > expirationStartedAt) {
|
|
|
|
|
[message updateWithExpireStartedAt:expirationStartedAt transaction:transaction];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Necessary that the async expiration run happens *after* the message is saved with expiration configuration.
|
|
|
|
|
[self runByDate:[NSDate ows_dateWithMillisecondsSince1970:message.expiresAt]];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
+ (void)setExpirationsForThread:(TSThread *)thread
|
|
|
|
|
{
|
|
|
|
|
dispatch_async(self.serialQueue, ^{
|
|
|
|
|
[[self sharedJob] setExpirationsForThread:thread];
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// This method should only be called on the serialQueue.
|
|
|
|
|
- (void)setExpirationsForThread:(TSThread *)thread
|
|
|
|
|
{
|
|
|
|
|
OWSBackgroundTask *_Nullable backgroundTask = [OWSBackgroundTask backgroundTaskWithLabelStr:__PRETTY_FUNCTION__];
|
|
|
|
|
|
|
|
|
|
uint64_t now = [NSDate ows_millisecondTimeStamp];
|
|
|
|
|
[self.databaseConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *_Nonnull transaction) {
|
|
|
|
|
[self.disappearingMessagesFinder
|
|
|
|
|
enumerateUnstartedExpiringMessagesInThread:thread
|
|
|
|
|
block:^(TSMessage *_Nonnull message) {
|
|
|
|
|
DDLogWarn(
|
|
|
|
|
@"%@ Starting expiring message which should have already "
|
|
|
|
|
@"been started.",
|
|
|
|
|
self.logTag);
|
|
|
|
|
// specify "now" in case D.M. have since been disabled, but we have
|
|
|
|
|
// existing unstarted expiring messages that still need to expire.
|
|
|
|
|
[self setExpirationForMessage:message
|
|
|
|
|
expirationStartedAt:now
|
|
|
|
|
transaction:transaction];
|
|
|
|
|
}
|
|
|
|
|
transaction:transaction];
|
|
|
|
|
}];
|
|
|
|
|
|
|
|
|
|
backgroundTask = nil;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
+ (void)becomeConsistentWithConfigurationForMessage:(TSMessage *)message
|
|
|
|
|
contactsManager:(id<ContactsManagerProtocol>)contactsManager
|
|
|
|
|
{
|
|
|
|
|
[[self sharedJob] becomeConsistentWithConfigurationForMessage:message contactsManager:contactsManager];
|
|
|
|
|
[transaction addCompletionQueue:nil
|
|
|
|
|
completionBlock:^{
|
|
|
|
|
// Necessary that the async expiration run happens *after* the message is saved with it's new
|
|
|
|
|
// expiration configuration.
|
|
|
|
|
[self scheduleRunByDate:[NSDate ows_dateWithMillisecondsSince1970:message.expiresAt]];
|
|
|
|
|
}];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)becomeConsistentWithConfigurationForMessage:(TSMessage *)message
|
|
|
|
|
contactsManager:(id<ContactsManagerProtocol>)contactsManager
|
|
|
|
|
transaction:(YapDatabaseReadWriteTransaction *)transaction
|
|
|
|
|
{
|
|
|
|
|
OWSAssert(message);
|
|
|
|
|
OWSAssert(contactsManager);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
__block OWSBackgroundTask *_Nullable backgroundTask = [OWSBackgroundTask backgroundTaskWithLabelStr:__PRETTY_FUNCTION__];
|
|
|
|
|
|
|
|
|
|
dispatch_async(OWSDisappearingMessagesJob.serialQueue, ^{
|
|
|
|
|
// Become eventually consistent in the case that the remote changed their settings at the same time.
|
|
|
|
|
// Also in case remote doesn't support expiring messages
|
|
|
|
|
OWSDisappearingMessagesConfiguration *disappearingMessagesConfiguration =
|
|
|
|
|
[OWSDisappearingMessagesConfiguration fetchOrCreateDefaultWithThreadId:message.uniqueThreadId];
|
|
|
|
|
|
|
|
|
|
BOOL changed = NO;
|
|
|
|
|
if (message.expiresInSeconds == 0) {
|
|
|
|
|
if (disappearingMessagesConfiguration.isEnabled) {
|
|
|
|
|
changed = YES;
|
|
|
|
|
DDLogWarn(@"%@ Received remote message which had no expiration set, disabling our expiration to become "
|
|
|
|
|
@"consistent.",
|
|
|
|
|
self.logTag);
|
|
|
|
|
disappearingMessagesConfiguration.enabled = NO;
|
|
|
|
|
[disappearingMessagesConfiguration save];
|
|
|
|
|
}
|
|
|
|
|
} else if (message.expiresInSeconds != disappearingMessagesConfiguration.durationSeconds) {
|
|
|
|
|
|
|
|
|
|
// Become eventually consistent in the case that the remote changed their settings at the same time.
|
|
|
|
|
// Also in case remote doesn't support expiring messages
|
|
|
|
|
OWSDisappearingMessagesConfiguration *disappearingMessagesConfiguration =
|
|
|
|
|
[OWSDisappearingMessagesConfiguration fetchOrCreateDefaultWithThreadId:message.uniqueThreadId
|
|
|
|
|
transaction:transaction];
|
|
|
|
|
|
|
|
|
|
BOOL changed = NO;
|
|
|
|
|
if (message.expiresInSeconds == 0) {
|
|
|
|
|
if (disappearingMessagesConfiguration.isEnabled) {
|
|
|
|
|
changed = YES;
|
|
|
|
|
DDLogInfo(@"%@ Received remote message with different expiration set, updating our expiration to become "
|
|
|
|
|
DDLogWarn(@"%@ Received remote message which had no expiration set, disabling our expiration to become "
|
|
|
|
|
@"consistent.",
|
|
|
|
|
self.logTag);
|
|
|
|
|
disappearingMessagesConfiguration.enabled = YES;
|
|
|
|
|
disappearingMessagesConfiguration.durationSeconds = message.expiresInSeconds;
|
|
|
|
|
[disappearingMessagesConfiguration save];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!changed) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ([message isKindOfClass:[TSIncomingMessage class]]) {
|
|
|
|
|
TSIncomingMessage *incomingMessage = (TSIncomingMessage *)message;
|
|
|
|
|
NSString *contactName = [contactsManager displayNameForPhoneIdentifier:incomingMessage.messageAuthorId];
|
|
|
|
|
|
|
|
|
|
// We want the info message to appear _before_ the message.
|
|
|
|
|
[[[OWSDisappearingConfigurationUpdateInfoMessage alloc] initWithTimestamp:message.timestamp - 1
|
|
|
|
|
thread:message.thread
|
|
|
|
|
configuration:disappearingMessagesConfiguration
|
|
|
|
|
createdByRemoteName:contactName] save];
|
|
|
|
|
} else {
|
|
|
|
|
// We want the info message to appear _before_ the message.
|
|
|
|
|
[[[OWSDisappearingConfigurationUpdateInfoMessage alloc] initWithTimestamp:message.timestamp - 1
|
|
|
|
|
thread:message.thread
|
|
|
|
|
configuration:disappearingMessagesConfiguration]
|
|
|
|
|
save];
|
|
|
|
|
self.logTag);
|
|
|
|
|
disappearingMessagesConfiguration.enabled = NO;
|
|
|
|
|
[disappearingMessagesConfiguration saveWithTransaction:transaction];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
backgroundTask = nil;
|
|
|
|
|
});
|
|
|
|
|
} else if (message.expiresInSeconds != disappearingMessagesConfiguration.durationSeconds) {
|
|
|
|
|
changed = YES;
|
|
|
|
|
DDLogInfo(@"%@ Received remote message with different expiration set, updating our expiration to become "
|
|
|
|
|
@"consistent.",
|
|
|
|
|
self.logTag);
|
|
|
|
|
disappearingMessagesConfiguration.enabled = YES;
|
|
|
|
|
disappearingMessagesConfiguration.durationSeconds = message.expiresInSeconds;
|
|
|
|
|
[disappearingMessagesConfiguration saveWithTransaction:transaction];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!changed) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ([message isKindOfClass:[TSIncomingMessage class]]) {
|
|
|
|
|
TSIncomingMessage *incomingMessage = (TSIncomingMessage *)message;
|
|
|
|
|
NSString *contactName = [contactsManager displayNameForPhoneIdentifier:incomingMessage.messageAuthorId];
|
|
|
|
|
|
|
|
|
|
// We want the info message to appear _before_ the message.
|
|
|
|
|
[[[OWSDisappearingConfigurationUpdateInfoMessage alloc] initWithTimestamp:message.timestampForSorting - 1
|
|
|
|
|
thread:message.thread
|
|
|
|
|
configuration:disappearingMessagesConfiguration
|
|
|
|
|
createdByRemoteName:contactName]
|
|
|
|
|
saveWithTransaction:transaction];
|
|
|
|
|
} else {
|
|
|
|
|
// We want the info message to appear _before_ the message.
|
|
|
|
|
[[[OWSDisappearingConfigurationUpdateInfoMessage alloc] initWithTimestamp:message.timestampForSorting - 1
|
|
|
|
|
thread:message.thread
|
|
|
|
|
configuration:disappearingMessagesConfiguration]
|
|
|
|
|
saveWithTransaction:transaction];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
backgroundTask = nil;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)startIfNecessary
|
|
|
|
@ -304,33 +278,27 @@ NS_ASSUME_NONNULL_BEGIN
|
|
|
|
|
}
|
|
|
|
|
self.hasStarted = YES;
|
|
|
|
|
|
|
|
|
|
[self runNow];
|
|
|
|
|
dispatch_async(OWSDisappearingMessagesJob.serialQueue, ^{
|
|
|
|
|
[self runLoop];
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)runNow
|
|
|
|
|
{
|
|
|
|
|
[self runByDate:[NSDate new] ignoreMinDelay:YES];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSTimeInterval)maxDelaySeconds
|
|
|
|
|
{
|
|
|
|
|
// Don't run less often than once per N minutes.
|
|
|
|
|
return 5 * kMinuteInterval;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Waits the maximum amount of time to run again.
|
|
|
|
|
- (void)runLater
|
|
|
|
|
- (NSDateFormatter *)dateFormatter
|
|
|
|
|
{
|
|
|
|
|
[self runByDate:[NSDate dateWithTimeIntervalSinceNow:self.maxDelaySeconds] ignoreMinDelay:YES];
|
|
|
|
|
}
|
|
|
|
|
static NSDateFormatter *dateFormatter;
|
|
|
|
|
static dispatch_once_t onceToken;
|
|
|
|
|
dispatch_once(&onceToken, ^{
|
|
|
|
|
dateFormatter = [NSDateFormatter new];
|
|
|
|
|
dateFormatter.dateStyle = NSDateFormatterNoStyle;
|
|
|
|
|
dateFormatter.timeStyle = kCFDateFormatterMediumStyle;
|
|
|
|
|
dateFormatter.locale = [NSLocale systemLocale];
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
- (void)runByDate:(NSDate *)date
|
|
|
|
|
{
|
|
|
|
|
[self runByDate:date ignoreMinDelay:NO];
|
|
|
|
|
return dateFormatter;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)runByDate:(NSDate *)date ignoreMinDelay:(BOOL)ignoreMinDelay
|
|
|
|
|
- (void)scheduleRunByDate:(NSDate *)date
|
|
|
|
|
{
|
|
|
|
|
OWSAssert(date);
|
|
|
|
|
|
|
|
|
@ -340,44 +308,39 @@ NS_ASSUME_NONNULL_BEGIN
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
NSDateFormatter *dateFormatter = [NSDateFormatter new];
|
|
|
|
|
dateFormatter.dateStyle = NSDateFormatterNoStyle;
|
|
|
|
|
dateFormatter.timeStyle = kCFDateFormatterMediumStyle;
|
|
|
|
|
dateFormatter.locale = [NSLocale systemLocale];
|
|
|
|
|
|
|
|
|
|
// Don't run more often than once per second.
|
|
|
|
|
const NSTimeInterval kMinDelaySeconds = ignoreMinDelay ? 0.f : 1.f;
|
|
|
|
|
NSTimeInterval delaySeconds
|
|
|
|
|
= MAX(kMinDelaySeconds, MIN(self.maxDelaySeconds, [date timeIntervalSinceDate:[NSDate new]]));
|
|
|
|
|
NSDate *timerScheduleDate = [NSDate dateWithTimeIntervalSinceNow:delaySeconds];
|
|
|
|
|
if (self.timerScheduleDate && [timerScheduleDate timeIntervalSinceDate:self.timerScheduleDate] > 0) {
|
|
|
|
|
DDLogVerbose(@"%@ Request to run at %@ (%d sec.) ignored due to scheduled run at %@ (%d sec.)",
|
|
|
|
|
const NSTimeInterval kMinDelaySeconds = 1.0;
|
|
|
|
|
NSTimeInterval delaySeconds = MAX(kMinDelaySeconds, date.timeIntervalSinceNow);
|
|
|
|
|
NSDate *newTimerScheduleDate = [NSDate dateWithTimeIntervalSinceNow:delaySeconds];
|
|
|
|
|
if (self.nextDisappearanceDate && [self.nextDisappearanceDate isBeforeDate:newTimerScheduleDate]) {
|
|
|
|
|
DDLogVerbose(@"%@ Request to run at %@ (%d sec.) ignored due to earlier scheduled run at %@ (%d sec.)",
|
|
|
|
|
self.logTag,
|
|
|
|
|
[dateFormatter stringFromDate:date],
|
|
|
|
|
[self.dateFormatter stringFromDate:date],
|
|
|
|
|
(int)round(MAX(0, [date timeIntervalSinceDate:[NSDate new]])),
|
|
|
|
|
[dateFormatter stringFromDate:self.timerScheduleDate],
|
|
|
|
|
(int)round(MAX(0, [self.timerScheduleDate timeIntervalSinceDate:[NSDate new]])));
|
|
|
|
|
[self.dateFormatter stringFromDate:self.nextDisappearanceDate],
|
|
|
|
|
(int)round(MAX(0, [self.nextDisappearanceDate timeIntervalSinceDate:[NSDate new]])));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Update Schedule
|
|
|
|
|
DDLogVerbose(@"%@ Scheduled run at %@ (%d sec.)",
|
|
|
|
|
self.logTag,
|
|
|
|
|
[dateFormatter stringFromDate:timerScheduleDate],
|
|
|
|
|
(int)round(MAX(0, [timerScheduleDate timeIntervalSinceDate:[NSDate new]])));
|
|
|
|
|
[self resetTimer];
|
|
|
|
|
self.timerScheduleDate = timerScheduleDate;
|
|
|
|
|
self.timer = [NSTimer weakScheduledTimerWithTimeInterval:delaySeconds
|
|
|
|
|
target:self
|
|
|
|
|
selector:@selector(timerDidFire)
|
|
|
|
|
userInfo:nil
|
|
|
|
|
repeats:NO];
|
|
|
|
|
[self.dateFormatter stringFromDate:newTimerScheduleDate],
|
|
|
|
|
(int)round(MAX(0, [newTimerScheduleDate timeIntervalSinceDate:[NSDate new]])));
|
|
|
|
|
[self resetNextDisappearanceTimer];
|
|
|
|
|
self.nextDisappearanceDate = newTimerScheduleDate;
|
|
|
|
|
self.nextDisappearanceTimer = [NSTimer weakScheduledTimerWithTimeInterval:delaySeconds
|
|
|
|
|
target:self
|
|
|
|
|
selector:@selector(disappearanceTimerDidFire)
|
|
|
|
|
userInfo:nil
|
|
|
|
|
repeats:NO];
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)timerDidFire
|
|
|
|
|
- (void)disappearanceTimerDidFire
|
|
|
|
|
{
|
|
|
|
|
OWSAssertIsOnMainThread();
|
|
|
|
|
DDLogDebug(@"%@ in %s", self.logTag, __PRETTY_FUNCTION__);
|
|
|
|
|
|
|
|
|
|
if (!CurrentAppContext().isMainAppAndActive) {
|
|
|
|
|
// Don't schedule run when inactive or not in main app.
|
|
|
|
@ -385,20 +348,43 @@ NS_ASSUME_NONNULL_BEGIN
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[self resetTimer];
|
|
|
|
|
[self resetNextDisappearanceTimer];
|
|
|
|
|
|
|
|
|
|
dispatch_async(OWSDisappearingMessagesJob.serialQueue, ^{
|
|
|
|
|
[self runLoop];
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)resetTimer
|
|
|
|
|
- (void)fallbackTimerDidFire
|
|
|
|
|
{
|
|
|
|
|
OWSAssertIsOnMainThread();
|
|
|
|
|
DDLogDebug(@"%@ in %s", self.logTag, __PRETTY_FUNCTION__);
|
|
|
|
|
|
|
|
|
|
BOOL recentlyScheduledDisappearanceTimer = NO;
|
|
|
|
|
if (fabs(self.nextDisappearanceDate.timeIntervalSinceNow) < 1.0) {
|
|
|
|
|
recentlyScheduledDisappearanceTimer = YES;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dispatch_async(OWSDisappearingMessagesJob.serialQueue, ^{
|
|
|
|
|
NSUInteger deletedCount = [self runLoop];
|
|
|
|
|
|
|
|
|
|
// Normally deletions should happen via the disappearanceTimer, to make sure that they're timely.
|
|
|
|
|
// So, if we're deleting something via the fallback timer, something may have gone wrong. The
|
|
|
|
|
// exception is if we're in close proximity to the disappearanceTimer, in which case a race condition
|
|
|
|
|
// is inevitable.
|
|
|
|
|
if (!recentlyScheduledDisappearanceTimer && deletedCount > 0) {
|
|
|
|
|
OWSProdLogAndFail(@"%@ unexpectedly deleted disappearing messages via fallback timer.");
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)resetNextDisappearanceTimer
|
|
|
|
|
{
|
|
|
|
|
OWSAssertIsOnMainThread();
|
|
|
|
|
|
|
|
|
|
[self.timer invalidate];
|
|
|
|
|
self.timer = nil;
|
|
|
|
|
self.timerScheduleDate = nil;
|
|
|
|
|
[self.nextDisappearanceTimer invalidate];
|
|
|
|
|
self.nextDisappearanceTimer = nil;
|
|
|
|
|
self.nextDisappearanceDate = nil;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#pragma mark - Notifications
|
|
|
|
@ -408,7 +394,9 @@ NS_ASSUME_NONNULL_BEGIN
|
|
|
|
|
OWSAssertIsOnMainThread();
|
|
|
|
|
|
|
|
|
|
[AppReadiness runNowOrWhenAppIsReady:^{
|
|
|
|
|
[self runNow];
|
|
|
|
|
dispatch_async(OWSDisappearingMessagesJob.serialQueue, ^{
|
|
|
|
|
[self runLoop];
|
|
|
|
|
});
|
|
|
|
|
}];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -416,7 +404,7 @@ NS_ASSUME_NONNULL_BEGIN
|
|
|
|
|
{
|
|
|
|
|
OWSAssertIsOnMainThread();
|
|
|
|
|
|
|
|
|
|
[self resetTimer];
|
|
|
|
|
[self resetNextDisappearanceTimer];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@end
|
|
|
|
|