Skip to content

Commit

Permalink
Merge pull request #1255 from WalletConnect/hotfix/notify-db-replace-…
Browse files Browse the repository at this point in the history
…method

[Notify] Handle empty subscriptions response
  • Loading branch information
flypaper0 authored Jan 11, 2024
2 parents a275924 + 47ae69c commit 3aa214e
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 9 deletions.
7 changes: 3 additions & 4 deletions Example/IntegrationTests/Push/NotifyTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -193,13 +193,12 @@ final class NotifyTests: XCTestCase {

await fulfillment(of: [created], timeout: InputConfig.defaultTimeout)

let updateScope = Set([subscription.scope.keys.first!])
try await walletNotifyClientA.update(topic: subscription.topic, scope: updateScope)
try await walletNotifyClientA.update(topic: subscription.topic, scope: [])

await fulfillment(of: [updated], timeout: InputConfig.defaultTimeout)

let updatedScope = Set(subscription.scope.filter { $0.value.enabled == true }.keys)
XCTAssertEqual(updatedScope, updateScope)
let updatedScope = subscription.scope.filter { $0.value.enabled == true }
XCTAssertTrue(updatedScope.isEmpty)

try await walletNotifyClientA.deleteSubscription(topic: subscription.topic)
}
Expand Down
8 changes: 6 additions & 2 deletions Sources/Database/SQLiteQuery.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Foundation

public struct SqliteQuery {

public static func replace(table: String, rows: [SqliteRow]) throws -> String {
public static func replace(table: String, rows: [SqliteRow]) -> String? {
var values: [String] = []

for row in rows {
Expand All @@ -12,7 +12,7 @@ public struct SqliteQuery {
}

guard let first = rows.first else {
throw Errors.rowsNotFound
return nil
}

let formattedArguments = first.encode().values
Expand All @@ -37,6 +37,10 @@ public struct SqliteQuery {
return "SELECT * FROM \(table) WHERE \(argument) = '\(value)';"
}

public static func delete(table: String) -> String {
return "DELETE FROM \(table);"
}

public static func delete(table: String, where argument: String, equals value: String) -> String {
return "DELETE FROM \(table) WHERE \(argument) = '\(value)';"
}
Expand Down
12 changes: 10 additions & 2 deletions Sources/WalletConnectNotify/Client/Wallet/NotifyDatabase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,19 @@ final class NotifyDatabase {
}

func save(subscriptions: [NotifySubscription]) throws {
let sql = try SqliteQuery.replace(table: Table.subscriptions, rows: subscriptions)
guard let sql = SqliteQuery.replace(table: Table.subscriptions, rows: subscriptions) else { return }
try execute(sql: sql)
try onSubscriptionsUpdate?()
}

func replace(subscriptions: [NotifySubscription]) throws {
try execute(sql: SqliteQuery.delete(table: Table.subscriptions))
if let sql = SqliteQuery.replace(table: Table.subscriptions, rows: subscriptions) {
try execute(sql: sql)
}
try onSubscriptionsUpdate?()
}

func getSubscription(topic: String) -> NotifySubscription? {
let sql = SqliteQuery.select(table: Table.subscriptions, where: "topic", equals: topic)
let subscriptions: [NotifySubscription]? = try? query(sql: sql)
Expand Down Expand Up @@ -95,7 +103,7 @@ final class NotifyDatabase {
}

func save(messages: [NotifyMessageRecord]) throws {
let sql = try SqliteQuery.replace(table: Table.messages, rows: messages)
guard let sql = SqliteQuery.replace(table: Table.messages, rows: messages) else { return }
try execute(sql: sql)
try onMessagesUpdate?()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ final class NotifyStorage: NotifyStoring {
}

func replaceAllSubscriptions(_ subscriptions: [NotifySubscription]) throws {
try database.save(subscriptions: subscriptions)
try database.replace(subscriptions: subscriptions)
}

func deleteSubscription(topic: String) throws {
Expand Down

0 comments on commit 3aa214e

Please sign in to comment.