Cleanup and split long lines

This commit is contained in:
Benoit Marty 2020-10-29 14:10:41 +01:00
parent f190356934
commit f48d4c021a
3 changed files with 70 additions and 76 deletions

View file

@ -1124,7 +1124,7 @@ internal class RealmCryptoStore @Inject constructor(
content = ContentMapper.map(event.content) content = ContentMapper.map(event.content)
).apply { ).apply {
sendState = SendState.SYNCED sendState = SendState.SYNCED
decryptionResultJson = MoshiProvider.providesMoshi().adapter<OlmDecryptionResult>(OlmDecryptionResult::class.java).toJson(event.mxDecryptionResult) decryptionResultJson = MoshiProvider.providesMoshi().adapter(OlmDecryptionResult::class.java).toJson(event.mxDecryptionResult)
decryptionErrorCode = event.mCryptoError?.name decryptionErrorCode = event.mCryptoError?.name
} }
realm.insertOrUpdate(entity) realm.insertOrUpdate(entity)
@ -1143,7 +1143,7 @@ internal class RealmCryptoStore @Inject constructor(
content = ContentMapper.map(event.content) content = ContentMapper.map(event.content)
).apply { ).apply {
sendState = SendState.SYNCED sendState = SendState.SYNCED
decryptionResultJson = MoshiProvider.providesMoshi().adapter<OlmDecryptionResult>(OlmDecryptionResult::class.java).toJson(event.mxDecryptionResult) decryptionResultJson = MoshiProvider.providesMoshi().adapter(OlmDecryptionResult::class.java).toJson(event.mxDecryptionResult)
decryptionErrorCode = event.mCryptoError?.name decryptionErrorCode = event.mCryptoError?.name
} }
realm.insertOrUpdate(entity) realm.insertOrUpdate(entity)

View file

@ -29,6 +29,7 @@ import com.airbnb.mvrx.Uninitialized
import com.airbnb.mvrx.ViewModelContext import com.airbnb.mvrx.ViewModelContext
import com.squareup.inject.assisted.Assisted import com.squareup.inject.assisted.Assisted
import com.squareup.inject.assisted.AssistedInject import com.squareup.inject.assisted.AssistedInject
import im.vector.app.core.extensions.exhaustive
import im.vector.app.core.platform.VectorViewEvents import im.vector.app.core.platform.VectorViewEvents
import im.vector.app.core.platform.VectorViewModel import im.vector.app.core.platform.VectorViewModel
import im.vector.app.core.platform.VectorViewModelAction import im.vector.app.core.platform.VectorViewModelAction
@ -54,7 +55,7 @@ sealed class KeyRequestEvents : VectorViewEvents {
} }
data class KeyRequestViewState( data class KeyRequestViewState(
val exporting: Async<String> = Uninitialized val exporting: Async<Unit> = Uninitialized
) : MvRxState ) : MvRxState
class KeyRequestViewModel @AssistedInject constructor( class KeyRequestViewModel @AssistedInject constructor(
@ -78,77 +79,75 @@ class KeyRequestViewModel @AssistedInject constructor(
override fun handle(action: KeyRequestAction) { override fun handle(action: KeyRequestAction) {
when (action) { when (action) {
is KeyRequestAction.ExportAudit -> { is KeyRequestAction.ExportAudit -> exportAudit(action)
setState { }.exhaustive
copy(exporting = Loading()) }
}
viewModelScope.launch(Dispatchers.IO) {
try {
// this can take long
val eventList = session.cryptoService().getGossipingEvents()
// clean it a bit to
val stringBuilder = StringBuilder()
eventList.forEach {
val clearType = it.getClearType()
stringBuilder.append("[${it.ageLocalTs}] : $clearType from:${it.senderId} - ")
when (clearType) {
EventType.ROOM_KEY_REQUEST -> {
val content = it.getClearContent().toModel<RoomKeyShareRequest>()
stringBuilder.append("reqId:${content?.requestId} action:${content?.action} ")
if (content?.action == GossipingToDeviceObject.ACTION_SHARE_REQUEST) {
stringBuilder.append("sessionId: ${content.body?.sessionId} ")
}
stringBuilder.append("requestedBy: ${content?.requestingDeviceId} ")
stringBuilder.append("\n")
}
EventType.FORWARDED_ROOM_KEY -> {
val encryptedContent = it.content.toModel<OlmEventContent>()
val content = it.getClearContent().toModel<ForwardedRoomKeyContent>()
stringBuilder.append("sessionId:${content?.sessionId} From Device (sender key):${encryptedContent?.senderKey} ") private fun exportAudit(action: KeyRequestAction.ExportAudit) {
span("\nFrom Device (sender key):") { setState {
textStyle = "bold" copy(exporting = Loading())
} }
stringBuilder.append("\n") viewModelScope.launch(Dispatchers.IO) {
try {
// this can take long
val eventList = session.cryptoService().getGossipingEvents()
// clean it a bit to
val raw = buildString {
eventList.forEach {
val clearType = it.getClearType()
append("[${it.ageLocalTs}] : $clearType from:${it.senderId} - ")
when (clearType) {
EventType.ROOM_KEY_REQUEST -> {
val content = it.getClearContent().toModel<RoomKeyShareRequest>()
append("reqId:${content?.requestId} action:${content?.action} ")
if (content?.action == GossipingToDeviceObject.ACTION_SHARE_REQUEST) {
append("sessionId: ${content.body?.sessionId} ")
} }
EventType.ROOM_KEY -> { append("requestedBy: ${content?.requestingDeviceId}")
val content = it.getClearContent() }
stringBuilder.append("sessionId:${content?.get("session_id")} roomId:${content?.get("room_id")} dest:${content?.get("_dest") ?: "me"}") EventType.FORWARDED_ROOM_KEY -> {
stringBuilder.append("\n") val encryptedContent = it.content.toModel<OlmEventContent>()
} val content = it.getClearContent().toModel<ForwardedRoomKeyContent>()
EventType.SEND_SECRET -> {
val content = it.getClearContent().toModel<SecretSendEventContent>() append("sessionId:${content?.sessionId} From Device (sender key):${encryptedContent?.senderKey}")
stringBuilder.append("requestId:${content?.requestId} From Device:${it.mxDecryptionResult?.payload?.get("sender_device")}") span("\nFrom Device (sender key):") {
stringBuilder.append("\n") textStyle = "bold"
}
EventType.REQUEST_SECRET -> {
val content = it.getClearContent().toModel<SecretShareRequest>()
stringBuilder.append("reqId:${content?.requestId} action:${content?.action} ")
if (content?.action == GossipingToDeviceObject.ACTION_SHARE_REQUEST) {
stringBuilder.append("secretName:${content.secretName} ")
}
stringBuilder.append("requestedBy:${content?.requestingDeviceId}")
stringBuilder.append("\n")
}
EventType.ENCRYPTED -> {
stringBuilder.append("Failed to Derypt \n")
}
else -> {
stringBuilder.append("?? \n")
} }
} }
EventType.ROOM_KEY -> {
val content = it.getClearContent()
append("sessionId:${content?.get("session_id")} roomId:${content?.get("room_id")} dest:${content?.get("_dest") ?: "me"}")
}
EventType.SEND_SECRET -> {
val content = it.getClearContent().toModel<SecretSendEventContent>()
append("requestId:${content?.requestId} From Device:${it.mxDecryptionResult?.payload?.get("sender_device")}")
}
EventType.REQUEST_SECRET -> {
val content = it.getClearContent().toModel<SecretShareRequest>()
append("reqId:${content?.requestId} action:${content?.action} ")
if (content?.action == GossipingToDeviceObject.ACTION_SHARE_REQUEST) {
append("secretName:${content.secretName} ")
}
append("requestedBy:${content?.requestingDeviceId}")
}
EventType.ENCRYPTED -> {
append("Failed to Decrypt")
}
else -> {
append("??")
}
} }
val raw = stringBuilder.toString() append("\n")
setState {
copy(exporting = Success(""))
}
_viewEvents.post(KeyRequestEvents.SaveAudit(action.uri, raw))
} catch (error: Throwable) {
setState {
copy(exporting = Fail(error))
}
} }
} }
setState {
copy(exporting = Success(Unit))
}
_viewEvents.post(KeyRequestEvents.SaveAudit(action.uri, raw))
} catch (error: Throwable) {
setState {
copy(exporting = Fail(error))
}
} }
} }
} }

View file

@ -30,6 +30,7 @@ import com.airbnb.mvrx.fragmentViewModel
import com.airbnb.mvrx.withState import com.airbnb.mvrx.withState
import com.google.android.material.tabs.TabLayoutMediator import com.google.android.material.tabs.TabLayoutMediator
import im.vector.app.R import im.vector.app.R
import im.vector.app.core.extensions.exhaustive
import im.vector.app.core.extensions.registerStartForActivityResult import im.vector.app.core.extensions.registerStartForActivityResult
import im.vector.app.core.platform.VectorBaseActivity import im.vector.app.core.platform.VectorBaseActivity
import im.vector.app.core.platform.VectorBaseFragment import im.vector.app.core.platform.VectorBaseFragment
@ -103,17 +104,11 @@ class KeyRequestsFragment @Inject constructor(
when (it) { when (it) {
is KeyRequestEvents.SaveAudit -> { is KeyRequestEvents.SaveAudit -> {
tryOrNull { tryOrNull {
val os = requireContext().contentResolver?.openOutputStream(it.uri) requireContext().contentResolver?.openOutputStream(it.uri)
if (os == null) { ?.use { os -> os.write(it.raw.toByteArray()) }
false
} else {
os.write(it.raw.toByteArray())
os.flush()
true
}
} }
} }
} }.exhaustive
} }
} }