-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: pick up messages and mark as read (#63)
1 parent
73f98c5
commit 087bb88
Showing
13 changed files
with
218 additions
and
69 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
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
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
67 changes: 67 additions & 0 deletions
67
...mmonMain/kotlin/io/iohk/atala/prism/walletsdk/prismagent/protocols/pickup/PickupRunner.kt
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,67 @@ | ||
package io.iohk.atala.prism.walletsdk.prismagent.protocols.pickup | ||
|
||
import io.iohk.atala.prism.walletsdk.domain.buildingblocks.Mercury | ||
import io.iohk.atala.prism.walletsdk.domain.models.AttachmentBase64 | ||
import io.iohk.atala.prism.walletsdk.domain.models.AttachmentDescriptor | ||
import io.iohk.atala.prism.walletsdk.domain.models.AttachmentJsonData | ||
import io.iohk.atala.prism.walletsdk.domain.models.Message | ||
import io.iohk.atala.prism.walletsdk.domain.models.PrismAgentError | ||
import io.iohk.atala.prism.walletsdk.prismagent.protocols.ProtocolType | ||
|
||
class PickupRunner(message: Message, private val mercury: Mercury) { | ||
|
||
enum class PickupResponseType(val type: String) { | ||
STATUS("status"), | ||
DELIVERY("delivery") | ||
} | ||
|
||
data class PickupResponse(val type: PickupResponseType, val message: Message) | ||
|
||
data class PickupAttachment( | ||
val attachmentId: String, | ||
val data: String | ||
) | ||
|
||
private val message: PickupResponse | ||
|
||
init { | ||
when (message.piuri) { | ||
ProtocolType.PickupStatus.value -> { | ||
this.message = PickupResponse(PickupResponseType.STATUS, message) | ||
} | ||
|
||
ProtocolType.PickupDelivery.value -> { | ||
this.message = PickupResponse(PickupResponseType.DELIVERY, message) | ||
} | ||
|
||
else -> { | ||
throw PrismAgentError.InvalidPickupDeliveryMessageError() | ||
} | ||
} | ||
} | ||
|
||
suspend fun run(): Array<Pair<String, Message>> { | ||
return if (message.type == PickupResponseType.DELIVERY) { | ||
message.message.attachments | ||
.mapNotNull { processAttachment(it) } | ||
.map { Pair(it.attachmentId, mercury.unpackMessage(it.data)) } | ||
.toTypedArray() | ||
} else { | ||
arrayOf() | ||
} | ||
} | ||
|
||
// TODO: Clean this method | ||
private fun processAttachment(attachment: AttachmentDescriptor): PickupAttachment? { | ||
return if (Message.isBase64Attachment(attachment.data)) { | ||
PickupAttachment(attachmentId = attachment.id, data = (attachment.data as AttachmentBase64).base64) | ||
} else if (Message.isJsonAttachment(attachment.data)) { | ||
PickupAttachment( | ||
attachmentId = attachment.id, | ||
data = (attachment.data as AttachmentJsonData).data | ||
) | ||
} else { | ||
null | ||
} | ||
} | ||
} |
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
Oops, something went wrong.