2017-05-09 20:38:49 +02:00
|
|
|
//
|
|
|
|
// Copyright (c) 2017 Open Whisper Systems. All rights reserved.
|
|
|
|
//
|
2016-10-05 17:42:44 +02:00
|
|
|
|
|
|
|
#import "OWSIncomingMessageReadObserver.h"
|
|
|
|
#import "NSDate+millisecondTimeStamp.h"
|
|
|
|
#import "OWSSendReadReceiptsJob.h"
|
|
|
|
#import "TSIncomingMessage.h"
|
|
|
|
|
|
|
|
NS_ASSUME_NONNULL_BEGIN
|
|
|
|
|
|
|
|
@interface OWSIncomingMessageReadObserver ()
|
|
|
|
|
2017-06-12 17:51:25 +02:00
|
|
|
@property (nonatomic) BOOL isObserving;
|
2016-10-05 17:42:44 +02:00
|
|
|
@property (nonatomic, readonly) OWSSendReadReceiptsJob *sendReadReceiptsJob;
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation OWSIncomingMessageReadObserver
|
|
|
|
|
|
|
|
- (void)dealloc
|
|
|
|
{
|
|
|
|
[[NSNotificationCenter defaultCenter] removeObserver:self];
|
|
|
|
}
|
|
|
|
|
2017-08-01 18:39:10 +02:00
|
|
|
- (instancetype)initWithMessageSender:(OWSMessageSender *)messageSender
|
2016-10-05 17:42:44 +02:00
|
|
|
{
|
|
|
|
self = [super init];
|
|
|
|
if (!self) {
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
_isObserving = NO;
|
2016-10-14 23:00:29 +02:00
|
|
|
_sendReadReceiptsJob = [[OWSSendReadReceiptsJob alloc] initWithMessageSender:messageSender];
|
2016-10-05 17:42:44 +02:00
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)startObserving
|
|
|
|
{
|
2017-06-12 17:51:25 +02:00
|
|
|
OWSAssert([NSThread isMainThread]);
|
|
|
|
|
2016-10-05 17:42:44 +02:00
|
|
|
if (self.isObserving) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
self.isObserving = true;
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
|
|
|
selector:@selector(handleLocalReadNotification:)
|
|
|
|
name:TSIncomingMessageWasReadOnThisDeviceNotification
|
|
|
|
object:nil];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)handleLocalReadNotification:(NSNotification *)notification
|
|
|
|
{
|
|
|
|
if (![notification.object isKindOfClass:[TSIncomingMessage class]]) {
|
|
|
|
DDLogError(@"%@ Read receipt notifier got unexpected object: %@", self.tag, notification.object);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
TSIncomingMessage *message = (TSIncomingMessage *)notification.object;
|
|
|
|
[self.sendReadReceiptsJob runWith:message];
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - Logging
|
|
|
|
|
|
|
|
+ (NSString *)tag
|
|
|
|
{
|
|
|
|
return [NSString stringWithFormat:@"[%@]", self.class];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *)tag
|
|
|
|
{
|
|
|
|
return self.class.tag;
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
NS_ASSUME_NONNULL_END
|