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"
|
|
|
|
|
|
2019-12-16 17:29:53 -08:00
|
|
|
#import "IMCore_ClassDump.h"
|
2024-01-02 18:14:31 -08:00
|
|
|
#import "IMMessageItem+Encoded.h"
|
2018-11-13 12:29:15 -08:00
|
|
|
|
|
|
|
|
@implementation MBIMSendMessageOperation
|
|
|
|
|
|
|
|
|
|
+ (void)load { [super load]; }
|
|
|
|
|
|
|
|
|
|
+ (NSString *)endpointName
|
|
|
|
|
{
|
|
|
|
|
return @"sendMessage";
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-05 15:41:07 -08:00
|
|
|
- (IMMessage *)_sendMessage:(NSString *)messageBody toChatWithGUID:(NSString *)chatGUID attachmentGUIDs:(NSArray<NSString *> *)guids
|
2018-11-13 12:29:15 -08:00
|
|
|
{
|
2023-12-05 15:41:07 -08:00
|
|
|
__block IMMessage *result = nil;
|
2018-11-20 19:57:35 -07:00
|
|
|
|
2018-11-21 15:51:51 -07:00
|
|
|
dispatch_sync([[self class] sharedIMAccessQueue], ^{
|
2019-12-16 17:29:53 -08:00
|
|
|
IMChat *chat = [[IMChatRegistry sharedInstance] existingChatWithGUID:chatGUID];
|
2018-11-21 15:51:51 -07:00
|
|
|
|
|
|
|
|
// 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];
|
2019-01-16 14:17:31 -08:00
|
|
|
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];
|
|
|
|
|
}
|
2018-11-21 15:51:51 -07:00
|
|
|
|
|
|
|
|
if (!chat) {
|
2019-01-22 23:31:36 -08:00
|
|
|
MBIMLogInfo(@"Chat does not exist: %@", chatGUID);
|
2018-11-21 15:51:51 -07:00
|
|
|
} else {
|
2023-12-05 15:41:07 -08:00
|
|
|
result = reply;
|
|
|
|
|
|
2023-07-06 15:27:31 -07:00
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
|
|
|
[chat sendMessage:reply];
|
|
|
|
|
});
|
2018-11-21 15:51:51 -07:00
|
|
|
}
|
|
|
|
|
});
|
2018-11-13 12:29:15 -08:00
|
|
|
|
2018-11-21 15:51:51 -07:00
|
|
|
return result;
|
2018-11-13 12:29:15 -08:00
|
|
|
}
|
|
|
|
|
|
2024-10-02 17:18:44 -07:00
|
|
|
#if 0
|
|
|
|
|
- (NSDictionary *)adjustMessageSummaryInfoForSending:(NSDictionary *)messageSummaryInfo
|
|
|
|
|
{
|
|
|
|
|
NSMutableDictionary *adjustedInfo = [messageSummaryInfo mutableCopy];
|
|
|
|
|
if (!adjustedInfo) {
|
|
|
|
|
adjustedInfo = [NSMutableDictionary dictionary];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ([fullText length] > 50) {
|
|
|
|
|
summary = [[summary substringToIndex:[summary rangeOfComposedCharacterSequenceAtIndex:kMaxSummaryLength].location] stringByAppendingString:@"…"];
|
|
|
|
|
adjustedInfo[IMMessageSummaryInfoSummary] = summary;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
adjustedInfo[IMMessageSummaryInfoTapbackRepresentationKey] = @"Loved";
|
|
|
|
|
|
|
|
|
|
return adjustedInfo;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2018-11-13 12:29:15 -08:00
|
|
|
- (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
|
|
|
|
2024-10-02 17:18:44 -07:00
|
|
|
// tapbacks
|
|
|
|
|
#if 0
|
|
|
|
|
IMMessage *acknowledgment = [IMMessage instantMessageWithAssociatedMessageContent: /* [NSString stringWithFormat:@"%@ \"%%@\"", tapbackAction] */
|
|
|
|
|
flags:0
|
|
|
|
|
associatedMessageGUID:guid
|
|
|
|
|
associatedMessageType:IMAssociatedMessageTypeAcknowledgmentHeart
|
|
|
|
|
associatedMessageRange:[imMessage messagePartRange]
|
|
|
|
|
messageSummaryInfo:[self adjustMessageSummaryInfoForSending:message]
|
|
|
|
|
threadIdentifier:[imMessage threadIdentifier]];
|
|
|
|
|
#endif
|
|
|
|
|
|
2019-01-16 14:17:31 -08:00
|
|
|
NSArray *transferGUIDs = [args objectForKey:@"fileTransferGUIDs"];
|
|
|
|
|
if (!transferGUIDs) {
|
|
|
|
|
transferGUIDs = @[];
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-05 15:41:07 -08:00
|
|
|
IMMessage *result = [self _sendMessage:messageBody toChatWithGUID:guid attachmentGUIDs:transferGUIDs];
|
2018-11-13 12:29:15 -08:00
|
|
|
if (result) {
|
2024-01-02 18:14:31 -08:00
|
|
|
response = [MBIMJSONDataResponse responseWithJSONObject:[result mbim_dictionaryRepresentation]];
|
2018-11-13 12:29:15 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self.serverCompletionBlock(response);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@end
|