From 20472c8bedfc9a19a37ff6434760c0e8ac5e5240 Mon Sep 17 00:00:00 2001 From: Niels Andriesse Date: Wed, 22 May 2019 14:29:59 +1000 Subject: [PATCH] Fix minor code style issues --- Signal/Signal-Info.plist | 2 +- Signal/src/AppDelegate.m | 26 ++++++-------- Signal/src/Loki/LokiP2PServer.swift | 35 ++++++++----------- .../src/Loki/API/LokiAPI+Message.swift | 6 +--- .../src/Loki/API/LokiMessageWrapper.swift | 5 ++- .../src/Loki/API/LokiP2PMessageHandler.swift | 26 ++++++-------- .../Loki/Messages/LKFriendRequestMessage.m | 5 +-- .../SSKProtoPrekeyBundleMessage+Loki.swift | 2 +- .../Messages/Interactions/TSOutgoingMessage.h | 4 +-- .../Messages/Interactions/TSOutgoingMessage.m | 2 +- 10 files changed, 44 insertions(+), 69 deletions(-) diff --git a/Signal/Signal-Info.plist b/Signal/Signal-Info.plist index 7396df8e5..258a90a89 100644 --- a/Signal/Signal-Info.plist +++ b/Signal/Signal-Info.plist @@ -7,7 +7,7 @@ CarthageVersion 0.33.0 OSXVersion - 10.14.4 + 10.14.5 WebRTCCommit 1445d719bf05280270e9f77576f80f973fd847f8 M73 diff --git a/Signal/src/AppDelegate.m b/Signal/src/AppDelegate.m index 123349b84..676b28697 100644 --- a/Signal/src/AppDelegate.m +++ b/Signal/src/AppDelegate.m @@ -63,12 +63,7 @@ static NSTimeInterval launchStartedAt; @property (nonatomic) BOOL hasInitialRootViewController; @property (nonatomic) BOOL areVersionMigrationsComplete; @property (nonatomic) BOOL didAppLaunchFail; - -@end - -@interface AppDelegate () - -@property (nonatomic) LokiP2PServer *lokiP2PServer; +@property (nonatomic) LKP2PServer *lokiP2PServer; // Loki @end @@ -194,9 +189,7 @@ static NSTimeInterval launchStartedAt; [DDLog flushLog]; - if (_lokiP2PServer) { - [_lokiP2PServer stop]; - } + if (self.lokiP2PServer) { [self.lokiP2PServer stop]; } } - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { @@ -319,20 +312,21 @@ static NSTimeInterval launchStartedAt; [OWSAnalytics appLaunchDidBegin]; // Loki - _lokiP2PServer = [LokiP2PServer new]; + self.lokiP2PServer = [LKP2PServer new]; // We try to bind to 8081, if we can't then we just fallback to any random port - NSArray *ports = @[@8081, @0]; + NSArray *ports = @[ @8081, @0 ]; for (NSNumber *port in ports) { - if (_lokiP2PServer.isRunning) { break; } - if ([_lokiP2PServer startOnPort:port.unsignedIntegerValue]) { - OWSLogInfo(@"[Loki P2P Server]: Started server at %@", _lokiP2PServer.serverURL); + if (self.lokiP2PServer.isRunning) { break; } + BOOL isStarted = [self.lokiP2PServer startOnPort:port.unsignedIntegerValue]; + if (isStarted) { + OWSLogInfo(@"[Loki] Started server at %@.", self.lokiP2PServer.serverURL); break; } } - if (!_lokiP2PServer.isRunning) { - OWSLogWarn(@"[Loki P2P Server]: Failed to start loki P2P server"); + if (!self.lokiP2PServer.isRunning) { + OWSLogWarn(@"[Loki] Failed to start P2P server."); } return YES; diff --git a/Signal/src/Loki/LokiP2PServer.swift b/Signal/src/Loki/LokiP2PServer.swift index aae5e7bb1..759b28079 100644 --- a/Signal/src/Loki/LokiP2PServer.swift +++ b/Signal/src/Loki/LokiP2PServer.swift @@ -1,14 +1,13 @@ import GCDWebServer -// Convenience functions - -fileprivate extension GCDWebServerResponse { +private extension GCDWebServerResponse { + convenience init(statusCode: E) where E.RawValue == Int { self.init(statusCode: statusCode.rawValue) } } -fileprivate extension GCDWebServerDataRequest { +private extension GCDWebServerDataRequest { var truncatedContentType: String? { guard let contentType = contentType else { return nil } guard let substring = contentType.split(separator: ";").first else { return contentType } @@ -17,24 +16,25 @@ fileprivate extension GCDWebServerDataRequest { // GCDWebServerDataRequest already provides this implementation // However it will abort when running in DEBUG, we don't want that so we just override it with a version which doesn't abort - var jsonObject: [String: Any]? { - let acceptedMimeTypes = ["application/json", "text/json", "text/javascript"] + var jsonObject: JSON? { + let acceptedMimeTypes = [ "application/json", "text/json", "text/javascript" ] guard let mimeType = truncatedContentType, acceptedMimeTypes.contains(mimeType) else { return nil } do { let object = try JSONSerialization.jsonObject(with: data, options: .allowFragments) - return object as? [String: Any] + return object as? JSON } catch let error { - Logger.debug("[Loki P2P Server] Failed to serialize json: \(error)") + Logger.debug("[Loki] Failed to serialize JSON: \(error).") } return nil } } -@objc class LokiP2PServer : NSObject { +@objc(LKP2PServer) +final class LokiP2PServer : NSObject { - fileprivate enum StatusCode: Int { + private enum StatusCode : Int { case ok = 200 case badRequest = 400 case notFound = 404 @@ -50,7 +50,7 @@ fileprivate extension GCDWebServerDataRequest { return GCDWebServerResponse(statusCode: StatusCode.methodNotAllowed) } - let invalidMethods = ["GET", "PUT", "DELETE"] + let invalidMethods = [ "GET", "PUT", "DELETE" ] for method in invalidMethods { webServer.addDefaultHandler(forMethod: method, request: GCDWebServerRequest.self, processBlock: invalidMethodProcessBlock) } @@ -87,13 +87,8 @@ fileprivate extension GCDWebServerDataRequest { return webServer }() - @objc public var serverURL: URL? { - return webServer.serverURL - } - - @objc public var isRunning: Bool { - return webServer.isRunning - } + @objc public var serverURL: URL? { return webServer.serverURL } + @objc public var isRunning: Bool { return webServer.isRunning } @objc @discardableResult func start(onPort port: UInt) -> Bool { guard !webServer.isRunning else { return false } @@ -101,7 +96,5 @@ fileprivate extension GCDWebServerDataRequest { return webServer.isRunning } - @objc func stop() { - webServer.stop() - } + @objc func stop() { webServer.stop() } } diff --git a/SignalServiceKit/src/Loki/API/LokiAPI+Message.swift b/SignalServiceKit/src/Loki/API/LokiAPI+Message.swift index 3abc5bd73..021348535 100644 --- a/SignalServiceKit/src/Loki/API/LokiAPI+Message.swift +++ b/SignalServiceKit/src/Loki/API/LokiAPI+Message.swift @@ -35,12 +35,8 @@ public extension LokiAPI { let wrappedMessage = try LokiMessageWrapper.wrap(message: signalMessage, timestamp: timestamp) let data = wrappedMessage.base64EncodedString() let destination = signalMessage["destination"] as! String - var ttl = LokiAPI.defaultMessageTTL - if let messageTTL = signalMessage["ttl"] as? UInt, messageTTL > 0 { - ttl = UInt64(messageTTL) - } - + if let messageTTL = signalMessage["ttl"] as? UInt, messageTTL > 0 { ttl = UInt64(messageTTL) } if isPoWRequired { // The storage server takes a time interval in milliseconds let now = NSDate.ows_millisecondTimeStamp() diff --git a/SignalServiceKit/src/Loki/API/LokiMessageWrapper.swift b/SignalServiceKit/src/Loki/API/LokiMessageWrapper.swift index 1b9ccaefb..03ae7dc70 100644 --- a/SignalServiceKit/src/Loki/API/LokiMessageWrapper.swift +++ b/SignalServiceKit/src/Loki/API/LokiMessageWrapper.swift @@ -1,6 +1,5 @@ -public class LokiMessageWrapper { - private init() {} +public enum LokiMessageWrapper { public enum WrappingError : LocalizedError { case failedToWrapData @@ -82,7 +81,7 @@ public class LokiMessageWrapper { /// - Parameter data: The data from the storage server (not base 64 encoded). /// - Returns: An `SSKProtoEnvelope` object. /// - Throws: A `WrappingError` if something went wrong. - static func unwrap(data: Data) throws -> SSKProtoEnvelope { + public static func unwrap(data: Data) throws -> SSKProtoEnvelope { do { let webSocketMessage = try WebSocketProtoWebSocketMessage.parseData(data) let envelope = webSocketMessage.request!.body! diff --git a/SignalServiceKit/src/Loki/API/LokiP2PMessageHandler.swift b/SignalServiceKit/src/Loki/API/LokiP2PMessageHandler.swift index 788edb2f4..593bad7fb 100644 --- a/SignalServiceKit/src/Loki/API/LokiP2PMessageHandler.swift +++ b/SignalServiceKit/src/Loki/API/LokiP2PMessageHandler.swift @@ -1,36 +1,32 @@ -public class LokiP2PMessageHandler { +public final class LokiP2PMessageHandler { + private let messageReceiver = SSKEnvironment.shared.messageReceiver + + // MARK: Initialization public static let shared = LokiP2PMessageHandler() - - private var messageReceiver: OWSMessageReceiver { - return SSKEnvironment.shared.messageReceiver - } - private init() {} + private init() { } + // MARK: General public func handleReceivedMessage(base64EncodedData: String) { guard let data = Data(base64Encoded: base64EncodedData) else { - Logger.warn("[LokiP2PMessageHandler] Failed to decode p2p message data") + Logger.warn("[Loki] Failed to decode data for P2P message.") return } - guard let envelope = try? LokiMessageWrapper.unwrap(data: data) else { - Logger.warn("[LokiP2PMessageHandler] Failed to unwrap p2p data") + Logger.warn("[Loki] Failed to unwrap data for P2P message.") return } - - // We need to set the p2p field on the envelope + // We need to set the P2P field on the envelope let builder = envelope.asBuilder() builder.setIsPtpMessage(true) - - // Send it to message receiver + // Send it to the message receiver do { let newEnvelope = try builder.build() let envelopeData = try newEnvelope.serializedData() messageReceiver.handleReceivedEnvelopeData(envelopeData) } catch let error { - Logger.warn("[LokiP2PMessageHandler] Something went wrong while converting proto: \(error)") - owsFailDebug("Failed to build envelope") + Logger.warn("[Loki] Something went wrong during proto conversion: \(error).") } } diff --git a/SignalServiceKit/src/Loki/Messages/LKFriendRequestMessage.m b/SignalServiceKit/src/Loki/Messages/LKFriendRequestMessage.m index 3375379f4..cddf75886 100644 --- a/SignalServiceKit/src/Loki/Messages/LKFriendRequestMessage.m +++ b/SignalServiceKit/src/Loki/Messages/LKFriendRequestMessage.m @@ -8,10 +8,7 @@ - (BOOL)isFriendRequest { return YES; } -- (uint)ttl { - // Friend requests should stay for the longest on the storage server - return 4 * kDayInterval; -} +- (uint)ttl { return 4 * kDayInterval; } // Friend requests should stay for the longest on the storage server - (SSKProtoContentBuilder *)contentBuilder:(SignalRecipient *)recipient { SSKProtoContentBuilder *contentBuilder = [super contentBuilder:recipient]; diff --git a/SignalServiceKit/src/Loki/Messages/SSKProtoPrekeyBundleMessage+Loki.swift b/SignalServiceKit/src/Loki/Messages/SSKProtoPrekeyBundleMessage+Loki.swift index e907709b0..a951dfd00 100644 --- a/SignalServiceKit/src/Loki/Messages/SSKProtoPrekeyBundleMessage+Loki.swift +++ b/SignalServiceKit/src/Loki/Messages/SSKProtoPrekeyBundleMessage+Loki.swift @@ -1,7 +1,7 @@ @objc public extension SSKProtoPrekeyBundleMessage { - @objc public class func builder(fromPreKeyBundle preKeyBundle: PreKeyBundle) -> SSKProtoPrekeyBundleMessageBuilder { + @objc public static func builder(fromPreKeyBundle preKeyBundle: PreKeyBundle) -> SSKProtoPrekeyBundleMessageBuilder { let builder = self.builder() builder.setIdentityKey(preKeyBundle.identityKey) diff --git a/SignalServiceKit/src/Messages/Interactions/TSOutgoingMessage.h b/SignalServiceKit/src/Messages/Interactions/TSOutgoingMessage.h index 598b30fa2..eca1b47e2 100644 --- a/SignalServiceKit/src/Messages/Interactions/TSOutgoingMessage.h +++ b/SignalServiceKit/src/Messages/Interactions/TSOutgoingMessage.h @@ -141,10 +141,10 @@ typedef NS_ENUM(NSInteger, TSGroupMetaMessage) { @property (nonatomic, readonly) BOOL isOnline; -// Loki: Bool to indicate if proof of work is being calculated for this message +/// Loki: Bool to indicate if proof of work is being calculated for this message @property (atomic, readonly) BOOL isCalculatingPoW; -// Loki: Time to live for the message in seconds +/// Loki: Time to live for the message in seconds @property (nonatomic, readonly) uint ttl; /** diff --git a/SignalServiceKit/src/Messages/Interactions/TSOutgoingMessage.m b/SignalServiceKit/src/Messages/Interactions/TSOutgoingMessage.m index 197c62fcf..7c8fd30ff 100644 --- a/SignalServiceKit/src/Messages/Interactions/TSOutgoingMessage.m +++ b/SignalServiceKit/src/Messages/Interactions/TSOutgoingMessage.m @@ -1155,7 +1155,7 @@ NSString *NSStringForOutgoingMessageRecipientState(OWSOutgoingMessageRecipientSt } - (uint)ttl { - // Time to live for all messages should be 1 day + // Time to live for all messages (except friend request messages) should be 1 day // TODO: Change this to return a value that the user chose return 1 * kDayInterval; }