Skip to content

Commit

Permalink
Merge pull request #339 from WalletConnect/#336-remove-messages-dupli…
Browse files Browse the repository at this point in the history
…cates

[Chat] #336 remove messages duplicates
  • Loading branch information
llbartekll authored Jul 15, 2022
2 parents 310d70f + 71bbeb9 commit 6e0b397
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ struct AccountNameResolver {
private static var staticMap: [String: String] = [
"swift.eth": "eip155:1:0xab16a96d359ec26a11e2c2b3d8f8b8942d5bfcdb",
"kotlin.eth": "eip155:2:0xab16a96d359ec26a11e2c2b3d8f8b8942d5bfcdb",
"js.eth": "eip155:3:0xab16a96d359ec26a11e2c2b3d8f8b8942d5bfcdb",
"js.eth": "eip155:3:0xab16a96d359ec26a11e2c2b3d8f8b8942d5bfcdb"
]

static func resolveName(_ account: Account) -> String {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ struct ThreadViewModel: Identifiable {
}

var title: String {
return AccountNameResolver.resolveName(thread.peerAccount)
return AccountNameResolver.resolveName(thread.peerAccount)
}

var subtitle: String {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ struct ImportView: View {
.padding(.top, 24.0)

TextFieldView(title: "Username", placeholder: "username.eth or 0x0…", input: $presenter.input)

Spacer()

BrandButton(title: "Ok, done", action: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ struct WelcomeView: View {
@EnvironmentObject var presenter: WelcomePresenter

var body: some View {
GeometryReader { geometry in
GeometryReader { _ in
ZStack {
Image("LaunchScreen")
.resizable()
Expand Down
16 changes: 14 additions & 2 deletions Sources/Chat/NetworkingInteractor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ protocol NetworkInteracting {
var socketConnectionStatusPublisher: AnyPublisher<SocketConnectionStatus, Never> { get }
var requestPublisher: AnyPublisher<RequestSubscriptionPayload, Never> {get}
var responsePublisher: AnyPublisher<ChatResponse, Never> {get}
func respondSuccess(payload: RequestSubscriptionPayload) async throws
func subscribe(topic: String) async throws
func request(_ request: JSONRPCRequest<ChatRequestParams>, topic: String, envelopeType: Envelope.EnvelopeType) async throws
func respond(topic: String, response: JsonRpcResult, tag: Int) async throws
Expand Down Expand Up @@ -59,7 +60,13 @@ class NetworkingInteractor: NetworkInteracting {
try await relayClient.publish(topic: topic, payload: message, tag: request.params.tag)
}

func respondSuccess(payload: RequestSubscriptionPayload) async throws {
let response = JSONRPCResponse<AnyCodable>(id: payload.request.id, result: AnyCodable(true))
try await respond(topic: payload.topic, response: JsonRpcResult.response(response), tag: payload.request.params.responseTag)
}

func respond(topic: String, response: JsonRpcResult, tag: Int) async throws {
_ = try jsonRpcHistory.resolve(response: response)
let message = try serializer.serialize(topic: topic, encodable: response.value)
try await relayClient.publish(topic: topic, payload: message, tag: tag, prompt: false)
}
Expand All @@ -81,8 +88,13 @@ class NetworkingInteractor: NetworkInteracting {
}

private func handleChatRequest(topic: String, request: JSONRPCRequest<ChatRequestParams>) {
let payload = RequestSubscriptionPayload(topic: topic, request: request)
requestPublisherSubject.send(payload)
do {
try jsonRpcHistory.set(topic: topic, request: request)
let payload = RequestSubscriptionPayload(topic: topic, request: request)
requestPublisherSubject.send(payload)
} catch {
logger.debug(error)
}
}

private func handleJsonRpcResponse(response: JSONRPCResponse<AnyCodable>) {
Expand Down
5 changes: 3 additions & 2 deletions Sources/Chat/ProtocolServices/Common/MessagingService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,16 @@ class MessagingService {
switch subscriptionPayload.request.params {
case .message(var message):
message.topic = subscriptionPayload.topic
handleMessage(message)
handleMessage(message, subscriptionPayload)
default:
return
}
}.store(in: &publishers)
}

private func handleMessage(_ message: Message) {
private func handleMessage(_ message: Message, _ payload: RequestSubscriptionPayload) {
Task(priority: .background) {
try await networkingInteractor.respondSuccess(payload: payload)
await messagesStore.add(message)
logger.debug("Received message")
onMessage?(message)
Expand Down
5 changes: 5 additions & 0 deletions Tests/ChatTests/Mocks/NetworkingInteractorMock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import WalletConnectUtils
import WalletConnectRelay

class NetworkingInteractorMock: NetworkInteracting {

var socketConnectionStatusPublisher: AnyPublisher<SocketConnectionStatus, Never> {
socketConnectionStatusPublisherSubject.eraseToAnyPublisher()
}
Expand Down Expand Up @@ -33,6 +34,10 @@ class NetworkingInteractorMock: NetworkInteracting {

}

func respondSuccess(payload: RequestSubscriptionPayload) async throws {

}

private(set) var subscriptions: [String] = []

func subscribe(topic: String) async throws {
Expand Down

0 comments on commit 6e0b397

Please sign in to comment.