Skip to content

Commit

Permalink
fetchMessageHistory
Browse files Browse the repository at this point in the history
  • Loading branch information
flypaper0 committed Jun 13, 2023
1 parent 554661a commit 91d93ba
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ let package = Package(
path: "Sources/WalletConnectSign"),
.target(
name: "WalletConnectChat",
dependencies: ["WalletConnectIdentity", "WalletConnectSync"],
dependencies: ["WalletConnectIdentity", "WalletConnectSync", "WalletConnectHistory"],
path: "Sources/Chat"),
.target(
name: "Auth",
Expand Down
7 changes: 7 additions & 0 deletions Sources/Chat/ChatClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Combine
public class ChatClient {
private var publishers = [AnyCancellable]()
private let identityClient: IdentityClient
private let historyClient: HistoryClient
private let messagingService: MessagingService
private let resubscriptionService: ResubscriptionService
private let invitationHandlingService: InvitationHandlingService
Expand Down Expand Up @@ -58,6 +59,7 @@ public class ChatClient {
// MARK: - Initialization

init(identityClient: IdentityClient,
historyClient: HistoryClient,
messagingService: MessagingService,
resubscriptionService: ResubscriptionService,
invitationHandlingService: InvitationHandlingService,
Expand All @@ -69,6 +71,7 @@ public class ChatClient {
socketConnectionStatusPublisher: AnyPublisher<SocketConnectionStatus, Never>
) {
self.identityClient = identityClient
self.historyClient = historyClient
self.messagingService = messagingService
self.resubscriptionService = resubscriptionService
self.invitationHandlingService = invitationHandlingService
Expand All @@ -93,6 +96,10 @@ public class ChatClient {
) async throws -> String {
let publicKey = try await identityClient.register(account: account, onSign: onSign)


let payload = RegisterPayload(tags: ["2002"], relayUrl: "wss://relay.walletconnect.com")
try await historyClient.registerTags(payload: payload, historyUrl: "https://history.walletconnect.com")

try await syncRegisterService.register(account: account, onSign: onSign)

guard !isPrivate else {
Expand Down
6 changes: 5 additions & 1 deletion Sources/Chat/ChatClientFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,17 @@ public struct ChatClientFactory {
storage: KeyValueStorage,
syncClient: SyncClient
) -> ChatClient {
let clientIdStorage = ClientIdStorage(keychain: keychain)
let historyClient = HistoryClient(clientIdStorage: clientIdStorage)
let kms = KeyManagementService(keychain: keychain)
let serializer = Serializer(kms: kms)
let messageStore = KeyedDatabase<[Message]>(storage: storage, identifier: ChatStorageIdentifiers.messages.rawValue)
let receivedInviteStore = KeyedDatabase<[ReceivedInvite]>(storage: storage, identifier: ChatStorageIdentifiers.receivedInvites.rawValue)
let threadStore: SyncStore<Thread> = SyncStoreFactory.create(name: ChatStorageIdentifiers.thread.rawValue, syncClient: syncClient, storage: storage)
let identityClient = IdentityClientFactory.create(keyserver: keyserverURL, keychain: keychain, logger: logger)
let inviteKeyDelegate = InviteKeyDelegate(networkingInteractor: networkingInteractor, kms: kms, identityClient: identityClient)
let sentInviteDelegate = SentInviteStoreDelegate(networkingInteractor: networkingInteractor, kms: kms)
let threadDelegate = ThreadStoreDelegate(networkingInteractor: networkingInteractor, kms: kms)
let threadDelegate = ThreadStoreDelegate(networkingInteractor: networkingInteractor, kms: kms, historyClient: historyClient, serializer: serializer)
let sentInviteStore: SyncStore<SentInvite> = SyncStoreFactory.create(name: ChatStorageIdentifiers.sentInvite.rawValue, syncClient: syncClient, storage: storage)
let inviteKeyStore: SyncStore<InviteKey> = SyncStoreFactory.create(name: ChatStorageIdentifiers.inviteKey.rawValue, syncClient: syncClient, storage: storage)
let receivedInviteStatusStore: SyncStore<ReceivedInviteStatus> = SyncStoreFactory.create(name: ChatStorageIdentifiers.receivedInviteStatus.rawValue, syncClient: syncClient, storage: storage)
Expand All @@ -47,6 +50,7 @@ public struct ChatClientFactory {

let client = ChatClient(
identityClient: identityClient,
historyClient: historyClient,
messagingService: messagingService,
resubscriptionService: resubscriptionService,
invitationHandlingService: invitationHandlingService,
Expand Down
1 change: 1 addition & 0 deletions Sources/Chat/ChatImports.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
@_exported import WalletConnectSigner
@_exported import WalletConnectIdentity
@_exported import WalletConnectSync
@_exported import WalletConnectHistory
#endif
2 changes: 1 addition & 1 deletion Sources/Chat/Storage/ChatStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ final class ChatStorage {

func initializeDelegates() async throws {
try await sentInviteStoreDelegate.onInitialization(sentInviteStore.getAll())
try await threadStoreDelegate.onInitialization(threadStore.getAll())
try await threadStoreDelegate.onInitialization(storage: self)
try await inviteKeyDelegate.onInitialization(inviteKeyStore.getAll())
try await receiviedInviteStatusDelegate.onInitialization()
}
Expand Down
49 changes: 45 additions & 4 deletions Sources/Chat/Storage/ThreadStoreDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,23 @@ final class ThreadStoreDelegate {

private let networkingInteractor: NetworkInteracting
private let kms: KeyManagementServiceProtocol
private let historyClient: HistoryClient
private let serializer: Serializing

init(networkingInteractor: NetworkInteracting, kms: KeyManagementServiceProtocol) {
init(networkingInteractor: NetworkInteracting, kms: KeyManagementServiceProtocol, historyClient: HistoryClient, serializer: Serializing) {
self.networkingInteractor = networkingInteractor
self.kms = kms
self.serializer = serializer
self.historyClient = historyClient
}

func onInitialization(_ threads: [Thread]) async throws {
let topics = threads.map { $0.topic }
try await networkingInteractor.batchSubscribe(topics: topics)
func onInitialization(storage: ChatStorage) async throws {
let threads = storage.getAllThreads()
try await networkingInteractor.batchSubscribe(topics: threads.map { $0.topic })

for thread in threads {
try await fetchMessageHistory(thread: thread, storage: storage)
}
}

func onUpdate(_ thread: Thread, storage: ChatStorage) {
Expand All @@ -24,10 +32,43 @@ final class ThreadStoreDelegate {
let symmetricKey = try SymmetricKey(hex: thread.symKey)
try kms.setSymmetricKey(symmetricKey, for: thread.topic)
try await networkingInteractor.subscribe(topic: thread.topic)

// Relay Client injection!
}
}

func onDelete(_ id: String) {

}
}

private extension ThreadStoreDelegate {

func fetchMessageHistory(thread: Thread, storage: ChatStorage) async throws {
let historyPayload = GetMessagesPayload(topic: thread.topic, originId: nil, messageCount: 200, direction: .forward)
let response = try await historyClient.getMessages(payload: historyPayload, historyUrl: "https://history.walletconnect.com")

for messagePayload in response.messages {
let (request, _, _): (RPCRequest, _, _) = try serializer.deserialize(
topic: thread.topic,
encodedEnvelope: messagePayload
)

let wrapper = try request.params!.get(MessagePayload.Wrapper.self)

let (messagePayload, messageClaims) = try MessagePayload.decodeAndVerify(from: wrapper)

let authorAccount = messagePayload.recipientAccount == thread.selfAccount
? thread.peerAccount
: thread.selfAccount

let message = Message(
topic: thread.topic,
message: messagePayload.message,
authorAccount: authorAccount,
timestamp: messageClaims.iat)

storage.set(message: message, account: thread.selfAccount)
}
}
}
7 changes: 7 additions & 0 deletions WalletConnectSwiftV2.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,17 @@ Pod::Spec.new do |spec|
ss.dependency 'WalletConnectSwiftV2/WalletConnectNetworking'
end

spec.subspec 'WalletConnectHistory' do |ss|
ss.source_files = 'Sources/WalletConnectHistory/**/*.{h,m,swift}'
ss.dependency 'WalletConnectSwiftV2/WalletConnectRelay'
ss.dependency 'WalletConnectSwiftV2/HTTPClient'
end

spec.subspec 'WalletConnectChat' do |ss|
ss.source_files = 'Sources/Chat/**/*.{h,m,swift}'
ss.dependency 'WalletConnectSwiftV2/WalletConnectSync'
ss.dependency 'WalletConnectSwiftV2/WalletConnectIdentity'
ss.dependency 'WalletConnectSwiftV2/WalletConnectHistory'
end

spec.subspec 'WalletConnectSync' do |ss|
Expand Down

0 comments on commit 91d93ba

Please sign in to comment.