Refactoring

This commit is contained in:
ariskotsomitopoulos 2022-03-29 14:28:57 +03:00
parent 2938fa92c0
commit f58f3ad6d9
3 changed files with 18 additions and 17 deletions

View File

@ -53,14 +53,14 @@ internal fun Map<String, EventEntity>.updateThreadSummaryIfNeeded(
for ((rootThreadEventId, eventEntity) in this) {
eventEntity.threadSummaryInThread(eventEntity.realm, rootThreadEventId, chunkEntity)?.let { threadSummary ->
val numberOfMessages = threadSummary.first
val inThreadMessages = threadSummary.first
val latestEventInThread = threadSummary.second
// If this is a thread message, find its root event if exists
val rootThreadEvent = if (eventEntity.isThread()) eventEntity.findRootThreadEvent() else eventEntity
rootThreadEvent?.markEventAsRoot(
threadsCounted = numberOfMessages,
inThreadMessages = inThreadMessages,
latestMessageTimelineEventEntity = latestEventInThread
)
}
@ -86,10 +86,10 @@ internal fun EventEntity.findRootThreadEvent(): EventEntity? =
* Mark or update the current event a root thread event
*/
internal fun EventEntity.markEventAsRoot(
threadsCounted: Int,
inThreadMessages: Int,
latestMessageTimelineEventEntity: TimelineEventEntity?) {
isRootThread = true
numberOfThreads = threadsCounted
numberOfThreads = inThreadMessages
threadSummaryLatestMessage = latestMessageTimelineEventEntity
}
@ -100,13 +100,13 @@ internal fun EventEntity.markEventAsRoot(
* @return A ThreadSummary containing the counted threads and the latest event message
*/
internal fun EventEntity.threadSummaryInThread(realm: Realm, rootThreadEventId: String, chunkEntity: ChunkEntity?): Summary {
val numberOfThread = countThreadReplies(
val inThreadMessages = countInThreadMessages(
realm = realm,
roomId = roomId,
rootThreadEventId = rootThreadEventId
) ?: return null
)
if (numberOfThread <= 0) return null
if (inThreadMessages <= 0) return null
// Find latest thread event, we know it exists
var chunk = ChunkEntity.findLastForwardChunkOfRoom(realm, roomId) ?: chunkEntity ?: return null
@ -128,26 +128,26 @@ internal fun EventEntity.threadSummaryInThread(realm: Realm, rootThreadEventId:
result ?: return null
return Summary(numberOfThread, result)
return Summary(inThreadMessages, result)
}
/**
* Counts the number of threads in the main timeline thread summary,
* Counts the number of thread replies in the main timeline thread summary,
* with respect to redactions.
*/
internal fun countThreadReplies(realm: Realm, roomId: String, rootThreadEventId: String): Int? =
internal fun countInThreadMessages(realm: Realm, roomId: String, rootThreadEventId: String): Int =
TimelineEventEntity
.whereRoomId(realm, roomId = roomId)
.equalTo(TimelineEventEntityFields.ROOT.ROOT_THREAD_EVENT_ID, rootThreadEventId)
.distinct(TimelineEventEntityFields.ROOT.EVENT_ID)
.findAll()
?.filterNot { timelineEvent ->
.filterNot { timelineEvent ->
timelineEvent.root
?.unsignedData
?.takeIf { it.isNotBlank() }
?.toUnsignedData()
.isRedacted()
}?.size
}.size
/**
* Mapping string to UnsignedData using Moshi

View File

@ -44,6 +44,7 @@ internal open class EventEntity(@Index var eventId: String = "",
// Thread related, no need to create a new Entity for performance
@Index var isRootThread: Boolean = false,
@Index var rootThreadEventId: String? = null,
// Number messages within the thread
var numberOfThreads: Int = 0,
var threadSummaryLatestMessage: TimelineEventEntity? = null
) : RealmObject() {

View File

@ -21,7 +21,7 @@ import org.matrix.android.sdk.api.session.events.model.Event
import org.matrix.android.sdk.api.session.events.model.EventType
import org.matrix.android.sdk.api.session.events.model.LocalEcho
import org.matrix.android.sdk.api.session.events.model.UnsignedData
import org.matrix.android.sdk.internal.database.helper.countThreadReplies
import org.matrix.android.sdk.internal.database.helper.countInThreadMessages
import org.matrix.android.sdk.internal.database.helper.findRootThreadEvent
import org.matrix.android.sdk.internal.database.mapper.ContentMapper
import org.matrix.android.sdk.internal.database.mapper.EventMapper
@ -123,14 +123,14 @@ internal class RedactionEventProcessor @Inject constructor() : EventInsertLivePr
val rootThreadEvent = eventToPrune.findRootThreadEvent() ?: return
val rootThreadEventId = eventToPrune.rootThreadEventId ?: return
val numberOfThreads = countThreadReplies(
val inThreadMessages = countInThreadMessages(
realm = realm,
roomId = roomId,
rootThreadEventId = rootThreadEventId
) ?: return
)
rootThreadEvent.numberOfThreads = numberOfThreads
if (numberOfThreads == 0) {
rootThreadEvent.numberOfThreads = inThreadMessages
if (inThreadMessages == 0) {
// We should also clear the thread summary list
rootThreadEvent.isRootThread = false
rootThreadEvent.threadSummaryLatestMessage = null