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 * @param event the event
*/ */
fun fromEvent(event: Event): IncomingRoomKeyRequest { fun fromEvent(event: Event): IncomingRoomKeyRequest? {
val roomKeyShareRequest = event.getClearContent().toModel<RoomKeyShareRequest>()!! return event.getClearContent()
.toModel<RoomKeyShareRequest>()
return IncomingRoomKeyRequest( ?.let {
IncomingRoomKeyRequest(
userId = event.senderId, userId = event.senderId,
deviceId = roomKeyShareRequest.requestingDeviceId, deviceId = it.requestingDeviceId,
requestId = roomKeyShareRequest.requestId, requestId = it.requestId,
requestBody = roomKeyShareRequest.body ?: RoomKeyRequestBody() requestBody = it.body ?: RoomKeyRequestBody()
) )
} }
} }
}
} }

View file

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

View file

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