Organize everything into operations
This commit is contained in:
63
kordophone/Bridge/Operations/MBIMSendMessageOperation.m
Normal file
63
kordophone/Bridge/Operations/MBIMSendMessageOperation.m
Normal file
@@ -0,0 +1,63 @@
|
||||
//
|
||||
// MBIMSendMessageOperation.m
|
||||
// kordophoned
|
||||
//
|
||||
// Created by James Magahern on 11/13/18.
|
||||
// Copyright © 2018 James Magahern. All rights reserved.
|
||||
//
|
||||
|
||||
#import "MBIMSendMessageOperation.h"
|
||||
|
||||
#import <IMCore/IMCore.h>
|
||||
#import <IMCore/IMCore_Private.h>
|
||||
|
||||
@implementation MBIMSendMessageOperation
|
||||
|
||||
+ (void)load { [super load]; }
|
||||
|
||||
+ (NSString *)endpointName
|
||||
{
|
||||
return @"sendMessage";
|
||||
}
|
||||
|
||||
- (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;
|
||||
}
|
||||
|
||||
- (void)main
|
||||
{
|
||||
assert([self.request isKindOfClass:[GCDWebServerURLEncodedFormRequest class]]);
|
||||
|
||||
GCDWebServerURLEncodedFormRequest *formRequest = (GCDWebServerURLEncodedFormRequest *)self.request;
|
||||
NSDictionary *args = [formRequest arguments];
|
||||
|
||||
NSString *guid = [args objectForKey:@"guid"];
|
||||
NSString *messageBody = [args objectForKey:@"body"];
|
||||
BOOL result = [self _sendMessage:messageBody toChatWithGUID:guid];
|
||||
|
||||
GCDWebServerResponse *response = nil;
|
||||
if (result) {
|
||||
response = [GCDWebServerDataResponse responseWithStatusCode:200];
|
||||
} else {
|
||||
response = [GCDWebServerDataResponse responseWithStatusCode:500];
|
||||
}
|
||||
|
||||
self.serverCompletionBlock(response);
|
||||
}
|
||||
|
||||
@end
|
||||
Reference in New Issue
Block a user