-
Notifications
You must be signed in to change notification settings - Fork 180
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #944 from WalletConnect/subscription-auto-update
[Push] Subscriptions auto update
- Loading branch information
Showing
11 changed files
with
199 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 5 additions & 1 deletion
6
...alletConnectPush/Client/Wallet/ProtocolEngine/wc_notifyUpdate/NotifyUpdateRequester.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
Sources/WalletConnectPush/Client/Wallet/SubscriptionsAutoUpdater.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
|
||
import Foundation | ||
|
||
class SubscriptionsAutoUpdater { | ||
private let notifyUpdateRequester: NotifyUpdateRequesting | ||
private let logger: ConsoleLogging | ||
private let pushStorage: PushStoring | ||
|
||
init(notifyUpdateRequester: NotifyUpdateRequesting, | ||
logger: ConsoleLogging, | ||
pushStorage: PushStoring) { | ||
self.notifyUpdateRequester = notifyUpdateRequester | ||
self.logger = logger | ||
self.pushStorage = pushStorage | ||
updateSubscriptionsIfNeeded() | ||
} | ||
|
||
private func updateSubscriptionsIfNeeded() { | ||
for subscription in pushStorage.getSubscriptions() { | ||
if shouldUpdate(subscription: subscription) { | ||
let scope = Set(subscription.scope.filter{ $0.value.enabled == true }.keys) | ||
let topic = subscription.topic | ||
Task { | ||
do { | ||
try await notifyUpdateRequester.update(topic: topic, scope: scope) | ||
} catch { | ||
logger.error("Failed to update subscription for topic: \(topic)") | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
private func shouldUpdate(subscription: PushSubscription) -> Bool { | ||
let currentDate = Date() | ||
let calendar = Calendar.current | ||
let expiryDate = subscription.expiry | ||
let dateComponents = calendar.dateComponents([.day], from: currentDate, to: expiryDate) | ||
if let numberOfDays = dateComponents.day, | ||
numberOfDays < 14 { | ||
return true | ||
} else { | ||
return false | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
|
||
import Foundation | ||
@testable import WalletConnectPush | ||
|
||
|
||
class MockNotifyUpdateRequester: NotifyUpdateRequesting { | ||
var updatedTopics: [String] = [] | ||
var completionHandler: (() -> Void)? | ||
|
||
func update(topic: String, scope: Set<String>) async throws { | ||
updatedTopics.append(topic) | ||
completionHandler?() | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
|
||
import Foundation | ||
@testable import WalletConnectPush | ||
|
||
class MockPushStoring: PushStoring { | ||
var subscriptions: [PushSubscription] | ||
|
||
init(subscriptions: [PushSubscription]) { | ||
self.subscriptions = subscriptions | ||
} | ||
|
||
func getSubscriptions() -> [PushSubscription] { | ||
return subscriptions | ||
} | ||
|
||
func getSubscription(topic: String) -> PushSubscription? { | ||
return subscriptions.first { $0.topic == topic } | ||
} | ||
|
||
func setSubscription(_ subscription: PushSubscription) async throws { | ||
if let index = subscriptions.firstIndex(where: { $0.topic == subscription.topic }) { | ||
subscriptions[index] = subscription | ||
} else { | ||
subscriptions.append(subscription) | ||
} | ||
} | ||
|
||
func deleteSubscription(topic: String) async throws { | ||
subscriptions.removeAll(where: { $0.topic == topic }) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
|
||
import Foundation | ||
@testable import WalletConnectPush | ||
|
||
extension PushSubscription { | ||
static func stub(topic: String, expiry: Date) -> PushSubscription { | ||
let account = Account(chainIdentifier: "eip155:1", address: "0x15bca56b6e2728aec2532df9d436bd1600e86688")! | ||
let relay = RelayProtocolOptions.stub() | ||
let metadata = AppMetadata.stub() | ||
let symKey = "key1" | ||
|
||
return PushSubscription( | ||
topic: topic, | ||
account: account, | ||
relay: relay, | ||
metadata: metadata, | ||
scope: ["test": ScopeValue(description: "desc", enabled: true)], | ||
expiry: expiry, | ||
symKey: symKey | ||
) | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import Foundation | ||
import XCTest | ||
import TestingUtils | ||
@testable import WalletConnectPush | ||
|
||
class SubscriptionsAutoUpdaterTests: XCTestCase { | ||
var sut: SubscriptionsAutoUpdater! | ||
|
||
func testUpdateSubscriptionsIfNeeded() async { | ||
let subscriptions: [PushSubscription] = [ | ||
PushSubscription.stub(topic: "topic1", expiry: Date().addingTimeInterval(60 * 60 * 24 * 20)), | ||
PushSubscription.stub(topic: "topic2", expiry: Date().addingTimeInterval(60 * 60 * 24 * 10)), | ||
PushSubscription.stub(topic: "topic3", expiry: Date().addingTimeInterval(60 * 60 * 24 * 30)) | ||
] | ||
|
||
let expectation = expectation(description: "update") | ||
|
||
let notifyUpdateRequester = MockNotifyUpdateRequester() | ||
let logger = ConsoleLoggerMock() | ||
let pushStorage = MockPushStoring(subscriptions: subscriptions) | ||
|
||
notifyUpdateRequester.completionHandler = { | ||
if notifyUpdateRequester.updatedTopics.contains("topic2") { | ||
expectation.fulfill() | ||
} | ||
} | ||
|
||
sut = SubscriptionsAutoUpdater(notifyUpdateRequester: notifyUpdateRequester, | ||
logger: logger, | ||
pushStorage: pushStorage) | ||
|
||
await waitForExpectations(timeout: 1, handler: nil) | ||
|
||
XCTAssertEqual(notifyUpdateRequester.updatedTopics, ["topic2"]) | ||
} | ||
} |