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

69 lines
2.0 KiB
Mathematica
Raw Normal View History

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"
#import "MBIMHTTPUtilities.h"
2018-11-13 12:29:15 -08:00
#import <IMCore/IMCore.h>
@implementation MBIMMessagesListOperation
+ (void)load { [super load]; }
+ (NSString *)endpointName
{
return @"messages";
}
- (void)main
{
NSObject<HTTPResponse> *response = nil;
2018-11-13 12:29:15 -08:00
do {
NSURLComponents *urlComponents = [NSURLComponents componentsWithURL:self.requestURL resolvingAgainstBaseURL:NO];
2018-11-13 12:29:15 -08:00
NSString *guid = nil;
for (NSURLQueryItem *queryItem in [urlComponents queryItems]) {
if ([[queryItem name] isEqualToString:@"guid"]) {
guid = [queryItem value];
break;
}
}
if (!guid) {
NSLog(@"No query item provided");
response = [[HTTPErrorResponse alloc] initWithErrorCode:500];
2018-11-13 12:29:15 -08:00
break;
}
IMChat *chat = [sChatRegistry existingChatWithGUID:guid];
if (!chat) {
NSLog(@"Chat with guid: %@ not found", guid);
response = [[HTTPErrorResponse alloc] initWithErrorCode:500];
2018-11-13 12:29:15 -08:00
break;
}
// Load messages
[chat loadMessagesBeforeDate:[NSDate date] limit:50 loadImmediately:YES];
NSMutableArray *messages = [NSMutableArray array];
for (IMMessageItem *imMessage in [[chat chatItems] messages]) {
NSMutableDictionary *messageDict = [NSMutableDictionary dictionary];
messageDict[@"text"] = [[imMessage body] string];
messageDict[@"date"] = MBIMWebServerFormatRFC822([imMessage time]);
2018-11-13 12:29:15 -08:00
messageDict[@"sender"] = [imMessage sender];
[messages addObject:messageDict];
}
response = [MBIMJSONDataResponse responseWithJSONObject:messages];
2018-11-13 12:29:15 -08:00
} while (0);
self.serverCompletionBlock(response);
}
@end