Skip to content

Commit

Permalink
Wrap only cryptoService.decryptEvent with runBlocking instead of the …
Browse files Browse the repository at this point in the history
…whole methods
  • Loading branch information
ariskotsomitopoulos committed Apr 27, 2022
1 parent 51b4292 commit 5cfe218
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) = runBlocking {
cryptoService ?: return@runBlocking
private fun decryptIfNeeded(cryptoService: CryptoService?, eventEntity: EventEntity, roomId: String) {
cryptoService ?: return
val event = eventEntity.asDomain()
if (event.isEncrypted() && event.mxDecryptionResult == null && event.eventId != null) {
try {
Timber.i("###THREADS ThreadSummaryHelper request decryption for eventId:${event.eventId}")
// 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(
payload = result.clearEvent,
senderKey = result.senderCurve25519Key,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,19 +119,19 @@ 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) = runBlocking {
private fun processDecryptRequest(request: DecryptionRequest, realm: Realm) {
val event = request.event
val timelineId = request.timelineId

if (!request.event.isEncrypted()) {
// Here we have requested a decryption to an event that is not encrypted
// We will simply make this event thread aware
threadAwareNonEncryptedEvents(request, realm)
return@runBlocking
return
}
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}")
realm.executeTransaction {
val eventId = event.eventId ?: return@executeTransaction
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,8 @@ internal class RoomSyncHandler @Inject constructor(private val readReceiptHandle
roomId = roomId,
eventEntity = eventEntity,
direction = PaginationDirection.FORWARDS,
roomMemberContentsByUser = roomMemberContentsByUser)
roomMemberContentsByUser = roomMemberContentsByUser
)
if (lightweightSettingsStorage.areThreadMessagesEnabled()) {
eventEntity.rootThreadEventId?.let {
// This is a thread event
Expand All @@ -439,7 +440,8 @@ internal class RoomSyncHandler @Inject constructor(private val readReceiptHandle
threadEventEntity = eventEntity,
roomMemberContentsByUser = roomMemberContentsByUser,
userId = userId,
roomEntity = roomEntity)
roomEntity = roomEntity
)
}
} ?: run {
// This is a normal event or a root thread one
Expand Down Expand Up @@ -477,7 +479,8 @@ internal class RoomSyncHandler @Inject constructor(private val readReceiptHandle
roomId = roomId,
realm = realm,
chunkEntity = chunkEntity,
currentUserId = userId)
currentUserId = userId
)
}

// posting new events to timeline if any is registered
Expand Down Expand Up @@ -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) = runBlocking {
private fun decryptIfNeeded(event: Event, roomId: String) {
try {
// 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(
payload = result.clearEvent,
senderKey = result.senderCurve25519Key,
Expand Down

0 comments on commit 5cfe218

Please sign in to comment.