2018-11-13 12:29:15 -08:00
|
|
|
//
|
|
|
|
|
// 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
|
|
|
|
|
{
|
2018-11-16 01:30:38 -08:00
|
|
|
NSObject<HTTPResponse> *response = [[HTTPErrorResponse alloc] initWithErrorCode:500];
|
2018-11-13 12:29:15 -08:00
|
|
|
|
2018-11-16 01:30:38 -08:00
|
|
|
NSError *error = nil;
|
|
|
|
|
NSDictionary *args = [NSJSONSerialization JSONObjectWithData:self.requestBodyData options:0 error:&error];
|
|
|
|
|
if (error || args.count == 0) {
|
|
|
|
|
self.serverCompletionBlock(response);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2018-11-13 12:29:15 -08:00
|
|
|
|
|
|
|
|
NSString *guid = [args objectForKey:@"guid"];
|
|
|
|
|
NSString *messageBody = [args objectForKey:@"body"];
|
2018-11-16 01:30:38 -08:00
|
|
|
if (!guid || !messageBody) {
|
|
|
|
|
self.serverCompletionBlock(response);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2018-11-13 12:29:15 -08:00
|
|
|
|
2018-11-16 01:30:38 -08:00
|
|
|
BOOL result = [self _sendMessage:messageBody toChatWithGUID:guid];
|
2018-11-13 12:29:15 -08:00
|
|
|
if (result) {
|
2018-11-16 01:30:38 -08:00
|
|
|
response = [[HTTPErrorResponse alloc] initWithErrorCode:200];
|
2018-11-13 12:29:15 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self.serverCompletionBlock(response);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@end
|