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)
).apply {
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
}
realm.insertOrUpdate(entity)
@ -1143,7 +1143,7 @@ internal class RealmCryptoStore @Inject constructor(
content = ContentMapper.map(event.content)
).apply {
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
}
realm.insertOrUpdate(entity)

View File

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

View File

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