Private
Public Access
1
0
Files
Kordophone/kordophone/Bridge/Operations/MBIMMessagesListOperation.m

60 lines
1.7 KiB
Objective-C

//
// 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/IMCore.h>
@implementation MBIMMessagesListOperation
+ (void)load { [super load]; }
+ (NSString *)endpointName
{
return @"messages";
}
- (void)main
{
__block NSObject<HTTPResponse> *response = nil;
do {
NSString *guid = [self valueForQueryItemWithName:@"guid"];
if (!guid) {
NSLog(@"No query item provided");
response = [[HTTPErrorResponse alloc] initWithErrorCode:500];
break;
}
__block NSMutableArray *messages = [NSMutableArray array];
dispatch_sync([[self class] sharedIMAccessQueue], ^{
IMChat *chat = [sChatRegistry existingChatWithGUID:guid];
if (!chat) {
NSLog(@"Chat with guid: %@ not found", guid);
response = [[HTTPErrorResponse alloc] initWithErrorCode:500];
} else {
// Load messages
[chat loadMessagesBeforeDate:[NSDate date] limit:50 loadImmediately:YES];
for (IMMessageItem *imMessage in [[chat chatItems] messages]) {
NSDictionary *messageDict = [imMessage mbim_dictionaryRepresentation];
[messages addObject:messageDict];
}
}
});
response = [MBIMJSONDataResponse responseWithJSONObject:messages];
} while (0);
self.serverCompletionBlock(response);
}
@end