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.RoomMemberContent
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.timeline.TimelineEvent
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 timber.log.Timber
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? {
// 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*/)
@ -150,6 +156,8 @@ class NotifiableEventResolver @Inject constructor(
senderName = senderDisplayName,
senderId = event.root.senderId,
body = body.toString(),
caption = event.getCaption(),
filename = event.getFilename(),
imageUriString = event.fetchImageIfPresent(session)?.toString(),
roomId = event.root.roomId!!,
threadId = event.root.getRootThreadEventId(),
@ -174,6 +182,8 @@ class NotifiableEventResolver @Inject constructor(
senderName = senderDisplayName,
senderId = event.root.senderId,
body = body,
caption = event.getCaption(),
filename = event.getFilename(),
imageUriString = event.fetchImageIfPresent(session)?.toString(),
roomId = event.root.roomId!!,
threadId = event.root.getRootThreadEventId(),

View file

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

View file

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

View file

@ -99,12 +99,17 @@ class RoomGroupMessageCreator @Inject constructor(
when {
event.isSmartReplyError() -> addMessage(stringProvider.getString(R.string.notification_inline_reply_failed), event.timestamp, senderPerson)
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 {
message.setData("image/", it)
}
}
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",
senderId = "sending-id",
body = "message-body",
caption = "caption",
filename = "filename",
roomId = roomId,
threadId = threadId,
roomName = "room-name",