filtering out spaces from the room overviews

This commit is contained in:
Adam Brown 2022-09-19 22:00:31 +01:00
parent 416d10a2b6
commit 3d7730bc32
3 changed files with 42 additions and 29 deletions

View File

@ -21,7 +21,13 @@ internal sealed class ApiTimelineEvent {
@Serializable
internal data class Content(
@SerialName("type") val type: String? = null
)
) {
object Type {
const val SPACE = "m.space"
}
}
}
@Serializable

View File

@ -12,33 +12,38 @@ internal class RoomOverviewProcessor(
private val roomMembersService: RoomMembersService,
) {
suspend fun process(roomToProcess: RoomToProcess, previousState: RoomOverview?, lastMessage: LastMessage?): RoomOverview {
suspend fun process(roomToProcess: RoomToProcess, previousState: RoomOverview?, lastMessage: LastMessage?): RoomOverview? {
val combinedEvents = roomToProcess.apiSyncRoom.state.stateEvents + roomToProcess.apiSyncRoom.timeline.apiTimelineEvents
val isEncrypted = combinedEvents.any { it is ApiTimelineEvent.Encryption }
val readMarker = roomToProcess.apiSyncRoom.accountData?.events?.filterIsInstance<ApiAccountEvent.FullyRead>()?.firstOrNull()?.content?.eventId
return when (previousState) {
null -> combinedEvents.filterIsInstance<ApiTimelineEvent.RoomCreate>().first().let { roomCreate ->
val roomName = roomDisplayName(roomToProcess, combinedEvents)
val isGroup = roomToProcess.directMessage == null
val processedName = roomName ?: roomToProcess.directMessage?.let {
roomMembersService.find(roomToProcess.roomId, it)?.let { it.displayName ?: it.id.value }
when (roomCreate.content.type) {
ApiTimelineEvent.RoomCreate.Content.Type.SPACE -> null
else -> {
val roomName = roomDisplayName(roomToProcess, combinedEvents)
val isGroup = roomToProcess.directMessage == null
val processedName = roomName ?: roomToProcess.directMessage?.let {
roomMembersService.find(roomToProcess.roomId, it)?.let { it.displayName ?: it.id.value }
}
RoomOverview(
roomName = processedName,
roomCreationUtc = roomCreate.utcTimestamp,
lastMessage = lastMessage,
roomId = roomToProcess.roomId,
isGroup = isGroup,
roomAvatarUrl = roomAvatar(
roomToProcess.roomId,
roomMembersService,
roomToProcess.directMessage,
combinedEvents,
roomToProcess.userCredentials.homeServer
),
readMarker = readMarker,
isEncrypted = isEncrypted,
)
}
}
RoomOverview(
roomName = processedName,
roomCreationUtc = roomCreate.utcTimestamp,
lastMessage = lastMessage,
roomId = roomToProcess.roomId,
isGroup = isGroup,
roomAvatarUrl = roomAvatar(
roomToProcess.roomId,
roomMembersService,
roomToProcess.directMessage,
combinedEvents,
roomToProcess.userCredentials.homeServer
),
readMarker = readMarker,
isEncrypted = isEncrypted,
)
}
else -> {

View File

@ -17,7 +17,7 @@ internal class RoomProcessor(
private val ephemeralEventsUseCase: EphemeralEventsUseCase,
) {
suspend fun processRoom(roomToProcess: RoomToProcess, isInitialSync: Boolean): RoomState {
suspend fun processRoom(roomToProcess: RoomToProcess, isInitialSync: Boolean): RoomState? {
val members = roomToProcess.apiSyncRoom.collectMembers(roomToProcess.userCredentials)
roomMembersService.insert(roomToProcess.roomId, members)
@ -28,16 +28,17 @@ internal class RoomProcessor(
previousState?.events ?: emptyList(),
)
val overview = createRoomOverview(distinctEvents, roomToProcess, previousState)
unreadEventsProcessor.processUnreadState(overview, previousState?.roomOverview, newEvents, roomToProcess.userCredentials.userId, isInitialSync)
return createRoomOverview(distinctEvents, roomToProcess, previousState)?.let {
unreadEventsProcessor.processUnreadState(it, previousState?.roomOverview, newEvents, roomToProcess.userCredentials.userId, isInitialSync)
return RoomState(overview, distinctEvents).also {
roomDataSource.persist(roomToProcess.roomId, previousState, it)
ephemeralEventsUseCase.processEvents(roomToProcess)
RoomState(it, distinctEvents).also {
roomDataSource.persist(roomToProcess.roomId, previousState, it)
ephemeralEventsUseCase.processEvents(roomToProcess)
}
}
}
private suspend fun createRoomOverview(distinctEvents: List<RoomEvent>, roomToProcess: RoomToProcess, previousState: RoomState?): RoomOverview {
private suspend fun createRoomOverview(distinctEvents: List<RoomEvent>, roomToProcess: RoomToProcess, previousState: RoomState?): RoomOverview? {
val lastMessage = distinctEvents.sortedByDescending { it.utcTimestamp }.findLastMessage()
return roomOverviewProcessor.process(roomToProcess, previousState?.roomOverview, lastMessage)
}
@ -56,6 +57,7 @@ private fun ApiSyncRoom.collectMembers(userCredentials: UserCredentials): List<R
avatarUrl = it.content.avatarUrl?.convertMxUrToUrl(userCredentials.homeServer)?.let { AvatarUrl(it) },
)
}
else -> null
}
}