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

@@ -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;
}