// // MBIMMessagesListOperation.m // kordophoned // // Created by James Magahern on 11/13/18. // Copyright © 2018 James Magahern. All rights reserved. // #import "MBIMMessagesListOperation.h" #import "MBIMHTTPUtilities.h" #import "IMMessageItem+Encoded.h" #import "IMCore_ClassDump.h" @implementation MBIMMessagesListOperation + (void)load { [super load]; } + (NSString *)endpointName { return @"messages"; } - (void)main { __block NSObject *response = nil; do { NSString *guid = [self valueForQueryItemWithName:@"guid"]; if (!guid) { MBIMLogInfo(@"No query item provided"); response = [[HTTPErrorResponse alloc] initWithErrorCode:500]; break; } __block NSMutableArray *messages = [NSMutableArray array]; dispatch_sync([[self class] sharedIMAccessQueue], ^{ IMChat *chat = [[IMChatRegistry sharedInstance] existingChatWithGUID:guid]; if (!chat) { MBIMLogInfo(@"Chat with guid: %@ not found", guid); response = [[HTTPErrorResponse alloc] initWithErrorCode:500]; } else { // Load messages [chat loadMessagesBeforeDate:[NSDate date] limit:50 loadImmediately:YES]; [[chat chatItems] enumerateMessagesWithOptions:0 usingBlock:^(IMMessage *message, BOOL *stop) { NSDictionary *messageDict = [message mbim_dictionaryRepresentation]; [messages addObject:messageDict]; }]; } }); response = [MBIMJSONDataResponse responseWithJSONObject:messages]; } while (0); self.serverCompletionBlock(response); } @end