Skip to content

Commit

Permalink
Merge pull request #1220 from WalletConnect/feature/notify-subscripti…
Browse files Browse the repository at this point in the history
…ons-publisher

[Notify] Client publishers updated
  • Loading branch information
flypaper0 authored Nov 7, 2023
2 parents 43baa1a + 2ecd85d commit 5348058
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 11 deletions.
5 changes: 3 additions & 2 deletions Example/IntegrationTests/Push/NotifyTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,9 @@ final class NotifyTests: XCTestCase {
}
}.store(in: &publishers)

walletNotifyClientA.notifyMessagePublisher
.sink { [unowned self] notifyMessageRecord in
walletNotifyClientA.messagesPublisher
.sink { [unowned self] messages in
guard let notifyMessageRecord = messages.first else { return }
XCTAssertEqual(notifyMessageRecord.message, notifyMessage)

Task(priority: .high) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Combine
final class NotificationsInteractor {

var subscriptionsPublisher: AnyPublisher<[NotifySubscription], Never> {
return Notify.instance.subscriptionsPublisher
return Notify.instance.subscriptionsPublisher(account: importAccount.account)
}

private let importAccount: ImportAccount
Expand Down
8 changes: 6 additions & 2 deletions Sources/WalletConnectNotify/Client/Wallet/NotifyClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ public class NotifyClient {
return notifyStorage.subscriptionsPublisher
}

public var notifyMessagePublisher: AnyPublisher<NotifyMessageRecord, Never> {
return notifyMessageSubscriber.notifyMessagePublisher
public var messagesPublisher: AnyPublisher<[NotifyMessageRecord], Never> {
return notifyStorage.messagesPublisher
}

public var logsPublisher: AnyPublisher<Log, Never> {
Expand Down Expand Up @@ -125,6 +125,10 @@ public class NotifyClient {
return identityService.isIdentityRegistered(account: account)
}

public func subscriptionsPublisher(account: Account) -> AnyPublisher<[NotifySubscription], Never> {
return notifyStorage.subscriptionsPublisher(account: account)
}

public func messagesPublisher(topic: String) -> AnyPublisher<[NotifyMessageRecord], Never> {
return notifyStorage.messagesPublisher(topic: topic)
}
Expand Down
10 changes: 10 additions & 0 deletions Sources/WalletConnectNotify/Client/Wallet/NotifyStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ final class NotifyStorage: NotifyStoring {
return subscriptionsSubject.eraseToAnyPublisher()
}

var messagesPublisher: AnyPublisher<[NotifyMessageRecord], Never> {
return messagesSubject.eraseToAnyPublisher()
}

init(database: NotifyDatabase, accountProvider: NotifyAccountProvider) {
self.database = database
self.accountProvider = accountProvider
Expand Down Expand Up @@ -89,6 +93,12 @@ final class NotifyStorage: NotifyStoring {
updateSubscriptionSubject.send(updated)
}

func subscriptionsPublisher(account: Account) -> AnyPublisher<[NotifySubscription], Never> {
return subscriptionsSubject
.map { $0.filter { $0.account == account } }
.eraseToAnyPublisher()
}

// MARK: Messages

func messagesPublisher(topic: String) -> AnyPublisher<[NotifyMessageRecord], Never> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@ class NotifyMessageSubscriber {
private let notifyStorage: NotifyStorage
private let crypto: CryptoProvider
private let logger: ConsoleLogging
private let notifyMessagePublisherSubject = PassthroughSubject<NotifyMessageRecord, Never>()

public var notifyMessagePublisher: AnyPublisher<NotifyMessageRecord, Never> {
notifyMessagePublisherSubject.eraseToAnyPublisher()
}

init(keyserver: URL, networkingInteractor: NetworkInteracting, identityClient: IdentityClient, notifyStorage: NotifyStorage, crypto: CryptoProvider, logger: ConsoleLogging) {
self.keyserver = keyserver
Expand All @@ -39,7 +34,6 @@ class NotifyMessageSubscriber {
let dappPubKey = try DIDKey(did: claims.iss)
let record = NotifyMessageRecord(id: payload.id.string, topic: payload.topic, message: messagePayload.message, publishedAt: payload.publishedAt)
try notifyStorage.setMessage(record)
notifyMessagePublisherSubject.send(record)

let receiptPayload = NotifyMessageReceiptPayload(
account: messagePayload.account,
Expand Down

0 comments on commit 5348058

Please sign in to comment.