Skip to content

Commit bc0a2e3

Browse files
committed
add delete accountInfo functions
1 parent b246c9f commit bc0a2e3

File tree

4 files changed

+50
-4
lines changed

4 files changed

+50
-4
lines changed

.swiftformat

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
--maxwidth 120
2+
--disable enumNamespaces
3+
--swiftversion 5.5
4+
--trimwhitespace always
5+

Sources/MatrixSQLiteStore/AccountInfo.swift

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,27 @@ import MatrixClient
1111
import MatrixCore
1212

1313
public struct MatrixSQLAccountInfo: MatrixStoreAccountInfo {
14+
public init(
15+
name: String,
16+
displayName: String? = nil,
17+
mxID: MatrixFullUserIdentifier,
18+
homeServer: MatrixHomeserver,
19+
accessToken: String? = nil
20+
) {
21+
self.name = name
22+
self.displayName = displayName
23+
self.mxID = mxID
24+
self.homeServer = homeServer
25+
self.accessToken = accessToken
26+
}
27+
28+
public typealias AccountIdentifier = MatrixFullUserIdentifier
29+
1430
public var name: String
1531

1632
public var displayName: String?
1733

18-
public var mxID: MatrixUserIdentifier
34+
public var mxID: MatrixFullUserIdentifier
1935

2036
public var homeServer: MatrixHomeserver
2137

Sources/MatrixSQLiteStore/MatrixSQLiteStore.swift

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,19 @@ public struct MatrixSQLiteStore {
99

1010
public init(_ dbWriter: DatabaseWriter) throws {
1111
self.dbWriter = dbWriter
12+
try migrate()
13+
}
14+
15+
public init(path: String) throws {
16+
dbWriter = try DatabasePool(path: path)
17+
try migrate()
18+
}
19+
20+
public static func inMemory() -> Self {
21+
try! MatrixSQLiteStore(DatabaseQueue())
22+
}
23+
24+
private func migrate() throws {
1225
try migrator.migrate(dbWriter)
1326
}
1427

@@ -43,9 +56,9 @@ extension MatrixSQLiteStore: MatrixStore {
4356
}
4457
}
4558

46-
public func getAccountInfo(accountID: MatrixUserIdentifier) async throws -> MatrixSQLAccountInfo {
59+
public func getAccountInfo(accountID: MatrixFullUserIdentifier) async throws -> MatrixSQLAccountInfo {
4760
let account = try await dbWriter.read { db in
48-
try MatrixSQLAccountInfo.fetchOne(db, key: accountID.FQMXID!)
61+
try MatrixSQLAccountInfo.fetchOne(db, key: accountID.FQMXID)
4962
}
5063

5164
guard var account = account else {
@@ -68,6 +81,13 @@ extension MatrixSQLiteStore: MatrixStore {
6881
return account
6982
}
7083
}
84+
85+
public func deleteAccountInfo(account: MatrixSQLAccountInfo) async throws {
86+
_ = try await dbWriter.write { db in
87+
try account.delete(db)
88+
}
89+
try account.deleteFromKeychain()
90+
}
7191
}
7292

7393
public extension MatrixSQLiteStore {

Tests/MatrixSQLiteStoreTests/MatrixAccountInfoTests.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,12 @@ class AccountInfoTests: XCTestCase {
1616
let store = try MatrixSQLiteStore(dbQueue)
1717

1818
let id = MatrixUserIdentifier(rawValue: "@test:example.com")!
19-
let data = MatrixSQLAccountInfo(name: "test", mxID: id, homeServer: MatrixHomeserver(string: "https://example.com")!, accessToken: "secret")
19+
let data = MatrixSQLAccountInfo(
20+
name: "test",
21+
mxID: id,
22+
homeServer: MatrixHomeserver(string: "https://example.com")!,
23+
accessToken: "secret"
24+
)
2025

2126
defer {
2227
try! data.deleteFromKeychain()

0 commit comments

Comments
 (0)