Revert "NotificationDataFactory: improve API"

This reverts commit 7d7ea5d67c.

# Conflicts:
#	libraries/push/impl/src/test/kotlin/io/element/android/libraries/push/impl/notifications/NotificationDataFactoryTest.kt
This commit is contained in:
Benoit Marty 2025-11-04 15:43:23 +01:00
parent fa55bfe70f
commit be807f4b5c
4 changed files with 46 additions and 37 deletions

View file

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

View file

@ -51,18 +51,10 @@ class NotificationRenderer(
showSessionId = numberOfAccounts > 1,
)
val groupedEvents = eventsToProcess.groupByType()
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 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 summaryNotification = notificationDataFactory.createSummaryNotification(
roomNotifications = roomNotifications,
invitationNotifications = invitationNotifications,

View file

@ -55,7 +55,9 @@ class NotificationDataFactoryTest {
aNotificationAccountParams(),
AN_INVITATION_EVENT,
)
val result = listOf(AN_INVITATION_EVENT).toNotifications(aNotificationAccountParams())
val roomInvitation = listOf(AN_INVITATION_EVENT)
val result = toNotifications(roomInvitation, aNotificationAccountParams())
assertThat(result).isEqualTo(
listOf(
OneShotNotification(
@ -75,7 +77,7 @@ class NotificationDataFactoryTest {
aNotificationAccountParams(),
AN_INVITATION_EVENT,
)
val result = listOf(A_SIMPLE_EVENT).toNotifications(aNotificationAccountParams())
val result = toNotifications(listOf(A_SIMPLE_EVENT), aNotificationAccountParams())
assertThat(result).containsExactly(
OneShotNotification(
notification = expectedNotification,
@ -109,7 +111,8 @@ class NotificationDataFactoryTest {
threadId = null,
)
val fakeImageLoader = FakeImageLoader()
val result = listOf(A_MESSAGE_EVENT).toNotifications(
val result = toNotifications(
messages = listOf(A_MESSAGE_EVENT),
notificationAccountParams = aNotificationAccountParams(
user = MatrixUser(A_SESSION_ID, A_SESSION_ID.value, MY_AVATAR_URL),
),
@ -125,7 +128,8 @@ class NotificationDataFactoryTest {
fun `given a room with only redacted events when mapping to notification then is Empty`() = testWith(notificationDataFactory) {
val redactedRoom = A_MESSAGE_EVENT.copy(isRedacted = true)
val fakeImageLoader = FakeImageLoader()
val result = listOf(redactedRoom).toNotifications(
val result = toNotifications(
messages = listOf(redactedRoom),
notificationAccountParams = aNotificationAccountParams(
user = MatrixUser(A_SESSION_ID, A_SESSION_ID.value, MY_AVATAR_URL),
),
@ -164,7 +168,8 @@ class NotificationDataFactoryTest {
)
val fakeImageLoader = FakeImageLoader()
val result = roomWithRedactedMessage.toNotifications(
val result = toNotifications(
messages = roomWithRedactedMessage,
notificationAccountParams = aNotificationAccountParams(
user = MatrixUser(A_SESSION_ID, A_SESSION_ID.value, MY_AVATAR_URL),
),

View file

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