Code robustness (avoid using !!)

This commit is contained in:
Benoit Marty 2020-02-25 11:20:11 +01:00
parent 08e4b4473c
commit 17e028178e
3 changed files with 23 additions and 20 deletions

View file

@ -64,15 +64,17 @@ data class IncomingRoomKeyRequest(
*
* @param event the event
*/
fun fromEvent(event: Event): IncomingRoomKeyRequest {
val roomKeyShareRequest = event.getClearContent().toModel<RoomKeyShareRequest>()!!
return IncomingRoomKeyRequest(
userId = event.senderId,
deviceId = roomKeyShareRequest.requestingDeviceId,
requestId = roomKeyShareRequest.requestId,
requestBody = roomKeyShareRequest.body ?: RoomKeyRequestBody()
)
fun fromEvent(event: Event): IncomingRoomKeyRequest? {
return event.getClearContent()
.toModel<RoomKeyShareRequest>()
?.let {
IncomingRoomKeyRequest(
userId = event.senderId,
deviceId = it.requestingDeviceId,
requestId = it.requestId,
requestBody = it.body ?: RoomKeyRequestBody()
)
}
}
}
}

View file

@ -40,20 +40,21 @@ data class IncomingRoomKeyRequestCancellation(
override val requestId: String? = null
) : IncomingRoomKeyRequestCommon {
companion object {
/**
* Factory
*
* @param event the event
*/
fun fromEvent(event: Event): IncomingRoomKeyRequestCancellation {
val roomKeyShareRequestCancellation = event.getClearContent().toModel<RoomKeyShareCancellation>()!!
return IncomingRoomKeyRequestCancellation(
userId = event.senderId,
deviceId = roomKeyShareRequestCancellation.requestingDeviceId,
requestId = roomKeyShareRequestCancellation.requestId
)
fun fromEvent(event: Event): IncomingRoomKeyRequestCancellation? {
return event.getClearContent()
.toModel<RoomKeyShareCancellation>()
?.let {
IncomingRoomKeyRequestCancellation(
userId = event.senderId,
deviceId = it.requestingDeviceId,
requestId = it.requestId
)
}
}
}
}

View file

@ -53,8 +53,8 @@ internal class IncomingRoomKeyRequestManager @Inject constructor(
fun onRoomKeyRequestEvent(event: Event) {
val roomKeyShare = event.getClearContent().toModel<RoomKeyShare>()
when (roomKeyShare?.action) {
RoomKeyShare.ACTION_SHARE_REQUEST -> receivedRoomKeyRequests.add(IncomingRoomKeyRequest.fromEvent(event))
RoomKeyShare.ACTION_SHARE_CANCELLATION -> receivedRoomKeyRequestCancellations.add(IncomingRoomKeyRequestCancellation.fromEvent(event))
RoomKeyShare.ACTION_SHARE_REQUEST -> IncomingRoomKeyRequest.fromEvent(event)?.let { receivedRoomKeyRequests.add(it) }
RoomKeyShare.ACTION_SHARE_CANCELLATION -> IncomingRoomKeyRequestCancellation.fromEvent(event)?.let { receivedRoomKeyRequestCancellations.add(it) }
else -> Timber.e("## onRoomKeyRequestEvent() : unsupported action ${roomKeyShare?.action}")
}
}