Refactoring
This commit is contained in:
parent
2938fa92c0
commit
f58f3ad6d9
@ -53,14 +53,14 @@ internal fun Map<String, EventEntity>.updateThreadSummaryIfNeeded(
|
|||||||
for ((rootThreadEventId, eventEntity) in this) {
|
for ((rootThreadEventId, eventEntity) in this) {
|
||||||
eventEntity.threadSummaryInThread(eventEntity.realm, rootThreadEventId, chunkEntity)?.let { threadSummary ->
|
eventEntity.threadSummaryInThread(eventEntity.realm, rootThreadEventId, chunkEntity)?.let { threadSummary ->
|
||||||
|
|
||||||
val numberOfMessages = threadSummary.first
|
val inThreadMessages = threadSummary.first
|
||||||
val latestEventInThread = threadSummary.second
|
val latestEventInThread = threadSummary.second
|
||||||
|
|
||||||
// If this is a thread message, find its root event if exists
|
// If this is a thread message, find its root event if exists
|
||||||
val rootThreadEvent = if (eventEntity.isThread()) eventEntity.findRootThreadEvent() else eventEntity
|
val rootThreadEvent = if (eventEntity.isThread()) eventEntity.findRootThreadEvent() else eventEntity
|
||||||
|
|
||||||
rootThreadEvent?.markEventAsRoot(
|
rootThreadEvent?.markEventAsRoot(
|
||||||
threadsCounted = numberOfMessages,
|
inThreadMessages = inThreadMessages,
|
||||||
latestMessageTimelineEventEntity = latestEventInThread
|
latestMessageTimelineEventEntity = latestEventInThread
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@ -86,10 +86,10 @@ internal fun EventEntity.findRootThreadEvent(): EventEntity? =
|
|||||||
* Mark or update the current event a root thread event
|
* Mark or update the current event a root thread event
|
||||||
*/
|
*/
|
||||||
internal fun EventEntity.markEventAsRoot(
|
internal fun EventEntity.markEventAsRoot(
|
||||||
threadsCounted: Int,
|
inThreadMessages: Int,
|
||||||
latestMessageTimelineEventEntity: TimelineEventEntity?) {
|
latestMessageTimelineEventEntity: TimelineEventEntity?) {
|
||||||
isRootThread = true
|
isRootThread = true
|
||||||
numberOfThreads = threadsCounted
|
numberOfThreads = inThreadMessages
|
||||||
threadSummaryLatestMessage = latestMessageTimelineEventEntity
|
threadSummaryLatestMessage = latestMessageTimelineEventEntity
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -100,13 +100,13 @@ internal fun EventEntity.markEventAsRoot(
|
|||||||
* @return A ThreadSummary containing the counted threads and the latest event message
|
* @return A ThreadSummary containing the counted threads and the latest event message
|
||||||
*/
|
*/
|
||||||
internal fun EventEntity.threadSummaryInThread(realm: Realm, rootThreadEventId: String, chunkEntity: ChunkEntity?): Summary {
|
internal fun EventEntity.threadSummaryInThread(realm: Realm, rootThreadEventId: String, chunkEntity: ChunkEntity?): Summary {
|
||||||
val numberOfThread = countThreadReplies(
|
val inThreadMessages = countInThreadMessages(
|
||||||
realm = realm,
|
realm = realm,
|
||||||
roomId = roomId,
|
roomId = roomId,
|
||||||
rootThreadEventId = rootThreadEventId
|
rootThreadEventId = rootThreadEventId
|
||||||
) ?: return null
|
)
|
||||||
|
|
||||||
if (numberOfThread <= 0) return null
|
if (inThreadMessages <= 0) return null
|
||||||
|
|
||||||
// Find latest thread event, we know it exists
|
// Find latest thread event, we know it exists
|
||||||
var chunk = ChunkEntity.findLastForwardChunkOfRoom(realm, roomId) ?: chunkEntity ?: return null
|
var chunk = ChunkEntity.findLastForwardChunkOfRoom(realm, roomId) ?: chunkEntity ?: return null
|
||||||
@ -128,26 +128,26 @@ internal fun EventEntity.threadSummaryInThread(realm: Realm, rootThreadEventId:
|
|||||||
|
|
||||||
result ?: return null
|
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.
|
* 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
|
TimelineEventEntity
|
||||||
.whereRoomId(realm, roomId = roomId)
|
.whereRoomId(realm, roomId = roomId)
|
||||||
.equalTo(TimelineEventEntityFields.ROOT.ROOT_THREAD_EVENT_ID, rootThreadEventId)
|
.equalTo(TimelineEventEntityFields.ROOT.ROOT_THREAD_EVENT_ID, rootThreadEventId)
|
||||||
.distinct(TimelineEventEntityFields.ROOT.EVENT_ID)
|
.distinct(TimelineEventEntityFields.ROOT.EVENT_ID)
|
||||||
.findAll()
|
.findAll()
|
||||||
?.filterNot { timelineEvent ->
|
.filterNot { timelineEvent ->
|
||||||
timelineEvent.root
|
timelineEvent.root
|
||||||
?.unsignedData
|
?.unsignedData
|
||||||
?.takeIf { it.isNotBlank() }
|
?.takeIf { it.isNotBlank() }
|
||||||
?.toUnsignedData()
|
?.toUnsignedData()
|
||||||
.isRedacted()
|
.isRedacted()
|
||||||
}?.size
|
}.size
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Mapping string to UnsignedData using Moshi
|
* Mapping string to UnsignedData using Moshi
|
||||||
|
@ -44,6 +44,7 @@ internal open class EventEntity(@Index var eventId: String = "",
|
|||||||
// Thread related, no need to create a new Entity for performance
|
// Thread related, no need to create a new Entity for performance
|
||||||
@Index var isRootThread: Boolean = false,
|
@Index var isRootThread: Boolean = false,
|
||||||
@Index var rootThreadEventId: String? = null,
|
@Index var rootThreadEventId: String? = null,
|
||||||
|
// Number messages within the thread
|
||||||
var numberOfThreads: Int = 0,
|
var numberOfThreads: Int = 0,
|
||||||
var threadSummaryLatestMessage: TimelineEventEntity? = null
|
var threadSummaryLatestMessage: TimelineEventEntity? = null
|
||||||
) : RealmObject() {
|
) : RealmObject() {
|
||||||
|
@ -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.EventType
|
||||||
import org.matrix.android.sdk.api.session.events.model.LocalEcho
|
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.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.helper.findRootThreadEvent
|
||||||
import org.matrix.android.sdk.internal.database.mapper.ContentMapper
|
import org.matrix.android.sdk.internal.database.mapper.ContentMapper
|
||||||
import org.matrix.android.sdk.internal.database.mapper.EventMapper
|
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 rootThreadEvent = eventToPrune.findRootThreadEvent() ?: return
|
||||||
val rootThreadEventId = eventToPrune.rootThreadEventId ?: return
|
val rootThreadEventId = eventToPrune.rootThreadEventId ?: return
|
||||||
|
|
||||||
val numberOfThreads = countThreadReplies(
|
val inThreadMessages = countInThreadMessages(
|
||||||
realm = realm,
|
realm = realm,
|
||||||
roomId = roomId,
|
roomId = roomId,
|
||||||
rootThreadEventId = rootThreadEventId
|
rootThreadEventId = rootThreadEventId
|
||||||
) ?: return
|
)
|
||||||
|
|
||||||
rootThreadEvent.numberOfThreads = numberOfThreads
|
rootThreadEvent.numberOfThreads = inThreadMessages
|
||||||
if (numberOfThreads == 0) {
|
if (inThreadMessages == 0) {
|
||||||
// We should also clear the thread summary list
|
// We should also clear the thread summary list
|
||||||
rootThreadEvent.isRootThread = false
|
rootThreadEvent.isRootThread = false
|
||||||
rootThreadEvent.threadSummaryLatestMessage = null
|
rootThreadEvent.threadSummaryLatestMessage = null
|
||||||
|
Loading…
Reference in New Issue
Block a user