Hooked up message send success and failure to LokiAPI.

Converted objective c functions from callbacks to promise in LokiAPI
pull/12/head
Mikunj Varsani 6 years ago
parent 3c7b769d20
commit a743698386

@ -718,10 +718,15 @@ static NSTimeInterval launchStartedAt;
[Environment.shared.contactsManager fetchSystemContactsOnceIfAlreadyAuthorized];
[[AppEnvironment.shared.messageFetcherJob run] retainUntilComplete];
[LokiAPI getMessages:^(id response, NSError *error) {
// TODO: Use the response
}];
[[LokiAPI getMessagesObjc]
.then(^(id result) {
// TODO: handle result
})
.catch(^(NSError *error) {
}) retainUntilComplete];
// TODO: Ping friends to let them know we're online
if (![UIApplication sharedApplication].isRegisteredForRemoteNotifications) {
@ -1156,14 +1161,14 @@ static NSTimeInterval launchStartedAt;
{
OWSLogInfo(@"performing background fetch");
[AppReadiness runNowOrWhenAppDidBecomeReady:^{
[LokiAPI getMessages:^(id response, NSError *error) {
if (response != nil) {
// TODO: Use the response
completionHandler(UIBackgroundFetchResultNewData);
} else {
completionHandler(UIBackgroundFetchResultFailed);
}
}];
[[LokiAPI getMessagesObjc]
.then(^(id result) {
completionHandler(UIBackgroundFetchResultNewData);
})
.catch(^(NSError *error) {
completionHandler(UIBackgroundFetchResultFailed);
}) retainUntilComplete];
// Loki: Original code
// ========
// __block AnyPromise *job = [AppEnvironment.shared.messageFetcherJob run].then(^{

@ -66,11 +66,26 @@ import PromiseKit
}
// MARK: Obj-C API
@objc public static func getMessages(_ completionHandler: @escaping (RawResponse?, NSError?) -> Void) {
getMessages().done { completionHandler($0, nil) }.catch { completionHandler(nil, $0 as NSError) }
@objc public static func getMessagesObjc() -> AnyPromise {
let promise = getMessages()
let anyPromise = AnyPromise(promise)
anyPromise.retainUntilComplete()
return anyPromise
}
@objc public static func sendSignalMessage(_ signalMessage: SignalMessage, to destination: String, requiringPoW isPoWRequired: Bool, completionHandler: @escaping (RawResponse?, NSError?) -> Void) {
LokiMessage.fromSignalMessage(signalMessage, requiringPoW: isPoWRequired).then(sendMessage).done { completionHandler($0, nil) }.catch { completionHandler(nil, $0 as NSError) }
@objc public static func sendSignalMessage(_ signalMessage: SignalMessage, to destination: String, requiringPoW isPoWRequired: Bool) -> AnyPromise {
let promise = LokiMessage.fromSignalMessage(signalMessage, requiringPoW: isPoWRequired)
.then(sendMessage)
.recover(on: DispatchQueue.global()) { error -> Promise<RawResponse> in
switch error {
case NetworkManagerError.taskError(_, let underlyingError):
throw underlyingError
default:
throw error
}
}
let anyPromise = AnyPromise(promise)
anyPromise.retainUntilComplete()
return anyPromise
}
}

@ -1111,9 +1111,37 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException";
// Convert the message to a Loki message and send it using the Loki messaging API
NSDictionary *signalMessage = deviceMessages.firstObject;
BOOL isPoWRequired = YES; // TODO: Base on message type
[LokiAPI sendSignalMessage:signalMessage to:recipient.recipientId requiringPoW:isPoWRequired completionHandler:^(id response, NSError *error) {
// TODO: Use the response
}];
[[LokiAPI sendSignalMessage:signalMessage to:recipient.recipientId requiringPoW:isPoWRequired]
.thenOn([OWSDispatch sendingQueue], ^(id result) {
[self messageSendDidSucceed:messageSend
deviceMessages:deviceMessages
wasSentByUD:false
wasSentByWebsocket:false];
})
.catchOn([OWSDispatch sendingQueue], ^(NSError *error) {
NSUInteger statusCode = 0;
NSData *_Nullable responseData = nil;
if ([error.domain isEqualToString:TSNetworkManagerErrorDomain]) {
statusCode = error.code;
NSError *_Nullable underlyingError = error.userInfo[NSUnderlyingErrorKey];
if (underlyingError) {
responseData
= underlyingError.userInfo[AFNetworkingOperationFailingURLResponseDataErrorKey];
} else {
OWSFailDebug(@"Missing underlying error: %@", error);
}
} else {
OWSFailDebug(@"Unexpected error: %@", error);
}
[self messageSendDidFail:messageSend
deviceMessages:deviceMessages
statusCode:statusCode
error:error
responseData:responseData];
}) retainUntilComplete];
// Loki: Original code
/*

Loading…
Cancel
Save