Private
Public Access
1
0

MessagesList: Add support for beforeMessageGUID and beforeDate

This commit is contained in:
James Magahern
2022-08-03 16:52:39 -07:00
parent ebad248c1c
commit c7087a394e
8 changed files with 168 additions and 8 deletions

View File

@@ -9,9 +9,12 @@
#import "MBIMMessagesListOperation.h"
#import "MBIMHTTPUtilities.h"
#import "IMMessageItem+Encoded.h"
#import "MBIMErrorResponse.h"
#import "IMCore_ClassDump.h"
#define kDefaultMessagesLimit 75
@implementation MBIMMessagesListOperation
+ (void)load { [super load]; }
@@ -25,11 +28,26 @@
{
__block NSObject<HTTPResponse> *response = nil;
do {
// Required parameters
NSString *guid = [self valueForQueryItemWithName:@"guid"];
// Optional
NSString *limitValue = [self valueForQueryItemWithName:@"limit"];
NSDate *beforeDate = nil;
NSString *beforeDateValue = [self valueForQueryItemWithName:@"beforeDate"];
if (beforeDateValue) {
beforeDate = [beforeDateValue ISO8601Date];
if (!beforeDate) {
response = [[MBIMErrorResponse alloc] initWithErrorCode:500 message:@"Unable to decode ISO8601 beforeDate value"];
break;
}
}
NSString *beforeMessageGUID = [self valueForQueryItemWithName:@"beforeMessageGUID"];
if (!guid) {
MBIMLogInfo(@"No query item provided");
response = [[HTTPErrorResponse alloc] initWithErrorCode:500];
response = [[MBIMErrorResponse alloc] initWithErrorCode:500 message:@"No GUID provided."];
break;
}
@@ -41,12 +59,33 @@
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];
}];
// (Must be done on main queue for some reason)
dispatch_sync(dispatch_get_main_queue(), ^{
NSUInteger limit = kDefaultMessagesLimit;
if (limitValue) {
limit = [limitValue integerValue];
}
if (beforeMessageGUID) {
if ([chat respondsToSelector:@selector(loadMessagesBeforeAndAfterGUID:numberOfMessagesToLoadBeforeGUID:numberOfMessagesToLoadAfterGUID:loadImmediately:threadIdentifier:)]) {
[chat loadMessagesBeforeAndAfterGUID:beforeMessageGUID numberOfMessagesToLoadBeforeGUID:limit numberOfMessagesToLoadAfterGUID:0 loadImmediately:YES threadIdentifier:nil];
} else {
[chat loadMessagesBeforeAndAfterGUID:beforeMessageGUID numberOfMessagesToLoadBeforeGUID:limit numberOfMessagesToLoadAfterGUID:0 loadImmediately:YES];
}
} else {
[chat loadMessagesBeforeDate:beforeDate limit:limit loadImmediately:YES];
}
[[chat chatItems] enumerateMessagesWithOptions:0 usingBlock:^(IMMessage *message, BOOL *stop) {
if ([[message guid] isEqual:beforeMessageGUID]) {
*stop = YES;
return;
}
NSDictionary *messageDict = [message mbim_dictionaryRepresentation];
[messages addObject:messageDict];
}];
});
}
});

View File

@@ -0,0 +1,18 @@
//
// MBIMErrorResponse.h
// kordophoned
//
// Created by James Magahern on 8/3/22.
// Copyright © 2022 James Magahern. All rights reserved.
//
#import "HTTPDataResponse.h"
NS_ASSUME_NONNULL_BEGIN
@interface MBIMErrorResponse : HTTPDataResponse
- (instancetype)initWithErrorCode:(int)httpErrorCode;
- (instancetype)initWithErrorCode:(int)httpErrorCode message:(NSString *)message;
@end
NS_ASSUME_NONNULL_END

View File

@@ -0,0 +1,38 @@
//
// MBIMErrorResponse.m
// kordophoned
//
// Created by James Magahern on 8/3/22.
// Copyright © 2022 James Magahern. All rights reserved.
//
#import "MBIMErrorResponse.h"
@implementation MBIMErrorResponse {
int _status;
}
- (instancetype)initWithErrorCode:(int)httpErrorCode
{
if (self = [super initWithData:nil]) {
_status = httpErrorCode;
}
return self;
}
- (instancetype)initWithErrorCode:(int)httpErrorCode message:(NSString *)message
{
if (self = [super initWithData:[message dataUsingEncoding:NSUTF8StringEncoding]]) {
_status = httpErrorCode;
}
return self;
}
- (NSInteger)status
{
return _status;
}
@end

View File

@@ -10,3 +10,13 @@
NSString* MBIMWebServerFormatRFC822(NSDate *date);
NSString* MBIMWebServerFormatISO8601(NSDate *date);
@interface NSDate (MBIMWebServerFormat)
- (NSString *)RFC822StringValue;
- (NSString *)ISO8601StringValue;
@end
@interface NSString (MBIMWebServerFormat)
- (NSDate *)RFC822Date;
- (NSDate *)ISO8601Date;
@end

View File

@@ -47,3 +47,31 @@ NSString* MBIMWebServerFormatISO8601(NSDate *date)
return string;
}
@implementation NSDate (MBIMWebServerFormat)
- (NSString *)RFC822StringValue
{
return MBIMWebServerFormatRFC822(self);
}
- (NSString *)ISO8601StringValue
{
return MBIMWebServerFormatISO8601(self);
}
@end
@implementation NSString (MBIMWebServerFormat)
- (NSDate *)RFC822Date
{
return [_dateFormatterRFC822 dateFromString:self];
}
- (NSDate *)ISO8601Date
{
return [_dateFormatterISO8601 dateFromString:self];
}
@end