Wrap only cryptoService.decryptEvent with runBlocking instead of the whole methods

This commit is contained in:
ariskotsomitopoulos 2022-04-27 12:38:25 +03:00
parent 51b42929bd
commit 5cfe218634
3 changed files with 17 additions and 14 deletions

View File

@ -213,15 +213,15 @@ internal fun ThreadSummaryEntity.Companion.createOrUpdate(
} }
} }
// note: runBlocking should be used here while we are in realm single thread executor, to avoid thread switching private fun decryptIfNeeded(cryptoService: CryptoService?, eventEntity: EventEntity, roomId: String) {
private fun decryptIfNeeded(cryptoService: CryptoService?, eventEntity: EventEntity, roomId: String) = runBlocking { cryptoService ?: return
cryptoService ?: return@runBlocking
val event = eventEntity.asDomain() val event = eventEntity.asDomain()
if (event.isEncrypted() && event.mxDecryptionResult == null && event.eventId != null) { if (event.isEncrypted() && event.mxDecryptionResult == null && event.eventId != null) {
try { try {
Timber.i("###THREADS ThreadSummaryHelper request decryption for eventId:${event.eventId}") Timber.i("###THREADS ThreadSummaryHelper request decryption for eventId:${event.eventId}")
// Event from sync does not have roomId, so add it to the event first // Event from sync does not have roomId, so add it to the event first
val result = cryptoService.decryptEvent(event.copy(roomId = roomId), "") // note: runBlocking should be used here while we are in realm single thread executor, to avoid thread switching
val result = runBlocking { cryptoService.decryptEvent(event.copy(roomId = roomId), "") }
event.mxDecryptionResult = OlmDecryptionResult( event.mxDecryptionResult = OlmDecryptionResult(
payload = result.clearEvent, payload = result.clearEvent,
senderKey = result.senderCurve25519Key, senderKey = result.senderCurve25519Key,

View File

@ -119,8 +119,7 @@ internal class TimelineEventDecryptor @Inject constructor(
} }
} }
// note: runBlocking should be used here while we are in realm single thread executor, to avoid thread switching private fun processDecryptRequest(request: DecryptionRequest, realm: Realm) {
private fun processDecryptRequest(request: DecryptionRequest, realm: Realm) = runBlocking {
val event = request.event val event = request.event
val timelineId = request.timelineId val timelineId = request.timelineId
@ -128,10 +127,11 @@ internal class TimelineEventDecryptor @Inject constructor(
// Here we have requested a decryption to an event that is not encrypted // Here we have requested a decryption to an event that is not encrypted
// We will simply make this event thread aware // We will simply make this event thread aware
threadAwareNonEncryptedEvents(request, realm) threadAwareNonEncryptedEvents(request, realm)
return@runBlocking return
} }
try { try {
val result = cryptoService.decryptEvent(request.event, timelineId) // note: runBlocking should be used here while we are in realm single thread executor, to avoid thread switching
val result = runBlocking { cryptoService.decryptEvent(request.event, timelineId) }
Timber.v("Successfully decrypted event ${event.eventId}") Timber.v("Successfully decrypted event ${event.eventId}")
realm.executeTransaction { realm.executeTransaction {
val eventId = event.eventId ?: return@executeTransaction val eventId = event.eventId ?: return@executeTransaction

View File

@ -423,7 +423,8 @@ internal class RoomSyncHandler @Inject constructor(private val readReceiptHandle
roomId = roomId, roomId = roomId,
eventEntity = eventEntity, eventEntity = eventEntity,
direction = PaginationDirection.FORWARDS, direction = PaginationDirection.FORWARDS,
roomMemberContentsByUser = roomMemberContentsByUser) roomMemberContentsByUser = roomMemberContentsByUser
)
if (lightweightSettingsStorage.areThreadMessagesEnabled()) { if (lightweightSettingsStorage.areThreadMessagesEnabled()) {
eventEntity.rootThreadEventId?.let { eventEntity.rootThreadEventId?.let {
// This is a thread event // This is a thread event
@ -439,7 +440,8 @@ internal class RoomSyncHandler @Inject constructor(private val readReceiptHandle
threadEventEntity = eventEntity, threadEventEntity = eventEntity,
roomMemberContentsByUser = roomMemberContentsByUser, roomMemberContentsByUser = roomMemberContentsByUser,
userId = userId, userId = userId,
roomEntity = roomEntity) roomEntity = roomEntity
)
} }
} ?: run { } ?: run {
// This is a normal event or a root thread one // This is a normal event or a root thread one
@ -477,7 +479,8 @@ internal class RoomSyncHandler @Inject constructor(private val readReceiptHandle
roomId = roomId, roomId = roomId,
realm = realm, realm = realm,
chunkEntity = chunkEntity, chunkEntity = chunkEntity,
currentUserId = userId) currentUserId = userId
)
} }
// posting new events to timeline if any is registered // posting new events to timeline if any is registered
@ -507,11 +510,11 @@ internal class RoomSyncHandler @Inject constructor(private val readReceiptHandle
} }
} }
// note: runBlocking should be used here while we are in realm single thread executor, to avoid thread switching private fun decryptIfNeeded(event: Event, roomId: String) {
private fun decryptIfNeeded(event: Event, roomId: String) = runBlocking {
try { try {
// Event from sync does not have roomId, so add it to the event first // Event from sync does not have roomId, so add it to the event first
val result = cryptoService.decryptEvent(event.copy(roomId = roomId), "") // note: runBlocking should be used here while we are in realm single thread executor, to avoid thread switching
val result = runBlocking { cryptoService.decryptEvent(event.copy(roomId = roomId), "") }
event.mxDecryptionResult = OlmDecryptionResult( event.mxDecryptionResult = OlmDecryptionResult(
payload = result.clearEvent, payload = result.clearEvent,
senderKey = result.senderCurve25519Key, senderKey = result.senderCurve25519Key,