session-ios/Signal/src/NotificationTracker.m

51 lines
1.6 KiB
Mathematica
Raw Normal View History

2014-05-06 19:41:08 +02:00
#import "NotificationTracker.h"
#import "CryptoTools.h"
#import "FunctionalUtil.h"
#define MAX_NOTIFICATIONS_TO_TRACK 100
#define NOTIFICATION_PAYLOAD_KEY @"m"
@implementation NotificationTracker {
NSMutableArray* _witnessedNotifications;
}
+(NotificationTracker*) notificationTracker {
NotificationTracker* notificationTracker = [NotificationTracker new];
notificationTracker->_witnessedNotifications = [NSMutableArray new];
return notificationTracker;
}
-(BOOL) shouldProcessNotification:(NSDictionary*) notification {
BOOL should = ![self wasNotificationProcessed:notification];
if (should) {
[self markNotificationAsProcessed:notification];
}
return should;
}
-(void) markNotificationAsProcessed:(NSDictionary*) notification {
NSData* data = [self getIdForNotification:notification];
[_witnessedNotifications insertObject:data atIndex:0];
2014-08-14 03:13:24 +02:00
while(MAX_NOTIFICATIONS_TO_TRACK < _witnessedNotifications.count){
2014-05-06 19:41:08 +02:00
[_witnessedNotifications removeLastObject];
}
}
-(BOOL) wasNotificationProcessed:(NSDictionary*) notification {
NSData* data = [self getIdForNotification:notification];
return [_witnessedNotifications any:^int(NSData* previousData) {
return [data isEqualToData:previousData];
}];
}
// Uniquely Identify a notification by the hash of the message payload.
-(NSData*) getIdForNotification:(NSDictionary*) notification {
NSData* data = [notification[NOTIFICATION_PAYLOAD_KEY] dataUsingEncoding:NSUTF8StringEncoding];
2014-05-06 19:41:08 +02:00
NSData* notificationHash = [data hashWithSha256];
return notificationHash;
}
@end