This commit is contained in:
Niels Andriesse 2019-05-21 15:21:51 +10:00
parent 472113bccb
commit dc103c7bd6
7 changed files with 56 additions and 59 deletions

View File

@ -1355,7 +1355,7 @@
D17BB5C25D615AB49813100C /* Pods_Signal.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_Signal.framework; sourceTree = BUILT_PRODUCTS_DIR; };
D2179CFB16BB0B3A0006F3AB /* CoreTelephony.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreTelephony.framework; path = System/Library/Frameworks/CoreTelephony.framework; sourceTree = SDKROOT; };
D2179CFD16BB0B480006F3AB /* SystemConfiguration.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = SystemConfiguration.framework; path = System/Library/Frameworks/SystemConfiguration.framework; sourceTree = SDKROOT; };
D221A089169C9E5E00537ABF /* Session.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Session.app; sourceTree = BUILT_PRODUCTS_DIR; };
D221A089169C9E5E00537ABF /* Signal.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Signal.app; sourceTree = BUILT_PRODUCTS_DIR; };
D221A08D169C9E5E00537ABF /* UIKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = UIKit.framework; path = System/Library/Frameworks/UIKit.framework; sourceTree = SDKROOT; };
D221A08F169C9E5E00537ABF /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; };
D221A091169C9E5E00537ABF /* CoreGraphics.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreGraphics.framework; path = System/Library/Frameworks/CoreGraphics.framework; sourceTree = SDKROOT; };
@ -2625,7 +2625,7 @@
D221A08A169C9E5E00537ABF /* Products */ = {
isa = PBXGroup;
children = (
D221A089169C9E5E00537ABF /* Session.app */,
D221A089169C9E5E00537ABF /* Signal.app */,
D221A0AA169C9E5F00537ABF /* SignalTests.xctest */,
453518681FC635DD00210559 /* SignalShareExtension.appex */,
453518921FC63DBF00210559 /* SignalMessaging.framework */,
@ -2861,7 +2861,7 @@
);
name = Signal;
productName = RedPhone;
productReference = D221A089169C9E5E00537ABF /* Session.app */;
productReference = D221A089169C9E5E00537ABF /* Signal.app */;
productType = "com.apple.product-type.application";
};
D221A0A9169C9E5F00537ABF /* SignalTests */ = {
@ -4353,12 +4353,13 @@
LLVM_LTO = NO;
OTHER_LDFLAGS = "$(inherited)";
PRODUCT_BUNDLE_IDENTIFIER = "com.niels-andriesse.loki-network.Loki-Messenger";
PRODUCT_NAME = Session;
PRODUCT_NAME = Signal;
PROVISIONING_PROFILE = "";
PROVISIONING_PROFILE_SPECIFIER = "";
RUN_CLANG_STATIC_ANALYZER = YES;
SDKROOT = iphoneos;
SWIFT_OBJC_BRIDGING_HEADER = "Signal/src/Signal-Bridging-Header.h";
SWIFT_OBJC_INTERFACE_HEADER_NAME = "Session-Swift.h";
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 5.0;
TEST_AFTER_BUILD = YES;
@ -4418,11 +4419,12 @@
LLVM_LTO = NO;
OTHER_LDFLAGS = "$(inherited)";
PRODUCT_BUNDLE_IDENTIFIER = "com.niels-andriesse.loki-network.Loki-Messenger";
PRODUCT_NAME = Session;
PRODUCT_NAME = Signal;
PROVISIONING_PROFILE = "";
RUN_CLANG_STATIC_ANALYZER = YES;
SDKROOT = iphoneos;
SWIFT_OBJC_BRIDGING_HEADER = "Signal/src/Signal-Bridging-Header.h";
SWIFT_OBJC_INTERFACE_HEADER_NAME = "Session-Swift.h";
SWIFT_VERSION = 5.0;
TEST_AFTER_BUILD = YES;
VALID_ARCHS = "arm64 armv7 armv7s";

View File

@ -175,12 +175,13 @@ public class MessageFetcherJob: NSObject {
}
private func fetchUndeliveredMessages() -> Promise<(envelopes: [SSKProtoEnvelope], more: Bool)> {
return Promise { resolver in
LokiAPI.getMessages().done { envelopes in
resolver.fulfill((envelopes: envelopes, more: false))
}.catch { error in
resolver.reject(error)
}
notImplemented()
// return Promise { resolver in
// LokiAPI.getMessages().done { envelopes in
// resolver.fulfill((envelopes: envelopes, more: false))
// }.catch { error in
// resolver.reject(error)
// }
// Loki: Original code
// ========
// let request = OWSRequestFactory.getMessagesRequest()
@ -203,7 +204,7 @@ public class MessageFetcherJob: NSObject {
// resolver.reject(error)
// })
// ========
}
// }
}
private func acknowledgeDelivery(envelope: SSKProtoEnvelope) {

View File

@ -619,7 +619,7 @@ typedef NS_ENUM(NSInteger, HomeViewControllerSection) {
switch (self.homeViewMode) {
case HomeViewMode_Inbox:
// TODO: Should our app name be translated? Probably not.
self.title = NSLocalizedString(@"Session", @"Title for the home view's default mode.");
self.title = NSLocalizedString(@"Signal", @"Title for the home view's default mode.");
break;
case HomeViewMode_Archive:
self.title = NSLocalizedString(@"HOME_VIEW_TITLE_ARCHIVE", @"Title for the home view's 'archive' mode.");

View File

@ -2578,7 +2578,7 @@
"New Message" = "New Message";
"Secure session reset in progress" = "Secure session reset in progress";
"Secure session reset done" = "Secure session reset done";
"Session" = "Session";
"Signal" = "Signal";
"You've sent %@ a friend request" = "You've sent %@ a friend request";
"You've declined %@'s friend request" = "You've declined %@'s friend request";
"You've accepted %@'s friend request" = "You've accepted %@'s friend request";

View File

@ -1,6 +1,7 @@
import PromiseKit
@objc public final class LokiAPI : NSObject {
private static let storage = OWSPrimaryStorage.shared()
// MARK: Caching
private static var swarmCache: [String:Set<Target>] = [:]
@ -34,7 +35,7 @@ import PromiseKit
}
}
public typealias MessagesPromise = Promise<[SSKProtoEnvelope]>
public typealias MessagesPromise = Promise<[SSKProtoEnvelope]> // To keep the return type of getMessages() readable
// MARK: Lifecycle
override private init() { }
@ -47,18 +48,15 @@ import PromiseKit
}
private static func getRandomSnode() -> Promise<Target> {
return Promise<Target> { seal in
seal.fulfill(Target(address: "http://13.238.53.205", port: 8080)) // TODO: Temporary
}
return Promise<Target> { _ in notImplemented() } // TODO: Implement
}
private static func getSwarm(for hexEncodedPublicKey: String) -> Promise<Set<Target>> {
if let cachedSwarm = swarmCache[hexEncodedPublicKey], cachedSwarm.count >= targetSnodeCount {
return Promise<Set<Target>> { $0.fulfill(cachedSwarm) }
} else {
return getRandomSnode().then { invoke(.getSwarm, on: $0, with: [ "pubKey" : hexEncodedPublicKey ]) }.map { rawResponse in
return [] // TODO: Parse targets from raw response
}.get { swarmCache[hexEncodedPublicKey] = $0 }
return getRandomSnode().then { invoke(.getSwarm, on: $0, with: [ "pubKey" : hexEncodedPublicKey ]) }
.map { parseTargets(from: $0) }.get { swarmCache[hexEncodedPublicKey] = $0 }
}
}
@ -72,12 +70,12 @@ import PromiseKit
return getTargetSnodes(for: hexEncodedPublicKey).mapValues { targetSnode in
let lastHash = getLastHash(for: targetSnode) ?? ""
let parameters: [String:Any] = [ "pubKey" : hexEncodedPublicKey, "lastHash" : lastHash ]
return invoke(.getMessages, on: targetSnode, with: parameters).map { response in
if let json = response as? JSON, let messages = json["messages"] as? [JSON], let lastMessage = messages.last,
return invoke(.getMessages, on: targetSnode, with: parameters).map { rawResponse in
if let json = rawResponse as? JSON, let messages = json["messages"] as? [JSON], let lastMessage = messages.last,
let hash = lastMessage["hash"] as? String, let expiresAt = lastMessage["expiration"] as? Int {
updateLastHash(for: targetSnode, hash: hash, expiresAt: UInt64(expiresAt))
setLastHash(for: targetSnode, hash: hash, expiresAt: UInt64(expiresAt))
}
return parseProtoEnvelopes(from: response)
return parseProtoEnvelopes(from: rawResponse)
}
}
}
@ -98,7 +96,26 @@ import PromiseKit
return anyPromise
}
// MARK: Convenience
// MARK: Last Hash
private static func setLastHash(for target: Target, hash: String, expiresAt: UInt64) {
storage.dbReadWriteConnection.readWrite { transaction in
storage.setLastMessageHashForServiceNode(target.address, hash: hash, expiresAt: expiresAt, transaction: transaction)
}
}
private static func getLastHash(for target: Target) -> String? {
var lastHash: String?
storage.dbReadWriteConnection.readWrite { transaction in
lastHash = storage.getLastMessageHash(forServiceNode: target.address, transaction: transaction)
}
return lastHash
}
// MARK: Parsing
private static func parseTargets(from rawResponse: Any) -> Set<Target> {
notImplemented()
}
private static func parseProtoEnvelopes(from rawResponse: Any) -> [SSKProtoEnvelope] {
guard let json = rawResponse as? JSON, let messages = json["messages"] as? [JSON] else { return [] }
return messages.compactMap { message in
@ -126,26 +143,3 @@ private extension Promise {
}
}
}
// MARK: Last Hash
fileprivate extension LokiAPI {
private static var primaryStorage: OWSPrimaryStorage {
return OWSPrimaryStorage.shared()
}
fileprivate static func updateLastHash(for node: Target, hash: String, expiresAt: UInt64) {
primaryStorage.dbReadWriteConnection.readWrite { transaction in
self.primaryStorage.setLastMessageHash(hash, expiresAt: expiresAt, serviceNode: node.address, transaction: transaction)
}
}
fileprivate static func getLastHash(for node: Target) -> String? {
var lastHash: String? = nil
primaryStorage.dbReadWriteConnection.readWrite { transaction in
lastHash = self.primaryStorage.getLastMessageHash(forServiceNode: node.address, transaction: transaction)
}
return lastHash
}
}

View File

@ -77,11 +77,11 @@ NS_ASSUME_NONNULL_BEGIN
/**
Get the last message hash for the given service node.
This function will check the stored last hash and remove it if the `expireAt` has already passed.
This function will check the stored last hash and remove it if the `expiresAt` has already passed.
@param serviceNode The service node id
@param transaction A read write transaction
@return The last hash or nil if it doesn't exist
@param serviceNode The service node ID.
@param transaction A read write transaction.
@return The last hash or `nil` if it doesn't exist.
*/
- (NSString *_Nullable)getLastMessageHashForServiceNode:(NSString *)serviceNode transaction:(YapDatabaseReadWriteTransaction *)transaction;
@ -89,12 +89,12 @@ NS_ASSUME_NONNULL_BEGIN
Set the last message hash for the given service node.
This will override any previous hashes stored for the given service node.
@param hash The last message hash
@param expiresAt The time the message expires on the server
@param serviceNode The service node
@param transaction A read write transaction
@param serviceNode The service node ID.
@param hash The last message hash.
@param expiresAt The time the message expires on the server.
@param transaction A read write transaction.
*/
- (void)setLastMessageHash:(NSString *)hash expiresAt:(u_int64_t)expiresAt serviceNode:(NSString *)serviceNode transaction:(YapDatabaseReadWriteTransaction *)transaction;
- (void)setLastMessageHashForServiceNode:(NSString *)serviceNode hash:(NSString *)hash expiresAt:(u_int64_t)expiresAt transaction:(YapDatabaseReadWriteTransaction *)transaction;
@end

View File

@ -143,7 +143,7 @@
return hash;
}
- (void)setLastMessageHash:(NSString *)hash expiresAt:(u_int64_t)expiresAt serviceNode:(NSString *)serviceNode transaction:(YapDatabaseReadWriteTransaction *)transaction {
- (void)setLastMessageHashForServiceNode:(NSString *)serviceNode hash:(NSString *)hash expiresAt:(u_int64_t)expiresAt transaction:(YapDatabaseReadWriteTransaction *)transaction {
NSDictionary *dict = @{
@"hash": hash,
@"expiresAt": @(expiresAt)