Skip to content

Commit 8eb23f5

Browse files
feat: add function to delete database (#264)
* add function for deleting database * Fix Linter Errors * Add Linting to Build Phase + Fix Linting Errors Also added swiftlint.yml to IOSExample project root in order to pick up rule modifications. --------- Co-authored-by: Ethan Mateja <zombieobject@users.noreply.github.com>
1 parent 880ec58 commit 8eb23f5

35 files changed

+156
-83
lines changed

.swiftlint.yml

+10
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,16 @@ disabled_rules: # rule identifiers turned on by default to exclude from running
1111
- nesting
1212
- type_body_length
1313
- file_length
14+
- todo
15+
- control_statement
16+
- switch_case_alignment
17+
- empty_enum_arguments
18+
- for_where
19+
- private_over_fileprivate
20+
- redundant_string_enum_value
21+
- redundant_optional_initialization
22+
- operator_whitespace
23+
- comma
1424

1525
opt_in_rules:
1626
- force_unwrapping

Sources/XMTPTestHelpers/TestHelpers.swift

+1-3
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ public class FakeApiClient: ApiClient {
197197
}
198198
}
199199
}
200-
200+
201201
if let direction = pagination?.direction {
202202
switch direction {
203203
case .ascending:
@@ -244,7 +244,6 @@ public class FakeApiClient: ApiClient {
244244
var queryResponse = XMTPiOS.BatchQueryResponse()
245245
queryResponse.responses = responses
246246
return queryResponse
247-
248247
}
249248

250249
public func query(request: XMTPiOS.QueryRequest) async throws -> XMTPiOS.QueryResponse {
@@ -254,7 +253,6 @@ public class FakeApiClient: ApiClient {
254253
public func publish(request: XMTPiOS.PublishRequest) async throws {
255254
abort() // Not supported on Fake
256255
}
257-
258256
}
259257

260258
@available(iOS 15, *)

Sources/XMTPiOS/Client.swift

+21-7
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ public final class Client {
8989
let apiClient: ApiClient
9090
let v3Client: LibXMTP.FfiXmtpClient?
9191
public let libXMTPVersion: String = getVersionInfo()
92+
let dbPath: String = ""
9293

9394
/// Access ``Conversations`` for this Client.
9495
public lazy var conversations: Conversations = .init(client: self)
@@ -132,7 +133,7 @@ public final class Client {
132133
) async throws -> FfiXmtpClient? {
133134
if options?.mlsAlpha == true, options?.api.env.supportsMLS == true {
134135
let dbURL = options?.mlsDbPath ?? URL.documentsDirectory.appendingPathComponent("xmtp-\(options?.api.env.rawValue ?? "")-\(address).db3").path
135-
136+
136137
var encryptionKey = options?.mlsEncryptionKey
137138
if (encryptionKey == nil) {
138139
let preferences = UserDefaults.standard
@@ -146,7 +147,7 @@ public final class Client {
146147
encryptionKey = preferences.data(forKey: key)
147148
}
148149
}
149-
150+
150151
let v3Client = try await LibXMTP.createClient(
151152
logger: XMTPLogger(),
152153
host: (options?.api.env ?? .local).url,
@@ -168,7 +169,7 @@ public final class Client {
168169
} else {
169170
try await v3Client.registerIdentity(recoverableWalletSignature: nil)
170171
}
171-
172+
172173
print("LibXMTP \(getVersionInfo())")
173174

174175
return v3Client
@@ -250,16 +251,15 @@ public final class Client {
250251

251252
return try await v3Client.canMessage(accountAddresses: [address]) == [true]
252253
}
253-
254+
254255
public func canMessageV3(addresses: [String]) async throws -> Bool {
255256
guard let v3Client else {
256257
return false
257258
}
258-
259+
259260
return try await !v3Client.canMessage(accountAddresses: addresses).contains(false)
260261
}
261262

262-
263263
public static func from(bundle: PrivateKeyBundle, options: ClientOptions? = nil) async throws -> Client {
264264
return try await from(v1Bundle: bundle.v1, options: options)
265265
}
@@ -280,7 +280,7 @@ public final class Client {
280280
privateKeyBundleV1: v1Bundle,
281281
signingKey: nil
282282
)
283-
283+
284284
let client = try await LibXMTP.createV2Client(host: options.api.env.url, isSecure: options.api.env.isSecure)
285285
let apiClient = try GRPCApiClient(
286286
environment: options.api.env,
@@ -451,6 +451,20 @@ public final class Client {
451451
return subscribe(topics: topics.map(\.description))
452452
}
453453

454+
public func deleteLocalDatabase() {
455+
let fm = FileManager.default
456+
let url = URL(string: dbPath)
457+
if (url != nil) {
458+
do {
459+
// swiftlint: disable force_unwrapping
460+
try fm.removeItem(at: url!)
461+
// swiftlint: enable force_unwrapping
462+
} catch {
463+
print("Error deleting file: \(dbPath)")
464+
}
465+
}
466+
}
467+
454468
func getUserContact(peerAddress: String) async throws -> ContactBundle? {
455469
let peerAddress = EthereumAddress(peerAddress).toChecksumAddress()
456470
return try await contacts.find(peerAddress)

Sources/XMTPiOS/Codecs/AttachmentCodec.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,11 @@ public struct AttachmentCodec: ContentCodec {
5555

5656
return attachment
5757
}
58-
58+
5959
public func fallback(content: Attachment) throws -> String? {
6060
return "Can’t display “\(content.filename)”. This app doesn’t support attachments."
6161
}
62-
62+
6363
public func shouldPush(content: Attachment) throws -> Bool {
6464
return true
6565
}

Sources/XMTPiOS/Codecs/Composite.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ struct CompositeCodec: ContentCodec {
4141
let decodedComposite = fromComposite(composite: composite)
4242
return decodedComposite
4343
}
44-
44+
4545
public func fallback(content: DecodedComposite) throws -> String? {
4646
return nil
4747
}
48-
48+
4949
public func shouldPush(content: DecodedComposite) throws -> Bool {
5050
return false
5151
}

Sources/XMTPiOS/Codecs/GroupMembershipChanged.swift

-1
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,3 @@ public struct GroupMembershipChangedCodec: ContentCodec {
4040
false
4141
}
4242
}
43-

Sources/XMTPiOS/Codecs/ReactionCodec.swift

+6-6
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,14 @@
77

88
import Foundation
99

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

1312
public struct Reaction: Codable {
1413
public var reference: String
1514
public var action: ReactionAction
1615
public var content: String
1716
public var schema: ReactionSchema
18-
17+
1918
public init(reference: String, action: ReactionAction, content: String, schema: ReactionSchema) {
2019
self.reference = reference
2120
self.action = action
@@ -26,7 +25,7 @@ public struct Reaction: Codable {
2625

2726
public enum ReactionAction: String, Codable {
2827
case added, removed, unknown
29-
28+
3029
public init(rawValue: String) {
3130
switch rawValue {
3231
case "added":
@@ -41,7 +40,7 @@ public enum ReactionAction: String, Codable {
4140

4241
public enum ReactionSchema: String, Codable {
4342
case unicode, shortcode, custom, unknown
44-
43+
4544
public init(rawValue: String) {
4645
switch rawValue {
4746
case "unicode":
@@ -85,8 +84,9 @@ public struct ReactionCodec: ContentCodec {
8584
content: String(data: content.content, encoding: .utf8) ?? "",
8685
schema: ReactionSchema(rawValue: content.parameters["schema"] ?? "")
8786
)
87+
// swiftlint:enable no_optional_try
8888
}
89-
89+
9090
public func fallback(content: Reaction) throws -> String? {
9191
switch content.action {
9292
case .added:
@@ -97,7 +97,7 @@ public struct ReactionCodec: ContentCodec {
9797
return nil
9898
}
9999
}
100-
100+
101101
public func shouldPush(content: Reaction) throws -> Bool {
102102
switch content.action {
103103
case .added:

Sources/XMTPiOS/Codecs/ReadReceiptCodec.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ public struct ReadReceiptCodec: ContentCodec {
3232
public func decode(content: EncodedContent, client _: Client) throws -> ReadReceipt {
3333
return ReadReceipt()
3434
}
35-
35+
3636
public func fallback(content: ReadReceipt) throws -> String? {
3737
return nil
3838
}
39-
39+
4040
public func shouldPush(content: ReadReceipt) throws -> Bool {
4141
return false
4242
}

Sources/XMTPiOS/Codecs/RemoteAttachmentCodec.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ public struct RemoteAttachment {
149149
}
150150

151151
public struct RemoteAttachmentCodec: ContentCodec {
152-
152+
153153
public typealias T = RemoteAttachment
154154

155155
public init() {}
@@ -207,7 +207,7 @@ public struct RemoteAttachmentCodec: ContentCodec {
207207

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

224224
return Data(parameterData)
225225
}
226-
226+
227227
public func shouldPush(content: RemoteAttachment) throws -> Bool {
228228
return true
229229
}

Sources/XMTPiOS/Codecs/ReplyCodec.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,11 @@ public struct ReplyCodec: ContentCodec {
6262
throw CodecError.invalidContent
6363
}
6464
}
65-
65+
6666
public func fallback(content: Reply) throws -> String? {
6767
return "Replied with “\(content.content)” to an earlier message"
6868
}
69-
69+
7070
public func shouldPush(content: Reply) throws -> Bool {
7171
return true
7272
}

Sources/XMTPiOS/Codecs/TextCodec.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ enum TextCodecError: Error {
1414
}
1515

1616
public struct TextCodec: ContentCodec {
17-
17+
1818
public typealias T = String
1919

2020
public init() { }
@@ -42,11 +42,11 @@ public struct TextCodec: ContentCodec {
4242
throw TextCodecError.unknownDecodingError
4343
}
4444
}
45-
45+
4646
public func fallback(content: String) throws -> String? {
4747
return nil
4848
}
49-
49+
5050
public func shouldPush(content: String) throws -> Bool {
5151
return true
5252
}

Sources/XMTPiOS/Conversation.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public enum Conversation: Sendable {
8989
return group.peerAddresses.joined(separator: ",")
9090
}
9191
}
92-
92+
9393
public var peerAddresses: [String] {
9494
switch self {
9595
case let .v1(conversationV1):
@@ -100,7 +100,7 @@ public enum Conversation: Sendable {
100100
return group.peerAddresses
101101
}
102102
}
103-
103+
104104
public var keyMaterial: Data? {
105105
switch self {
106106
case let .v1(conversationV1):
@@ -182,7 +182,7 @@ public enum Conversation: Sendable {
182182
throw GroupError.notSupportedByGroups
183183
}
184184
}
185-
185+
186186
public func prepareMessage(encodedContent: EncodedContent, options: SendOptions? = nil) async throws -> PreparedMessage {
187187
switch self {
188188
case let .v1(conversationV1):

Sources/XMTPiOS/ConversationV2.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -107,19 +107,19 @@ public struct ConversationV2 {
107107
}
108108

109109
var encoded = try encode(codec: codec, content: content)
110-
110+
111111
func fallback<Codec: ContentCodec>(codec: Codec, content: Any) throws -> String? {
112112
if let content = content as? Codec.T {
113113
return try codec.fallback(content: content)
114114
} else {
115115
throw CodecError.invalidContent
116116
}
117117
}
118-
118+
119119
if let fallback = try fallback(codec: codec, content: content) {
120120
encoded.fallback = fallback
121121
}
122-
122+
123123
if let compression = options?.compression {
124124
encoded = try encoded.compress(compression)
125125
}

0 commit comments

Comments
 (0)