From 332d6b104e8d686aaae2d7a980618b772cd909cd Mon Sep 17 00:00:00 2001 From: sbertix Date: Fri, 26 Jul 2019 10:56:51 +0200 Subject: [PATCH] Improve lint and remove `SessionCache` on logout --- .swiftlint.yml | 50 +++++++++++++++++-- SwiftyInsta/API/Services/APIHandler.swift | 28 ++++++++--- SwiftyInsta/API/Services/MediaHandler.swift | 5 +- SwiftyInsta/API/Services/UserHandler.swift | 16 +++--- .../Classes/Models/CustomUserAgent.swift | 13 ++++- SwiftyInsta/Web/LoginWebView.swift | 1 + 6 files changed, 91 insertions(+), 22 deletions(-) diff --git a/.swiftlint.yml b/.swiftlint.yml index c27b988..1fdf370 100644 --- a/.swiftlint.yml +++ b/.swiftlint.yml @@ -1,22 +1,64 @@ disabled_rules: - operator_whitespace -nesting: - type_level: - warning: 2 +opt_in_rules: + - array_init + - class_delegate_protocol + - collection_alignment + - contains_over_first_not_nil + - empty_count + - empty_string + - fallthrough + - fatal_error_message + - first_where + - for_where + - last_where + - legacy_multiple + - legacy_random + - multiline_arguments + - multiline_parameters + - reduce_into + - sorted_first_last + - sorted_imports + - static_operator + - toggle_bool + - unavailable_function + - vertical_parameter_alignment_on_call + - yoda_condition included: - SwiftyInsta excluded: - Pods - - SwiftyInsta/*/*/APIHandler.swift # skip a bug with #if(os(_)) @available() + #- SwiftyInsta/*/*/APIHandler.swift # skip a bug with #if(os(_)) @available() - SwiftyInsta/*/CryptoHelper.swift - SwiftyInsta/*/KeychainSwiftDistrib.swift analyzer_rules: - explicit_self +array_init: error +class_delegate_protocol: error +contains_over_first_not_nil: error +empty_count: error +empty_string: error +fatal_error_message: error +first_where: error +for_where: error +last_where: error +legacy_multiple: error +legacy_random: error +multiline_parameters: error +sorted_first_last: error +static_operator: error +unavailable_function: error +vertical_parameter_alignment_on_call: error + +nesting: + type_level: + warning: 2 + cyclomatic_complexity: warning: 12 error: 15 diff --git a/SwiftyInsta/API/Services/APIHandler.swift b/SwiftyInsta/API/Services/APIHandler.swift index b9b31db..88ef870 100755 --- a/SwiftyInsta/API/Services/APIHandler.swift +++ b/SwiftyInsta/API/Services/APIHandler.swift @@ -80,7 +80,7 @@ public class APIHandler { } case .user(let credentials): authentication.authenticate(user: credentials, completionHandler: completionHandler) - #if os(iOS) + #if os(iOS) case .webView(let webView): webView.authenticate { [weak self] in guard let handler = self else { return completionHandler(.failure(CustomErrors.weakReferenceReleased)) } @@ -112,16 +112,23 @@ public class APIHandler { handler.authenticate(with: .cache(cache), completionHandler: completionHandler) } } - #endif + #endif } } /// Log out. public func invalidate(completionHandler: @escaping (Result) -> Void) throws { + let key = response?.cache?.storage?.dsUserId authentication.invalidate { [weak self] in - // empty response if needed. - if (try? $0.get()) == true { self?.response = nil } - completionHandler($0) + switch $0 { + case .failure(let error): completionHandler(.failure(error)) + case .success(let success) where success: + self?.response = nil + // remove cache. + if let key = key { KeychainSwift().delete(key) } + completionHandler(.success(success)) + default: completionHandler(.success(false)) + } } } @@ -200,19 +207,26 @@ public struct Credentials { /// An abstract `struct` holding login references . public struct Login { + #if os(iOS) public enum Request { /// Log in with username and password. case user(Credentials) - #if os(iOS) @available(iOS 11, *) /// Log in through web view. case webView(LoginWebView) - #endif /// Log in using `SessionCache` (either a stored one, or through `Siwa`). case cache(SessionCache) } + #else + public enum Request { + /// Log in with username and password. + case user(Credentials) + /// Log in using `SessionCache` (either a stored one, or through `Siwa`). + case cache(SessionCache) + } + #endif public struct Response { /// The login model. public var model: LoginResultModel diff --git a/SwiftyInsta/API/Services/MediaHandler.swift b/SwiftyInsta/API/Services/MediaHandler.swift index e8e11eb..0ff515c 100644 --- a/SwiftyInsta/API/Services/MediaHandler.swift +++ b/SwiftyInsta/API/Services/MediaHandler.swift @@ -113,7 +113,7 @@ public class MediaHandler: Handler { content.append(string: "Content-Type: application/octet-stream\n") content.append(string: ["Content-Disposition: form-data; name=photo;", "filename=pending_media_\(uploadId).jpg;", - "filename*=utf-8''pending_media_\(uploadId).jpg\n\n"].joined(separator: " ")) + "filename*=utf-8''pending_media_\(uploadId).jpg\n\n"].joined(separator: " ")) #if os(macOS) let optionalImageData = photo.image.tiffRepresentation @@ -523,7 +523,8 @@ public class MediaHandler: Handler { formatter.dateFormat = "yyyy-dd-MM'T'H:mm:ss-0SSS" let clips = ClipsModel.init(length: 10, creationDate: formatter.string(from: Date()), - sourceType: "3", cameraPosition: "back") + sourceType: "3", + cameraPosition: "back") let content = ConfigureVideoModel(caption: caption, uploadId: uploadId, sourceType: "3", diff --git a/SwiftyInsta/API/Services/UserHandler.swift b/SwiftyInsta/API/Services/UserHandler.swift index 3fbcd46..c9743cc 100644 --- a/SwiftyInsta/API/Services/UserHandler.swift +++ b/SwiftyInsta/API/Services/UserHandler.swift @@ -170,8 +170,8 @@ public class UserHandler: Handler { pages.fetch(UserFeedModel.self, with: paginationParameters, at: { URLs.getUserTagsUrl(userPk: pk, - rankToken: storage.rankToken, - maxId: $0.nextMaxId ?? "") }, + rankToken: storage.rankToken, + maxId: $0.nextMaxId ?? "") }, updateHandler: updateHandler, completionHandler: completionHandler) } @@ -210,9 +210,9 @@ public class UserHandler: Handler { pages.fetch(UserShortListModel.self, with: paginationParameters, at: { URLs.getUserFollowers(userPk: pk, - rankToken: storage.rankToken, - searchQuery: query ?? "", - maxId: $0.nextMaxId ?? "") }, + rankToken: storage.rankToken, + searchQuery: query ?? "", + maxId: $0.nextMaxId ?? "") }, updateHandler: updateHandler, completionHandler: completionHandler) } @@ -251,9 +251,9 @@ public class UserHandler: Handler { pages.fetch(UserShortListModel.self, with: paginationParameters, at: { URLs.getUserFollowing(userPk: pk, - rankToken: storage.rankToken, - searchQuery: query ?? "", - maxId: $0.nextMaxId ?? "") }, + rankToken: storage.rankToken, + searchQuery: query ?? "", + maxId: $0.nextMaxId ?? "") }, updateHandler: updateHandler, completionHandler: completionHandler) } diff --git a/SwiftyInsta/Classes/Models/CustomUserAgent.swift b/SwiftyInsta/Classes/Models/CustomUserAgent.swift index 2160279..dc322de 100644 --- a/SwiftyInsta/Classes/Models/CustomUserAgent.swift +++ b/SwiftyInsta/Classes/Models/CustomUserAgent.swift @@ -25,7 +25,18 @@ public struct CustomUserAgent { /// Compute and return the user agent. public var string: String { return String(format: "Instagram %@ %@ (%@/%@; %@dpi; %@; %@; %@; %@; %@; %@; %@)", - apiVersion, osName, osVersion, osRelease, dpi, resolution, company, model, model, modem, locale, fbCode) + apiVersion, + osName, + osVersion, + osRelease, + dpi, + resolution, + company, + model, + model, + modem, + locale, + fbCode) } /// Manually create a user-agent. diff --git a/SwiftyInsta/Web/LoginWebView.swift b/SwiftyInsta/Web/LoginWebView.swift index 5685639..e102d7e 100644 --- a/SwiftyInsta/Web/LoginWebView.swift +++ b/SwiftyInsta/Web/LoginWebView.swift @@ -36,6 +36,7 @@ public class LoginWebView: WKWebView, WKNavigationDelegate { private override init(frame: CGRect, configuration: WKWebViewConfiguration) { fatalError("init(frame:, configuration:) has been removed") } + @available(*, unavailable) public required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") }