Skip to content

Commit

Permalink
Add special notifications for mentions (#1846)
Browse files Browse the repository at this point in the history
* Add special notifications for mentions

---------

Co-authored-by: ElementBot <[email protected]>
  • Loading branch information
jmartinesp and ElementBot authored Nov 28, 2023
1 parent 12b3196 commit 4de256b
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 2 deletions.
1 change: 1 addition & 0 deletions changelog.d/1451.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Display different notifications for mentions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ class NotifiableEventResolver @Inject constructor(
return when (val content = this.content) {
is NotificationContent.MessageLike.RoomMessage -> {
val messageBody = descriptionFromMessageContent(content, senderDisplayName ?: content.senderId.value)
val notificationBody = if (hasMention) {
stringProvider.getString(R.string.notification_mentioned_you_body, messageBody)
} else {
messageBody
}
buildNotifiableMessageEvent(
sessionId = userId,
senderId = content.senderId,
Expand All @@ -90,7 +95,7 @@ class NotifiableEventResolver @Inject constructor(
noisy = isNoisy,
timestamp = this.timestamp,
senderName = senderDisplayName,
body = messageBody,
body = notificationBody,
imageUriString = this.contentUrl,
roomName = roomDisplayName,
roomIsDirect = isDirect,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<string name="notification_invitation_action_join">"Vstoupit"</string>
<string name="notification_invitation_action_reject">"Odmítnout"</string>
<string name="notification_invite_body">"Vás pozval(a) do chatu"</string>
<string name="notification_mentioned_you_body">"Zmínili vás: %1$s"</string>
<string name="notification_new_messages">"Nové zprávy"</string>
<string name="notification_reaction_body">"Reagoval(a) s %1$s"</string>
<string name="notification_room_action_mark_as_read">"Označit jako přečtené"</string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ import io.element.android.libraries.matrix.api.room.RoomMembershipState
import io.element.android.libraries.matrix.api.timeline.item.event.AudioMessageType
import io.element.android.libraries.matrix.api.timeline.item.event.EmoteMessageType
import io.element.android.libraries.matrix.api.timeline.item.event.FileMessageType
import io.element.android.libraries.matrix.api.timeline.item.event.FormattedBody
import io.element.android.libraries.matrix.api.timeline.item.event.ImageMessageType
import io.element.android.libraries.matrix.api.timeline.item.event.LocationMessageType
import io.element.android.libraries.matrix.api.timeline.item.event.MessageFormat
import io.element.android.libraries.matrix.api.timeline.item.event.NoticeMessageType
import io.element.android.libraries.matrix.api.timeline.item.event.TextMessageType
import io.element.android.libraries.matrix.api.timeline.item.event.VideoMessageType
Expand All @@ -51,6 +53,7 @@ import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.RuntimeEnvironment
import org.robolectric.annotation.Config

@RunWith(RobolectricTestRunner::class)
class NotifiableEventResolverTest {
Expand Down Expand Up @@ -97,6 +100,71 @@ class NotifiableEventResolverTest {
assertThat(result).isEqualTo(expectedResult)
}

@Test
@Config(qualifiers = "en")
fun `resolve event message with mention`() = runTest {
val sut = createNotifiableEventResolver(
notificationResult = Result.success(
createNotificationData(
content = NotificationContent.MessageLike.RoomMessage(
senderId = A_USER_ID_2,
messageType = TextMessageType(body = "Hello world", formatted = null)
),
hasMention = true,
)
)
)
val result = sut.resolveEvent(A_SESSION_ID, A_ROOM_ID, AN_EVENT_ID)
val expectedResult = createNotifiableMessageEvent(body = "Mentioned you: Hello world")
assertThat(result).isEqualTo(expectedResult)
}

@Test
fun `resolve HTML formatted event message text takes plain text version`() = runTest {
val sut = createNotifiableEventResolver(
notificationResult = Result.success(
createNotificationData(
content = NotificationContent.MessageLike.RoomMessage(
senderId = A_USER_ID_2,
messageType = TextMessageType(
body = "Hello world!",
formatted = FormattedBody(
body = "<b>Hello world</b>",
format = MessageFormat.HTML,
)
)
)
)
)
)
val result = sut.resolveEvent(A_SESSION_ID, A_ROOM_ID, AN_EVENT_ID)
val expectedResult = createNotifiableMessageEvent(body = "Hello world")
assertThat(result).isEqualTo(expectedResult)
}

@Test
fun `resolve incorrectly formatted event message text uses fallback`() = runTest {
val sut = createNotifiableEventResolver(
notificationResult = Result.success(
createNotificationData(
content = NotificationContent.MessageLike.RoomMessage(
senderId = A_USER_ID_2,
messageType = TextMessageType(
body = "Hello world",
formatted = FormattedBody(
body = "???Hello world!???",
format = MessageFormat.UNKNOWN,
)
)
)
)
)
)
val result = sut.resolveEvent(A_SESSION_ID, A_ROOM_ID, AN_EVENT_ID)
val expectedResult = createNotifiableMessageEvent(body = "Hello world")
assertThat(result).isEqualTo(expectedResult)
}

@Test
fun `resolve event message audio`() = runTest {
val sut = createNotifiableEventResolver(
Expand Down Expand Up @@ -430,6 +498,7 @@ class NotifiableEventResolverTest {
private fun createNotificationData(
content: NotificationContent,
isDirect: Boolean = false,
hasMention: Boolean = false,
): NotificationData {
return NotificationData(
eventId = AN_EVENT_ID,
Expand All @@ -444,7 +513,7 @@ class NotifiableEventResolverTest {
timestamp = A_TIMESTAMP,
content = content,
contentUrl = null,
hasMention = false,
hasMention = hasMention,
)
}

Expand Down
3 changes: 3 additions & 0 deletions libraries/ui-strings/src/main/res/values/localazy.xml
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,9 @@
<item quantity="other">"%d votes"</item>
</plurals>
<string name="preference_rageshake">"Rageshake to report bug"</string>
<string name="screen_edit_poll_delete_confirmation">"Are you sure you want to delete this poll?"</string>
<string name="screen_edit_poll_delete_confirmation_title">"Delete Poll"</string>
<string name="screen_edit_poll_title">"Edit poll"</string>
<string name="screen_media_picker_error_failed_selection">"Failed selecting media, please try again."</string>
<string name="screen_media_upload_preview_error_failed_processing">"Failed processing media to upload, please try again."</string>
<string name="screen_media_upload_preview_error_failed_sending">"Failed uploading media, please try again."</string>
Expand Down

0 comments on commit 4de256b

Please sign in to comment.