session-ios/SignalServiceKit/src/Messages/OWSIncomingMessageReadObserver.m
2017-08-02 10:38:23 -04:00

79 lines
1.8 KiB
Objective-C

//
// Copyright (c) 2017 Open Whisper Systems. All rights reserved.
//
#import "OWSIncomingMessageReadObserver.h"
#import "NSDate+millisecondTimeStamp.h"
#import "OWSSendReadReceiptsJob.h"
#import "TSIncomingMessage.h"
NS_ASSUME_NONNULL_BEGIN
@interface OWSIncomingMessageReadObserver ()
@property (nonatomic) BOOL isObserving;
@property (nonatomic, readonly) OWSSendReadReceiptsJob *sendReadReceiptsJob;
@end
@implementation OWSIncomingMessageReadObserver
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (instancetype)initWithMessageSender:(OWSMessageSender *)messageSender
{
self = [super init];
if (!self) {
return self;
}
_isObserving = NO;
_sendReadReceiptsJob = [[OWSSendReadReceiptsJob alloc] initWithMessageSender:messageSender];
return self;
}
- (void)startObserving
{
OWSAssert([NSThread isMainThread]);
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