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