Skip to content

Commit

Permalink
Fix missing UILocalNotifications
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisballinger committed Jan 17, 2014
1 parent 682f4b9 commit 839df96
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 53 deletions.
2 changes: 1 addition & 1 deletion Off the Record/OTRCodec.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

@interface OTRCodec : NSObject

+ (void)decodeMessage:(OTRManagedMessage*)theMessage;
+ (void)decodeMessage:(OTRManagedMessage*)theMessage completionBlock:(void (^)(OTRManagedMessage * message))completionBlock;
+ (void)encodeMessage:(OTRManagedMessage*)theMessage completionBlock:(void (^)(OTRManagedMessage * message))completionBlock;

+ (void)generateOtrInitiateOrRefreshMessageTobuddy:(OTRManagedBuddy*)buddy
Expand Down
42 changes: 32 additions & 10 deletions Off the Record/OTRCodec.m
Original file line number Diff line number Diff line change
Expand Up @@ -29,28 +29,37 @@
@implementation OTRCodec


+(void) decodeMessage:(OTRManagedMessage*)theMessage;
+(void) decodeMessage:(OTRManagedMessage*)theMessage completionBlock:(void (^)(OTRManagedMessage *))completionBlock
{
NSString *message = theMessage.message;
NSString *friendAccount = theMessage.buddy.accountName;
NSString *protocol = theMessage.buddy.account.protocol;
NSString *myAccountName = theMessage.buddy.account.username;
NSManagedObjectID *messageObjectID = theMessage.objectID;

[[OTRKit sharedInstance] decodeMessage:message recipient:friendAccount accountName:myAccountName protocol:protocol completionBlock:^(NSString *decodedMessageString) {
NSError *error = nil;
OTRManagedMessage *localMessage = (OTRManagedMessage*)[[NSManagedObjectContext MR_contextForCurrentThread] existingObjectWithID:messageObjectID error:&error];
if (error) {
DDLogError(@"Error fetching message: %@", error);
error = nil;
}
if([decodedMessageString length]) {
theMessage.message = [OTRUtilities stripHTML:decodedMessageString];
[theMessage setIsEncryptedValue:NO];
localMessage.message = [OTRUtilities stripHTML:decodedMessageString];
[localMessage setIsEncryptedValue:NO];
} else {
[theMessage setIsEncryptedValue:YES];
[localMessage setIsEncryptedValue:YES];
}


OTRKitMessageState messageState = [[OTRKit sharedInstance] messageStateForUsername:friendAccount accountName:myAccountName protocol:protocol];
[localMessage.buddy setNewEncryptionStatus:messageState];

NSManagedObjectContext * context = [NSManagedObjectContext MR_contextForCurrentThread];
[context MR_saveToPersistentStoreAndWait];

OTRKitMessageState messageState = [[OTRKit sharedInstance] messageStateForUsername:friendAccount accountName:myAccountName protocol:protocol];
[theMessage.buddy setNewEncryptionStatus:messageState];
if (completionBlock) {
completionBlock(localMessage);
}
}];
}

Expand All @@ -60,13 +69,26 @@ +(void)encodeMessage:(OTRManagedMessage *)theMessage completionBlock:(void (^)(O
NSString *recipientAccount = theMessage.buddy.accountName;
NSString *protocol = theMessage.buddy.account.protocol;
NSString *sendingAccount = theMessage.buddy.account.username;
NSManagedObjectID *buddyObjectID = theMessage.buddy.objectID;
NSManagedObjectID *messageObjectID = theMessage.objectID;
//theMessage.isEncryptedValue = NO;

//NSString *encodedMessageString = [[OTRKit sharedInstance] encodeMessage:message recipient:recipientAccount accountName:sendingAccount protocol:protocol];
[[OTRKit sharedInstance] encodeMessage:message recipient:recipientAccount accountName:sendingAccount protocol:protocol completionBlock:^(NSString *message) {
OTRManagedMessage *newOTRMessage = [OTRManagedMessage newMessageToBuddy:theMessage.buddy message:message encrypted:YES];
newOTRMessage.date = theMessage.date;
newOTRMessage.uniqueID = theMessage.uniqueID;
NSError *error = nil;
OTRManagedBuddy *localBuddy = (OTRManagedBuddy*)[[NSManagedObjectContext MR_contextForCurrentThread] existingObjectWithID:buddyObjectID error:&error];
if (error) {
DDLogError(@"Error fetching buddy: %@", error);
error = nil;
}
OTRManagedMessage *localMessage = (OTRManagedMessage*)[[NSManagedObjectContext MR_contextForCurrentThread] existingObjectWithID:messageObjectID error:&error];
if (error) {
DDLogError(@"Error fetching message: %@", error);
error = nil;
}
OTRManagedMessage *newOTRMessage = [OTRManagedMessage newMessageToBuddy:localBuddy message:message encrypted:YES];
newOTRMessage.date = localMessage.date;
newOTRMessage.uniqueID = localMessage.uniqueID;

NSManagedObjectContext * context = [NSManagedObjectContext MR_contextForCurrentThread];
[context MR_saveToPersistentStoreAndWait];
Expand Down
1 change: 0 additions & 1 deletion Off the Record/OTRConstants.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
#define kOTRProtocolLoginFailSSLStatusKey @"kOTRProtocolLoginFailSSLStatusKey"
#define kOTRProtocolLoginFailHostnameKey @"kOTRProtocolLoginFailHostnameKey"
#define kOTRProtocolLoginFailSSLCertificateDataKey @"kOTRProtocolLoginFailSSLCertificateData"
#define kOTRBuddyListUpdate @"BuddyListUpdateNotification"
#define kOTRMessageReceived @"MessageReceivedNotification"
#define kOTRMessageReceiptResonseReceived @"MessageReceiptResponseNotification"
#define kOTRStatusUpdate @"StatusUpdatedNotification"
Expand Down
1 change: 0 additions & 1 deletion Off the Record/OTRManagedBuddy.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@

@interface OTRManagedBuddy : _OTRManagedBuddy

-(void)receiveMessage:(NSString *)message;
-(void)receiveChatStateMessage:(OTRChatState) chatState;

-(BOOL)protocolIsXMPP;
Expand Down
33 changes: 0 additions & 33 deletions Off the Record/OTRManagedBuddy.m
Original file line number Diff line number Diff line change
Expand Up @@ -94,39 +94,6 @@ -(void)invalidateInactiveChatStateTimer
OTRXMPPManager * protocol = (OTRXMPPManager *)[protocolManager protocolForAccount:self.account];
[[protocol inactiveChatStateTimerForBuddyObjectID:self.objectID] invalidate];
}

}

-(void)receiveMessage:(NSString *)message
{
//DDLogVerbose(@"received: %@",message);
if (message) {
self.lastMessageDisconnected = NO;
// Strip the shit out of it, but hopefully you're talking with someone who is trusted in the first place
// TODO: fix this so it doesn't break some cyrillic encodings
//NSString *rawMessage = [[[message stringByConvertingHTMLToPlainText]stringByEncodingHTMLEntities] stringByLinkifyingURLs];
NSString * rawMessage = [message stringByConvertingHTMLToPlainText];

//[[NSNotificationCenter defaultCenter] postNotificationName:MESSAGE_PROCESSED_NOTIFICATION object:self];

if (![[UIApplication sharedApplication] applicationState] == UIApplicationStateActive)
{
// We are not active, so use a local notification instead
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.alertAction = REPLY_STRING;
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.applicationIconBadgeNumber = [UIApplication sharedApplication].applicationIconBadgeNumber + 1;
localNotification.alertBody = [NSString stringWithFormat:@"%@: %@",self.displayName,rawMessage];

NSMutableDictionary *userInfo = [NSMutableDictionary dictionaryWithCapacity:3];
[userInfo setObject:self.accountName forKey:kOTRNotificationUserNameKey];
[userInfo setObject:self.account.username forKey:kOTRNotificationAccountNameKey];
[userInfo setObject:self.account.protocol forKey:kOTRNotificationProtocolKey];
localNotification.userInfo = userInfo;

[[UIApplication sharedApplication] presentLocalNotificationNow:localNotification];
}
}
}

-(void)receiveChatStateMessage:(OTRChatState) newChatState
Expand Down
2 changes: 2 additions & 0 deletions Off the Record/OTRManagedMessage.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@

@interface OTRManagedMessage : _OTRManagedMessage

+ (void) showLocalNotificationForMessage:(OTRManagedMessage*)message;

+(OTRManagedMessage*)newMessageFromBuddy:(OTRManagedBuddy *)theBuddy message:(NSString *)theMessage encrypted:(BOOL)encryptionStatus delayedDate:(NSDate *)date;
+(OTRManagedMessage *)newMessageToBuddy:(OTRManagedBuddy *)theBuddy message:(NSString *)theMessage encrypted:(BOOL)encryptionStatus;

Expand Down
32 changes: 32 additions & 0 deletions Off the Record/OTRManagedMessage.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
#import "OTRConstants.h"
#import "OTRProtocolManager.h"
#import "OTRUtilities.h"
#import "NSString+HTML.h"
#import "Strings.h"


@implementation OTRManagedMessage

Expand Down Expand Up @@ -67,6 +70,35 @@ +(OTRManagedMessage*)newMessageFromBuddy:(OTRManagedBuddy *)theBuddy message:(NS

}

+ (void) showLocalNotificationForMessage:(OTRManagedMessage*)message {
OTRManagedMessage *localMessage = [message MR_inThreadContext];
if (localMessage.isEncryptedValue) {
DDLogWarn(@"Message was unable to be decrypted, not showing local notification");
return;
}
localMessage.buddy.lastMessageDisconnected = NO;
NSString * rawMessage = [localMessage.message stringByConvertingHTMLToPlainText];
[[NSManagedObjectContext MR_contextForCurrentThread] MR_saveToPersistentStoreAndWait];
if (![[UIApplication sharedApplication] applicationState] == UIApplicationStateActive)
{
// We are not active, so use a local notification instead
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.alertAction = REPLY_STRING;
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.applicationIconBadgeNumber = [UIApplication sharedApplication].applicationIconBadgeNumber + 1;
localNotification.alertBody = [NSString stringWithFormat:@"%@: %@",localMessage.buddy.displayName,rawMessage];

NSMutableDictionary *userInfo = [NSMutableDictionary dictionaryWithCapacity:3];
[userInfo setObject:localMessage.buddy.accountName forKey:kOTRNotificationUserNameKey];
[userInfo setObject:localMessage.buddy.account.username forKey:kOTRNotificationAccountNameKey];
[userInfo setObject:localMessage.buddy.account.protocol forKey:kOTRNotificationProtocolKey];
localNotification.userInfo = userInfo;
dispatch_async(dispatch_get_main_queue(), ^{
[[UIApplication sharedApplication] presentLocalNotificationNow:localNotification];
});
}
}

+(OTRManagedMessage *)newMessageToBuddy:(OTRManagedBuddy *)theBuddy message:(NSString *)theMessage encrypted:(BOOL)encryptionStatus
{
OTRManagedMessage *message = [OTRManagedMessage newMessageWithBuddy:theBuddy message:theMessage];
Expand Down
10 changes: 3 additions & 7 deletions Off the Record/OTRXMPPManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -514,13 +514,9 @@ - (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message
NSDate * date = [message delayedDeliveryDate];

OTRManagedMessage *otrMessage = [OTRManagedMessage newMessageFromBuddy:messageBuddy message:body encrypted:YES delayedDate:date];
[OTRCodec decodeMessage:otrMessage];

if(otrMessage && !otrMessage.isEncryptedValue)
{
[messageBuddy receiveMessage:otrMessage.message];

}
[OTRCodec decodeMessage:otrMessage completionBlock:^(OTRManagedMessage *message) {
[OTRManagedMessage showLocalNotificationForMessage:message];
}];
}

}
Expand Down
1 change: 1 addition & 0 deletions Off the Record/Off the Record-Prefix.pch
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#define MR_ENABLE_ACTIVE_RECORD_LOGGING 0
#else
static int ddLogLevel = LOG_LEVEL_OFF;
#define MR_ENABLE_ACTIVE_RECORD_LOGGING 0
#endif

#import "CoreData+MagicalRecord.h"
Expand Down

0 comments on commit 839df96

Please sign in to comment.