mirror of https://github.com/oxen-io/session-ios
parent
5c481c38ba
commit
2694699e4e
@ -1 +1 @@
|
|||||||
Subproject commit 6174a578a93622fc02184ba8983fc809ef7bc0ec
|
Subproject commit e5dc17be2dc588fde41caa91484aa9e6b9c56ebd
|
@ -0,0 +1,75 @@
|
|||||||
|
|
||||||
|
extension LokiAPI {
|
||||||
|
private static let messageSender: MessageSender = SSKEnvironment.shared.messageSender
|
||||||
|
internal static var ourP2PAddress: Target? = nil
|
||||||
|
|
||||||
|
/// Set our local P2P address
|
||||||
|
///
|
||||||
|
/// - Parameter url: The url to our local server
|
||||||
|
@objc public static func setOurP2PAddress(url: URL) {
|
||||||
|
guard let port = url.port else { return }
|
||||||
|
let target = Target(address: url.absoluteString, port: UInt32(port))
|
||||||
|
ourP2PAddress = target
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Broadcash an online message to all our friends.
|
||||||
|
/// This shouldn't be called inside a transaction.
|
||||||
|
@objc public static func broadcastOnlineStatus() {
|
||||||
|
AssertIsOnMainThread()
|
||||||
|
|
||||||
|
let friendThreads = getAllFriendThreads()
|
||||||
|
for thread in friendThreads {
|
||||||
|
sendOnlineBroadcastMessage(forThread: thread)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get the `LokiAddressMessage` for the given thread.
|
||||||
|
///
|
||||||
|
/// - Parameter thread: The contact thread.
|
||||||
|
/// - Returns: The `LokiAddressMessage` for that thread.
|
||||||
|
@objc public static func onlineBroadcastMessage(forThread thread: TSThread) -> LokiAddressMessage? {
|
||||||
|
guard let ourAddress = ourP2PAddress else {
|
||||||
|
Logger.error("P2P Address not set")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return LokiAddressMessage(in: thread, address: ourAddress.address, port: ourAddress.port)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Send a `Loki Address` message to the given thread
|
||||||
|
///
|
||||||
|
/// - Parameter thread: The contact thread to send the message to
|
||||||
|
@objc public static func sendOnlineBroadcastMessage(forThread thread: TSContactThread) {
|
||||||
|
AssertIsOnMainThread()
|
||||||
|
|
||||||
|
guard let message = onlineBroadcastMessage(forThread: thread) else {
|
||||||
|
owsFailDebug("P2P Address not set")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
messageSender.sendPromise(message: message).catch { error in
|
||||||
|
Logger.warn("Failed to send online status to \(thread.contactIdentifier())")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@objc public static func sendOnlineBroadcastMessage(forThread thread: TSContactThread, transaction: YapDatabaseReadWriteTransaction) {
|
||||||
|
guard let ourAddress = ourP2PAddress else {
|
||||||
|
owsFailDebug("P2P Address not set")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static func getAllFriendThreads() -> [TSContactThread] {
|
||||||
|
var friendThreadIds = [String]()
|
||||||
|
TSContactThread.enumerateCollectionObjects { (object, _) in
|
||||||
|
guard let thread = object as? TSContactThread, let uniqueId = thread.uniqueId else { return }
|
||||||
|
|
||||||
|
if thread.friendRequestStatus == .friends && thread.contactIdentifier() != ourHexEncodedPubKey {
|
||||||
|
friendThreadIds.append(thread.uniqueId!)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return friendThreadIds.compactMap { TSContactThread.fetch(uniqueId: $0) }
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,21 @@
|
|||||||
|
//
|
||||||
|
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
|
||||||
|
//
|
||||||
|
|
||||||
|
#import <SignalServiceKit/SignalServiceKit.h>
|
||||||
|
|
||||||
|
NS_ASSUME_NONNULL_BEGIN
|
||||||
|
|
||||||
|
NS_SWIFT_NAME(LokiAddressMessage)
|
||||||
|
@interface LKAddressMessage : TSOutgoingMessage
|
||||||
|
|
||||||
|
- (instancetype)initInThread:(nullable TSThread *)thread
|
||||||
|
address:(NSString *)address
|
||||||
|
port:(uint)port;
|
||||||
|
|
||||||
|
@property (nonatomic, readonly) NSString *address;
|
||||||
|
@property (nonatomic, readonly) uint port;
|
||||||
|
|
||||||
|
@end
|
||||||
|
|
||||||
|
NS_ASSUME_NONNULL_END
|
@ -0,0 +1,62 @@
|
|||||||
|
#import "LKAddressMessage.h"
|
||||||
|
#import "NSDate+OWS.h"
|
||||||
|
#import "SignalRecipient.h"
|
||||||
|
#import <SignalServiceKit/SignalServiceKit-Swift.h>
|
||||||
|
|
||||||
|
@interface LKAddressMessage ()
|
||||||
|
|
||||||
|
@property (nonatomic) NSString *address;
|
||||||
|
@property (nonatomic) uint port;
|
||||||
|
|
||||||
|
@end
|
||||||
|
|
||||||
|
@implementation LKAddressMessage
|
||||||
|
|
||||||
|
- (instancetype)initAddressMessageInThread:(nullable TSThread *)thread
|
||||||
|
address:(NSString *)address
|
||||||
|
port:(uint)port
|
||||||
|
{
|
||||||
|
self = [super initOutgoingMessageWithTimestamp:NSDate.ows_millisecondTimeStamp inThread:thread messageBody:nil attachmentIds:[NSMutableArray<NSString *> new]
|
||||||
|
expiresInSeconds:0 expireStartedAt:0 isVoiceMessage:NO groupMetaMessage:TSGroupMetaMessageUnspecified quotedMessage:nil contactShare:nil linkPreview:nil];
|
||||||
|
if (!self) {
|
||||||
|
return self;
|
||||||
|
}
|
||||||
|
|
||||||
|
_address = address;
|
||||||
|
_port = port;
|
||||||
|
|
||||||
|
return self;
|
||||||
|
}
|
||||||
|
|
||||||
|
- (SSKProtoContentBuilder *)contentBuilder:(SignalRecipient *)recipient {
|
||||||
|
SSKProtoContentBuilder *contentBuilder = [super contentBuilder:recipient];
|
||||||
|
|
||||||
|
// Se
|
||||||
|
SSKProtoLokiAddressMessageBuilder *addressBuilder = SSKProtoLokiAddressMessage.builder;
|
||||||
|
[addressBuilder setPtpAddress:self.address];
|
||||||
|
[addressBuilder setPtpPort:self.port];
|
||||||
|
|
||||||
|
NSError *error;
|
||||||
|
SSKProtoLokiAddressMessage *addressMessage = [addressBuilder buildAndReturnError:&error];
|
||||||
|
if (error || !addressMessage) {
|
||||||
|
OWSFailDebug(@"Failed to build lokiAddressMessage for %@: %@", recipient.recipientId, error);
|
||||||
|
} else {
|
||||||
|
[contentBuilder setLokiAddressMessage:addressMessage];
|
||||||
|
}
|
||||||
|
|
||||||
|
return contentBuilder;
|
||||||
|
}
|
||||||
|
|
||||||
|
// We don't need to send any data message in this address message
|
||||||
|
- (nullable SSKProtoDataMessage *)buildDataMessage:(NSString *_Nullable)recipientId {
|
||||||
|
return nil;
|
||||||
|
}
|
||||||
|
|
||||||
|
- (BOOL)shouldBeSaved { return false; }
|
||||||
|
|
||||||
|
- (uint)ttl {
|
||||||
|
// Address messages should only last 1 minute
|
||||||
|
return 1 * kMinuteInterval;
|
||||||
|
}
|
||||||
|
|
||||||
|
@end
|
Loading…
Reference in New Issue