mirror of
https://github.com/SchildiChat/SchildiChat-android.git
synced 2025-01-04 07:17:41 +03:00
Small cleanup and format
This commit is contained in:
parent
d4090c4b0a
commit
54c3b4192e
1 changed files with 47 additions and 57 deletions
|
@ -228,6 +228,7 @@ internal class DefaultCryptoService @Inject constructor(
|
||||||
private val isStarted = AtomicBoolean(false)
|
private val isStarted = AtomicBoolean(false)
|
||||||
|
|
||||||
private var olmMachine: OlmMachine? = null
|
private var olmMachine: OlmMachine? = null
|
||||||
|
|
||||||
// The verification service.
|
// The verification service.
|
||||||
private var verificationService: RustVerificationService? = null
|
private var verificationService: RustVerificationService? = null
|
||||||
|
|
||||||
|
@ -254,7 +255,7 @@ internal class DefaultCryptoService @Inject constructor(
|
||||||
EventType.STATE_ROOM_ENCRYPTION -> onRoomEncryptionEvent(roomId, event)
|
EventType.STATE_ROOM_ENCRYPTION -> onRoomEncryptionEvent(roomId, event)
|
||||||
EventType.STATE_ROOM_MEMBER -> onRoomMembershipEvent(roomId, event)
|
EventType.STATE_ROOM_MEMBER -> onRoomMembershipEvent(roomId, event)
|
||||||
EventType.STATE_ROOM_HISTORY_VISIBILITY -> onRoomHistoryVisibilityEvent(roomId, event)
|
EventType.STATE_ROOM_HISTORY_VISIBILITY -> onRoomHistoryVisibilityEvent(roomId, event)
|
||||||
else -> this.verificationService?.onEvent(event)
|
else -> this.verificationService?.onEvent(event)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -361,7 +362,7 @@ internal class DefaultCryptoService @Inject constructor(
|
||||||
* devices.
|
* devices.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
suspend fun start() {
|
fun start() {
|
||||||
internalStart()
|
internalStart()
|
||||||
// Just update
|
// Just update
|
||||||
fetchDevicesList(NoOpMatrixCallback())
|
fetchDevicesList(NoOpMatrixCallback())
|
||||||
|
@ -383,7 +384,7 @@ internal class DefaultCryptoService @Inject constructor(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private suspend fun internalStart() {
|
private fun internalStart() {
|
||||||
if (isStarted.get() || isStarting.get()) {
|
if (isStarted.get() || isStarting.get()) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -392,11 +393,11 @@ internal class DefaultCryptoService @Inject constructor(
|
||||||
try {
|
try {
|
||||||
setRustLogger()
|
setRustLogger()
|
||||||
val machine = OlmMachine(userId, deviceId!!, dataDir, deviceObserver)
|
val machine = OlmMachine(userId, deviceId!!, dataDir, deviceObserver)
|
||||||
this.olmMachine = machine
|
olmMachine = machine
|
||||||
this.verificationService = RustVerificationService(machine, this.sender)
|
verificationService = RustVerificationService(machine, this.sender)
|
||||||
Timber.v(
|
Timber.v(
|
||||||
"## CRYPTO | Successfully started up an Olm machine for " +
|
"## CRYPTO | Successfully started up an Olm machine for " +
|
||||||
"${userId}, ${deviceId}, identity keys: ${this.olmMachine?.identityKeys()}")
|
"${userId}, ${deviceId}, identity keys: ${this.olmMachine?.identityKeys()}")
|
||||||
} catch (throwable: Throwable) {
|
} catch (throwable: Throwable) {
|
||||||
Timber.v("Failed create an Olm machine: $throwable")
|
Timber.v("Failed create an Olm machine: $throwable")
|
||||||
}
|
}
|
||||||
|
@ -404,18 +405,8 @@ internal class DefaultCryptoService @Inject constructor(
|
||||||
// Open the store
|
// Open the store
|
||||||
cryptoStore.open()
|
cryptoStore.open()
|
||||||
|
|
||||||
runCatching {
|
isStarting.set(false)
|
||||||
}.fold(
|
isStarted.set(true)
|
||||||
{
|
|
||||||
isStarting.set(false)
|
|
||||||
isStarted.set(true)
|
|
||||||
},
|
|
||||||
{
|
|
||||||
isStarting.set(false)
|
|
||||||
isStarted.set(false)
|
|
||||||
Timber.e(it, "Start failed")
|
|
||||||
}
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -456,9 +447,7 @@ internal class DefaultCryptoService @Inject constructor(
|
||||||
// Let's initialize here as a workaround until we figure out if the
|
// Let's initialize here as a workaround until we figure out if the
|
||||||
// above conclusion is correct.
|
// above conclusion is correct.
|
||||||
if (verificationService == null) {
|
if (verificationService == null) {
|
||||||
runBlocking {
|
internalStart()
|
||||||
internalStart()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return verificationService!!
|
return verificationService!!
|
||||||
|
@ -740,8 +729,8 @@ internal class DefaultCryptoService @Inject constructor(
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun notifyRoomKeyReceived(
|
private fun notifyRoomKeyReceived(
|
||||||
roomId: String,
|
roomId: String,
|
||||||
sessionId: String,
|
sessionId: String,
|
||||||
) {
|
) {
|
||||||
// The sender key is actually unused since it's unimportant for megolm
|
// The sender key is actually unused since it's unimportant for megolm
|
||||||
// Our events don't contain the info so pass an empty string until we
|
// Our events don't contain the info so pass an empty string until we
|
||||||
|
@ -757,38 +746,38 @@ internal class DefaultCryptoService @Inject constructor(
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun receiveSyncChanges(
|
suspend fun receiveSyncChanges(
|
||||||
toDevice: ToDeviceSyncResponse?,
|
toDevice: ToDeviceSyncResponse?,
|
||||||
deviceChanges: DeviceListResponse?,
|
deviceChanges: DeviceListResponse?,
|
||||||
keyCounts: DeviceOneTimeKeysCountSyncResponse?) {
|
keyCounts: DeviceOneTimeKeysCountSyncResponse?) {
|
||||||
// Decrypt and handle our to-device events
|
// Decrypt and handle our to-device events
|
||||||
val toDeviceEvents = this.olmMachine!!.receiveSyncChanges(toDevice, deviceChanges, keyCounts)
|
val toDeviceEvents = this.olmMachine!!.receiveSyncChanges(toDevice, deviceChanges, keyCounts)
|
||||||
|
|
||||||
// Notify the our listeners about room keys so decryption is retried.
|
// Notify the our listeners about room keys so decryption is retried.
|
||||||
if (toDeviceEvents.events != null) {
|
if (toDeviceEvents.events != null) {
|
||||||
toDeviceEvents.events.forEach { event ->
|
toDeviceEvents.events.forEach { event ->
|
||||||
when (event.type) {
|
when (event.type) {
|
||||||
EventType.ROOM_KEY -> {
|
EventType.ROOM_KEY -> {
|
||||||
val content = event.getClearContent().toModel<RoomKeyContent>() ?: return@forEach
|
val content = event.getClearContent().toModel<RoomKeyContent>() ?: return@forEach
|
||||||
|
|
||||||
val roomId = content.sessionId ?: return@forEach
|
val roomId = content.sessionId ?: return@forEach
|
||||||
val sessionId = content.sessionId
|
val sessionId = content.sessionId
|
||||||
|
|
||||||
notifyRoomKeyReceived(roomId, sessionId)
|
notifyRoomKeyReceived(roomId, sessionId)
|
||||||
}
|
}
|
||||||
EventType.FORWARDED_ROOM_KEY -> {
|
EventType.FORWARDED_ROOM_KEY -> {
|
||||||
val content = event.getClearContent().toModel<ForwardedRoomKeyContent>() ?: return@forEach
|
val content = event.getClearContent().toModel<ForwardedRoomKeyContent>() ?: return@forEach
|
||||||
|
|
||||||
val roomId = content.sessionId ?: return@forEach
|
val roomId = content.sessionId ?: return@forEach
|
||||||
val sessionId = content.sessionId
|
val sessionId = content.sessionId
|
||||||
|
|
||||||
notifyRoomKeyReceived(roomId, sessionId)
|
notifyRoomKeyReceived(roomId, sessionId)
|
||||||
}
|
}
|
||||||
else -> {
|
else -> {
|
||||||
this.verificationService?.onEvent(event)
|
this.verificationService?.onEvent(event)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private suspend fun preshareRoomKey(roomId: String, roomMembers: List<String>) {
|
private suspend fun preshareRoomKey(roomId: String, roomMembers: List<String>) {
|
||||||
|
@ -800,7 +789,8 @@ internal class DefaultCryptoService @Inject constructor(
|
||||||
is Request.KeysClaim -> {
|
is Request.KeysClaim -> {
|
||||||
claimKeys(request)
|
claimKeys(request)
|
||||||
}
|
}
|
||||||
else -> {}
|
else -> {
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -816,7 +806,7 @@ internal class DefaultCryptoService @Inject constructor(
|
||||||
sendToDevice(it)
|
sendToDevice(it)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else -> {
|
else -> {
|
||||||
// This request can only be a to-device request but
|
// This request can only be a to-device request but
|
||||||
// we need to handle all our cases and put this
|
// we need to handle all our cases and put this
|
||||||
// async block for our joinAll to work.
|
// async block for our joinAll to work.
|
||||||
|
@ -861,22 +851,22 @@ internal class DefaultCryptoService @Inject constructor(
|
||||||
coroutineScope {
|
coroutineScope {
|
||||||
olmMachine!!.outgoingRequests().map {
|
olmMachine!!.outgoingRequests().map {
|
||||||
when (it) {
|
when (it) {
|
||||||
is Request.KeysUpload -> {
|
is Request.KeysUpload -> {
|
||||||
async {
|
async {
|
||||||
uploadKeys(it)
|
uploadKeys(it)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
is Request.KeysQuery -> {
|
is Request.KeysQuery -> {
|
||||||
async {
|
async {
|
||||||
queryKeys(it)
|
queryKeys(it)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
is Request.ToDevice -> {
|
is Request.ToDevice -> {
|
||||||
async {
|
async {
|
||||||
sendToDevice(it)
|
sendToDevice(it)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
is Request.KeysClaim -> {
|
is Request.KeysClaim -> {
|
||||||
async {
|
async {
|
||||||
claimKeys(it)
|
claimKeys(it)
|
||||||
}
|
}
|
||||||
|
@ -912,8 +902,8 @@ internal class DefaultCryptoService @Inject constructor(
|
||||||
* @return the result ImportRoomKeysResult
|
* @return the result ImportRoomKeysResult
|
||||||
*/
|
*/
|
||||||
override suspend fun importRoomKeys(roomKeysAsArray: ByteArray,
|
override suspend fun importRoomKeys(roomKeysAsArray: ByteArray,
|
||||||
password: String,
|
password: String,
|
||||||
progressListener: ProgressListener?): ImportRoomKeysResult {
|
progressListener: ProgressListener?): ImportRoomKeysResult {
|
||||||
return olmMachine!!.importKeys(roomKeysAsArray, password, progressListener)
|
return olmMachine!!.importKeys(roomKeysAsArray, password, progressListener)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1022,7 +1012,7 @@ internal class DefaultCryptoService @Inject constructor(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
when(request) {
|
when (request) {
|
||||||
is Request.ToDevice -> {
|
is Request.ToDevice -> {
|
||||||
sendToDevice(request)
|
sendToDevice(request)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue