SendMessage: allows creation of new conversations in addition to replying to guids
This commit is contained in:
@@ -19,40 +19,95 @@
|
||||
return @"sendMessage";
|
||||
}
|
||||
|
||||
- (BOOL)_sendMessage:(NSString *)messageBody toHandles:(NSArray<NSString *> *)handleIDs attachmentGUIDs:(NSArray<NSString *> *)guids
|
||||
{
|
||||
__block BOOL result = YES;
|
||||
dispatch_sync([[self class] sharedIMAccessQueue], ^{
|
||||
IMAccount *iMessageAccount = [[IMAccountController sharedInstance] bestAccountForService:[IMServiceImpl iMessageService]];
|
||||
|
||||
NSMutableArray<IMHandle *> *handles = [NSMutableArray array];
|
||||
for (NSString *handleID in handleIDs) {
|
||||
IMHandle *handle = [iMessageAccount imHandleWithID:handleID];
|
||||
if (handle) {
|
||||
[handles addObject:handle];
|
||||
} else {
|
||||
MBIMLogError(@"MBIMSendMessageOperation: Invalid handle ID: %@", handleID);
|
||||
}
|
||||
}
|
||||
|
||||
IMChat *chat = nil;
|
||||
if (handles.count == 1) {
|
||||
// Single recipient
|
||||
IMHandle *handle = [handles firstObject];
|
||||
chat = [[IMChatRegistry sharedInstance] existingChatForIMHandle:handle];
|
||||
if (chat == nil) {
|
||||
MBIMLogInfo(@"MBIMSendMessageOperation: Creating chat with handle: %@", handle);
|
||||
chat = [[IMChatRegistry sharedInstance] chatForIMHandle:handle];
|
||||
}
|
||||
} else if (handles.count > 1) {
|
||||
// Group chat
|
||||
chat = [[IMChatRegistry sharedInstance] existingChatForIMHandles:handles allowRetargeting:NO groupID:nil displayName:nil joinedChatsOnly:YES];
|
||||
if (chat == nil) {
|
||||
MBIMLogInfo(@"MBIMSendMessageOperation: Creating group chat with handles: %@", handles);
|
||||
chat = [[IMChatRegistry sharedInstance] chatForIMHandles:handles];
|
||||
}
|
||||
} else {
|
||||
// No handles?
|
||||
result = NO;
|
||||
}
|
||||
|
||||
if (chat) {
|
||||
result = [self _sendMessage:messageBody toChat:chat attachmentGUIDs:guids];
|
||||
} else {
|
||||
result = NO;
|
||||
}
|
||||
});
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
- (BOOL)_sendMessage:(NSString *)messageBody toChatWithGUID:(NSString *)chatGUID attachmentGUIDs:(NSArray<NSString *> *)guids
|
||||
{
|
||||
__block BOOL result = YES;
|
||||
|
||||
dispatch_sync([[self class] sharedIMAccessQueue], ^{
|
||||
IMChat *chat = [[IMChatRegistry sharedInstance] 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];
|
||||
NSAttributedString *attrStringWithFileTransfers = IMCreateSuperFormatStringWithAppendedFileTransfers(replyAttrString, guids);
|
||||
|
||||
IMMessage *reply = [IMMessage fromMeIMHandle:senderHandle
|
||||
withText:attrStringWithFileTransfers
|
||||
fileTransferGUIDs:guids
|
||||
flags:(kIMMessageFinished | kIMMessageIsFromMe)];
|
||||
|
||||
for (NSString *guid in [reply fileTransferGUIDs]) {
|
||||
[[IMFileTransferCenter sharedInstance] assignTransfer:guid toHandle:chat.recipient];
|
||||
}
|
||||
|
||||
if (!chat) {
|
||||
MBIMLogInfo(@"Chat does not exist: %@", chatGUID);
|
||||
result = NO;
|
||||
} else {
|
||||
[chat sendMessage:reply];
|
||||
result = [self _sendMessage:messageBody toChat:chat attachmentGUIDs:guids];
|
||||
}
|
||||
});
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
- (BOOL)_sendMessage:(NSString *)messageBody toChat:(IMChat *)chat attachmentGUIDs:(NSArray<NSString *> *)guids
|
||||
{
|
||||
dispatch_assert_queue([[self class] sharedIMAccessQueue]);
|
||||
|
||||
BOOL result = YES;
|
||||
|
||||
// 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];
|
||||
NSAttributedString *attrStringWithFileTransfers = IMCreateSuperFormatStringWithAppendedFileTransfers(replyAttrString, guids);
|
||||
|
||||
IMMessage *reply = [IMMessage fromMeIMHandle:senderHandle
|
||||
withText:attrStringWithFileTransfers
|
||||
fileTransferGUIDs:guids
|
||||
flags:(kIMMessageFinished | kIMMessageIsFromMe)];
|
||||
|
||||
for (NSString *guid in [reply fileTransferGUIDs]) {
|
||||
[[IMFileTransferCenter sharedInstance] assignTransfer:guid toHandle:chat.recipient];
|
||||
}
|
||||
|
||||
[chat sendMessage:reply];
|
||||
return result;
|
||||
}
|
||||
|
||||
- (void)main
|
||||
{
|
||||
NSObject<HTTPResponse> *response = [[HTTPErrorResponse alloc] initWithErrorCode:500];
|
||||
@@ -64,9 +119,8 @@
|
||||
return;
|
||||
}
|
||||
|
||||
NSString *guid = [args objectForKey:@"guid"];
|
||||
NSString *messageBody = [args objectForKey:@"body"];
|
||||
if (!guid || !messageBody) {
|
||||
if (!messageBody) {
|
||||
self.serverCompletionBlock(response);
|
||||
return;
|
||||
}
|
||||
@@ -76,7 +130,15 @@
|
||||
transferGUIDs = @[];
|
||||
}
|
||||
|
||||
BOOL result = [self _sendMessage:messageBody toChatWithGUID:guid attachmentGUIDs:transferGUIDs];
|
||||
BOOL result = NO;
|
||||
NSString *guid = [args objectForKey:@"guid"];
|
||||
NSArray<NSString *> *handles = [args objectForKey:@"handles"];
|
||||
if (guid) {
|
||||
result = [self _sendMessage:messageBody toChatWithGUID:guid attachmentGUIDs:transferGUIDs];
|
||||
} else if ([handles count] > 0) {
|
||||
result = [self _sendMessage:messageBody toHandles:handles attachmentGUIDs:transferGUIDs];
|
||||
}
|
||||
|
||||
if (result) {
|
||||
response = [[HTTPErrorResponse alloc] initWithErrorCode:200];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user