taking heroes into account when processing room display name

This commit is contained in:
Adam Brown 2022-09-19 20:49:56 +01:00
parent 355d4a021c
commit d30925fd5a
4 changed files with 16 additions and 8 deletions

View File

@ -56,6 +56,12 @@ internal data class ApiSyncRoom(
@SerialName("state") val state: ApiSyncRoomState, @SerialName("state") val state: ApiSyncRoomState,
@SerialName("account_data") val accountData: ApiAccountData? = null, @SerialName("account_data") val accountData: ApiAccountData? = null,
@SerialName("ephemeral") val ephemeral: ApiEphemeral? = null, @SerialName("ephemeral") val ephemeral: ApiEphemeral? = null,
@SerialName("summary") val summary: ApiRoomSummary? = null,
)
@Serializable
internal data class ApiRoomSummary(
@SerialName("m.heroes") val heroes: List<UserId>? = null
) )
@Serializable @Serializable

View File

@ -15,16 +15,14 @@ internal class RoomOverviewProcessor(
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 combinedEvents = roomToProcess.apiSyncRoom.state.stateEvents + roomToProcess.apiSyncRoom.timeline.apiTimelineEvents
val isEncrypted = combinedEvents.any { it is ApiTimelineEvent.Encryption } val isEncrypted = combinedEvents.any { it is ApiTimelineEvent.Encryption }
val readMarker = roomToProcess.apiSyncRoom.accountData?.events?.filterIsInstance<ApiAccountEvent.FullyRead>()?.firstOrNull()?.content?.eventId val readMarker = roomToProcess.apiSyncRoom.accountData?.events?.filterIsInstance<ApiAccountEvent.FullyRead>()?.firstOrNull()?.content?.eventId
return when (previousState) { return when (previousState) {
null -> combinedEvents.filterIsInstance<ApiTimelineEvent.RoomCreate>().first().let { roomCreate -> null -> combinedEvents.filterIsInstance<ApiTimelineEvent.RoomCreate>().first().let { roomCreate ->
val roomName = roomDisplayName(combinedEvents) val roomName = roomDisplayName(roomToProcess, combinedEvents)
val isGroup = roomToProcess.directMessage == null val isGroup = roomToProcess.directMessage == null
val processedName = roomName ?: roomToProcess.directMessage?.let { val processedName = roomName ?: roomToProcess.directMessage?.let {
roomMembersService.find(roomToProcess.roomId, it)?.let { it.displayName ?: it.id.value } roomMembersService.find(roomToProcess.roomId, it)?.let { it.displayName ?: it.id.value }
} }
RoomOverview( RoomOverview(
roomName = processedName, roomName = processedName,
roomCreationUtc = roomCreate.utcTimestamp, roomCreationUtc = roomCreate.utcTimestamp,
@ -45,7 +43,7 @@ internal class RoomOverviewProcessor(
else -> { else -> {
previousState.copy( previousState.copy(
roomName = previousState.roomName ?: roomDisplayName(combinedEvents), roomName = previousState.roomName ?: roomDisplayName(roomToProcess, combinedEvents),
lastMessage = lastMessage ?: previousState.lastMessage, lastMessage = lastMessage ?: previousState.lastMessage,
roomAvatarUrl = previousState.roomAvatarUrl ?: roomAvatar( roomAvatarUrl = previousState.roomAvatarUrl ?: roomAvatar(
roomToProcess.roomId, roomToProcess.roomId,
@ -61,10 +59,13 @@ internal class RoomOverviewProcessor(
} }
} }
private fun roomDisplayName(combinedEvents: List<ApiTimelineEvent>): String? { private suspend fun roomDisplayName(roomToProcess: RoomToProcess, combinedEvents: List<ApiTimelineEvent>): String? {
val roomName = combinedEvents.filterIsInstance<ApiTimelineEvent.RoomName>().lastOrNull() val roomName = combinedEvents.filterIsInstance<ApiTimelineEvent.RoomName>().lastOrNull()?.content?.name
return (roomName?.content?.name)
?: combinedEvents.filterIsInstance<ApiTimelineEvent.CanonicalAlias>().lastOrNull()?.content?.alias ?: combinedEvents.filterIsInstance<ApiTimelineEvent.CanonicalAlias>().lastOrNull()?.content?.alias
?: roomToProcess.heroes?.let {
roomMembersService.find(roomToProcess.roomId, it).joinToString { it.displayName ?: it.id.value }
}
return roomName?.takeIf { it.isNotEmpty() }
} }
private suspend fun roomAvatar( private suspend fun roomAvatar(

View File

@ -10,4 +10,5 @@ internal data class RoomToProcess(
val apiSyncRoom: ApiSyncRoom, val apiSyncRoom: ApiSyncRoom,
val directMessage: UserId?, val directMessage: UserId?,
val userCredentials: UserCredentials, val userCredentials: UserCredentials,
val heroes: List<UserId>?,
) )

View File

@ -30,7 +30,6 @@ internal class SyncReducer(
suspend fun reduce(isInitialSync: Boolean, sideEffects: SideEffectResult, response: ApiSyncResponse, userCredentials: UserCredentials): ReducerResult { suspend fun reduce(isInitialSync: Boolean, sideEffects: SideEffectResult, response: ApiSyncResponse, userCredentials: UserCredentials): ReducerResult {
val directMessages = response.directMessages() val directMessages = response.directMessages()
val invites = response.rooms?.invite?.map { roomInvite(it, userCredentials) } ?: emptyList() val invites = response.rooms?.invite?.map { roomInvite(it, userCredentials) } ?: emptyList()
val roomsLeft = findRoomsLeft(response, userCredentials) val roomsLeft = findRoomsLeft(response, userCredentials)
val newRooms = response.rooms?.join?.keys?.filterNot { roomDataSource.contains(it) } ?: emptyList() val newRooms = response.rooms?.join?.keys?.filterNot { roomDataSource.contains(it) } ?: emptyList()
@ -46,6 +45,7 @@ internal class SyncReducer(
apiSyncRoom = apiRoom, apiSyncRoom = apiRoom,
directMessage = directMessages[roomId], directMessage = directMessages[roomId],
userCredentials = userCredentials, userCredentials = userCredentials,
heroes = apiRoom.summary?.heroes,
), ),
isInitialSync = isInitialSync isInitialSync = isInitialSync
) )