ensuring that we remove read messages when they come through by respecting the processed type when creating the notifications

This commit is contained in:
Adam Brown 2021-10-11 17:04:07 +01:00
parent 0bdc65b47f
commit c67b9ee81e
3 changed files with 17 additions and 11 deletions

View file

@ -27,16 +27,21 @@ class NotificationFactory @Inject constructor(
private val summaryGroupMessageCreator: SummaryGroupMessageCreator private val summaryGroupMessageCreator: SummaryGroupMessageCreator
) { ) {
fun Map<String, List<NotifiableMessageEvent>>.toNotifications(myUserDisplayName: String, myUserAvatarUrl: String?): List<RoomNotification> { fun Map<String, List<Pair<ProcessedType, NotifiableMessageEvent>>>.toNotifications(myUserDisplayName: String, myUserAvatarUrl: String?): List<RoomNotification> {
return map { (roomId, events) -> return map { (roomId, events) ->
when { when {
events.hasNoEventsToDisplay() -> RoomNotification.Removed(roomId) events.hasNoEventsToDisplay() -> RoomNotification.Removed(roomId)
else -> roomGroupMessageCreator.createRoomMessage(events, roomId, myUserDisplayName, myUserAvatarUrl) else -> {
val messageEvents = events.filter { it.first == ProcessedType.KEEP }.map { it.second }
roomGroupMessageCreator.createRoomMessage(messageEvents, roomId, myUserDisplayName, myUserAvatarUrl)
}
} }
} }
} }
private fun List<NotifiableMessageEvent>.hasNoEventsToDisplay() = isEmpty() || all { it.canNotBeDisplayed() } private fun List<Pair<ProcessedType, NotifiableMessageEvent>>.hasNoEventsToDisplay() = isEmpty() || all {
it.first == ProcessedType.REMOVE || it.second.canNotBeDisplayed()
}
private fun NotifiableMessageEvent.canNotBeDisplayed() = isRedacted private fun NotifiableMessageEvent.canNotBeDisplayed() = isRedacted

View file

@ -34,7 +34,7 @@ class NotificationRenderer @Inject constructor(private val notificationDisplayer
myUserDisplayName: String, myUserDisplayName: String,
myUserAvatarUrl: String?, myUserAvatarUrl: String?,
useCompleteNotificationFormat: Boolean, useCompleteNotificationFormat: Boolean,
eventsToProcess: List<Pair<ProcessedType, NotifiableEvent>>) { eventsToProcess: List<ProcessedEvent>) {
val (roomEvents, simpleEvents, invitationEvents) = eventsToProcess.groupByType() val (roomEvents, simpleEvents, invitationEvents) = eventsToProcess.groupByType()
with(notificationFactory) { with(notificationFactory) {
val roomNotifications = roomEvents.toNotifications(myUserDisplayName, myUserAvatarUrl) val roomNotifications = roomEvents.toNotifications(myUserDisplayName, myUserAvatarUrl)
@ -108,8 +108,8 @@ class NotificationRenderer @Inject constructor(private val notificationDisplayer
} }
} }
private fun List<Pair<ProcessedType, NotifiableEvent>>.groupByType(): GroupedNotificationEvents { private fun List<ProcessedEvent>.groupByType(): GroupedNotificationEvents {
val roomIdToEventMap: MutableMap<String, MutableList<NotifiableMessageEvent>> = LinkedHashMap() val roomIdToEventMap: MutableMap<String, MutableList<Pair<ProcessedType, NotifiableMessageEvent>>> = LinkedHashMap()
val simpleEvents: MutableList<Pair<ProcessedType, SimpleNotifiableEvent>> = ArrayList() val simpleEvents: MutableList<Pair<ProcessedType, SimpleNotifiableEvent>> = ArrayList()
val invitationEvents: MutableList<Pair<ProcessedType, InviteNotifiableEvent>> = ArrayList() val invitationEvents: MutableList<Pair<ProcessedType, InviteNotifiableEvent>> = ArrayList()
forEach { forEach {
@ -117,7 +117,7 @@ private fun List<Pair<ProcessedType, NotifiableEvent>>.groupByType(): GroupedNot
is InviteNotifiableEvent -> invitationEvents.add(it.asPair()) is InviteNotifiableEvent -> invitationEvents.add(it.asPair())
is NotifiableMessageEvent -> { is NotifiableMessageEvent -> {
val roomEvents = roomIdToEventMap.getOrPut(event.roomId) { ArrayList() } val roomEvents = roomIdToEventMap.getOrPut(event.roomId) { ArrayList() }
roomEvents.add(event) roomEvents.add(it.asPair())
} }
is SimpleNotifiableEvent -> simpleEvents.add(it.asPair()) is SimpleNotifiableEvent -> simpleEvents.add(it.asPair())
} }
@ -129,7 +129,7 @@ private fun List<Pair<ProcessedType, NotifiableEvent>>.groupByType(): GroupedNot
private fun <T : NotifiableEvent> Pair<ProcessedType, *>.asPair(): Pair<ProcessedType, T> = this as Pair<ProcessedType, T> private fun <T : NotifiableEvent> Pair<ProcessedType, *>.asPair(): Pair<ProcessedType, T> = this as Pair<ProcessedType, T>
data class GroupedNotificationEvents( data class GroupedNotificationEvents(
val roomEvents: Map<String, List<NotifiableMessageEvent>>, val roomEvents: Map<String, List<Pair<ProcessedType, NotifiableMessageEvent>>>,
val simpleEvents: List<Pair<ProcessedType, SimpleNotifiableEvent>>, val simpleEvents: List<Pair<ProcessedType, SimpleNotifiableEvent>>,
val invitationEvents: List<Pair<ProcessedType, InviteNotifiableEvent>> val invitationEvents: List<Pair<ProcessedType, InviteNotifiableEvent>>
) )

View file

@ -105,7 +105,7 @@ class NotificationFactoryTest {
fun `given room with message when mapping to notification then delegates to room group message creator`() = testWith(notificationFactory) { fun `given room with message when mapping to notification then delegates to room group message creator`() = testWith(notificationFactory) {
val events = listOf(A_MESSAGE_EVENT) val events = listOf(A_MESSAGE_EVENT)
val expectedNotification = roomGroupMessageCreator.givenCreatesRoomMessageFor(events, A_ROOM_ID, MY_USER_ID, MY_AVATAR_URL) val expectedNotification = roomGroupMessageCreator.givenCreatesRoomMessageFor(events, A_ROOM_ID, MY_USER_ID, MY_AVATAR_URL)
val roomWithMessage = mapOf(A_ROOM_ID to events) val roomWithMessage = mapOf(A_ROOM_ID to listOf(ProcessedType.KEEP to A_MESSAGE_EVENT))
val result = roomWithMessage.toNotifications(MY_USER_ID, MY_AVATAR_URL) val result = roomWithMessage.toNotifications(MY_USER_ID, MY_AVATAR_URL)
@ -114,7 +114,8 @@ class NotificationFactoryTest {
@Test @Test
fun `given a room with no events to display when mapping to notification then is Empty`() = testWith(notificationFactory) { fun `given a room with no events to display when mapping to notification then is Empty`() = testWith(notificationFactory) {
val emptyRoom: Map<String, List<NotifiableMessageEvent>> = mapOf(A_ROOM_ID to emptyList()) val events = listOf(ProcessedType.REMOVE to A_MESSAGE_EVENT)
val emptyRoom = mapOf(A_ROOM_ID to events)
val result = emptyRoom.toNotifications(MY_USER_ID, MY_AVATAR_URL) val result = emptyRoom.toNotifications(MY_USER_ID, MY_AVATAR_URL)
@ -125,7 +126,7 @@ class NotificationFactoryTest {
@Test @Test
fun `given a room with only redacted events when mapping to notification then is Empty`() = testWith(notificationFactory) { fun `given a room with only redacted events when mapping to notification then is Empty`() = testWith(notificationFactory) {
val redactedRoom = mapOf(A_ROOM_ID to listOf(A_MESSAGE_EVENT.copy(isRedacted = true))) val redactedRoom = mapOf(A_ROOM_ID to listOf(ProcessedType.KEEP to A_MESSAGE_EVENT.copy(isRedacted = true)))
val result = redactedRoom.toNotifications(MY_USER_ID, MY_AVATAR_URL) val result = redactedRoom.toNotifications(MY_USER_ID, MY_AVATAR_URL)