notifications: show reaction emoji as icon + quoted message snippet
Reaction notifications previously had no icon and a generic body. Now the reaction emoji is rendered as the notification icon and the body quotes the reacted-to message. Applied to both the web/Electron/Tauri path and the Android background sync service.
This commit is contained in:
@@ -377,12 +377,50 @@ class MatrixSyncService : Service() {
|
|||||||
val isMessageLike =
|
val isMessageLike =
|
||||||
eventType == "m.room.message" ||
|
eventType == "m.room.message" ||
|
||||||
eventType == "m.room.encrypted" ||
|
eventType == "m.room.encrypted" ||
|
||||||
eventType == "m.sticker"
|
eventType == "m.sticker" ||
|
||||||
|
eventType == "m.reaction"
|
||||||
if (!isMessageLike) continue
|
if (!isMessageLike) continue
|
||||||
if (sender.isNotBlank() && sender == myUserId) continue
|
if (sender.isNotBlank() && sender == myUserId) continue
|
||||||
if (eventId.isNotBlank() && !shownEventIds.add(eventId)) continue
|
if (eventId.isNotBlank() && !shownEventIds.add(eventId)) continue
|
||||||
|
|
||||||
val content = event.optJSONObject("content") ?: JSONObject()
|
val content = event.optJSONObject("content") ?: JSONObject()
|
||||||
|
|
||||||
|
// Reactions have no media, so use the emoji as the icon and quote the
|
||||||
|
// reacted-to message as the body.
|
||||||
|
if (eventType == "m.reaction") {
|
||||||
|
val relates = content.optJSONObject("m.relates_to")
|
||||||
|
val key = relates?.optString("key").orEmpty()
|
||||||
|
if (key.isBlank()) continue
|
||||||
|
val parentEventId = relates?.optString("event_id").orEmpty()
|
||||||
|
val snippet =
|
||||||
|
if (parentEventId.isNotBlank())
|
||||||
|
fetchEventBodySnippet(homeserver, token, roomId, parentEventId)
|
||||||
|
else ""
|
||||||
|
val reactionBody =
|
||||||
|
if (snippet.isNotBlank()) "reacted with $key to \"$snippet\""
|
||||||
|
else "reacted with $key"
|
||||||
|
|
||||||
|
val profile = try {
|
||||||
|
resolveProfile(sender, homeserver, token)
|
||||||
|
} catch (e: Exception) {
|
||||||
|
Log.w(TAG, "profile fetch failed: ${e.message}")
|
||||||
|
UserProfile(sender.substringAfter("@").substringBefore(":").ifBlank { "Someone" }, null)
|
||||||
|
}
|
||||||
|
|
||||||
|
Log.i(TAG, "notify room=$roomId type=$eventType sender=$sender")
|
||||||
|
showMessageNotification(
|
||||||
|
nm,
|
||||||
|
roomId,
|
||||||
|
profile.displayName,
|
||||||
|
reactionBody,
|
||||||
|
renderEmojiBitmap(key),
|
||||||
|
null,
|
||||||
|
resolveGroupInfo(roomId, notifyCtx),
|
||||||
|
)
|
||||||
|
notifiedForRoom = true
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
val encrypted = eventType == "m.room.encrypted"
|
val encrypted = eventType == "m.room.encrypted"
|
||||||
val msgtype = content.optString("msgtype")
|
val msgtype = content.optString("msgtype")
|
||||||
val rawBody = content.optString("body")
|
val rawBody = content.optString("body")
|
||||||
@@ -587,6 +625,25 @@ class MatrixSyncService : Service() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Fetch the body of the event a reaction points at, as a short single-line snippet. */
|
||||||
|
private fun fetchEventBodySnippet(
|
||||||
|
homeserver: String,
|
||||||
|
token: String,
|
||||||
|
roomId: String,
|
||||||
|
eventId: String,
|
||||||
|
maxLength: Int = 60,
|
||||||
|
): String {
|
||||||
|
val obj = fetchJson(
|
||||||
|
"$homeserver/_matrix/client/v3/rooms/${urlEncode(roomId)}/event/${urlEncode(eventId)}",
|
||||||
|
token,
|
||||||
|
) ?: return ""
|
||||||
|
val content = obj.optJSONObject("content") ?: return ""
|
||||||
|
val rawBody = content.optString("body")
|
||||||
|
if (rawBody.isBlank()) return ""
|
||||||
|
val trimmed = rawBody.replace(Regex("\\s+"), " ").trim()
|
||||||
|
return if (trimmed.length > maxLength) trimmed.substring(0, maxLength) + "…" else trimmed
|
||||||
|
}
|
||||||
|
|
||||||
private fun resolveProfile(mxid: String, homeserver: String, token: String): UserProfile {
|
private fun resolveProfile(mxid: String, homeserver: String, token: String): UserProfile {
|
||||||
val cachedName = displayNameCache[mxid]
|
val cachedName = displayNameCache[mxid]
|
||||||
if (cachedName != null) return UserProfile(cachedName, avatarCache[mxid])
|
if (cachedName != null) return UserProfile(cachedName, avatarCache[mxid])
|
||||||
@@ -1164,6 +1221,25 @@ class MatrixSyncService : Service() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Render a reaction emoji to a bitmap so it can be used as the notification icon. */
|
||||||
|
fun renderEmojiBitmap(emoji: String): Bitmap? {
|
||||||
|
return try {
|
||||||
|
val size = 128
|
||||||
|
val bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888)
|
||||||
|
val canvas = Canvas(bitmap)
|
||||||
|
val paint = Paint(Paint.ANTI_ALIAS_FLAG)
|
||||||
|
paint.textSize = size * 0.7f
|
||||||
|
paint.textAlign = Paint.Align.CENTER
|
||||||
|
val fontMetrics = paint.fontMetrics
|
||||||
|
val y = size / 2f - (fontMetrics.ascent + fontMetrics.descent) / 2f
|
||||||
|
canvas.drawText(emoji, size / 2f, y, paint)
|
||||||
|
bitmap
|
||||||
|
} catch (e: Exception) {
|
||||||
|
Log.w(TAG, "Failed to render emoji icon: ${e.message}")
|
||||||
|
null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/** Crop a bitmap into a circle for the notification avatar slot. */
|
/** Crop a bitmap into a circle for the notification avatar slot. */
|
||||||
fun toCircularBitmap(bitmap: Bitmap): Bitmap {
|
fun toCircularBitmap(bitmap: Bitmap): Bitmap {
|
||||||
val size = minOf(bitmap.width, bitmap.height)
|
val size = minOf(bitmap.width, bitmap.height)
|
||||||
|
|||||||
@@ -104,14 +104,52 @@ async function waitForEventDecryption(mx: MatrixClient, mEvent: MatrixEvent) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Short, single-line snippet of an event's body for use inside a notification. */
|
||||||
|
function eventBodySnippet(mEvent: MatrixEvent, maxLength = 60): string {
|
||||||
|
const content = mEvent.getClearContent() ?? mEvent.getContent();
|
||||||
|
const rawBody = typeof content.body === 'string' ? content.body : '';
|
||||||
|
if (!rawBody) return '';
|
||||||
|
const trimmed = rawBody.replace(/\s+/g, ' ').trim();
|
||||||
|
return trimmed.length > maxLength ? `${trimmed.slice(0, maxLength)}…` : trimmed;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Render a reaction emoji to a PNG base64 string so it can be used as the notification icon. */
|
||||||
|
async function renderEmojiToBase64(emoji: string): Promise<string | undefined> {
|
||||||
|
try {
|
||||||
|
const size = 128;
|
||||||
|
const canvas = document.createElement('canvas');
|
||||||
|
canvas.width = size;
|
||||||
|
canvas.height = size;
|
||||||
|
const ctx = canvas.getContext('2d');
|
||||||
|
if (!ctx) return undefined;
|
||||||
|
|
||||||
|
ctx.font = `${Math.floor(size * 0.7)}px "Apple Color Emoji","Segoe UI Emoji","Noto Color Emoji",sans-serif`;
|
||||||
|
ctx.textBaseline = 'middle';
|
||||||
|
ctx.textAlign = 'center';
|
||||||
|
ctx.fillText(emoji, size / 2, size / 2);
|
||||||
|
|
||||||
|
const dataUrl = canvas.toDataURL('image/png');
|
||||||
|
return dataUrl.split(',')[1];
|
||||||
|
} catch (err) {
|
||||||
|
console.warn('[Notifications] Failed to render emoji icon:', err);
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/** Build a human-readable notification body from (possibly decrypted) event content. */
|
/** Build a human-readable notification body from (possibly decrypted) event content. */
|
||||||
function notificationBodyFromEvent(mEvent: MatrixEvent): string | undefined {
|
function notificationBodyFromEvent(mx: MatrixClient, mEvent: MatrixEvent): string | undefined {
|
||||||
const content = mEvent.getClearContent() ?? mEvent.getContent();
|
const content = mEvent.getClearContent() ?? mEvent.getContent();
|
||||||
const eventType = mEvent.getType();
|
const eventType = mEvent.getType();
|
||||||
|
|
||||||
if (eventType === 'm.reaction') {
|
if (eventType === 'm.reaction') {
|
||||||
const reactionKey = content['m.relates_to']?.key;
|
const relates = content['m.relates_to'];
|
||||||
return reactionKey ? `reacted with ${reactionKey}` : 'reacted to a message';
|
const reactionKey = relates?.key;
|
||||||
|
const parent = relates?.event_id ? mx.getCachedEventById(relates.event_id) : undefined;
|
||||||
|
const snippet = parent ? eventBodySnippet(parent) : '';
|
||||||
|
|
||||||
|
if (reactionKey && snippet) return `reacted with ${reactionKey} to "${snippet}"`;
|
||||||
|
if (reactionKey) return `reacted with ${reactionKey}`;
|
||||||
|
return 'reacted to a message';
|
||||||
}
|
}
|
||||||
|
|
||||||
if (eventType === 'm.room.encrypted' || mEvent.isDecryptionFailure()) {
|
if (eventType === 'm.room.encrypted' || mEvent.isDecryptionFailure()) {
|
||||||
@@ -684,12 +722,26 @@ function MessageNotifications() {
|
|||||||
: Promise.resolve(undefined),
|
: Promise.resolve(undefined),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
const messageBody = notificationBodyFromEvent(mEvent);
|
// Reactions have no media, so render the reaction emoji as the notification icon.
|
||||||
|
let finalIconBase64 = iconBase64;
|
||||||
|
let finalRoomAvatar = roomAvatar;
|
||||||
|
if (mEvent.getType() === 'm.reaction') {
|
||||||
|
const emoji = (mEvent.getClearContent() ?? mEvent.getContent())['m.relates_to']?.key;
|
||||||
|
if (emoji) {
|
||||||
|
const emojiBase64 = await renderEmojiToBase64(emoji);
|
||||||
|
if (emojiBase64) {
|
||||||
|
finalIconBase64 = emojiBase64;
|
||||||
|
finalRoomAvatar = `data:image/png;base64,${emojiBase64}`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const messageBody = notificationBodyFromEvent(mx, mEvent);
|
||||||
|
|
||||||
notify({
|
notify({
|
||||||
roomName: room.name ?? 'Unknown',
|
roomName: room.name ?? 'Unknown',
|
||||||
roomAvatar,
|
roomAvatar: finalRoomAvatar,
|
||||||
iconBase64,
|
iconBase64: finalIconBase64,
|
||||||
bigPictureBase64,
|
bigPictureBase64,
|
||||||
username,
|
username,
|
||||||
messageBody,
|
messageBody,
|
||||||
|
|||||||
Reference in New Issue
Block a user