Send message to server if the recipient is public channel

pull/36/head
Mikunj 6 years ago
parent c8a97f6668
commit d72e7da7ca

@ -12,8 +12,8 @@ public final class LokiGroupChatAPI : NSObject {
internal static var userHexEncodedPublicKey: String { return OWSIdentityManager.shared().identityKeyPair()!.hexEncodedPublicKey }
@objc public static let serverURL = "https://chat.lokinet.org"
public static let publicChatMessageType = "network.loki.messenger.publicChat"
public static let publicChatID = 1
@objc public static let publicChatMessageType = "network.loki.messenger.publicChat"
@objc public static let publicChatID = 1
private static let batchCount = 8

@ -1,7 +1,8 @@
import PromiseKit
@objc public final class LokiGroupMessage: NSObject {
let serverID: UInt
@objc(LKGroupMessage)
public final class LokiGroupMessage: NSObject {
let serverID: UInt?
let hexEncodedPublicKey: String
let displayName: String
let body: String
@ -10,13 +11,18 @@ import PromiseKit
/// - Note: Expressed as milliseconds since 00:00:00 UTC on 1 January 1970.
let timestamp: UInt64
@objc public init(serverID: UInt, hexEncodedPublicKey: String, displayName: String, body: String, type: String, timestamp: UInt64) {
public init(serverID: UInt?, hexEncodedPublicKey: String, displayName: String, body: String, type: String, timestamp: UInt64) {
self.serverID = serverID
self.hexEncodedPublicKey = hexEncodedPublicKey
self.displayName = displayName
self.body = body
self.type = type
self.timestamp = timestamp
super.init()
}
@objc public convenience init(hexEncodedPublicKey: String, displayName: String, body: String, type: String, timestamp: UInt64) {
self.init(serverID: nil, hexEncodedPublicKey: hexEncodedPublicKey, displayName: displayName, body: body, type: type, timestamp: timestamp)
}
public func toJSON() -> JSON {

@ -1104,6 +1104,39 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException";
return messageSend.failure(error);
}
void (^failedMessageSend)(NSError *error) = ^(NSError *error) {
// Handle the 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];
};
// Check to see if we're sending to a public channel
if ([recipient.recipientId isEqualToString:LKGroupChatAPI.serverURL]) {
NSString *userHexEncodedPublicKey = OWSIdentityManager.sharedManager.identityKeyPair.hexEncodedPublicKey;
NSString *displayName = [SSKEnvironment.shared.contactsManager displayNameForPhoneIdentifier:userHexEncodedPublicKey];
if (displayName == nil) displayName = @"Anonymous";
LKGroupMessage *groupMessage = [[LKGroupMessage alloc] initWithHexEncodedPublicKey:userHexEncodedPublicKey displayName:displayName body:message.body type:LKGroupChatAPI.publicChatMessageType timestamp:message.timestamp];
[[LKGroupChatAPI sendMessage:groupMessage groupID:LKGroupChatAPI.publicChatID]
.thenOn(OWSDispatch.sendingQueue, ^(id result) {
[self messageSendDidSucceed:messageSend deviceMessages:deviceMessages wasSentByUD:false wasSentByWebsocket:false];
})
.catchOn(OWSDispatch.sendingQueue, ^(NSError *error) { // The snode is unreachable
failedMessageSend(error);
}) retainUntilComplete];
} else {
// Gather the message info
NSDictionary *signalMessageInfo = deviceMessages.firstObject;
SSKProtoEnvelopeType type = ((NSNumber *)signalMessageInfo[@"type"]).integerValue;
@ -1137,20 +1170,7 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException";
[message saveIsCalculatingProofOfWork:NO withTransaction:transaction];
}];
// Handle the 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];
failedMessageSend(error);
};
// Send the message using the Loki API
[[LKAPI sendSignalMessage:signalMessage onP2PSuccess:onP2PSuccess]
@ -1189,6 +1209,7 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException";
.catchOn(OWSDispatch.sendingQueue, ^(NSError *error) { // The snode is unreachable
handleError(error);
}) retainUntilComplete];
}
// Loki: Original code
/*

Loading…
Cancel
Save