fixing replies and image messages jumping to the wrong position

This commit is contained in:
Adam Brown 2022-06-08 18:51:59 +01:00
parent e3ac2586ad
commit 8743052899
3 changed files with 30 additions and 3 deletions

View File

@ -20,7 +20,7 @@ internal class MergeWithLocalEchosUseCaseImpl(
val existingWithEcho = updateExistingEventsWithLocalEchoMeta(roomState, echosByEventId)
val sortedEvents = (existingWithEcho + uniqueEchos)
.sortedByDescending { if (it is RoomEvent.Message) it.utcTimestamp else null }
.sortedByDescending { it.utcTimestamp }
.distinctBy { it.eventId }
return roomState.copy(events = sortedEvents)
}

View File

@ -10,6 +10,7 @@ import org.amshove.kluent.shouldBeEqualTo
import org.junit.Test
private val A_ROOM_MESSAGE_EVENT = aRoomMessageEvent(eventId = anEventId("1"))
private val A_ROOM_IMAGE_MESSAGE_EVENT = aRoomMessageEvent(eventId = anEventId("2"))
private val A_LOCAL_ECHO_EVENT_ID = anEventId("2")
private const val A_LOCAL_ECHO_BODY = "body"
private val A_ROOM_MEMBER = aRoomMember()
@ -21,7 +22,7 @@ class MergeWithLocalEchosUseCaseTest {
private val mergeWithLocalEchosUseCase = MergeWithLocalEchosUseCaseImpl(fakeLocalEchoMapper.instance)
@Test
fun `given no local echos, when merging, then returns original state`() {
fun `given no local echos, when merging text message, then returns original state`() {
val roomState = aRoomState(events = listOf(A_ROOM_MESSAGE_EVENT))
val result = mergeWithLocalEchosUseCase.invoke(roomState, A_ROOM_MEMBER, emptyList())
@ -29,6 +30,15 @@ class MergeWithLocalEchosUseCaseTest {
result shouldBeEqualTo roomState
}
@Test
fun `given no local echos, when merging events, then returns original ordered by timestamp descending`() {
val roomState = aRoomState(events = listOf(A_ROOM_IMAGE_MESSAGE_EVENT.copy(utcTimestamp = 1500), A_ROOM_MESSAGE_EVENT.copy(utcTimestamp = 1000)))
val result = mergeWithLocalEchosUseCase.invoke(roomState, A_ROOM_MEMBER, emptyList())
result shouldBeEqualTo roomState.copy(events = roomState.events.sortedByDescending { it.utcTimestamp })
}
@Test
fun `given local echo with sending state, when merging then maps to room event with local echo state`() {
val second = createLocalEcho(A_LOCAL_ECHO_EVENT_ID, A_LOCAL_ECHO_BODY, state = MessageService.LocalEcho.State.Sending)

View File

@ -14,6 +14,16 @@ fun aRoomMessageEvent(
edited: Boolean = false,
) = RoomEvent.Message(eventId, utcTimestamp, content, author, meta, encryptedContent, edited)
fun aRoomImageMessageEvent(
eventId: EventId = anEventId(),
utcTimestamp: Long = 0L,
content: RoomEvent.Image.ImageMeta = anImageMeta(),
author: RoomMember = aRoomMember(),
meta: MessageMeta = MessageMeta.FromServer,
encryptedContent: RoomEvent.Message.MegOlmV1? = null,
edited: Boolean = false,
) = RoomEvent.Image(eventId, utcTimestamp, content, author, meta, encryptedContent, edited)
fun aRoomReplyMessageEvent(
message: RoomEvent = aRoomMessageEvent(),
replyingTo: RoomEvent = aRoomMessageEvent(eventId = anEventId("in-reply-to-id")),
@ -34,4 +44,11 @@ fun aMegolmV1(
deviceId: DeviceId = aDeviceId(),
senderKey: String = "a-sender-key",
sessionId: SessionId = aSessionId(),
) = RoomEvent.Message.MegOlmV1(cipherText, deviceId, senderKey, sessionId)
) = RoomEvent.Message.MegOlmV1(cipherText, deviceId, senderKey, sessionId)
fun anImageMeta(
width: Int? = 100,
height: Int? = 100,
url: String = "https://a-url.com",
keys: RoomEvent.Image.ImageMeta.Keys? = null
) = RoomEvent.Image.ImageMeta(width, height, url, keys)