NotificationDataFactory: improve API

This commit is contained in:
Benoit Marty
2025-10-26 08:49:48 +01:00
committed by Benoit Marty
parent 031ca4f333
commit b0e5e6cc61
4 changed files with 45 additions and 64 deletions

View File

@@ -28,30 +28,26 @@ import io.element.android.libraries.push.impl.notifications.model.SimpleNotifiab
import io.element.android.services.toolbox.api.strings.StringProvider
interface NotificationDataFactory {
suspend fun toNotifications(
messages: List<NotifiableMessageEvent>,
suspend fun List<NotifiableMessageEvent>.toNotifications(
imageLoader: ImageLoader,
notificationAccountParams: NotificationAccountParams,
): List<RoomNotification>
@JvmName("toNotificationInvites")
@Suppress("INAPPLICABLE_JVM_NAME")
fun toNotifications(
invites: List<InviteNotifiableEvent>,
fun List<InviteNotifiableEvent>.toNotifications(
notificationAccountParams: NotificationAccountParams,
): List<OneShotNotification>
@JvmName("toNotificationSimpleEvents")
@Suppress("INAPPLICABLE_JVM_NAME")
fun toNotifications(
simpleEvents: List<SimpleNotifiableEvent>,
fun List<SimpleNotifiableEvent>.toNotifications(
notificationAccountParams: NotificationAccountParams,
): List<OneShotNotification>
@JvmName("toNotificationFallbackEvents")
@Suppress("INAPPLICABLE_JVM_NAME")
fun toNotifications(
fallback: List<FallbackNotifiableEvent>,
fun List<FallbackNotifiableEvent>.toNotifications(
notificationAccountParams: NotificationAccountParams,
): List<OneShotNotification>
@@ -72,12 +68,11 @@ class DefaultNotificationDataFactory(
private val activeNotificationsProvider: ActiveNotificationsProvider,
private val stringProvider: StringProvider,
) : NotificationDataFactory {
override suspend fun toNotifications(
messages: List<NotifiableMessageEvent>,
override suspend fun List<NotifiableMessageEvent>.toNotifications(
imageLoader: ImageLoader,
notificationAccountParams: NotificationAccountParams,
): List<RoomNotification> {
val messagesToDisplay = messages.filterNot { it.canNotBeDisplayed() }
val messagesToDisplay = filterNot { it.canNotBeDisplayed() }
.groupBy { it.roomId }
return messagesToDisplay.flatMap { (roomId, events) ->
val roomName = events.lastOrNull()?.roomName ?: roomId.value
@@ -114,11 +109,10 @@ class DefaultNotificationDataFactory(
@JvmName("toNotificationInvites")
@Suppress("INAPPLICABLE_JVM_NAME")
override fun toNotifications(
invites: List<InviteNotifiableEvent>,
override fun List<InviteNotifiableEvent>.toNotifications(
notificationAccountParams: NotificationAccountParams,
): List<OneShotNotification> {
return invites.map { event ->
return map { event ->
OneShotNotification(
key = event.roomId.value,
notification = notificationCreator.createRoomInvitationNotification(notificationAccountParams, event),
@@ -131,11 +125,10 @@ class DefaultNotificationDataFactory(
@JvmName("toNotificationSimpleEvents")
@Suppress("INAPPLICABLE_JVM_NAME")
override fun toNotifications(
simpleEvents: List<SimpleNotifiableEvent>,
override fun List<SimpleNotifiableEvent>.toNotifications(
notificationAccountParams: NotificationAccountParams,
): List<OneShotNotification> {
return simpleEvents.map { event ->
return map { event ->
OneShotNotification(
key = event.eventId.value,
notification = notificationCreator.createSimpleEventNotification(notificationAccountParams, event),
@@ -148,11 +141,10 @@ class DefaultNotificationDataFactory(
@JvmName("toNotificationFallbackEvents")
@Suppress("INAPPLICABLE_JVM_NAME")
override fun toNotifications(
fallback: List<FallbackNotifiableEvent>,
override fun List<FallbackNotifiableEvent>.toNotifications(
notificationAccountParams: NotificationAccountParams,
): List<OneShotNotification> {
return fallback.map { event ->
return map { event ->
OneShotNotification(
key = event.eventId.value,
notification = notificationCreator.createFallbackNotification(notificationAccountParams, event),

View File

@@ -51,10 +51,18 @@ class NotificationRenderer(
showSessionId = numberOfAccounts > 1,
)
val groupedEvents = eventsToProcess.groupByType()
val roomNotifications = notificationDataFactory.toNotifications(groupedEvents.roomEvents, imageLoader, notificationAccountParams)
val invitationNotifications = notificationDataFactory.toNotifications(groupedEvents.invitationEvents, notificationAccountParams)
val simpleNotifications = notificationDataFactory.toNotifications(groupedEvents.simpleEvents, notificationAccountParams)
val fallbackNotifications = notificationDataFactory.toNotifications(groupedEvents.fallbackEvents, notificationAccountParams)
val roomNotifications = with(notificationDataFactory) {
groupedEvents.roomEvents.toNotifications(imageLoader, notificationAccountParams)
}
val invitationNotifications = with(notificationDataFactory) {
groupedEvents.invitationEvents.toNotifications(notificationAccountParams)
}
val simpleNotifications = with(notificationDataFactory) {
groupedEvents.simpleEvents.toNotifications(notificationAccountParams)
}
val fallbackNotifications = with(notificationDataFactory) {
groupedEvents.fallbackEvents.toNotifications(notificationAccountParams)
}
val summaryNotification = notificationDataFactory.createSummaryNotification(
roomNotifications = roomNotifications,
invitationNotifications = invitationNotifications,

View File

@@ -55,10 +55,7 @@ class NotificationDataFactoryTest {
aNotificationAccountParams(),
AN_INVITATION_EVENT,
)
val roomInvitation = listOf(AN_INVITATION_EVENT)
val result = toNotifications(roomInvitation, aNotificationAccountParams())
val result = listOf(AN_INVITATION_EVENT).toNotifications(aNotificationAccountParams())
assertThat(result).isEqualTo(
listOf(
OneShotNotification(
@@ -78,19 +75,14 @@ class NotificationDataFactoryTest {
aNotificationAccountParams(),
AN_INVITATION_EVENT,
)
val roomInvitation = listOf(A_SIMPLE_EVENT)
val result = toNotifications(roomInvitation, aNotificationAccountParams())
assertThat(result).isEqualTo(
listOf(
OneShotNotification(
notification = expectedNotification,
key = AN_EVENT_ID.value,
summaryLine = A_SIMPLE_EVENT.description,
isNoisy = A_SIMPLE_EVENT.noisy,
timestamp = AN_INVITATION_EVENT.timestamp
)
val result = listOf(A_SIMPLE_EVENT).toNotifications(aNotificationAccountParams())
assertThat(result).containsExactly(
OneShotNotification(
notification = expectedNotification,
key = AN_EVENT_ID.value,
summaryLine = A_SIMPLE_EVENT.description,
isNoisy = A_SIMPLE_EVENT.noisy,
timestamp = AN_INVITATION_EVENT.timestamp
)
)
}
@@ -116,14 +108,11 @@ class NotificationDataFactoryTest {
shouldBing = events.any { it.noisy },
threadId = null,
)
val roomWithMessage = listOf(A_MESSAGE_EVENT)
val fakeImageLoader = FakeImageLoader()
val result = toNotifications(
val result = listOf(A_MESSAGE_EVENT).toNotifications(
notificationAccountParams = aNotificationAccountParams(
user = MatrixUser(A_SESSION_ID, A_SESSION_ID.value, MY_AVATAR_URL),
),
messages = roomWithMessage,
imageLoader = fakeImageLoader.getImageLoader(),
)
@@ -134,17 +123,14 @@ class NotificationDataFactoryTest {
@Test
fun `given a room with only redacted events when mapping to notification then is Empty`() = testWith(notificationDataFactory) {
val redactedRoom = listOf(A_MESSAGE_EVENT.copy(isRedacted = true))
val redactedRoom = A_MESSAGE_EVENT.copy(isRedacted = true)
val fakeImageLoader = FakeImageLoader()
val result = toNotifications(
val result = listOf(redactedRoom).toNotifications(
notificationAccountParams = aNotificationAccountParams(
user = MatrixUser(A_SESSION_ID, A_SESSION_ID.value, MY_AVATAR_URL),
),
messages = redactedRoom,
imageLoader = fakeImageLoader.getImageLoader(),
)
assertThat(result).isEmpty()
assertThat(fakeImageLoader.getCoilRequests().size).isEqualTo(0)
}
@@ -178,11 +164,10 @@ class NotificationDataFactoryTest {
)
val fakeImageLoader = FakeImageLoader()
val result = toNotifications(
val result = roomWithRedactedMessage.toNotifications(
notificationAccountParams = aNotificationAccountParams(
user = MatrixUser(A_SESSION_ID, A_SESSION_ID.value, MY_AVATAR_URL),
),
messages = roomWithRedactedMessage,
imageLoader = fakeImageLoader.getImageLoader(),
)

View File

@@ -40,39 +40,35 @@ class FakeNotificationDataFactory(
var fallbackEventToNotificationsResult: LambdaOneParamRecorder<List<FallbackNotifiableEvent>, List<OneShotNotification>> =
lambdaRecorder { _ -> emptyList() },
) : NotificationDataFactory {
override suspend fun toNotifications(
messages: List<NotifiableMessageEvent>,
override suspend fun List<NotifiableMessageEvent>.toNotifications(
imageLoader: ImageLoader,
notificationAccountParams: NotificationAccountParams,
): List<RoomNotification> {
return messageEventToNotificationsResult(messages, imageLoader, notificationAccountParams)
return messageEventToNotificationsResult(this, imageLoader, notificationAccountParams)
}
@JvmName("toNotificationInvites")
@Suppress("INAPPLICABLE_JVM_NAME")
override fun toNotifications(
invites: List<InviteNotifiableEvent>,
override fun List<InviteNotifiableEvent>.toNotifications(
notificationAccountParams: NotificationAccountParams,
): List<OneShotNotification> {
return inviteToNotificationsResult(invites)
return inviteToNotificationsResult(this)
}
@JvmName("toNotificationSimpleEvents")
@Suppress("INAPPLICABLE_JVM_NAME")
override fun toNotifications(
simpleEvents: List<SimpleNotifiableEvent>,
override fun List<SimpleNotifiableEvent>.toNotifications(
notificationAccountParams: NotificationAccountParams,
): List<OneShotNotification> {
return simpleEventToNotificationsResult(simpleEvents)
return simpleEventToNotificationsResult(this)
}
@JvmName("toNotificationFallbackEvents")
@Suppress("INAPPLICABLE_JVM_NAME")
override fun toNotifications(
fallback: List<FallbackNotifiableEvent>,
override fun List<FallbackNotifiableEvent>.toNotifications(
notificationAccountParams: NotificationAccountParams,
): List<OneShotNotification> {
return fallbackEventToNotificationsResult(fallback)
return fallbackEventToNotificationsResult(this)
}
override fun createSummaryNotification(