Room summary; handle encrypted events

This commit is contained in:
Benoit Marty 2019-07-01 12:58:10 +02:00 committed by Benoit Marty
parent e1b6f4bd74
commit 5e9ecfbcc0
2 changed files with 31 additions and 7 deletions

View File

@ -46,22 +46,45 @@ internal interface CacheableTimelineEventFactory : TimelineEventFactory {
* It handles decryption, extracting additional data around an event as sender data and relation.
*/
internal class SimpleTimelineEventFactory @Inject constructor(private val roomMemberExtractor: SenderRoomMemberExtractor,
private val relationExtractor: EventRelationExtractor
private val relationExtractor: EventRelationExtractor,
private val cryptoService: CryptoService
) : TimelineEventFactory {

override fun create(eventEntity: EventEntity, realm: Realm): TimelineEvent {
val senderRoomMember = roomMemberExtractor.extractFrom(eventEntity, realm)
val relations = relationExtractor.extractFrom(eventEntity, realm)

val event = eventEntity.asDomain()
if (event.getClearType() == EventType.ENCRYPTED) {
handleEncryptedEvent(event)
}

val isUniqueDisplayName = RoomMembers(realm, eventEntity.roomId).isUniqueDisplayName(senderRoomMember?.displayName)

return TimelineEvent(
eventEntity.asDomain(),
event,
eventEntity.localId,
eventEntity.displayIndex,
senderRoomMember?.displayName,
/* TODO Rebase */ true,
isUniqueDisplayName,
senderRoomMember?.avatarUrl,
eventEntity.sendState,
relations
)
}

private fun handleEncryptedEvent(event: Event) {
Timber.v("Encrypted event: try to decrypt ${event.eventId}")
try {
val result = cryptoService.decryptEvent(event, UUID.randomUUID().toString())
event.setClearData(result)
} catch (failure: Throwable) {
Timber.e(failure, "Encrypted event: decryption failed")
if (failure is MXDecryptionException) {
event.setCryptoError(failure.cryptoError)
}
}
}
}

internal class InMemoryTimelineEventFactory @Inject constructor(private val roomMemberExtractor: SenderRoomMemberExtractor,
@ -125,6 +148,7 @@ internal class InMemoryTimelineEventFactory @Inject constructor(private val room

override fun clear() {
senderCache.clear()
decryptionCache.clear()
}

private data class SenderData(

View File

@ -45,11 +45,11 @@ class RoomSummaryItemFactory @Inject constructor(private val noticeEventFormatte
val latestEvent = roomSummary.latestEvent
if (latestEvent != null) {
val date = latestEvent.root.localDateTime()
val currentData = DateProvider.currentLocalDateTime()
val isSameDay = date.toLocalDate() == currentData.toLocalDate()
latestFormattedEvent = if (latestEvent.root.type == EventType.MESSAGE) {
val currentDate = DateProvider.currentLocalDateTime()
val isSameDay = date.toLocalDate() == currentDate.toLocalDate()
latestFormattedEvent = if (latestEvent.root.getClearType() == EventType.MESSAGE) {
val senderName = latestEvent.senderName() ?: latestEvent.root.senderId
val content = latestEvent.root.content?.toModel<MessageContent>()
val content = latestEvent.root.getClearContent()?.toModel<MessageContent>()
val message = content?.body ?: ""
if (roomSummary.isDirect.not() && senderName != null) {
span {