Render MSC2530 captions in notifications

Change-Id: I0de1c61ded81fe6fc8ef79ec6effc42aca693dc6
This commit is contained in:
SpiritCroc 2023-05-03 20:02:52 +02:00
parent de1cd864cf
commit acf7d374b9
5 changed files with 22 additions and 1 deletions

View file

@ -44,9 +44,12 @@ import org.matrix.android.sdk.api.session.room.getTimelineEvent
import org.matrix.android.sdk.api.session.room.model.Membership import org.matrix.android.sdk.api.session.room.model.Membership
import org.matrix.android.sdk.api.session.room.model.RoomMemberContent import org.matrix.android.sdk.api.session.room.model.RoomMemberContent
import org.matrix.android.sdk.api.session.room.model.message.MessageWithAttachmentContent import org.matrix.android.sdk.api.session.room.model.message.MessageWithAttachmentContent
import org.matrix.android.sdk.api.session.room.model.message.getCaption
import org.matrix.android.sdk.api.session.room.model.message.getFileName
import org.matrix.android.sdk.api.session.room.sender.SenderInfo import org.matrix.android.sdk.api.session.room.sender.SenderInfo
import org.matrix.android.sdk.api.session.room.timeline.TimelineEvent import org.matrix.android.sdk.api.session.room.timeline.TimelineEvent
import org.matrix.android.sdk.api.session.room.timeline.getEditedEventId import org.matrix.android.sdk.api.session.room.timeline.getEditedEventId
import org.matrix.android.sdk.api.session.room.timeline.getLastMessageContent
import org.matrix.android.sdk.api.util.toMatrixItem import org.matrix.android.sdk.api.util.toMatrixItem
import timber.log.Timber import timber.log.Timber
import java.util.UUID import java.util.UUID
@ -130,6 +133,9 @@ class NotifiableEventResolver @Inject constructor(
} }
} }
fun TimelineEvent.getCaption(): String? = (getLastMessageContent() as? MessageWithAttachmentContent)?.getCaption()
fun TimelineEvent.getFilename(): String? = (getLastMessageContent() as? MessageWithAttachmentContent)?.getFileName()
private suspend fun resolveMessageEvent(event: TimelineEvent, session: Session, canBeReplaced: Boolean, isNoisy: Boolean): NotifiableMessageEvent? { private suspend fun resolveMessageEvent(event: TimelineEvent, session: Session, canBeReplaced: Boolean, isNoisy: Boolean): NotifiableMessageEvent? {
// The event only contains an eventId, and roomId (type is m.room.*) , we need to get the displayable content (names, avatar, text, etc...) // The event only contains an eventId, and roomId (type is m.room.*) , we need to get the displayable content (names, avatar, text, etc...)
val room = session.getRoom(event.root.roomId!! /*roomID cannot be null*/) val room = session.getRoom(event.root.roomId!! /*roomID cannot be null*/)
@ -150,6 +156,8 @@ class NotifiableEventResolver @Inject constructor(
senderName = senderDisplayName, senderName = senderDisplayName,
senderId = event.root.senderId, senderId = event.root.senderId,
body = body.toString(), body = body.toString(),
caption = event.getCaption(),
filename = event.getFilename(),
imageUriString = event.fetchImageIfPresent(session)?.toString(), imageUriString = event.fetchImageIfPresent(session)?.toString(),
roomId = event.root.roomId!!, roomId = event.root.roomId!!,
threadId = event.root.getRootThreadEventId(), threadId = event.root.getRootThreadEventId(),
@ -174,6 +182,8 @@ class NotifiableEventResolver @Inject constructor(
senderName = senderDisplayName, senderName = senderDisplayName,
senderId = event.root.senderId, senderId = event.root.senderId,
body = body, body = body,
caption = event.getCaption(),
filename = event.getFilename(),
imageUriString = event.fetchImageIfPresent(session)?.toString(), imageUriString = event.fetchImageIfPresent(session)?.toString(),
roomId = event.root.roomId!!, roomId = event.root.roomId!!,
threadId = event.root.getRootThreadEventId(), threadId = event.root.getRootThreadEventId(),

View file

@ -27,6 +27,8 @@ data class NotifiableMessageEvent(
val senderName: String?, val senderName: String?,
val senderId: String?, val senderId: String?,
val body: String?, val body: String?,
val caption: String?,
val filename: String?,
// We cannot use Uri? type here, as that could trigger a // We cannot use Uri? type here, as that could trigger a
// NotSerializableException when persisting this to storage // NotSerializableException when persisting this to storage
val imageUriString: String?, val imageUriString: String?,

View file

@ -154,6 +154,8 @@ class NotificationBroadcastReceiver : BroadcastReceiver() {
?: context?.getString(R.string.notification_sender_me), ?: context?.getString(R.string.notification_sender_me),
senderId = session.myUserId, senderId = session.myUserId,
body = message, body = message,
caption = null,
filename = null,
imageUriString = null, imageUriString = null,
roomId = room.roomId, roomId = room.roomId,
threadId = threadId, threadId = threadId,

View file

@ -99,12 +99,17 @@ class RoomGroupMessageCreator @Inject constructor(
when { when {
event.isSmartReplyError() -> addMessage(stringProvider.getString(R.string.notification_inline_reply_failed), event.timestamp, senderPerson) event.isSmartReplyError() -> addMessage(stringProvider.getString(R.string.notification_inline_reply_failed), event.timestamp, senderPerson)
else -> { else -> {
val message = NotificationCompat.MessagingStyle.Message(event.body, event.timestamp, senderPerson).also { message -> val body = event.filename.takeIf { event.imageUri != null } ?: event.body
val message = NotificationCompat.MessagingStyle.Message(body, event.timestamp, senderPerson).also { message ->
event.imageUri?.let { event.imageUri?.let {
message.setData("image/", it) message.setData("image/", it)
} }
} }
addMessage(message) addMessage(message)
if (event.imageUri != null && !event.caption.isNullOrEmpty()) {
val captionMessage = NotificationCompat.MessagingStyle.Message(event.caption, event.timestamp, senderPerson)
addMessage(captionMessage)
}
} }
} }
} }

View file

@ -73,6 +73,8 @@ fun aNotifiableMessageEvent(
senderName = "sender-name", senderName = "sender-name",
senderId = "sending-id", senderId = "sending-id",
body = "message-body", body = "message-body",
caption = "caption",
filename = "filename",
roomId = roomId, roomId = roomId,
threadId = threadId, threadId = threadId,
roomName = "room-name", roomName = "room-name",