Private
Public Access
1
0

Updates to sending and message sequences

This commit is contained in:
James Magahern
2018-11-20 19:57:35 -07:00
parent ddec4be8d6
commit 4f5cd058c5
17 changed files with 264 additions and 45 deletions

View File

@@ -106,12 +106,12 @@ static NSString *const MBIMBridgeToken = @"net.buzzert.kordophone";
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_chatRegistryDidLoad:) name:IMChatRegistryDidLoadNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_chatItemsDidChange:) name:IMChatItemsDidChangeNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserverForName: IMChatRegistryUnreadCountChangedNotification
object: nil
queue: [NSOperationQueue mainQueue]
usingBlock:^(NSNotification *note) {
NSLog(@"Unread count changed: %d", (int)[[IMChatRegistry sharedInstance] unreadCount]);
}];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_unreadCountChanged:) name:IMChatRegistryUnreadCountChangedNotification object:nil];
}
- (void)_unreadCountChanged:(NSNotification *)notification
{
// Not a lot of useful information plumbed here...
}
- (void)_messageReceived:(NSNotification *)notification
@@ -124,7 +124,7 @@ static NSString *const MBIMBridgeToken = @"net.buzzert.kordophone";
if (![message isFromMe]) {
MBIMUpdateItem *updateItem = [[MBIMUpdateItem alloc] init];
updateItem.changedChat = chat;
updateItem.message = message;
updateItem.addedMessage = message;
[[MBIMUpdateQueue sharedInstance] enqueueUpdateItem:updateItem];
} else {
@@ -140,7 +140,14 @@ static NSString *const MBIMBridgeToken = @"net.buzzert.kordophone";
- (void)_chatItemsDidChange:(NSNotification *)notification
{
NSLog(@"chat items changed: %@", notification);
IMChat *chat = [notification object];
if (chat) {
NSLog(@"Chat items change for GUID: %@", [chat guid]);
MBIMUpdateItem *updateItem = [[MBIMUpdateItem alloc] init];
updateItem.changedChat = chat;
[[MBIMUpdateQueue sharedInstance] enqueueUpdateItem:updateItem];
}
}
#pragma mark -

View File

@@ -36,17 +36,26 @@
NSURL *url = [NSURL URLWithString:path];
NSString *endpointName = [url lastPathComponent];
BOOL requestTimedOut = NO;
Class operationClass = [MBIMBridgeOperation operationClassForEndpointName:endpointName];
if (operationClass != nil) {
_currentOperation = [[operationClass alloc] initWithRequestURL:url completion:completion];
_currentOperation.requestBodyData = _bodyData;
[[[MBIMBridge sharedInstance] operationQueue] addOperation:_currentOperation];
dispatch_semaphore_wait(sema, dispatch_time(DISPATCH_TIME_NOW, (int64_t)(60.0 * NSEC_PER_SEC)));
long status = dispatch_semaphore_wait(sema, dispatch_time(DISPATCH_TIME_NOW, (int64_t)(60.0 * NSEC_PER_SEC)));
if (status != 0) {
requestTimedOut = YES;
}
} else {
response = [[HTTPErrorResponse alloc] initWithErrorCode:404];
}
if (requestTimedOut) {
response = [_currentOperation cancelAndReturnTimeoutResponse];
}
return response;
}

View File

@@ -13,7 +13,9 @@ NS_ASSUME_NONNULL_BEGIN
@interface MBIMUpdateItem : NSObject
@property (nonatomic, strong) IMChat *changedChat;
@property (nonatomic, strong) IMMessage *message;
@property (nonatomic, strong) IMMessage *addedMessage;
- (NSDictionary *)dictionaryRepresentation;
@end
typedef void (^MBIMUpdateConsumer)(MBIMUpdateItem *item);
@@ -23,6 +25,8 @@ typedef void (^MBIMUpdateConsumer)(MBIMUpdateItem *item);
+ (instancetype)sharedInstance;
- (void)addConsumer:(MBIMUpdateConsumer)consumer;
- (void)removeConsumer:(MBIMUpdateConsumer)consumer;
- (void)enqueueUpdateItem:(MBIMUpdateItem *)item;
@end

View File

@@ -7,8 +7,15 @@
//
#import "MBIMUpdateQueue.h"
#import "IMMessageItem+Encoded.h"
#import "IMChat+Encoded.h"
@interface MBIMUpdateItem (/*INTERNAL*/)
@property (nonatomic, assign) NSUInteger messageSequenceNumber;
@end
@implementation MBIMUpdateQueue {
NSUInteger _messageSequenceNumber;
dispatch_queue_t _accessQueue;
NSMutableArray *_consumers;
}
@@ -30,6 +37,7 @@
if (self) {
_accessQueue = dispatch_queue_create("net.buzzert.MBIMUpdateQueue", DISPATCH_QUEUE_SERIAL);
_consumers = [[NSMutableArray alloc] init];
_messageSequenceNumber = 0;
}
return self;
@@ -43,8 +51,19 @@
});
}
- (void)removeConsumer:(MBIMUpdateConsumer)consumer
{
__weak NSMutableArray *consumers = _consumers;
dispatch_async(_accessQueue, ^{
[consumers removeObject:consumer];
});
}
- (void)enqueueUpdateItem:(MBIMUpdateItem *)item
{
_messageSequenceNumber++;
item.messageSequenceNumber = _messageSequenceNumber;
__weak NSMutableArray *consumers = _consumers;
dispatch_async(_accessQueue, ^{
for (MBIMUpdateConsumer consumer in consumers) {
@@ -58,4 +77,20 @@
@end
@implementation MBIMUpdateItem
- (NSDictionary *)dictionaryRepresentation
{
NSMutableDictionary *updateDict = [NSMutableDictionary dictionary];
updateDict[@"messageSequenceNumber"] = @(_messageSequenceNumber);
if ([self changedChat]) {
updateDict[@"conversation"] = [[self changedChat] mbim_dictionaryRepresentation];
}
if ([self addedMessage]) {
updateDict[@"message"] = [[self addedMessage] mbim_dictionaryRepresentation];
}
return updateDict;
}
@end

View File

@@ -26,6 +26,8 @@ typedef void (^MBIMBridgeOperationCompletionBlock)(NSObject<HTTPResponse> * _Nul
+ (nullable Class)operationClassForEndpointName:(NSString *)endpointName;
- (instancetype)initWithRequestURL:(NSURL *)requestURL completion:(MBIMBridgeOperationCompletionBlock)completionBlock;
- (NSObject<HTTPResponse> *)cancelAndReturnTimeoutResponse;
@end
NS_ASSUME_NONNULL_END

View File

@@ -55,4 +55,10 @@
return self;
}
- (NSObject<HTTPResponse> *)cancelAndReturnTimeoutResponse
{
[self cancel];
return [[HTTPErrorResponse alloc] initWithErrorCode:500];
}
@end

View File

@@ -8,6 +8,7 @@
#import "MBIMConversationListOperation.h"
#import "MBIMHTTPUtilities.h"
#import "IMChat+Encoded.h"
#import <IMCore/IMCore.h>
@@ -26,26 +27,7 @@
NSMutableArray *conversations = [NSMutableArray array];
for (IMChat *chat in chats) {
NSMutableDictionary *chatDict = [NSMutableDictionary dictionary];
chatDict[@"guid"] = [chat guid];
chatDict[@"displayName"] = [chat displayName];
chatDict[@"date"] = MBIMWebServerFormatRFC822([chat lastFinishedMessageDate]);
IMMessage *lastMessage = [chat lastMessage];
if (lastMessage) {
chatDict[@"lastMessagePreview"] = [[lastMessage text] string];
}
NSMutableArray *participantStrings = [NSMutableArray array];
for (IMHandle *participantHandle in chat.participants) {
NSString *participantString = [participantHandle displayNameForChat:chat];
if (participantString) {
[participantStrings addObject:participantString];
}
}
chatDict[@"participantDisplayNames"] = participantStrings;
NSDictionary *chatDict = [chat mbim_dictionaryRepresentation];
[conversations addObject:chatDict];
}

View File

@@ -0,0 +1,17 @@
//
// MBIMMarkOperation.h
// kordophoned
//
// Created by James Magahern on 11/19/18.
// Copyright © 2018 James Magahern. All rights reserved.
//
#import "MBIMBridgeOperation.h"
NS_ASSUME_NONNULL_BEGIN
@interface MBIMMarkOperation : MBIMBridgeOperation
@end
NS_ASSUME_NONNULL_END

View File

@@ -0,0 +1,60 @@
//
// MBIMMarkOperation.m
// kordophoned
//
// Created by James Magahern on 11/19/18.
// Copyright © 2018 James Magahern. All rights reserved.
//
#import "MBIMMarkOperation.h"
#import <IMCore/IMCore.h>
@implementation MBIMMarkOperation
+ (void)load { [super load]; }
+ (NSString *)endpointName
{
return @"markConversation";
}
- (void)main
{
NSObject<HTTPResponse> *response = nil;
do {
NSURLComponents *urlComponents = [NSURLComponents componentsWithURL:self.requestURL resolvingAgainstBaseURL:NO];
NSString *guid = nil;
for (NSURLQueryItem *queryItem in [urlComponents queryItems]) {
if ([[queryItem name] isEqualToString:@"guid"]) {
guid = [queryItem value];
break;
}
}
if (!guid) {
NSLog(@"No query item provided");
response = [[HTTPErrorResponse alloc] initWithErrorCode:500];
break;
}
IMChat *chat = [sChatRegistry existingChatWithGUID:guid];
if (!chat) {
NSLog(@"Chat with guid: %@ not found", guid);
response = [[HTTPErrorResponse alloc] initWithErrorCode:500];
break;
}
// TODO: be smarter about this and mark individual messages as read? Could lead
// to a race condition
if ([chat unreadMessageCount] > 0) {
[chat markAllMessagesAsRead];
}
response = [[HTTPErrorResponse alloc] initWithErrorCode:200];
} while (0);
self.serverCompletionBlock(response);
}
@end

View File

@@ -22,13 +22,15 @@
- (BOOL)_sendMessage:(NSString *)messageBody toChatWithGUID:(NSString *)chatGUID
{
IMChat *chat = [sChatRegistry existingChatWithGUID:chatGUID];
// TODO: chat might not be an iMessage chat!
IMAccount *iMessageAccount = [[IMAccountController sharedInstance] bestAccountForService:[IMServiceImpl iMessageService]];
IMHandle *handle = [[iMessageAccount arrayOfAllIMHandles] firstObject];
IMHandle *senderHandle = [iMessageAccount loginIMHandle];
NSAttributedString *replyAttrString = [[NSAttributedString alloc] initWithString:messageBody];
IMMessage *reply = [IMMessage fromMeIMHandle:handle withText:replyAttrString fileTransferGUIDs:@[] flags:kIMMessageFinished];
IMMessage *reply = [IMMessage fromMeIMHandle:senderHandle withText:replyAttrString fileTransferGUIDs:@[] flags:kIMMessageFinished];
IMChat *chat = [sChatRegistry existingChatWithGUID:chatGUID];
if (!chat) {
NSLog(@"Chat does not exist: %@", chatGUID);
return NO;

View File

@@ -8,9 +8,10 @@
#import "MBIMUpdatePollOperation.h"
#import "MBIMUpdateQueue.h"
#import "IMMessageItem+Encoded.h"
@implementation MBIMUpdatePollOperation
@implementation MBIMUpdatePollOperation {
__strong MBIMUpdateConsumer _updateConsumer;
}
+ (void)load { [super load]; }
@@ -21,19 +22,27 @@
- (void)main
{
MBIMUpdateConsumer consumer = ^(MBIMUpdateItem *nextUpdateItem) {
NSDictionary *updateDict = @{
@"guid" : [[nextUpdateItem changedChat] guid],
@"message" : [[nextUpdateItem message] mbim_dictionaryRepresentation]
};
__weak __auto_type weakSelf = self;
_updateConsumer = ^(MBIMUpdateItem *nextUpdateItem) {
NSDictionary *updateDict = [nextUpdateItem dictionaryRepresentation];
MBIMJSONDataResponse *response = [MBIMJSONDataResponse responseWithJSONObject:updateDict];
self.serverCompletionBlock(response);
weakSelf.serverCompletionBlock(response);
};
[[MBIMUpdateQueue sharedInstance] addConsumer:consumer];
[[MBIMUpdateQueue sharedInstance] addConsumer:_updateConsumer];
}
// TODO: cancel needs to remove the consumer from the update queue
- (void)cancel
{
[super cancel];
[[MBIMUpdateQueue sharedInstance] removeConsumer:_updateConsumer];
}
- (NSObject<HTTPResponse> *)cancelAndReturnTimeoutResponse
{
[self cancel];
return [[HTTPErrorResponse alloc] initWithErrorCode:205]; // 205 = nothing to report
}
@end

View File

@@ -9,3 +9,4 @@
#import <Foundation/Foundation.h>
NSString* MBIMWebServerFormatRFC822(NSDate *date);
NSString* MBIMWebServerFormatISO8601(NSDate *date);

View File

@@ -9,6 +9,7 @@
#include "MBIMHTTPUtilities.h"
static NSDateFormatter* _dateFormatterRFC822 = nil;
static NSDateFormatter* _dateFormatterISO8601 = nil;
static dispatch_queue_t _dateFormatterQueue = NULL;
__attribute__((constructor))
@@ -20,6 +21,11 @@ static void __InitializeDateFormatter()
_dateFormatterRFC822.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
_dateFormatterRFC822.dateFormat = @"EEE',' dd MMM yyyy HH':'mm':'ss 'GMT'";
_dateFormatterRFC822.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
_dateFormatterISO8601 = [[NSDateFormatter alloc] init];
_dateFormatterISO8601.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
_dateFormatterISO8601.dateFormat = @"yyyy-MM-dd'T'HH:mm:ss'+00:00'";
_dateFormatterISO8601.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
}
NSString* MBIMWebServerFormatRFC822(NSDate *date)
@@ -31,3 +37,13 @@ NSString* MBIMWebServerFormatRFC822(NSDate *date)
return string;
}
NSString* MBIMWebServerFormatISO8601(NSDate *date)
{
__block NSString *string = nil;
dispatch_sync(_dateFormatterQueue, ^{
string = [_dateFormatterISO8601 stringFromDate:date];
});
return string;
}