Merge branch 'dev' of https://github.com/loki-project/session-ios into PN-with-preview

This commit is contained in:
ryanzhao 2020-03-30 11:09:55 +11:00
commit 471870d8a5
12 changed files with 57 additions and 60 deletions

View File

@ -4952,6 +4952,7 @@
LLVM_LTO = NO;
MARKETING_VERSION = 1.0.8;
OTHER_LDFLAGS = "$(inherited)";
OTHER_SWIFT_FLAGS = "$(inherited) \"-D\" \"COCOAPODS\" \"-DDEBUG\"";
PRODUCT_BUNDLE_IDENTIFIER = "com.loki-project.loki-messenger";
PRODUCT_NAME = Session;
PROVISIONING_PROFILE = "";

View File

@ -59,7 +59,9 @@ static NSString *const kURLHostVerifyPrefix = @"verify";
static NSTimeInterval launchStartedAt;
// Debug settings
static BOOL isInternalTestVersion = NO;
static BOOL isUsingFullAPNs = YES;
@interface AppDelegate () <UNUserNotificationCenterDelegate>
@ -587,9 +589,11 @@ static BOOL isInternalTestVersion = NO;
}
OWSLogInfo(@"Registered for push notifications with token: %@.", deviceToken);
//TODO: For normal push notification test
[LKPushNotificationManager.shared registerWithToken:deviceToken pubkey:self.tsAccountManager.localNumber];
// [LKPushNotificationManager.shared registerWithToken:deviceToken];
if (isUsingFullAPNs) {
[LKPushNotificationManager registerWithToken:deviceToken hexEncodedPublicKey:self.tsAccountManager.localNumber];
} else {
[LKPushNotificationManager registerWithToken:deviceToken];
}
// [self.pushRegistrationManager didReceiveVanillaPushToken:deviceToken];
}
@ -1555,7 +1559,7 @@ static BOOL isInternalTestVersion = NO;
{
OWSLogInfo(@"");
if (notification.request.content.userInfo[@"remote"]) {
OWSLogInfo(@"[Loki] Ignore remote notifications when app is foreground.");
OWSLogInfo(@"[Loki] Ignoring remote notifications while the app is in the foreground.");
return;
}
[AppReadiness runNowOrWhenAppDidBecomeReady:^() {

View File

@ -1,75 +1,71 @@
import UIKit
// Ideally this should be in SignalServiceKit, but somehow linking fails when it is.
@objc(LKPushNotificationManager)
final class LokiPushNotificationManager : NSObject {
@objc static let shared = LokiPushNotificationManager()
private override init() { super.init() }
// MARK: Settings
#if DEBUG
private static let url = URL(string: "https://dev.apns.getsession.org/register")!
#else
private static let url = URL(string: "https://live.apns.getsession.org/register")!
#endif
private static let tokenExpirationInterval: TimeInterval = 2 * 24 * 60 * 60
// MARK: Initialization
private override init() { }
// MARK: Registration
@objc(registerWithToken:)
func register(with token: Data) {
let hexEncodedToken = token.map { String(format: "%02.2hhx", $0) }.joined()
static func register(with token: Data) {
let hexEncodedToken = token.toHexString()
let userDefaults = UserDefaults.standard
let oldToken = userDefaults[.deviceToken]
let lastUploadTime = userDefaults[.lastDeviceTokenUpload]
let applyNormalNotification = userDefaults[.applyNormalNotification]
let isUsingFullAPNs = userDefaults[.isUsingFullAPNs]
let now = Date().timeIntervalSince1970
if hexEncodedToken == oldToken && now - lastUploadTime < 2 * 24 * 60 * 60 {
print("[Loki] Device token hasn't changed; no need to upload.")
return
guard hexEncodedToken != oldToken || now - lastUploadTime < tokenExpirationInterval else {
return print("[Loki] Device token hasn't changed; no need to re-upload.")
}
if applyNormalNotification {
print("[Loki] Using normal notification; no need to upload.")
return
guard !isUsingFullAPNs else {
return print("[Loki] Using full APNs; no need to upload device token.")
}
// Send token to Loki server
let parameters = [ "token" : hexEncodedToken ]
#if DEBUG
let url = URL(string: "https://dev.apns.getsession.org/register")!
#else
let url = URL(string: "https://live.apns.getsession.org/register")!
#endif
let request = TSRequest(url: url, method: "POST", parameters: parameters)
request.allHTTPHeaderFields = [ "Content-Type" : "application/json" ]
TSNetworkManager.shared().makeRequest(request, success: { _, response in
guard let json = response as? JSON else { return }
guard let json = response as? JSON else {
return print("[Loki] Couldn't register device token.")
}
guard json["code"] as? Int != 0 else {
return print("[Loki] An error occured during device token registration: \(json["message"] as? String ?? "nil").")
return print("[Loki] Couldn't register device token due to error: \(json["message"] as? String ?? "nil").")
}
userDefaults[.deviceToken] = hexEncodedToken
userDefaults[.lastDeviceTokenUpload] = now
userDefaults[.applyNormalNotification] = false
userDefaults[.isUsingFullAPNs] = false
}, failure: { _, error in
print("[Loki] Couldn't register device token.")
})
}
@objc(registerWithToken: pubkey:)
func register(with token: Data, pubkey: String) {
let hexEncodedToken = token.map { String(format: "%02.2hhx", $0) }.joined()
@objc(registerWithToken:hexEncodedPublicKey:)
static func register(with token: Data, hexEncodedPublicKey: String) {
let hexEncodedToken = token.toHexString()
let userDefaults = UserDefaults.standard
let now = Date().timeIntervalSince1970
// Send token to Loki server
let parameters = [ "token" : hexEncodedToken,
"pubKey": pubkey]
#if DEBUG
let url = URL(string: "https://dev.apns.getsession.org/register")!
#else
let url = URL(string: "https://live.apns.getsession.org/register")!
#endif
let parameters = [ "token" : hexEncodedToken, "pubKey" : hexEncodedPublicKey]
let request = TSRequest(url: url, method: "POST", parameters: parameters)
request.allHTTPHeaderFields = [ "Content-Type" : "application/json" ]
TSNetworkManager.shared().makeRequest(request, success: { _, response in
guard let json = response as? JSON else { return }
guard let json = response as? JSON else {
return print("[Loki] Couldn't register device token.")
}
guard json["code"] as? Int != 0 else {
return print("[Loki] An error occured during device token registration: \(json["message"] as? String ?? "nil").")
return print("[Loki] Couldn't register device token due to error: \(json["message"] as? String ?? "nil").")
}
userDefaults[.deviceToken] = hexEncodedToken
userDefaults[.lastDeviceTokenUpload] = now
userDefaults[.applyNormalNotification] = true
userDefaults[.isUsingFullAPNs] = true
}, failure: { _, error in
print("[Loki] Couldn't register device token.")
})

View File

@ -15,7 +15,7 @@ class BaseVC : UIViewController {
@objc private func handleUnexpectedDeviceLinkRequestReceivedNotification() {
guard DeviceLinkingUtilities.shouldShowUnexpectedDeviceLinkRequestReceivedAlert else { return }
DispatchQueue.main.async {
let alert = UIAlertController(title: "Device Link Request Received", message: "Open the device link screen by going to \"Settings\"> \"Devices\" > \"Link a Device\" to link your devices.", preferredStyle: .alert)
let alert = UIAlertController(title: "Device Link Request Received", message: "Open the device link screen by going to \"Settings\" > \"Devices\" > \"Link a Device\" to link your devices.", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
self.present(alert, animated: true, completion: nil)
}

View File

@ -642,8 +642,7 @@ class NotificationActionHandler {
func showThread(userInfo: [AnyHashable: Any]) throws -> Promise<Void> {
guard let threadId = userInfo[AppNotificationUserInfoKey.threadId] as? String else {
return showHomePage()
// throw NotificationError.failDebug("threadId was unexpectedly nil")
return showHomeVC()
}
// If this happens when the the app is not, visible we skip the animation so the thread
@ -654,7 +653,7 @@ class NotificationActionHandler {
return Promise.value(())
}
func showHomePage() -> Promise<Void> {
func showHomeVC() -> Promise<Void> {
signalApp.showHomeView()
return Promise.value(())
}

View File

@ -5488,7 +5488,7 @@ typedef enum : NSUInteger {
{
if (!LKDeviceLinkingUtilities.shouldShowUnexpectedDeviceLinkRequestReceivedAlert) { return; }
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Device Link Request Received" message:@"Open the device link screen by going to \"Settings\"> \"Devices\" > \"Link a Device\" to link your devices." preferredStyle:UIAlertControllerStyleAlert];
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Device Link Request Received" message:@"Open the device link screen by going to \"Settings\" > \"Devices\" > \"Link a Device\" to link your devices." preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil]];
[self presentViewController:alert animated:YES completion:nil];
});

View File

@ -352,8 +352,7 @@ public class SystemContactsFetcher: NSObject {
guard let contacts = fetchedContacts else {
owsFailDebug("contacts was unexpectedly not set.")
completion(nil)
return
return completion(nil)
}
Logger.info("fetched \(contacts.count) contacts.")

View File

@ -48,7 +48,7 @@ public extension LokiAPI {
]
]
])
print("[Loki] Invoking get_n_service_nodes on \(target).")
print("[Loki] Invoking get_service_nodes on \(target).")
return TSNetworkManager.shared().perform(request, withCompletionQueue: DispatchQueue.global()).map(on: DispatchQueue.global()) { intermediate in
let rawResponse = intermediate.responseObject
guard let json = rawResponse as? JSON, let intermediate = json["result"] as? JSON, let rawTargets = intermediate["service_node_states"] as? [JSON] else { throw LokiAPIError.randomSnodePoolUpdatingFailed }

View File

@ -2,10 +2,12 @@ import PromiseKit
@objc(LKAPI)
public final class LokiAPI : NSObject {
private static let stateQueue = DispatchQueue(label: "stateQueue")
/// Only ever modified from the message processing queue (`OWSBatchMessageProcessor.processingQueue`).
private static var syncMessageTimestamps: [String:Set<UInt64>] = [:]
public static var _lastDeviceLinkUpdate: [String:Date] = [:]
private static var _lastDeviceLinkUpdate: [String:Date] = [:]
/// A mapping from hex encoded public key to date updated.
public static var lastDeviceLinkUpdate: [String:Date] {
get { stateQueue.sync { _lastDeviceLinkUpdate } }
@ -19,8 +21,6 @@ public final class LokiAPI : NSObject {
set { stateQueue.sync { _userHexEncodedPublicKeyCache = newValue } }
}
private static let stateQueue = DispatchQueue(label: "stateQueue")
/// All service node related errors must be handled on this queue to avoid race conditions maintaining e.g. failure counts.
public static let errorHandlingQueue = DispatchQueue(label: "errorHandlingQueue")

View File

@ -79,7 +79,7 @@
}
#pragma mark Settings
- (uint)ttl { return 2 * kMinuteInMs; }
- (uint)ttl { return 4 * kMinuteInMs; }
- (BOOL)shouldSyncTranscript { return NO; }
- (BOOL)shouldBeSaved { return NO; }

View File

@ -8,7 +8,7 @@ public enum LKUserDefaults {
case hasViewedSeed
/// Whether the device was unlinked as a slave device (used to notify the user on the landing screen).
case wasUnlinked
case applyNormalNotification
case isUsingFullAPNs
}
public enum Date : Swift.String {

View File

@ -60,11 +60,6 @@ public class TypingIndicatorMessage: TSOutgoingMessage {
public override var isOnline: Bool {
return true
}
@objc
public override var ttl: UInt32 {
return UInt32(10 * kMinuteInMs)
}
private func protoAction(forAction action: TypingIndicatorAction) -> SSKProtoTypingMessage.SSKProtoTypingMessageAction {
switch action {
@ -104,6 +99,9 @@ public class TypingIndicatorMessage: TSOutgoingMessage {
public override func shouldBeSaved() -> Bool {
return false
}
@objc
public override var ttl: UInt32 { return UInt32(2 * kMinuteInMs) }
@objc
public override var debugDescription: String {