Skip to content

Commit

Permalink
fix: Support alias (DID) in Live mode (#235)
Browse files Browse the repository at this point in the history
Fix #230

Signed-off-by: Fabio Pinheiro <fabiomgpinheiro@gmail.com>

Signed-off-by: Shailesh Patil <shailesh.patil@iohk.io>
  • Loading branch information
FabioPinheiro authored and mineme0110 committed May 1, 2024
1 parent 4a5f00b commit 5d258bb
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@ case class MediatorTransportManager(
liveMode.get(subject).toSeq.flatMap(transportId => transports.filter(t => transportId.contains(t.id)))

def sendForLiveMode(
next: TO,
next: FROMTO,
msg: /*lazy*/ => SignedMessage | EncryptedMessage
): ZIO[Any, DidFail, Unit] =
for {
transportIDs <- ZIO.succeed(this.liveMode.getOrElse(next.asFROMTO, Set.empty))
transportIDs <- ZIO.succeed(this.liveMode.getOrElse(next, Set.empty))
myChannels <- ZIO.succeed(transportIDs.flatMap(id => this.transports.find(_.id == id)))
_ <- ZIO.foreach(myChannels) { _.send(msg) }
} yield ()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,25 @@ class UserAccountRepo(reactiveMongoApi: ReactiveMongoApi)(using ec: ExecutionCon
} yield result
}

def getDidAccountFromAlias(alias: DIDSubject): IO[StorageError, Option[DidAccount]] = {
def selector: BSONDocument = BSONDocument("alias" -> alias.did) // BSONDocument("$in" -> BSONArray(alias.did)))
def projection: Option[BSONDocument] = None

for {
_ <- ZIO.logInfo("getDidAccountFromAlias")
coll <- collection
result <- ZIO
.fromFuture(implicit ec =>
coll
.find(selector, projection)
.cursor[DidAccount]()
.collect[Seq](1, Cursor.FailOnError[Seq[DidAccount]]()) // Just one
)
.tapError(err => ZIO.logError(s"getDidAccount : ${err.getMessage}"))
.mapError(ex => StorageThrowable(ex))
} yield result.headOption
}

def getDidAccount(did: DIDSubject): IO[StorageError, Option[DidAccount]] = {
def selector: BSONDocument = BSONDocument("did" -> did)
def projection: Option[BSONDocument] = None
Expand All @@ -92,7 +111,6 @@ class UserAccountRepo(reactiveMongoApi: ReactiveMongoApi)(using ec: ExecutionCon
.tapError(err => ZIO.logError(s"getDidAccount : ${err.getMessage}"))
.mapError(ex => StorageThrowable(ex))
} yield result.headOption

}

def addAlias(owner: DIDSubject, newAlias: DIDSubject): ZIO[Any, StorageError, Either[String, Int]] = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,22 @@ object ForwardMessageExecuter
eMsgDelivery <- Operations
.authEncrypt(messageDelivery)
.mapError(didFail => MediatorDidError(didFail))
_ <- mediatorTransportManager
.sendForLiveMode(m.next.asTO, eMsgDelivery)
.mapError(didFail => MediatorDidError(didFail))
_ <- for {
maybeDidAccount <- repoDidAccount
.getDidAccountFromAlias(m.next)
.tapErrorCause(errorCause =>
ZIO.logErrorCause("Error when retrieving account for live mode forward message", errorCause)
)
.catchAll(ex => ZIO.none) // ignoring error
ret <- maybeDidAccount match {
case None => ZIO.unit // nothing to do
case Some(didAccount) =>
val accountOwner = didAccount.did
mediatorTransportManager
.sendForLiveMode(accountOwner.asFROMTO, eMsgDelivery)
.mapError(didFail => MediatorDidError(didFail))
}
} yield ret
} yield NoReply
} else {
for {
Expand Down

0 comments on commit 5d258bb

Please sign in to comment.