Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add function to delete database #264

Merged
merged 3 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
28 changes: 21 additions & 7 deletions Sources/XMTPiOS/Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ public final class Client {
let apiClient: ApiClient
let v3Client: LibXMTP.FfiXmtpClient?
public let libXMTPVersion: String = getVersionInfo()
let dbPath: String = ""

/// Access ``Conversations`` for this Client.
public lazy var conversations: Conversations = .init(client: self)
Expand Down Expand Up @@ -132,7 +133,7 @@ public final class Client {
) async throws -> FfiXmtpClient? {
if options?.mlsAlpha == true, options?.api.env.supportsMLS == true {
let dbURL = options?.mlsDbPath ?? URL.documentsDirectory.appendingPathComponent("xmtp-\(options?.api.env.rawValue ?? "")-\(address).db3").path

var encryptionKey = options?.mlsEncryptionKey
if (encryptionKey == nil) {
let preferences = UserDefaults.standard
Expand All @@ -146,7 +147,7 @@ public final class Client {
encryptionKey = preferences.data(forKey: key)
}
}

let v3Client = try await LibXMTP.createClient(
logger: XMTPLogger(),
host: (options?.api.env ?? .local).url,
Expand All @@ -168,7 +169,7 @@ public final class Client {
} else {
try await v3Client.registerIdentity(recoverableWalletSignature: nil)
}

print("LibXMTP \(getVersionInfo())")

return v3Client
Expand Down Expand Up @@ -250,16 +251,15 @@ public final class Client {

return try await v3Client.canMessage(accountAddresses: [address]) == [true]
}

public func canMessageV3(addresses: [String]) async throws -> Bool {
guard let v3Client else {
return false
}

return try await !v3Client.canMessage(accountAddresses: addresses).contains(false)
}


public static func from(bundle: PrivateKeyBundle, options: ClientOptions? = nil) async throws -> Client {
return try await from(v1Bundle: bundle.v1, options: options)
}
Expand All @@ -280,7 +280,7 @@ public final class Client {
privateKeyBundleV1: v1Bundle,
signingKey: nil
)

let client = try await LibXMTP.createV2Client(host: options.api.env.url, isSecure: options.api.env.isSecure)
let apiClient = try GRPCApiClient(
environment: options.api.env,
Expand Down Expand Up @@ -451,6 +451,20 @@ public final class Client {
return subscribe(topics: topics.map(\.description))
}

public func deleteLocalDatabase() {
let fm = FileManager.default
let url = URL(string: dbPath)
if (url != nil) {
do {
// swiftlint: disable force_unwrapping
try fm.removeItem(at: url!)
// swiftlint: enable force_unwrapping
} catch {
print("Error deleting file: \(dbPath)")
}
}
}

func getUserContact(peerAddress: String) async throws -> ContactBundle? {
let peerAddress = EthereumAddress(peerAddress).toChecksumAddress()
return try await contacts.find(peerAddress)
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
Loading
Loading