mirror of
https://github.com/ouchadam/small-talk.git
synced 2025-02-16 12:10:45 +01:00
Merge pull request #149 from ouchadam/feature/room-parsing
Improved room name parsing
This commit is contained in:
commit
416d10a2b6
@ -18,7 +18,7 @@ private const val ENABLED_MATERIAL_YOU = true
|
||||
|
||||
class SettingsItemFactoryTest {
|
||||
|
||||
private val buildMeta = BuildMeta(versionName = "a-version-name", versionCode = 100)
|
||||
private val buildMeta = BuildMeta(versionName = "a-version-name", versionCode = 100, isDebug = false)
|
||||
private val deviceMeta = DeviceMeta(apiVersion = 31)
|
||||
private val fakePushTokenRegistrars = FakePushRegistrars()
|
||||
private val fakeThemeStore = FakeThemeStore()
|
||||
|
@ -56,6 +56,12 @@ internal data class ApiSyncRoom(
|
||||
@SerialName("state") val state: ApiSyncRoomState,
|
||||
@SerialName("account_data") val accountData: ApiAccountData? = 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
|
||||
|
@ -50,6 +50,18 @@ internal sealed class ApiTimelineEvent {
|
||||
)
|
||||
}
|
||||
|
||||
@Serializable
|
||||
@SerialName("m.room.canonical_alias")
|
||||
internal data class CanonicalAlias(
|
||||
@SerialName("event_id") val id: EventId,
|
||||
@SerialName("content") val content: Content,
|
||||
) : ApiTimelineEvent() {
|
||||
|
||||
@Serializable
|
||||
internal data class Content(
|
||||
@SerialName("alias") val alias: String
|
||||
)
|
||||
}
|
||||
|
||||
@Serializable
|
||||
@SerialName("m.room.avatar")
|
||||
|
@ -15,16 +15,16 @@ internal class RoomOverviewProcessor(
|
||||
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(combinedEvents)
|
||||
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 = roomName ?: roomToProcess.directMessage?.let {
|
||||
roomMembersService.find(roomToProcess.roomId, it)?.let { it.displayName ?: it.id.value }
|
||||
},
|
||||
roomName = processedName,
|
||||
roomCreationUtc = roomCreate.utcTimestamp,
|
||||
lastMessage = lastMessage,
|
||||
roomId = roomToProcess.roomId,
|
||||
@ -40,9 +40,10 @@ internal class RoomOverviewProcessor(
|
||||
isEncrypted = isEncrypted,
|
||||
)
|
||||
}
|
||||
|
||||
else -> {
|
||||
previousState.copy(
|
||||
roomName = previousState.roomName ?: roomDisplayName(combinedEvents),
|
||||
roomName = previousState.roomName ?: roomDisplayName(roomToProcess, combinedEvents),
|
||||
lastMessage = lastMessage ?: previousState.lastMessage,
|
||||
roomAvatarUrl = previousState.roomAvatarUrl ?: roomAvatar(
|
||||
roomToProcess.roomId,
|
||||
@ -58,9 +59,13 @@ internal class RoomOverviewProcessor(
|
||||
}
|
||||
}
|
||||
|
||||
private fun roomDisplayName(combinedEvents: List<ApiTimelineEvent>): String? {
|
||||
val roomName = combinedEvents.filterIsInstance<ApiTimelineEvent.RoomName>().lastOrNull()
|
||||
return roomName?.content?.name
|
||||
private suspend fun roomDisplayName(roomToProcess: RoomToProcess, combinedEvents: List<ApiTimelineEvent>): String? {
|
||||
val roomName = combinedEvents.filterIsInstance<ApiTimelineEvent.RoomName>().lastOrNull()?.content?.name
|
||||
?: 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(
|
||||
@ -75,6 +80,7 @@ internal class RoomOverviewProcessor(
|
||||
val filterIsInstance = combinedEvents.filterIsInstance<ApiTimelineEvent.RoomAvatar>()
|
||||
filterIsInstance.lastOrNull()?.content?.url?.convertMxUrToUrl(homeServerUrl)?.let { AvatarUrl(it) }
|
||||
}
|
||||
|
||||
else -> membersService.find(roomId, dmUser)?.avatarUrl
|
||||
}
|
||||
}
|
||||
|
@ -10,4 +10,5 @@ internal data class RoomToProcess(
|
||||
val apiSyncRoom: ApiSyncRoom,
|
||||
val directMessage: UserId?,
|
||||
val userCredentials: UserCredentials,
|
||||
val heroes: List<UserId>?,
|
||||
)
|
@ -30,7 +30,6 @@ internal class SyncReducer(
|
||||
|
||||
suspend fun reduce(isInitialSync: Boolean, sideEffects: SideEffectResult, response: ApiSyncResponse, userCredentials: UserCredentials): ReducerResult {
|
||||
val directMessages = response.directMessages()
|
||||
|
||||
val invites = response.rooms?.invite?.map { roomInvite(it, userCredentials) } ?: emptyList()
|
||||
val roomsLeft = findRoomsLeft(response, userCredentials)
|
||||
val newRooms = response.rooms?.join?.keys?.filterNot { roomDataSource.contains(it) } ?: emptyList()
|
||||
@ -46,6 +45,7 @@ internal class SyncReducer(
|
||||
apiSyncRoom = apiRoom,
|
||||
directMessage = directMessages[roomId],
|
||||
userCredentials = userCredentials,
|
||||
heroes = apiRoom.summary?.heroes,
|
||||
),
|
||||
isInitialSync = isInitialSync
|
||||
)
|
||||
|
@ -38,6 +38,7 @@ internal class TimelineEventsProcessor(
|
||||
is ApiTimelineEvent.RoomMember -> null
|
||||
is ApiTimelineEvent.RoomName -> null
|
||||
is ApiTimelineEvent.RoomTopic -> null
|
||||
is ApiTimelineEvent.CanonicalAlias -> null
|
||||
ApiTimelineEvent.Ignored -> null
|
||||
}
|
||||
roomEvent
|
||||
|
@ -70,4 +70,5 @@ private fun aRoomToProcess(ephemeral: ApiEphemeral? = null) = RoomToProcess(
|
||||
anApiSyncRoom(ephemeral = ephemeral),
|
||||
directMessage = null,
|
||||
userCredentials = aUserCredentials(),
|
||||
heroes = null,
|
||||
)
|
||||
|
@ -101,4 +101,4 @@ internal fun aRoomToProcess(
|
||||
apiSyncRoom: ApiSyncRoom = anApiSyncRoom(),
|
||||
directMessage: UserId? = null,
|
||||
userCredentials: UserCredentials = aUserCredentials(),
|
||||
) = RoomToProcess(roomId, apiSyncRoom, directMessage, userCredentials)
|
||||
) = RoomToProcess(roomId, apiSyncRoom, directMessage, userCredentials, heroes = null)
|
||||
|
Loading…
x
Reference in New Issue
Block a user