2017-05-09 20:38:49 +02:00
|
|
|
//
|
2018-01-10 16:54:17 +01:00
|
|
|
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
|
2017-05-09 20:38:49 +02:00
|
|
|
//
|
2016-10-05 17:42:44 +02:00
|
|
|
|
|
|
|
#import "OWSDisappearingMessagesJob.h"
|
2017-11-29 21:27:19 +01:00
|
|
|
#import "AppContext.h"
|
2018-01-26 22:23:39 +01:00
|
|
|
#import "AppReadiness.h"
|
2016-10-14 23:00:29 +02:00
|
|
|
#import "ContactsManagerProtocol.h"
|
2017-05-09 20:38:49 +02:00
|
|
|
#import "NSTimer+OWS.h"
|
2018-02-27 14:42:30 +01:00
|
|
|
#import "OWSBackgroundTask.h"
|
2016-10-14 23:00:29 +02:00
|
|
|
#import "OWSDisappearingConfigurationUpdateInfoMessage.h"
|
2016-10-05 17:42:44 +02:00
|
|
|
#import "OWSDisappearingMessagesConfiguration.h"
|
|
|
|
#import "OWSDisappearingMessagesFinder.h"
|
2018-03-05 15:30:58 +01:00
|
|
|
#import "OWSPrimaryStorage.h"
|
2018-10-10 23:36:41 +02:00
|
|
|
#import "SSKEnvironment.h"
|
2016-10-14 23:00:29 +02:00
|
|
|
#import "TSIncomingMessage.h"
|
2016-10-05 17:42:44 +02:00
|
|
|
#import "TSMessage.h"
|
2018-05-07 16:47:40 +02:00
|
|
|
#import "TSThread.h"
|
2018-09-21 21:41:10 +02:00
|
|
|
#import <SignalCoreKit/NSDate+OWS.h>
|
2016-10-05 17:42:44 +02:00
|
|
|
|
|
|
|
NS_ASSUME_NONNULL_BEGIN
|
2018-04-16 17:18:39 +02:00
|
|
|
|
2017-11-24 19:22:51 +01:00
|
|
|
// Can we move to Signal-iOS?
|
2016-10-05 17:42:44 +02:00
|
|
|
@interface OWSDisappearingMessagesJob ()
|
|
|
|
|
2017-06-15 21:36:21 +02:00
|
|
|
@property (nonatomic, readonly) YapDatabaseConnection *databaseConnection;
|
|
|
|
|
2016-10-05 17:42:44 +02:00
|
|
|
@property (nonatomic, readonly) OWSDisappearingMessagesFinder *disappearingMessagesFinder;
|
2017-05-09 20:38:49 +02:00
|
|
|
|
2018-04-16 17:18:39 +02:00
|
|
|
+ (dispatch_queue_t)serialQueue;
|
|
|
|
|
2017-05-09 20:38:49 +02:00
|
|
|
// These three properties should only be accessed on the main thread.
|
|
|
|
@property (nonatomic) BOOL hasStarted;
|
2018-04-16 17:18:39 +02:00
|
|
|
@property (nonatomic, nullable) NSTimer *nextDisappearanceTimer;
|
|
|
|
@property (nonatomic, nullable) NSDate *nextDisappearanceDate;
|
|
|
|
@property (nonatomic, nullable) NSTimer *fallbackTimer;
|
2016-10-05 17:42:44 +02:00
|
|
|
|
|
|
|
@end
|
|
|
|
|
2018-04-16 17:18:39 +02:00
|
|
|
void AssertIsOnDisappearingMessagesQueue()
|
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
|
|
|
if (@available(iOS 10.0, *)) {
|
|
|
|
dispatch_assert_queue(OWSDisappearingMessagesJob.serialQueue);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-05-09 20:38:49 +02:00
|
|
|
#pragma mark -
|
|
|
|
|
2016-10-05 17:42:44 +02:00
|
|
|
@implementation OWSDisappearingMessagesJob
|
|
|
|
|
2017-05-09 20:38:49 +02:00
|
|
|
+ (instancetype)sharedJob
|
|
|
|
{
|
2018-10-10 23:36:41 +02:00
|
|
|
OWSAssertDebug(SSKEnvironment.shared.disappearingMessagesJob);
|
|
|
|
|
|
|
|
return SSKEnvironment.shared.disappearingMessagesJob;
|
2017-05-09 20:38:49 +02:00
|
|
|
}
|
|
|
|
|
2018-03-05 15:30:58 +01:00
|
|
|
- (instancetype)initWithPrimaryStorage:(OWSPrimaryStorage *)primaryStorage
|
2016-10-05 17:42:44 +02:00
|
|
|
{
|
|
|
|
self = [super init];
|
|
|
|
if (!self) {
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2018-03-05 15:30:58 +01:00
|
|
|
_databaseConnection = primaryStorage.newDatabaseConnection;
|
2017-06-15 16:23:55 +02:00
|
|
|
_disappearingMessagesFinder = [OWSDisappearingMessagesFinder new];
|
2016-10-05 17:42:44 +02:00
|
|
|
|
2018-04-16 17:18:39 +02:00
|
|
|
// suspenders in case a deletion schedule is missed.
|
|
|
|
NSTimeInterval kFallBackTimerInterval = 5 * kMinuteInterval;
|
2018-11-02 18:51:46 +01:00
|
|
|
[AppReadiness runNowOrWhenAppDidBecomeReady:^{
|
2018-04-16 17:18:39 +02:00
|
|
|
if (CurrentAppContext().isMainApp) {
|
2018-04-18 21:27:36 +02:00
|
|
|
self.fallbackTimer = [NSTimer weakScheduledTimerWithTimeInterval:kFallBackTimerInterval
|
|
|
|
target:self
|
|
|
|
selector:@selector(fallbackTimerDidFire)
|
|
|
|
userInfo:nil
|
|
|
|
repeats:YES];
|
2018-04-16 17:18:39 +02:00
|
|
|
}
|
|
|
|
}];
|
|
|
|
|
2017-05-09 20:38:49 +02:00
|
|
|
OWSSingletonAssert();
|
|
|
|
|
2017-05-10 15:25:27 +02:00
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
|
|
|
selector:@selector(applicationDidBecomeActive:)
|
2018-01-10 16:54:17 +01:00
|
|
|
name:OWSApplicationDidBecomeActiveNotification
|
2017-05-10 15:25:27 +02:00
|
|
|
object:nil];
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
|
|
|
selector:@selector(applicationWillResignActive:)
|
2018-01-10 16:54:17 +01:00
|
|
|
name:OWSApplicationWillResignActiveNotification
|
2017-05-10 15:25:27 +02:00
|
|
|
object:nil];
|
|
|
|
|
2016-10-05 17:42:44 +02:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2017-05-10 15:25:27 +02:00
|
|
|
- (void)dealloc
|
|
|
|
{
|
|
|
|
[[NSNotificationCenter defaultCenter] removeObserver:self];
|
|
|
|
}
|
|
|
|
|
2017-05-09 20:38:49 +02:00
|
|
|
+ (dispatch_queue_t)serialQueue
|
|
|
|
{
|
|
|
|
static dispatch_queue_t queue = nil;
|
|
|
|
static dispatch_once_t onceToken;
|
|
|
|
dispatch_once(&onceToken, ^{
|
|
|
|
queue = dispatch_queue_create("org.whispersystems.disappearing.messages", DISPATCH_QUEUE_SERIAL);
|
|
|
|
});
|
|
|
|
return queue;
|
|
|
|
}
|
|
|
|
|
2018-10-03 23:41:43 +02:00
|
|
|
#pragma mark - Dependencies
|
|
|
|
|
|
|
|
- (id<ContactsManagerProtocol>)contactsManager
|
|
|
|
{
|
|
|
|
return SSKEnvironment.shared.contactsManager;
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark -
|
|
|
|
|
2018-04-16 17:18:39 +02:00
|
|
|
- (NSUInteger)deleteExpiredMessages
|
2016-10-05 17:42:44 +02:00
|
|
|
{
|
2018-04-16 17:18:39 +02:00
|
|
|
AssertIsOnDisappearingMessagesQueue();
|
|
|
|
|
2016-10-05 17:42:44 +02:00
|
|
|
uint64_t now = [NSDate ows_millisecondTimeStamp];
|
|
|
|
|
2018-02-27 14:42:30 +01:00
|
|
|
OWSBackgroundTask *_Nullable backgroundTask = [OWSBackgroundTask backgroundTaskWithLabelStr:__PRETTY_FUNCTION__];
|
|
|
|
|
2018-04-16 17:18:39 +02:00
|
|
|
__block NSUInteger expirationCount = 0;
|
2017-06-15 21:36:21 +02:00
|
|
|
[self.databaseConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *_Nonnull transaction) {
|
2017-06-15 16:23:55 +02:00
|
|
|
[self.disappearingMessagesFinder enumerateExpiredMessagesWithBlock:^(TSMessage *message) {
|
|
|
|
// sanity check
|
|
|
|
if (message.expiresAt > now) {
|
2018-08-27 18:51:32 +02:00
|
|
|
OWSFailDebug(@"Refusing to remove message which doesn't expire until: %lld", message.expiresAt);
|
2017-06-15 16:23:55 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-08-27 18:51:32 +02:00
|
|
|
OWSLogInfo(@"Removing message which expired at: %lld", message.expiresAt);
|
2017-06-15 16:23:55 +02:00
|
|
|
[message removeWithTransaction:transaction];
|
|
|
|
expirationCount++;
|
2016-10-05 17:42:44 +02:00
|
|
|
}
|
2017-06-15 16:23:55 +02:00
|
|
|
transaction:transaction];
|
2016-10-05 17:42:44 +02:00
|
|
|
}];
|
|
|
|
|
2018-08-27 18:55:37 +02:00
|
|
|
OWSLogDebug(@"Removed %lu expired messages", (unsigned long)expirationCount);
|
2018-02-27 14:42:30 +01:00
|
|
|
|
2018-09-06 19:01:24 +02:00
|
|
|
OWSAssertDebug(backgroundTask);
|
2018-02-27 14:42:30 +01:00
|
|
|
backgroundTask = nil;
|
2018-04-16 17:18:39 +02:00
|
|
|
return expirationCount;
|
2016-10-05 17:42:44 +02:00
|
|
|
}
|
|
|
|
|
2018-04-16 17:18:39 +02:00
|
|
|
// deletes any expired messages and schedules the next run.
|
|
|
|
- (NSUInteger)runLoop
|
2016-10-05 17:42:44 +02:00
|
|
|
{
|
2018-08-27 18:51:32 +02:00
|
|
|
OWSLogVerbose(@"in runLoop");
|
2018-04-16 17:18:39 +02:00
|
|
|
AssertIsOnDisappearingMessagesQueue();
|
2016-10-05 17:42:44 +02:00
|
|
|
|
2018-04-16 17:18:39 +02:00
|
|
|
NSUInteger deletedCount = [self deleteExpiredMessages];
|
2016-10-05 17:42:44 +02:00
|
|
|
|
2017-06-15 16:23:55 +02:00
|
|
|
__block NSNumber *nextExpirationTimestampNumber;
|
2017-06-15 21:36:21 +02:00
|
|
|
[self.databaseConnection readWithBlock:^(YapDatabaseReadTransaction *_Nonnull transaction) {
|
2017-06-15 16:23:55 +02:00
|
|
|
nextExpirationTimestampNumber =
|
|
|
|
[self.disappearingMessagesFinder nextExpirationTimestampWithTransaction:transaction];
|
|
|
|
}];
|
2018-04-16 17:18:39 +02:00
|
|
|
|
2016-10-05 17:42:44 +02:00
|
|
|
if (!nextExpirationTimestampNumber) {
|
2018-08-27 18:51:32 +02:00
|
|
|
OWSLogDebug(@"No more expiring messages.");
|
2018-04-16 17:18:39 +02:00
|
|
|
return deletedCount;
|
2016-10-05 17:42:44 +02:00
|
|
|
}
|
|
|
|
|
2018-04-16 17:18:39 +02:00
|
|
|
uint64_t nextExpirationAt = nextExpirationTimestampNumber.unsignedLongLongValue;
|
|
|
|
NSDate *nextEpirationDate = [NSDate ows_dateWithMillisecondsSince1970:nextExpirationAt];
|
|
|
|
[self scheduleRunByDate:nextEpirationDate];
|
|
|
|
|
|
|
|
return deletedCount;
|
2017-05-09 20:38:49 +02:00
|
|
|
}
|
2016-10-05 17:42:44 +02:00
|
|
|
|
2018-04-18 02:53:27 +02:00
|
|
|
- (void)startAnyExpirationForMessage:(TSMessage *)message
|
|
|
|
expirationStartedAt:(uint64_t)expirationStartedAt
|
|
|
|
transaction:(YapDatabaseReadWriteTransaction *_Nonnull)transaction
|
2016-10-05 17:42:44 +02:00
|
|
|
{
|
2018-09-06 19:01:24 +02:00
|
|
|
OWSAssertDebug(transaction);
|
2017-06-15 16:23:55 +02:00
|
|
|
|
2016-10-05 17:42:44 +02:00
|
|
|
if (!message.isExpiringMessage) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-18 02:53:27 +02:00
|
|
|
NSTimeInterval startedSecondsAgo = ([NSDate ows_millisecondTimeStamp] - expirationStartedAt) / 1000.0;
|
2018-08-27 18:51:32 +02:00
|
|
|
OWSLogDebug(@"Starting expiration for message read %f seconds ago", startedSecondsAgo);
|
2016-10-05 17:42:44 +02:00
|
|
|
|
|
|
|
// Don't clobber if multiple actions simultaneously triggered expiration.
|
|
|
|
if (message.expireStartedAt == 0 || message.expireStartedAt > expirationStartedAt) {
|
2017-11-15 13:11:49 +01:00
|
|
|
[message updateWithExpireStartedAt:expirationStartedAt transaction:transaction];
|
2016-10-05 17:42:44 +02:00
|
|
|
}
|
|
|
|
|
2018-04-17 00:38:29 +02:00
|
|
|
[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]];
|
|
|
|
}];
|
2016-10-05 17:42:44 +02:00
|
|
|
}
|
|
|
|
|
2018-10-03 23:41:43 +02:00
|
|
|
#pragma mark - Apply Remote Configuration
|
2018-06-14 16:08:38 +02:00
|
|
|
|
|
|
|
- (void)becomeConsistentWithDisappearingDuration:(uint32_t)duration
|
|
|
|
thread:(TSThread *)thread
|
2018-10-03 23:41:43 +02:00
|
|
|
createdByRemoteRecipientId:(nullable NSString *)remoteRecipientId
|
2018-06-14 16:35:47 +02:00
|
|
|
createdInExistingGroup:(BOOL)createdInExistingGroup
|
2018-06-14 16:08:38 +02:00
|
|
|
transaction:(YapDatabaseReadWriteTransaction *)transaction
|
|
|
|
{
|
2018-09-06 19:01:24 +02:00
|
|
|
OWSAssertDebug(thread);
|
|
|
|
OWSAssertDebug(transaction);
|
2018-06-14 16:08:38 +02:00
|
|
|
|
2018-08-02 21:18:40 +02:00
|
|
|
OWSBackgroundTask *_Nullable backgroundTask = [OWSBackgroundTask backgroundTaskWithLabelStr:__PRETTY_FUNCTION__];
|
|
|
|
|
2018-10-03 23:41:43 +02:00
|
|
|
NSString *_Nullable remoteContactName = nil;
|
|
|
|
if (remoteRecipientId) {
|
2018-12-19 22:13:34 +01:00
|
|
|
remoteContactName = [self.contactsManager displayNameForPhoneIdentifier:remoteRecipientId
|
|
|
|
transaction:transaction];
|
2018-10-03 23:41:43 +02:00
|
|
|
}
|
|
|
|
|
2018-04-17 00:38:29 +02:00
|
|
|
// 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 =
|
2018-06-14 16:08:38 +02:00
|
|
|
[thread disappearingMessagesConfigurationWithTransaction:transaction];
|
2018-05-15 21:13:11 +02:00
|
|
|
|
2018-06-14 16:08:38 +02:00
|
|
|
if (duration == 0) {
|
2018-05-07 16:47:40 +02:00
|
|
|
disappearingMessagesConfiguration.enabled = NO;
|
|
|
|
} else {
|
2018-04-17 00:38:29 +02:00
|
|
|
disappearingMessagesConfiguration.enabled = YES;
|
2018-06-14 16:08:38 +02:00
|
|
|
disappearingMessagesConfiguration.durationSeconds = duration;
|
2018-04-17 00:38:29 +02:00
|
|
|
}
|
2018-05-07 16:47:40 +02:00
|
|
|
|
|
|
|
if (!disappearingMessagesConfiguration.dictionaryValueDidChange) {
|
|
|
|
return;
|
|
|
|
}
|
2018-06-14 16:08:38 +02:00
|
|
|
|
2018-08-27 18:51:32 +02:00
|
|
|
OWSLogInfo(@"becoming consistent with disappearing message configuration: %@",
|
2018-05-07 16:47:40 +02:00
|
|
|
disappearingMessagesConfiguration.dictionaryValue);
|
2018-06-14 16:08:38 +02:00
|
|
|
|
2018-05-07 16:47:40 +02:00
|
|
|
[disappearingMessagesConfiguration saveWithTransaction:transaction];
|
|
|
|
|
2018-10-03 23:41:43 +02:00
|
|
|
// MJK TODO - should be safe to remove this senderTimestamp
|
2018-06-14 16:08:38 +02:00
|
|
|
OWSDisappearingConfigurationUpdateInfoMessage *infoMessage =
|
2018-10-03 23:41:43 +02:00
|
|
|
[[OWSDisappearingConfigurationUpdateInfoMessage alloc] initWithTimestamp:[NSDate ows_millisecondTimeStamp]
|
2018-09-20 22:10:35 +02:00
|
|
|
thread:thread
|
|
|
|
configuration:disappearingMessagesConfiguration
|
|
|
|
createdByRemoteName:remoteContactName
|
|
|
|
createdInExistingGroup:createdInExistingGroup];
|
2018-06-14 16:08:38 +02:00
|
|
|
[infoMessage saveWithTransaction:transaction];
|
|
|
|
|
2018-09-06 19:01:24 +02:00
|
|
|
OWSAssertDebug(backgroundTask);
|
2018-04-17 00:38:29 +02:00
|
|
|
backgroundTask = nil;
|
2016-10-14 23:00:29 +02:00
|
|
|
}
|
|
|
|
|
2018-10-03 23:41:43 +02:00
|
|
|
#pragma mark -
|
|
|
|
|
2017-05-09 20:38:49 +02:00
|
|
|
- (void)startIfNecessary
|
|
|
|
{
|
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
|
|
if (self.hasStarted) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
self.hasStarted = YES;
|
|
|
|
|
2018-04-16 17:18:39 +02:00
|
|
|
dispatch_async(OWSDisappearingMessagesJob.serialQueue, ^{
|
2018-05-03 01:15:46 +02:00
|
|
|
// Theoretically this shouldn't be necessary, but there was a race condition when receiving a backlog
|
|
|
|
// of messages across timer changes which could cause a disappearing message's timer to never be started.
|
|
|
|
[self.databaseConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *_Nonnull transaction) {
|
|
|
|
[self cleanupMessagesWhichFailedToStartExpiringWithTransaction:transaction];
|
|
|
|
}];
|
|
|
|
|
2018-04-16 17:18:39 +02:00
|
|
|
[self runLoop];
|
|
|
|
});
|
2017-05-09 20:38:49 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-04-13 21:59:16 +02:00
|
|
|
- (NSDateFormatter *)dateFormatter
|
|
|
|
{
|
|
|
|
static NSDateFormatter *dateFormatter;
|
|
|
|
static dispatch_once_t onceToken;
|
|
|
|
dispatch_once(&onceToken, ^{
|
|
|
|
dateFormatter = [NSDateFormatter new];
|
|
|
|
dateFormatter.dateStyle = NSDateFormatterNoStyle;
|
|
|
|
dateFormatter.timeStyle = kCFDateFormatterMediumStyle;
|
|
|
|
dateFormatter.locale = [NSLocale systemLocale];
|
|
|
|
});
|
|
|
|
|
|
|
|
return dateFormatter;
|
|
|
|
}
|
|
|
|
|
2018-04-16 17:18:39 +02:00
|
|
|
- (void)scheduleRunByDate:(NSDate *)date
|
2017-05-09 20:38:49 +02:00
|
|
|
{
|
2018-09-06 19:01:24 +02:00
|
|
|
OWSAssertDebug(date);
|
2017-05-09 20:38:49 +02:00
|
|
|
|
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
2017-11-29 21:27:19 +01:00
|
|
|
if (!CurrentAppContext().isMainAppAndActive) {
|
|
|
|
// Don't schedule run when inactive or not in main app.
|
|
|
|
return;
|
|
|
|
}
|
2017-05-10 15:25:27 +02:00
|
|
|
|
2017-05-09 20:38:49 +02:00
|
|
|
// Don't run more often than once per second.
|
2018-04-16 17:18:39 +02:00
|
|
|
const NSTimeInterval kMinDelaySeconds = 1.0;
|
|
|
|
NSTimeInterval delaySeconds = MAX(kMinDelaySeconds, date.timeIntervalSinceNow);
|
2018-04-13 21:59:16 +02:00
|
|
|
NSDate *newTimerScheduleDate = [NSDate dateWithTimeIntervalSinceNow:delaySeconds];
|
2018-04-16 17:18:39 +02:00
|
|
|
if (self.nextDisappearanceDate && [self.nextDisappearanceDate isBeforeDate:newTimerScheduleDate]) {
|
2018-08-27 18:55:37 +02:00
|
|
|
OWSLogVerbose(@"Request to run at %@ (%d sec.) ignored due to earlier scheduled run at %@ (%d sec.)",
|
2018-04-13 21:59:16 +02:00
|
|
|
[self.dateFormatter stringFromDate:date],
|
2017-05-09 20:38:49 +02:00
|
|
|
(int)round(MAX(0, [date timeIntervalSinceDate:[NSDate new]])),
|
2018-04-16 17:18:39 +02:00
|
|
|
[self.dateFormatter stringFromDate:self.nextDisappearanceDate],
|
|
|
|
(int)round(MAX(0, [self.nextDisappearanceDate timeIntervalSinceDate:[NSDate new]])));
|
2017-05-09 20:38:49 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update Schedule
|
2018-08-27 18:55:37 +02:00
|
|
|
OWSLogVerbose(@"Scheduled run at %@ (%d sec.)",
|
2018-04-13 21:59:16 +02:00
|
|
|
[self.dateFormatter stringFromDate:newTimerScheduleDate],
|
|
|
|
(int)round(MAX(0, [newTimerScheduleDate timeIntervalSinceDate:[NSDate new]])));
|
2018-04-16 17:18:39 +02:00
|
|
|
[self resetNextDisappearanceTimer];
|
|
|
|
self.nextDisappearanceDate = newTimerScheduleDate;
|
|
|
|
self.nextDisappearanceTimer = [NSTimer weakScheduledTimerWithTimeInterval:delaySeconds
|
|
|
|
target:self
|
|
|
|
selector:@selector(disappearanceTimerDidFire)
|
|
|
|
userInfo:nil
|
|
|
|
repeats:NO];
|
2017-05-09 20:38:49 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-04-16 17:18:39 +02:00
|
|
|
- (void)disappearanceTimerDidFire
|
2017-05-09 20:38:49 +02:00
|
|
|
{
|
2017-12-19 17:38:25 +01:00
|
|
|
OWSAssertIsOnMainThread();
|
2018-08-27 18:12:53 +02:00
|
|
|
OWSLogDebug(@"");
|
2017-05-10 15:25:27 +02:00
|
|
|
|
2017-11-29 21:27:19 +01:00
|
|
|
if (!CurrentAppContext().isMainAppAndActive) {
|
|
|
|
// Don't schedule run when inactive or not in main app.
|
2018-08-27 18:51:32 +02:00
|
|
|
OWSFailDebug(@"Disappearing messages job timer fired while main app inactive.");
|
2017-11-29 21:27:19 +01:00
|
|
|
return;
|
|
|
|
}
|
2017-05-10 15:25:27 +02:00
|
|
|
|
2018-04-16 17:18:39 +02:00
|
|
|
[self resetNextDisappearanceTimer];
|
2017-05-09 20:38:49 +02:00
|
|
|
|
|
|
|
dispatch_async(OWSDisappearingMessagesJob.serialQueue, ^{
|
|
|
|
[self runLoop];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-04-16 17:18:39 +02:00
|
|
|
- (void)fallbackTimerDidFire
|
|
|
|
{
|
|
|
|
OWSAssertIsOnMainThread();
|
2018-08-27 18:12:53 +02:00
|
|
|
OWSLogDebug(@"");
|
2018-04-16 17:18:39 +02:00
|
|
|
|
|
|
|
BOOL recentlyScheduledDisappearanceTimer = NO;
|
|
|
|
if (fabs(self.nextDisappearanceDate.timeIntervalSinceNow) < 1.0) {
|
|
|
|
recentlyScheduledDisappearanceTimer = YES;
|
|
|
|
}
|
|
|
|
|
2018-05-18 19:23:10 +02:00
|
|
|
if (!CurrentAppContext().isMainAppAndActive) {
|
2018-08-27 18:51:32 +02:00
|
|
|
OWSLogInfo(@"Ignoring fallbacktimer for app which is not main and active.");
|
2018-05-18 19:23:10 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-16 17:18:39 +02:00
|
|
|
dispatch_async(OWSDisappearingMessagesJob.serialQueue, ^{
|
|
|
|
NSUInteger deletedCount = [self runLoop];
|
|
|
|
|
2018-05-15 21:13:11 +02:00
|
|
|
// Normally deletions should happen via the disappearanceTimer, to make sure that they're prompt.
|
|
|
|
// So, if we're deleting something via this fallback timer, something may have gone wrong. The
|
2018-04-16 17:18:39 +02:00
|
|
|
// exception is if we're in close proximity to the disappearanceTimer, in which case a race condition
|
|
|
|
// is inevitable.
|
|
|
|
if (!recentlyScheduledDisappearanceTimer && deletedCount > 0) {
|
2018-08-27 18:51:32 +02:00
|
|
|
OWSFailDebug(@"unexpectedly deleted disappearing messages via fallback timer.");
|
2018-04-16 17:18:39 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)resetNextDisappearanceTimer
|
2017-05-10 15:25:27 +02:00
|
|
|
{
|
2017-12-19 17:38:25 +01:00
|
|
|
OWSAssertIsOnMainThread();
|
2017-05-10 15:25:27 +02:00
|
|
|
|
2018-04-16 17:18:39 +02:00
|
|
|
[self.nextDisappearanceTimer invalidate];
|
|
|
|
self.nextDisappearanceTimer = nil;
|
|
|
|
self.nextDisappearanceDate = nil;
|
2017-05-10 15:25:27 +02:00
|
|
|
}
|
|
|
|
|
2018-05-03 01:15:46 +02:00
|
|
|
#pragma mark - Cleanup
|
|
|
|
|
|
|
|
- (void)cleanupMessagesWhichFailedToStartExpiringWithTransaction:(YapDatabaseReadWriteTransaction *)transaction
|
|
|
|
{
|
2018-05-15 21:13:11 +02:00
|
|
|
[self.disappearingMessagesFinder
|
|
|
|
enumerateMessagesWhichFailedToStartExpiringWithBlock:^(TSMessage *_Nonnull message) {
|
2018-08-27 18:55:37 +02:00
|
|
|
OWSFailDebug(@"starting old timer for message timestamp: %lu", (unsigned long)message.timestamp);
|
2018-05-15 21:13:11 +02:00
|
|
|
|
|
|
|
// We don't know when it was actually read, so assume it was read as soon as it was received.
|
2018-10-03 23:41:43 +02:00
|
|
|
uint64_t readTimeBestGuess = message.receivedAtTimestamp;
|
2018-06-13 19:20:58 +02:00
|
|
|
[self startAnyExpirationForMessage:message expirationStartedAt:readTimeBestGuess transaction:transaction];
|
2018-05-15 21:13:11 +02:00
|
|
|
}
|
|
|
|
transaction:transaction];
|
2018-05-03 01:15:46 +02:00
|
|
|
}
|
|
|
|
|
2017-05-10 15:25:27 +02:00
|
|
|
#pragma mark - Notifications
|
|
|
|
|
|
|
|
- (void)applicationDidBecomeActive:(NSNotification *)notification
|
|
|
|
{
|
2017-12-19 17:38:25 +01:00
|
|
|
OWSAssertIsOnMainThread();
|
2017-05-10 15:25:27 +02:00
|
|
|
|
2018-11-02 18:51:46 +01:00
|
|
|
[AppReadiness runNowOrWhenAppDidBecomeReady:^{
|
2018-04-16 17:18:39 +02:00
|
|
|
dispatch_async(OWSDisappearingMessagesJob.serialQueue, ^{
|
|
|
|
[self runLoop];
|
|
|
|
});
|
2018-01-26 22:23:39 +01:00
|
|
|
}];
|
2017-05-10 15:25:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)applicationWillResignActive:(NSNotification *)notification
|
|
|
|
{
|
2017-12-19 17:38:25 +01:00
|
|
|
OWSAssertIsOnMainThread();
|
2017-05-10 15:25:27 +02:00
|
|
|
|
2018-04-16 17:18:39 +02:00
|
|
|
[self resetNextDisappearanceTimer];
|
2017-05-10 15:25:27 +02:00
|
|
|
}
|
|
|
|
|
2016-10-05 17:42:44 +02:00
|
|
|
@end
|
|
|
|
|
|
|
|
NS_ASSUME_NONNULL_END
|