Private
Public Access
1
0
Files
Kordophone/kordophone/Bridge/Operations/MBIMSendMessageOperation.m

75 lines
2.1 KiB
Objective-C

//
// 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
{
__block BOOL result = YES;
dispatch_sync([[self class] sharedIMAccessQueue], ^{
IMChat *chat = [sChatRegistry existingChatWithGUID:chatGUID];
// TODO: chat might not be an iMessage chat!
IMAccount *iMessageAccount = [[IMAccountController sharedInstance] bestAccountForService:[IMServiceImpl iMessageService]];
IMHandle *senderHandle = [iMessageAccount loginIMHandle];
NSAttributedString *replyAttrString = [[NSAttributedString alloc] initWithString:messageBody];
IMMessage *reply = [IMMessage fromMeIMHandle:senderHandle withText:replyAttrString fileTransferGUIDs:@[] flags:kIMMessageFinished];
if (!chat) {
NSLog(@"Chat does not exist: %@", chatGUID);
result = NO;
} else {
[chat sendMessage:reply];
}
});
return result;
}
- (void)main
{
NSObject<HTTPResponse> *response = [[HTTPErrorResponse alloc] initWithErrorCode:500];
NSError *error = nil;
NSDictionary *args = [NSJSONSerialization JSONObjectWithData:self.requestBodyData options:0 error:&error];
if (error || args.count == 0) {
self.serverCompletionBlock(response);
return;
}
NSString *guid = [args objectForKey:@"guid"];
NSString *messageBody = [args objectForKey:@"body"];
if (!guid || !messageBody) {
self.serverCompletionBlock(response);
return;
}
BOOL result = [self _sendMessage:messageBody toChatWithGUID:guid];
if (result) {
response = [[HTTPErrorResponse alloc] initWithErrorCode:200];
}
self.serverCompletionBlock(response);
}
@end