Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5f93ad815e | ||
|
|
39945c5e99 | ||
|
|
9be4ee4225 | ||
|
|
8c20c3d3a6 | ||
|
|
9b97fb8c32 | ||
|
|
cef9f28dfd |
@@ -377,12 +377,50 @@ class MatrixSyncService : Service() {
|
||||
val isMessageLike =
|
||||
eventType == "m.room.message" ||
|
||||
eventType == "m.room.encrypted" ||
|
||||
eventType == "m.sticker"
|
||||
eventType == "m.sticker" ||
|
||||
eventType == "m.reaction"
|
||||
if (!isMessageLike) continue
|
||||
if (sender.isNotBlank() && sender == myUserId) continue
|
||||
if (eventId.isNotBlank() && !shownEventIds.add(eventId)) continue
|
||||
|
||||
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 msgtype = content.optString("msgtype")
|
||||
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 {
|
||||
val cachedName = displayNameCache[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. */
|
||||
fun toCircularBitmap(bitmap: Bitmap): Bitmap {
|
||||
val size = minOf(bitmap.width, bitmap.height)
|
||||
|
||||
+1
-1
Submodule cinny updated: fabbbc40af...bd649d4e16
@@ -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. */
|
||||
function notificationBodyFromEvent(mEvent: MatrixEvent): string | undefined {
|
||||
function notificationBodyFromEvent(mx: MatrixClient, mEvent: MatrixEvent): string | undefined {
|
||||
const content = mEvent.getClearContent() ?? mEvent.getContent();
|
||||
const eventType = mEvent.getType();
|
||||
|
||||
if (eventType === 'm.reaction') {
|
||||
const reactionKey = content['m.relates_to']?.key;
|
||||
return reactionKey ? `reacted with ${reactionKey}` : 'reacted to a message';
|
||||
const relates = content['m.relates_to'];
|
||||
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()) {
|
||||
@@ -684,12 +722,26 @@ function MessageNotifications() {
|
||||
: 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({
|
||||
roomName: room.name ?? 'Unknown',
|
||||
roomAvatar,
|
||||
iconBase64,
|
||||
roomAvatar: finalRoomAvatar,
|
||||
iconBase64: finalIconBase64,
|
||||
bigPictureBase64,
|
||||
username,
|
||||
messageBody,
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "paarrot",
|
||||
"version": "4.11.180",
|
||||
"version": "4.11.183",
|
||||
"description": "Paarrot - A Matrix client based on Cinny",
|
||||
"engines": {
|
||||
"node": ">=18.0.0"
|
||||
|
||||
Reference in New Issue
Block a user