Organize everything into operations
This commit is contained in:
@@ -7,6 +7,7 @@
|
||||
//
|
||||
|
||||
#import "MBIMBridge.h"
|
||||
#import "MBIMBridgeOperation.h"
|
||||
|
||||
#import <GCDWebServers/GCDWebServers.h>
|
||||
|
||||
@@ -18,12 +19,9 @@
|
||||
|
||||
static NSString *const MBIMBridgeToken = @"net.buzzert.kordophone";
|
||||
|
||||
static NSString *const kAPIEndpointConversationList = @"conversations";
|
||||
static NSString *const kAPIEndpointConversationContents = @"messages";
|
||||
static NSString *const kAPIEndpointSendMessage = @"sendMessage";
|
||||
|
||||
@interface MBIMBridge (/* INTERNAL */)
|
||||
@property (nonatomic, strong) GCDWebServer *webServer;
|
||||
@property (nonatomic, strong) NSOperationQueue *operationQueue;
|
||||
|
||||
- (instancetype)_init;
|
||||
@end
|
||||
@@ -50,6 +48,8 @@ static NSString *const kAPIEndpointSendMessage = @"sendMessage";
|
||||
|
||||
[sDaemonController setDelegate:self];
|
||||
[sDaemonListener addHandler:self];
|
||||
|
||||
_operationQueue = [[NSOperationQueue alloc] init];
|
||||
}
|
||||
|
||||
return self;
|
||||
@@ -108,28 +108,21 @@ static NSString *const kAPIEndpointSendMessage = @"sendMessage";
|
||||
NSLog(@"chat items changed: %@", notification);
|
||||
}
|
||||
|
||||
- (BOOL)_sendMessage:(NSString *)messageBody toChatWithGUID:(NSString *)chatGUID
|
||||
{
|
||||
IMAccount *iMessageAccount = [[IMAccountController sharedInstance] bestAccountForService:[IMServiceImpl iMessageService]];
|
||||
IMHandle *handle = [[iMessageAccount arrayOfAllIMHandles] firstObject];
|
||||
|
||||
NSAttributedString *replyAttrString = [[NSAttributedString alloc] initWithString:messageBody];
|
||||
IMMessage *reply = [IMMessage fromMeIMHandle:handle withText:replyAttrString fileTransferGUIDs:@[] flags:kIMMessageFinished];
|
||||
|
||||
IMChat *chat = [sChatRegistry existingChatWithGUID:chatGUID];
|
||||
if (!chat) {
|
||||
NSLog(@"Chat does not exist: %@", chatGUID);
|
||||
return NO;
|
||||
}
|
||||
|
||||
[chat sendMessage:reply];
|
||||
|
||||
return YES;
|
||||
}
|
||||
|
||||
#pragma mark -
|
||||
#pragma mark Web Server initialization
|
||||
|
||||
- (void)_handleAsyncServerRequest:(GCDWebServerRequest *)request completion:(GCDWebServerCompletionBlock)completion
|
||||
{
|
||||
NSString *endpointName = [[request URL] lastPathComponent];
|
||||
Class operationClass = [MBIMBridgeOperation operationClassForEndpointName:endpointName];
|
||||
if (operationClass != nil) {
|
||||
MBIMBridgeOperation *operation = [[operationClass alloc] initWithRequest:request completion:completion];
|
||||
[[self operationQueue] addOperation:operation];
|
||||
} else {
|
||||
completion([GCDWebServerDataResponse responseWithStatusCode:404]);
|
||||
}
|
||||
}
|
||||
|
||||
- (void)startWebServer
|
||||
{
|
||||
[GCDWebServer setLogLevel:3];
|
||||
@@ -138,96 +131,15 @@ static NSString *const kAPIEndpointSendMessage = @"sendMessage";
|
||||
self.webServer = [[GCDWebServer alloc] init];
|
||||
[self.webServer addDefaultHandlerForMethod:@"GET"
|
||||
requestClass:[GCDWebServerRequest class]
|
||||
processBlock:^GCDWebServerResponse * _Nullable(__kindof GCDWebServerRequest * _Nonnull request) { return [weakSelf _handleWebServerRequest:request]; }];
|
||||
asyncProcessBlock:^(__kindof GCDWebServerRequest * _Nonnull request, GCDWebServerCompletionBlock _Nonnull completionBlock) { [weakSelf _handleAsyncServerRequest:request completion:completionBlock]; }];
|
||||
|
||||
[self.webServer addDefaultHandlerForMethod:@"POST"
|
||||
requestClass:[GCDWebServerURLEncodedFormRequest class]
|
||||
processBlock:^GCDWebServerResponse * _Nullable(__kindof GCDWebServerRequest * _Nonnull request) { return [weakSelf _handleWebServerPOST:(GCDWebServerURLEncodedFormRequest *)request]; }];
|
||||
asyncProcessBlock:^(__kindof GCDWebServerRequest * _Nonnull request, GCDWebServerCompletionBlock _Nonnull completionBlock) { [weakSelf _handleAsyncServerRequest:request completion:completionBlock]; }];
|
||||
|
||||
[self.webServer startWithPort:8080 bonjourName:nil];
|
||||
}
|
||||
|
||||
- (GCDWebServerResponse *)_handleWebServerPOST:(__kindof GCDWebServerURLEncodedFormRequest * _Nonnull)request
|
||||
{
|
||||
NSURLComponents *urlComponents = [NSURLComponents componentsWithURL:[request URL] resolvingAgainstBaseURL:NO];
|
||||
NSString *endpoint = [[urlComponents path] lastPathComponent];
|
||||
|
||||
if ([endpoint isEqualToString:kAPIEndpointSendMessage]) {
|
||||
NSDictionary *args = [request arguments];
|
||||
|
||||
NSString *guid = [args objectForKey:@"guid"];
|
||||
NSString *messageBody = [args objectForKey:@"body"];
|
||||
BOOL result = [self _sendMessage:messageBody toChatWithGUID:guid];
|
||||
if (result) {
|
||||
return [GCDWebServerDataResponse responseWithStatusCode:200];
|
||||
} else {
|
||||
return [GCDWebServerDataResponse responseWithStatusCode:500];
|
||||
}
|
||||
}
|
||||
|
||||
return [GCDWebServerDataResponse responseWithStatusCode:404];
|
||||
}
|
||||
|
||||
- (GCDWebServerResponse *)_handleWebServerRequest:(__kindof GCDWebServerRequest * _Nonnull)request
|
||||
{
|
||||
NSURLComponents *urlComponents = [NSURLComponents componentsWithURL:[request URL] resolvingAgainstBaseURL:NO];
|
||||
NSString *endpoint = [[urlComponents path] lastPathComponent];
|
||||
|
||||
if ([endpoint isEqualToString:kAPIEndpointConversationList]) {
|
||||
NSArray<IMChat *> *chats = [sChatRegistry allExistingChats];
|
||||
|
||||
NSMutableArray *conversations = [NSMutableArray array];
|
||||
for (IMChat *chat in chats) {
|
||||
NSMutableDictionary *chatDict = [NSMutableDictionary dictionary];
|
||||
chatDict[@"guid"] = [chat guid];
|
||||
chatDict[@"displayName"] = [chat displayName];
|
||||
chatDict[@"date"] = GCDWebServerFormatRFC822([chat lastFinishedMessageDate]);
|
||||
|
||||
[conversations addObject:chatDict];
|
||||
}
|
||||
|
||||
return [GCDWebServerDataResponse responseWithJSONObject:conversations];
|
||||
}
|
||||
|
||||
if ([endpoint isEqualToString:kAPIEndpointConversationContents]) {
|
||||
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");
|
||||
return [GCDWebServerDataResponse responseWithStatusCode:500];
|
||||
}
|
||||
|
||||
IMChat *chat = [sChatRegistry existingChatWithGUID:guid];
|
||||
if (!chat) {
|
||||
NSLog(@"Chat with guid: %@ not found", guid);
|
||||
return [GCDWebServerDataResponse responseWithStatusCode:500];
|
||||
}
|
||||
|
||||
// Load messages
|
||||
[chat loadMessagesBeforeDate:[NSDate date] limit:50 loadImmediately:YES];
|
||||
|
||||
NSMutableArray *messages = [NSMutableArray array];
|
||||
for (IMMessageItem *imMessage in [[chat chatItems] messages]) {
|
||||
NSMutableDictionary *messageDict = [NSMutableDictionary dictionary];
|
||||
messageDict[@"text"] = [[imMessage body] string];
|
||||
messageDict[@"date"] = GCDWebServerFormatRFC822([imMessage time]);
|
||||
messageDict[@"sender"] = [imMessage sender];
|
||||
[messages addObject:messageDict];
|
||||
}
|
||||
|
||||
return [GCDWebServerDataResponse responseWithJSONObject:messages];
|
||||
}
|
||||
|
||||
|
||||
return [GCDWebServerDataResponse responseWithStatusCode:404];
|
||||
}
|
||||
|
||||
#pragma mark -
|
||||
#pragma mark Daemon lifecycle
|
||||
|
||||
|
||||
Reference in New Issue
Block a user