Skip to content

Commit

Permalink
Add Linting to Build Phase + Fix Linting Errors
Browse files Browse the repository at this point in the history
Also added swiftlint.yml to IOSExample project root in order to pick up rule modifications.
  • Loading branch information
zombieobject committed Feb 23, 2024
1 parent 5f9628b commit 55dabfd
Show file tree
Hide file tree
Showing 31 changed files with 117 additions and 53 deletions.
10 changes: 10 additions & 0 deletions .swiftlint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ disabled_rules: # rule identifiers turned on by default to exclude from running
- nesting
- type_body_length
- file_length
- todo
- control_statement
- switch_case_alignment
- empty_enum_arguments
- for_where
- private_over_fileprivate
- redundant_string_enum_value
- redundant_optional_initialization
- operator_whitespace
- comma

opt_in_rules:
- force_unwrapping
Expand Down
4 changes: 1 addition & 3 deletions Sources/XMTPTestHelpers/TestHelpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ public class FakeApiClient: ApiClient {
}
}
}

if let direction = pagination?.direction {
switch direction {
case .ascending:
Expand Down Expand Up @@ -244,7 +244,6 @@ public class FakeApiClient: ApiClient {
var queryResponse = XMTPiOS.BatchQueryResponse()
queryResponse.responses = responses
return queryResponse

}

public func query(request: XMTPiOS.QueryRequest) async throws -> XMTPiOS.QueryResponse {
Expand All @@ -254,7 +253,6 @@ public class FakeApiClient: ApiClient {
public func publish(request: XMTPiOS.PublishRequest) async throws {
abort() // Not supported on Fake
}

}

@available(iOS 15, *)
Expand Down
4 changes: 2 additions & 2 deletions Sources/XMTPiOS/Codecs/AttachmentCodec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ public struct AttachmentCodec: ContentCodec {

return attachment
}

public func fallback(content: Attachment) throws -> String? {
return "Can’t display “\(content.filename)”. This app doesn’t support attachments."
}

public func shouldPush(content: Attachment) throws -> Bool {
return true
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/XMTPiOS/Codecs/Composite.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ struct CompositeCodec: ContentCodec {
let decodedComposite = fromComposite(composite: composite)
return decodedComposite
}

public func fallback(content: DecodedComposite) throws -> String? {
return nil
}

public func shouldPush(content: DecodedComposite) throws -> Bool {
return false
}
Expand Down
1 change: 0 additions & 1 deletion Sources/XMTPiOS/Codecs/GroupMembershipChanged.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,3 @@ public struct GroupMembershipChangedCodec: ContentCodec {
false
}
}

12 changes: 6 additions & 6 deletions Sources/XMTPiOS/Codecs/ReactionCodec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@

import Foundation


public let ContentTypeReaction = ContentTypeID(authorityID: "xmtp.org", typeID: "reaction", versionMajor: 1, versionMinor: 0)

public struct Reaction: Codable {
public var reference: String
public var action: ReactionAction
public var content: String
public var schema: ReactionSchema

public init(reference: String, action: ReactionAction, content: String, schema: ReactionSchema) {
self.reference = reference
self.action = action
Expand All @@ -26,7 +25,7 @@ public struct Reaction: Codable {

public enum ReactionAction: String, Codable {
case added, removed, unknown

public init(rawValue: String) {
switch rawValue {
case "added":
Expand All @@ -41,7 +40,7 @@ public enum ReactionAction: String, Codable {

public enum ReactionSchema: String, Codable {
case unicode, shortcode, custom, unknown

public init(rawValue: String) {
switch rawValue {
case "unicode":
Expand Down Expand Up @@ -85,8 +84,9 @@ public struct ReactionCodec: ContentCodec {
content: String(data: content.content, encoding: .utf8) ?? "",
schema: ReactionSchema(rawValue: content.parameters["schema"] ?? "")
)
// swiftlint:enable no_optional_try
}

public func fallback(content: Reaction) throws -> String? {
switch content.action {
case .added:
Expand All @@ -97,7 +97,7 @@ public struct ReactionCodec: ContentCodec {
return nil
}
}

public func shouldPush(content: Reaction) throws -> Bool {
switch content.action {
case .added:
Expand Down
4 changes: 2 additions & 2 deletions Sources/XMTPiOS/Codecs/ReadReceiptCodec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ public struct ReadReceiptCodec: ContentCodec {
public func decode(content: EncodedContent, client _: Client) throws -> ReadReceipt {
return ReadReceipt()
}

public func fallback(content: ReadReceipt) throws -> String? {
return nil
}

public func shouldPush(content: ReadReceipt) throws -> Bool {
return false
}
Expand Down
6 changes: 3 additions & 3 deletions Sources/XMTPiOS/Codecs/RemoteAttachmentCodec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public struct RemoteAttachment {
}

public struct RemoteAttachmentCodec: ContentCodec {

public typealias T = RemoteAttachment

public init() {}
Expand Down Expand Up @@ -207,7 +207,7 @@ public struct RemoteAttachmentCodec: ContentCodec {

return attachment
}

public func fallback(content: RemoteAttachment) throws -> String? {
return "Can’t display “\(String(describing: content.filename))”. This app doesn’t support attachments."
}
Expand All @@ -223,7 +223,7 @@ public struct RemoteAttachmentCodec: ContentCodec {

return Data(parameterData)
}

public func shouldPush(content: RemoteAttachment) throws -> Bool {
return true
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/XMTPiOS/Codecs/ReplyCodec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ public struct ReplyCodec: ContentCodec {
throw CodecError.invalidContent
}
}

public func fallback(content: Reply) throws -> String? {
return "Replied with “\(content.content)” to an earlier message"
}

public func shouldPush(content: Reply) throws -> Bool {
return true
}
Expand Down
6 changes: 3 additions & 3 deletions Sources/XMTPiOS/Codecs/TextCodec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ enum TextCodecError: Error {
}

public struct TextCodec: ContentCodec {

public typealias T = String

public init() { }
Expand Down Expand Up @@ -42,11 +42,11 @@ public struct TextCodec: ContentCodec {
throw TextCodecError.unknownDecodingError
}
}

public func fallback(content: String) throws -> String? {
return nil
}

public func shouldPush(content: String) throws -> Bool {
return true
}
Expand Down
6 changes: 3 additions & 3 deletions Sources/XMTPiOS/Conversation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public enum Conversation: Sendable {
return group.peerAddresses.joined(separator: ",")
}
}

public var peerAddresses: [String] {
switch self {
case let .v1(conversationV1):
Expand All @@ -100,7 +100,7 @@ public enum Conversation: Sendable {
return group.peerAddresses
}
}

public var keyMaterial: Data? {
switch self {
case let .v1(conversationV1):
Expand Down Expand Up @@ -182,7 +182,7 @@ public enum Conversation: Sendable {
throw GroupError.notSupportedByGroups
}
}

public func prepareMessage(encodedContent: EncodedContent, options: SendOptions? = nil) async throws -> PreparedMessage {
switch self {
case let .v1(conversationV1):
Expand Down
6 changes: 3 additions & 3 deletions Sources/XMTPiOS/ConversationV2.swift
Original file line number Diff line number Diff line change
Expand Up @@ -107,19 +107,19 @@ public struct ConversationV2 {
}

var encoded = try encode(codec: codec, content: content)

func fallback<Codec: ContentCodec>(codec: Codec, content: Any) throws -> String? {
if let content = content as? Codec.T {
return try codec.fallback(content: content)
} else {
throw CodecError.invalidContent
}
}

if let fallback = try fallback(codec: codec, content: content) {
encoded.fallback = fallback
}

if let compression = options?.compression {
encoded = try encoded.compress(compression)
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/XMTPiOS/Messages/Invitation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ extension InvitationV1 {
let context = context ?? InvitationV1.Context()
let myAddress = try sender.toV1().walletAddress
let theirAddress = try recipient.walletAddress

let secret = try sender.sharedSecret(
peer: recipient,
myPreKey: sender.preKeys[0].publicKey,
Expand Down
4 changes: 2 additions & 2 deletions Sources/XMTPiOS/Messages/MessageV2.swift
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,15 @@ extension MessageV2 {
let signedBytes = try signedContent.serializedData()

let ciphertext = try Crypto.encrypt(keyMaterial, signedBytes, additionalData: headerBytes)

let thirtyDayPeriodsSinceEpoch = Int(date.timeIntervalSince1970 / 60 / 60 / 24 / 30)
let info = "\(thirtyDayPeriodsSinceEpoch)-\(client.address)"
guard let infoEncoded = info.data(using: .utf8) else {
throw MessageV2Error.invalidData
}

let senderHmac = try Crypto.generateHmacSignature(secret: keyMaterial, info: infoEncoded, message: headerBytes)

let decoded = try codec.decode(content: encodedContent, client: client)
let calculatedShouldPush = try codec.shouldPush(content: decoded)

Expand Down
2 changes: 1 addition & 1 deletion Sources/XMTPiOS/Messages/PagingInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public struct Pagination {
public var before: Date?
public var after: Date?
public var direction: PagingInfoSortDirection?

public init(limit: Int? = nil, before: Date? = nil, after: Date? = nil, direction: PagingInfoSortDirection? = .descending) {
self.limit = limit
self.before = before
Expand Down
5 changes: 2 additions & 3 deletions Sources/XMTPiOS/Messages/PrivateKeyBundle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import Foundation


public typealias PrivateKeyBundle = Xmtp_MessageContents_PrivateKeyBundle

enum PrivateKeyBundleError: Error {
Expand All @@ -23,9 +22,9 @@ extension PrivateKeyBundle {
func encrypted(with key: SigningKey, preEnableIdentityCallback: PreEventCallback? = nil) async throws -> EncryptedPrivateKeyBundle {
let bundleBytes = try serializedData()
let walletPreKey = try Crypto.secureRandomBytes(count: 32)

try await preEnableIdentityCallback?()

let signature = try await key.sign(message: Signature.enableIdentityText(key: walletPreKey))
let cipherText = try Crypto.encrypt(signature.rawDataWithNormalizedRecovery, bundleBytes)

Expand Down
1 change: 0 additions & 1 deletion Sources/XMTPiOS/Messages/PrivateKeyBundleV1.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import CryptoKit
import Foundation
import LibXMTP


public typealias PrivateKeyBundleV1 = Xmtp_MessageContents_PrivateKeyBundleV1

extension PrivateKeyBundleV1 {
Expand Down
2 changes: 0 additions & 2 deletions Sources/XMTPiOS/Messages/PublicKeyBundle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
// Created by Pat Nakajima on 11/23/22.
//



typealias PublicKeyBundle = Xmtp_MessageContents_PublicKeyBundle

extension PublicKeyBundle {
Expand Down
1 change: 0 additions & 1 deletion Sources/XMTPiOS/Messages/SealedInvitationV1.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import Foundation


typealias SealedInvitationV1 = Xmtp_MessageContents_SealedInvitationV1

extension SealedInvitationV1 {
Expand Down
1 change: 0 additions & 1 deletion Sources/XMTPiOS/Messages/SignedContent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import Foundation


typealias SignedContent = Xmtp_MessageContents_SignedContent

extension SignedContent {
Expand Down
1 change: 0 additions & 1 deletion Sources/XMTPiOS/Messages/SignedPrivateKey.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import Foundation


public typealias SignedPrivateKey = Xmtp_MessageContents_SignedPrivateKey

extension SignedPrivateKey {
Expand Down
2 changes: 0 additions & 2 deletions Sources/XMTPiOS/Messages/SignedPublicKeyBundle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
// Created by Pat Nakajima on 11/23/22.
//



public typealias SignedPublicKeyBundle = Xmtp_MessageContents_SignedPublicKeyBundle

extension SignedPublicKeyBundle {
Expand Down
1 change: 0 additions & 1 deletion Sources/XMTPiOS/Messages/Token.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,4 @@

import Foundation


typealias Token = Xmtp_MessageApi_V1_Token
4 changes: 1 addition & 3 deletions Sources/XMTPiOS/Messages/Topic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
// Created by Pat Nakajima on 11/17/22.
//



public enum Topic {
case userPrivateStoreKeyBundle(String),
contact(String),
Expand Down Expand Up @@ -39,7 +37,7 @@ public enum Topic {
private func wrap(_ value: String) -> String {
"/xmtp/0/\(value)/proto"
}

static func isValidTopic(topic: String) -> Bool {
return topic.allSatisfy(\.isASCII)
}
Expand Down
1 change: 0 additions & 1 deletion Sources/XMTPiOS/Messages/UnsignedPublicKey.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import Foundation


typealias UnsignedPublicKey = Xmtp_MessageContents_UnsignedPublicKey

extension UnsignedPublicKey {
Expand Down
Loading

0 comments on commit 55dabfd

Please sign in to comment.