Skip to content

Commit

Permalink
feat(agent): implement send messages
Browse files Browse the repository at this point in the history
Fixes ATL-2708
  • Loading branch information
goncalo-frade-iohk committed Dec 11, 2022
1 parent 8a1a065 commit 99989cc
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 4 deletions.
3 changes: 1 addition & 2 deletions Castor/Tests/PeerDIDCreationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,11 @@ final class PeerDIDCreationTests: XCTestCase {
let mypeerDID = DID(
schema: "did",
method: "peer",
methodId: "2.Ez6LScuRnbZAJaVthcL5RELq75EBK2sBmBxsSs98LKNeriHQJ.Vz6MkpeaW7DeptJW7pi2qNXTdCXeQV4EYpZnAouH1LrfHj6uf.SeyJ0IjoiZG0iLCJzIjoiaHR0cHM6Ly9rOHMtZGV2LmF0YWxhcHJpc20uaW8vcHJpc20tYWdlbnQvZGlkY29tbSIsInIiOltdLCJhIjpbImRpZGNvbW0vdjIiXX0"
methodId: "2.Ez6LSfuJvTtcmcFrjNYYSAuD32tMZWQUD2HYDfXrJqy3ui6MQ.Vz6MkoPyvuxAecSezqmL4ERE7eW2XPbiUEHRH9aqay6LA8Eqr.SeyJ0IjoiZG0iLCJzIjoiaHR0cDovL2hvc3QuZG9ja2VyLmludGVybmFsOjgwL2RpZGNvbW0vIiwiciI6W10sImEiOlsiZGlkY29tbS92MiJdfQ"
)

let apollo = ApolloImpl()
let castor = CastorImpl(apollo: apollo)
let document = try await castor.resolveDID(did: mypeerDID)
print()
}
}
14 changes: 14 additions & 0 deletions Core/Sources/Helpers/First+AsyncAwait.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import Combine

extension Publishers {
struct MissingOutputError: Error {}
}

public extension Publishers.First where Failure == Error {
func await() async throws -> Output {
for try await output in values {
return output
}
throw Publishers.MissingOutputError()
}
}
File renamed without changes.
11 changes: 11 additions & 0 deletions PrismAgent/Sources/ConnectionsManager/ConnectionsManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,17 @@ class ConnectionsManagerImpl: ConnectionsManager {
}

extension ConnectionsManagerImpl: DIDCommConnection {
func sendMessage(_ message: Message) async throws -> Message? {
let mercury = self.mercury
return try await pluto
.storeMessage(message: message)
.flatMap {
Future { try await mercury.sendMessageParseMessage(msg: message) }
}
.first()
.await()
}

func awaitMessages() async throws -> [Message] {
let stream: AnyPublisher<[Message], Error> = try awaitMessages()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Foundation
protocol DIDCommConnection {
func awaitMessages() async throws -> [Message]
func awaitMessageResponse(id: String) async throws -> Message?
func sendMessage(_ message: Message) async throws -> Message?
}

protocol ConnectionsManager {
Expand Down
6 changes: 5 additions & 1 deletion PrismAgent/Sources/PrismAgent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class PrismAgent {
private var connectionManager: ConnectionsManagerImpl
private var cancellables = [AnyCancellable]()
// Not a "stream"
private var messagesStreamTask: Task?
private var messagesStreamTask: Task<(), Error>?

public let seed: Seed

Expand Down Expand Up @@ -318,6 +318,10 @@ public class PrismAgent {
}
}

public func sendMessage(_ message: Message) async throws -> Message? {
try await connectionManager.sendMessage(message)
}

public func startFetchingMessages() {
// TODO: This needs to be better thought for sure it cannot be left like this
let manager = connectionManager
Expand Down
5 changes: 5 additions & 0 deletions PrismAgent/Tests/Helper/DIDCommConnection+Testing.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ import Domain
class ConnectionStub: DIDCommConnection, ConnectionsManager {
var awaitMessagesResponse: [Message]!
var awaitMessageResponse: Message?
var sendMessageResponse: Message?

func sendMessage(_ message: Message) async throws -> Message? {
sendMessageResponse
}

func awaitMessages() async throws -> [Message] {
awaitMessagesResponse
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ final class SetupPrismAgentViewModelImpl: ObservableObject, SetupPrismAgentViewM
func updateKeyList() async throws {
do {
_ = try await agent.createNewPeerDID(updateMediator: true)
try await agent.awaitMessages()
// try await agent.awaitMessages()
} catch {
await MainActor.run {
self.error = error.localizedDescription
Expand Down

0 comments on commit 99989cc

Please sign in to comment.