diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 9742edea..8159b4a4 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -28,6 +28,11 @@ jobs: python3 test_otp_desktop.py - name: Mac tools compile (python3 syntax, no macOS needed) run: python3 -m py_compile bridge/linux/blip-bridged bridge/mac/imsg bridge/mac/imsg-send bridge/mac/contacts bridge/mac/tcc-check bridge/mac/blip-check bridge/mac/blip-dispatch bridge/mac/imsg-read bridge/mac/imsg-react bridge/mac/calling_codes.py bridge/mac/contact-save bridge/mac/read_state.py bridge/mac/findmy_city.py + - name: Sticker metadata and bounded animated rendering + run: | + sudo apt-get install -y -q ffmpeg imagemagick qt6-image-formats-plugins + python3 bridge/mac/test_stickers.py + python3 scripts/test-sticker-ui.py - name: shared city (synthetic AX and Contacts, no macOS needed) run: python3 bridge/mac/test_findmy_city.py - name: read pushes require verified completion (no macOS needed) diff --git a/BlipView.qml b/BlipView.qml index 1d62cf8f..2b593da6 100644 --- a/BlipView.qml +++ b/BlipView.qml @@ -1,3 +1,4 @@ +import "Stickers.mjs" as Stickers import "SendState.mjs" as SendState import "MessageActions.mjs" as MessageActions import "SourceId.mjs" as SourceId @@ -1143,11 +1144,12 @@ FocusScope { } } fetchQueue.push({ id: id, name: String(att.name || "file"), - mime: String(att.mime || ""), action: action || "", + mime: String(att.mime || ""), sticker: !!att.sticker, action: action || "", auto: auto === true }) pumpFetch() } + property bool fetchJobSticker: false property string fetchJobAction: "" property string fetchJobMime: "" property string fetchJobName: "" @@ -1158,8 +1160,9 @@ FocusScope { fetchJobAction = job.action fetchJobMime = job.mime fetchJobName = job.name + fetchJobSticker = job.sticker === true // Auto-pulls carry a hard transfer cap: claimed metadata is not the limit. - fetchProc.command = ["bun", root.fetchScript, job.id, job.name, job.mime, job.auto ? "5242880" : ""] + fetchProc.command = ["bun", root.fetchScript, job.id, job.name, job.mime, job.auto ? "5242880" : ""].concat(job.sticker ? ["--sticker"] : []) fetchProc.running = true } @@ -1171,7 +1174,7 @@ FocusScope { function autoFetchImages() { if (!inThread) return for (var i = 0; i < bubbles.length; i++) { - var atts = bubbles[i].attachments || [] + var atts = (bubbles[i].attachments || []).concat((bubbles[i].stickers || []).map(function(sticker) { return sticker.attachment })) for (var j = 0; j < atts.length; j++) { // Bytes must be KNOWN — null/0 must not slip through and auto-pull // something the click-path 100 MB ceiling would allow. The SOURCE cap @@ -1180,7 +1183,7 @@ FocusScope { // while sips turns a 5.07 MB HEIC into a 7.80 MB JPEG, so full-size // iPhone photos were rejected at both ends and simply never appeared. var b = atts[j].bytes - if (isImageMime(atts[j].mime) && typeof b === "number" && b > 0 && b <= root.autoFetchMaxSource) + if ((isImageMime(atts[j].mime) || (atts[j].sticker && String(atts[j].mime || "").indexOf("video/") === 0)) && typeof b === "number" && b > 0 && b <= root.autoFetchMaxSource) enqueueFetch(atts[j], "", true) } // link-card preview PNGs are small; the auto-fetch transfer cap bounds them @@ -1192,6 +1195,15 @@ FocusScope { /** Chip/image click: fetch-then-open. ALWAYS round-trips fetch.ts, even * when attFiles already has a url — the LRU may have evicted the file * since (Codex finding #5); a cache hit is instant anyway. */ + function stickerLoading(id) { + return id !== "" && (root.fetchingId === id || root.fetchQueue.some(function(job) { return job.id === id })) + } + function activateSticker(att) { + var id = String(att.id || "") + if (!id) return + if (!root.attFiles[id]) root.enqueueFetch(att, "preview", true) + else root.openAttachment(att) + } function openAttachment(att) { var id = String(att.id || "") if (id === "") return @@ -1983,7 +1995,7 @@ FocusScope { if (!isFinite(pixelHeight) || pixelHeight < 1 || pixelHeight > 100000) pixelHeight = 0 if (!keepInline) { var metrics = Object.assign({}, root.attMetrics) - metrics[id] = { pixelRatio: ratio, pixelWidth: pixelWidth, pixelHeight: pixelHeight } + metrics[id] = { pixelRatio: ratio, pixelWidth: pixelWidth, pixelHeight: pixelHeight, animated: d.animated === true, renderMime: String(d.renderMime || ""), animationUnavailable: d.animationUnavailable === true } root.attMetrics = metrics } if (d.ok === true && root.fetchJobAction === "copy") { @@ -3551,7 +3563,8 @@ FocusScope { readonly property var fileUrl: root.attFiles[chipRow.attId] readonly property bool failed: chipRow.fileUrl === "" readonly property var imageMetrics: root.attMetrics[chipRow.attId] || ({}) - readonly property bool showImage: + readonly property bool isSticker: !!modelData.sticker + readonly property bool showImage: !isSticker && root.isImageMime(chipRow.modelData.mime) && chipRow.fileUrl !== undefined && chipRow.fileUrl !== "" Layout.fillWidth: true @@ -3571,6 +3584,18 @@ FocusScope { // every right-aligned element off-screen with it. Item { id: attImage + StickerLayer { + anchors.fill: parent + z: 3 + visible: chipRow.index === 0 && !textBubbleRow.visible + stickers: visible ? Stickers.placedStickers(bubbleRow.modelData.stickers) : [] + files: root.attFiles; metrics: root.attMetrics + fetchingId: root.fetchingId; fetchQueue: root.fetchQueue + textColor: root.dim + playing: root.surfaceOpen && bubbleRow.y + chipRow.y + height >= flick.contentY && bubbleRow.y + chipRow.y <= flick.contentY + flick.height + onOpenRequested: function(att) { root.activateSticker(att) } + onContextRequested: function(att) { root.openAttachmentMenu(bubbleRow.modelData, att) } + } TapbackPill { visible: chipRow.pillHere; mine: bubbleRow.mine; tapbacks: bubbleRow.modelData.tapbacks } visible: chipRow.showImage readonly property bool animated: root.isAnimatedMime(chipRow.modelData.mime) @@ -3652,9 +3677,38 @@ FocusScope { } } + StickerImage { + visible: chipRow.isSticker + Layout.preferredWidth: Math.min(Style.space(160), content.width * 0.50) + Layout.preferredHeight: Layout.preferredWidth + attachment: chipRow.modelData + source: chipRow.isSticker ? (chipRow.fileUrl || "") : "" + loading: root.stickerLoading(chipRow.attId) + failed: chipRow.attId === "" || chipRow.failed + animated: chipRow.imageMetrics.animated === true + animationUnavailable: chipRow.imageMetrics.animationUnavailable === true + playing: root.surfaceOpen && chipRow.y + bubbleRow.y + height >= flick.contentY && chipRow.y + bubbleRow.y <= flick.contentY + flick.height + textColor: root.dim + fontSize: root.fontCaption + onTapped: root.activateSticker(chipRow.modelData) + onContextRequested: root.openAttachmentMenu(bubbleRow.modelData, chipRow.modelData) + StickerLayer { + anchors.fill: parent + z: 3 + visible: chipRow.index === 0 && !textBubbleRow.visible + stickers: visible ? Stickers.placedStickers(bubbleRow.modelData.stickers) : [] + files: root.attFiles; metrics: root.attMetrics + fetchingId: root.fetchingId; fetchQueue: root.fetchQueue + textColor: root.dim + playing: root.surfaceOpen && bubbleRow.y + chipRow.y + height >= flick.contentY && bubbleRow.y + chipRow.y <= flick.contentY + flick.height + onOpenRequested: function(att) { root.activateSticker(att) } + onContextRequested: function(att) { root.openAttachmentMenu(bubbleRow.modelData, att) } + } + TapbackPill { visible: chipRow.pillHere; mine: bubbleRow.mine; tapbacks: bubbleRow.shownTapbacks } + } Rectangle { TapbackPill { visible: chipRow.pillHere; mine: bubbleRow.mine; tapbacks: bubbleRow.modelData.tapbacks } - visible: !chipRow.showImage + visible: !chipRow.showImage && !chipRow.isSticker Layout.preferredWidth: Math.ceil(chipText.implicitWidth) + Style.space(18) Layout.preferredHeight: Math.ceil(chipText.implicitHeight) + Style.space(12) radius: Style.space(14) @@ -3800,6 +3854,7 @@ FocusScope { // the sender's far side guarantees right/left placement even // when the delegate's own width collapses to its content. RowLayout { + id: textBubbleRow Layout.fillWidth: true // a tapback pill overlaps the top edge — leave room for it Layout.topMargin: (modelData.groupStart ? Style.space(6) : 0) @@ -3894,6 +3949,17 @@ FocusScope { } } + StickerLayer { + anchors.fill: parent + z: 3 + stickers: Stickers.placedStickers(bubbleRow.modelData.stickers) + files: root.attFiles; metrics: root.attMetrics + fetchingId: root.fetchingId; fetchQueue: root.fetchQueue + textColor: root.dim + playing: root.surfaceOpen && bubbleRow.y + height >= flick.contentY && bubbleRow.y <= flick.contentY + flick.height + onOpenRequested: function(att) { root.activateSticker(att) } + onContextRequested: function(att) { root.openAttachmentMenu(bubbleRow.modelData, att) } + } TapbackPill { mine: bubbleRow.mine; tapbacks: bubbleRow.shownTapbacks } } @@ -3934,6 +4000,40 @@ FocusScope { Item { Layout.fillWidth: true; visible: !bubbleRow.mine } } + RowLayout { + Layout.fillWidth: true + visible: unplacedStickerFlow.stickers.length > 0 + Item { Layout.fillWidth: true; visible: bubbleRow.mine } + Flow { + id: unplacedStickerFlow + readonly property bool imageSurface: (bubbleRow.modelData.attachments || []).length > 0 && (root.isImageMime(bubbleRow.modelData.attachments[0].mime) || !!bubbleRow.modelData.attachments[0].sticker) && !!root.attFiles[String(bubbleRow.modelData.attachments[0].id)] + readonly property var stickers: textBubbleRow.visible || imageSurface ? (bubbleRow.modelData.stickers || []).filter(function(sticker) { return sticker.reaction === true || !sticker.placement || sticker.part !== 0 }) : (bubbleRow.modelData.stickers || []) + Layout.preferredWidth: Math.min(content.width * 0.78, stickers.length * Style.space(58)) + Layout.preferredHeight: childrenRect.height + spacing: Style.space(6) + Repeater { + model: unplacedStickerFlow.stickers + delegate: StickerImage { + required property var modelData + readonly property string attachmentId: String(modelData.attachment.id || "") + width: Style.space(52); height: width + attachment: modelData.attachment + source: root.attFiles[attachmentId] || "" + loading: root.stickerLoading(attachmentId) + failed: attachmentId === "" || root.attFiles[attachmentId] === "" + animated: (root.attMetrics[attachmentId] || {}).animated === true + animationUnavailable: (root.attMetrics[attachmentId] || {}).animationUnavailable === true + playing: root.surfaceOpen && bubbleRow.y + height >= flick.contentY && bubbleRow.y <= flick.contentY + flick.height + compact: true + textColor: root.dim + Accessible.description: (animationUnavailable ? "Static preview; animation unavailable. " : "") + "Sticker attached to message part " + String(modelData.part + 1) + onTapped: root.activateSticker(modelData.attachment) + onContextRequested: root.openAttachmentMenu(bubbleRow.modelData, modelData.attachment) + } + } + } + Item { Layout.fillWidth: true; visible: !bubbleRow.mine } + } // Delivered becomes Read under the newest sent message. RowLayout { Layout.fillWidth: true diff --git a/CLAUDE.md b/CLAUDE.md index bfad9a8e..7e577080 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -745,3 +745,5 @@ Read-sync UI regression: `python3 scripts/test-read-ui.py` executes the shipping QML badge bindings and read/unread functions with synthetic data under QtTest. `threadsJson` must remain bound to the CURRENT threads, including optimistic edits; a poll-only cache allowed a 3-unread badge alongside only 2 unread rows. + +Sticker Save also accepts the `-png-` transform: HEIC is a bounded PNG conversion, and HEICS is a single-frame still, not the original sequence. diff --git a/StickerImage.qml b/StickerImage.qml new file mode 100644 index 00000000..cd2b87dc --- /dev/null +++ b/StickerImage.qml @@ -0,0 +1,95 @@ +import QtQuick + +// Presentation only. Attribution, animation detection, and geometry come +// from TypeScript; the parent owns fetching and conversation positioning. +Item { + id: root + property var attachment: ({}) + property url source: "" + property bool loading: false + property bool failed: false + property bool animated: false + property bool animationUnavailable: false + property bool playing: true + property bool compact: false + property bool iconOnly: false + property color textColor: "#8e8e93" + property int fontSize: 12 + readonly property bool ready: animated ? motion.status === Image.Ready : still.status === Image.Ready + readonly property bool decodeFailed: animated ? motion.status === Image.Error : still.status === Image.Error + readonly property real paintedWidth: animated ? motion.paintedWidth : still.paintedWidth + readonly property real paintedHeight: animated ? motion.paintedHeight : still.paintedHeight + readonly property int currentFrame: motion.currentFrame + readonly property int frameCount: motion.frameCount + signal tapped() + signal contextRequested() + + implicitWidth: compact ? 48 : 160 + implicitHeight: compact ? 48 : 160 + activeFocusOnTab: true + Accessible.role: Accessible.Button + Accessible.name: String(attachment.sticker && attachment.sticker.label || "Sticker") + Accessible.description: failed || decodeFailed ? "Unavailable. Activate to retry." + : animationUnavailable ? "Static preview; animation unavailable. Open sticker." : "Open sticker" + Accessible.onPressAction: tapped() + + Image { + id: still + anchors.fill: parent + source: !root.animated ? root.source : "" + visible: !root.animated + asynchronous: true + autoTransform: true + fillMode: Image.PreserveAspectFit + sourceSize.width: 640 + sourceSize.height: 640 + smooth: true + mipmap: true + } + AnimatedImage { + id: motion + anchors.fill: parent + source: root.animated ? root.source : "" + visible: root.animated + playing: root.playing && root.visible && root.animated + asynchronous: true + autoTransform: true + fillMode: Image.PreserveAspectFit + // AnimatedImage.sourceSize was read-only in Qt 6.4. A static assignment + // prevents the whole component from loading there. Newer Qt can bound + // decoder output; older Qt still receives validated <=2048px rasters. + Component.onCompleted: { + try { motion.sourceSize = Qt.size(640, 640) } catch (error) { } + } + cache: false + smooth: true + } + Text { + anchors.centerIn: parent + width: parent.width + visible: !root.ready + text: root.iconOnly ? (root.loading ? "…" : root.failed || root.decodeFailed ? "!" : "◇") + : root.loading ? "Loading…" : root.failed || root.decodeFailed ? "Sticker unavailable" : "Sticker" + textFormat: Text.PlainText + horizontalAlignment: Text.AlignHCenter + wrapMode: Text.Wrap + color: root.textColor + font.pixelSize: root.fontSize + } + Rectangle { + anchors.fill: parent + visible: root.activeFocus + color: "transparent" + border.color: root.textColor + border.width: 1 + radius: 6 + } + TapHandler { onTapped: root.tapped() } + TapHandler { + acceptedButtons: Qt.RightButton + onTapped: root.contextRequested() + } + Keys.onReturnPressed: tapped() + Keys.onEnterPressed: tapped() + Keys.onMenuPressed: contextRequested() +} diff --git a/StickerLayer.qml b/StickerLayer.qml new file mode 100644 index 00000000..813fbb16 --- /dev/null +++ b/StickerLayer.qml @@ -0,0 +1,41 @@ +import QtQuick +import "Stickers.mjs" as Stickers + +// Position only when the bridge supplied a body-part mapping we understand. +// Unknown placements stay in a separate attachment strip in BlipView. +Item { + id: root + property var stickers: [] + property var files: ({}) + property var metrics: ({}) + property bool playing: true + property string fetchingId: "" + property var fetchQueue: [] + property color textColor: "#8e8e93" + signal openRequested(var attachment) + signal contextRequested(var attachment) + Repeater { + model: root.stickers + delegate: StickerImage { + required property var modelData + readonly property string attachmentId: String(modelData.attachment.id || "") + readonly property var metric: root.metrics[attachmentId] || ({}) + readonly property var geometry: Stickers.stickerGeometry(modelData.placement, root.width, root.height, Number(metric.pixelWidth || 64), Number(metric.pixelHeight || 64)) + x: geometry.x; y: geometry.y + width: geometry.width; height: geometry.height + rotation: geometry.rotation + z: 2 + attachment: modelData.attachment + source: root.files[attachmentId] || "" + loading: attachmentId !== "" && (root.fetchingId === attachmentId || root.fetchQueue.some(function(job) { return job.id === attachmentId })) + failed: attachmentId === "" || root.files[attachmentId] === "" + animated: metric.animated === true + animationUnavailable: metric.animationUnavailable === true + playing: root.playing + compact: true + textColor: root.textColor + onTapped: root.openRequested(modelData.attachment) + onContextRequested: root.contextRequested(modelData.attachment) + } + } +} diff --git a/Stickers.mjs b/Stickers.mjs new file mode 100644 index 00000000..178d5bfc --- /dev/null +++ b/Stickers.mjs @@ -0,0 +1,133 @@ +// stickers.ts +var MAX_VISIBLE_STICKERS = 24; +var sources = new Set(["memoji", "genmoji", "user", "app", "unknown"]); +var record = (value) => !!value && typeof value === "object" && !Array.isArray(value); +var finite = (value, min, max) => typeof value === "number" && Number.isFinite(value) && value >= min && value <= max; +var label = (value, max = 160) => typeof value === "string" ? value.replace(/[\p{Cc}\p{Cf}]/gu, "").trim().slice(0, max) : ""; +function normalizeStickerPlacement(value) { + if (!record(value) || !finite(value.x, -3, 3) || !finite(value.y, -3, 3) || !finite(value.scale, 0.1, 4) || !finite(value.rotation, -Math.PI * 2, Math.PI * 2)) + return; + return Object.assign({ x: value.x, y: value.y, scale: value.scale, rotation: value.rotation }, finite(value.parentWidth, 1, 1e4) ? { parentWidth: value.parentWidth } : {}); +} +function normalizeStickerMetadata(value) { + if (!record(value)) + return; + const placement = normalizeStickerPlacement(value.placement); + const app = label(value.app_name, 80); + return Object.assign({ + source: typeof value.source === "string" && sources.has(value.source) ? value.source : "unknown", + label: label(value.label) || "Sticker", + animated: value.animated === true + }, app ? { app_name: app } : {}, placement ? { placement } : {}); +} +function normalizeStickerAttachment(attachment) { + const sticker = normalizeStickerMetadata(attachment.sticker); + if (sticker) + return Object.assign({}, attachment, { sticker }); + const plain = Object.assign({}, attachment); + delete plain.sticker; + return plain; +} +function normalizeStickerOverlays(value) { + if (!Array.isArray(value)) + return { stickers: [], overflow: 0 }; + const valid = []; + const identities = new Set; + let total = 0; + for (const raw of value) { + if (!record(raw) || !record(raw.attachment)) + continue; + const a = raw.attachment; + const sticker = normalizeStickerMetadata(a.sticker); + if (!sticker) + continue; + const id = typeof a.id === "string" && /^[0-9]{1,18}$/.test(a.id) ? a.id : ""; + const guid = label(raw.guid, 128); + const key = `${guid}\x00${id}`; + if ((guid || id) && identities.has(key)) + continue; + if (guid || id) + identities.add(key); + total++; + if (valid.length >= MAX_VISIBLE_STICKERS) + continue; + const placement = normalizeStickerPlacement(raw.placement) ?? sticker.placement; + valid.push(Object.assign({ + attachment: { + id, + name: label(a.name, 256) || "Sticker", + mime: typeof a.mime === "string" ? a.mime.slice(0, 100) : null, + bytes: finite(a.bytes, 0, Number.MAX_SAFE_INTEGER) ? a.bytes : null, + sticker + }, + guid, + part: finite(raw.part, 0, 999999) && Number.isInteger(raw.part) ? raw.part : 0, + from_me: raw.from_me === true, + by: label(raw.by, 160) || null, + ts: typeof raw.ts === "string" ? raw.ts : "" + }, raw.reaction === true ? { reaction: true } : {}, placement ? { placement } : {})); + } + return { stickers: valid, overflow: Math.max(0, total - valid.length) }; +} +function mergeStickers(a, b) { + const all = [...a ?? [], ...b ?? []]; + if (!all.length) + return; + const seen = new Set; + return all.filter((s) => { + const key = `${s.guid}\x00${s.attachment?.id ?? ""}`; + if (seen.has(key)) + return false; + seen.add(key); + return true; + }); +} +function placedStickers(stickers) { + return (stickers ?? []).filter((s) => s.reaction !== true && s.part === 0 && !!normalizeStickerPlacement(s.placement)); +} +function unplacedStickers(stickers) { + return (stickers ?? []).filter((s) => s.reaction !== true && (s.part !== 0 || !normalizeStickerPlacement(s.placement))); +} +function reactionStickers(stickers) { + return (stickers ?? []).filter((s) => s.reaction === true); +} +function stickerGeometry(placement, width, height, naturalWidth = 64, naturalHeight = 64) { + const w = finite(width, 1, 1e4) ? width : 64; + const h = finite(height, 1, 1e4) ? height : 64; + const nw = finite(naturalWidth, 1, 1e5) ? naturalWidth : 64; + const nh = finite(naturalHeight, 1, 1e5) ? naturalHeight : 64; + const p = normalizeStickerPlacement(placement); + const scale = p?.scale ?? 1; + const longEdge = Math.min(Math.max(40 * scale, 24), 120, w); + let sw = nw >= nh ? longEdge : longEdge * nw / nh; + let sh = nh >= nw ? longEdge : longEdge * nh / nw; + const angle = p?.rotation ?? 0; + const cos = Math.abs(Math.cos(angle)), sin = Math.abs(Math.sin(angle)); + let bw = sw * cos + sh * sin, bh = sh * cos + sw * sin; + const fit = Math.min(1, w / bw, h / bh); + sw *= fit; + sh *= fit; + bw *= fit; + bh *= fit; + const insetX = (bw - sw) / 2, insetY = (bh - sh) / 2; + return { + x: p ? Math.max(insetX, Math.min(w - sw - insetX, p.x * w - sw / 2)) : 0, + y: p ? Math.max(insetY, Math.min(h - sh - insetY, p.y * h - sh / 2)) : 0, + width: sw, + height: sh, + rotation: p ? p.rotation * 180 / Math.PI : 0, + placed: !!p + }; +} +export { + MAX_VISIBLE_STICKERS, + mergeStickers, + normalizeStickerAttachment, + normalizeStickerMetadata, + normalizeStickerOverlays, + normalizeStickerPlacement, + placedStickers, + reactionStickers, + stickerGeometry, + unplacedStickers +}; diff --git a/bridge/mac/imsg b/bridge/mac/imsg index 139564a5..843bfb0a 100755 --- a/bridge/mac/imsg +++ b/bridge/mac/imsg @@ -28,6 +28,7 @@ import hashlib import html import io import json +import math import os import plistlib import re @@ -37,6 +38,7 @@ import subprocess import sys import unicodedata from glob import glob +from xml.parsers.expat import ExpatError sys.path.insert(0, os.path.dirname(os.path.realpath(__file__))) from read_state import read_state @@ -68,7 +70,13 @@ ATTACHMENT_MIME_SQL = """COALESCE(NULLIF(a.mime_type, ''), CASE a.uti WHEN 'org.3gpp.adaptive-multi-rate-audio' THEN 'audio/amr' WHEN 'public.jpeg' THEN 'image/jpeg' WHEN 'public.png' THEN 'image/png' + WHEN 'public.webp' THEN 'image/webp' + WHEN 'org.webmproject.webp' THEN 'image/webp' + WHEN 'com.compuserve.gif' THEN 'image/gif' WHEN 'public.heic' THEN 'image/heic' + WHEN 'public.heif' THEN 'image/heif' + WHEN 'public.heics' THEN 'image/heic-sequence' + WHEN 'public.heifs' THEN 'image/heif-sequence' WHEN 'com.apple.quicktime-movie' THEN 'video/quicktime' WHEN 'public.mpeg-4' THEN 'video/mp4' END)""" @@ -899,7 +907,7 @@ def pin_order_for(pins: list[set[str]], *candidates: str | None) -> int | None: return None -# ---------- --rich enrichment (tapbacks, read receipts, replies, attachments) ---------- +# ---------- --rich enrichment (tapbacks, stickers, read receipts, replies, attachments) ---------- # Tapback rows are their own messages pointing at a target via # associated_message_guid ("p:0/" / "bp:"). 2xxx adds, 3xxx removes; @@ -928,6 +936,105 @@ def _strip_assoc(guid: str | None) -> str: return guid +def _assoc_part(guid: str | None) -> int: + """The body-part index in p:N/GUID; a balloon/bare GUID targets part 0.""" + match = re.match(r"^p:(\d{1,6})/", guid or "") + return int(match.group(1)) if match else 0 + + +def _row_value(row, key, default=None): + # Older schemas and callers' projected rows need not have new columns. + try: + return row[key] + except (KeyError, IndexError): + return default + + +def _small_plist_dict(blob) -> dict: + """Sticker metadata is untrusted, optional, and never an executable archive.""" + if not isinstance(blob, (bytes, bytearray, memoryview)) or len(blob) > 65536: + return {} + try: + value = plistlib.loads(bytes(blob)) + return value if isinstance(value, dict) else {} + except (plistlib.InvalidFileException, ExpatError, ValueError, TypeError, OverflowError, RecursionError): + return {} + + +def _sticker_number(value, low, high): + # Apple's geometry values are usually decimal strings. Never accept a + # boolean, NaN, infinity, or an enormous value as a layout coordinate. + if isinstance(value, bool) or not isinstance(value, (str, int, float)): + return None + try: + number = float(value) + except (ValueError, OverflowError): + return None + return number if math.isfinite(number) and low <= number <= high else None + + +def _sticker_label(value, limit=160) -> str: + if not isinstance(value, str): + return "" + return "".join(c for c in value if not unicodedata.category(c).startswith("C"))[:limit].strip() + + +def sticker_metadata(row, force=False) -> dict | None: + """Recognize by Messages' flag or association, never by size/name/MIME. + + sticker_user_info uses sxs/sys (normalized center), ssa (scale), sro + (clockwise radians), spw (parent preview width), and pid (source bundle). + See docs/STICKERS.md for the independently inspected format sources. + Unknown/malformed geometry falls back to a separate attached-sticker row. + No raw plist, path, or arbitrary metadata is forwarded. + """ + if not (force or _row_value(row, "is_sticker") == 1): + return None + info = _small_plist_dict(_row_value(row, "sticker_user_info")) + attribution = _small_plist_dict(_row_value(row, "attribution_info")) + bundle = info.get("pid", "") + bundle = bundle.rsplit(":", 1)[-1] if isinstance(bundle, str) else "" + source = { + "com.apple.messages.genmoji": "genmoji", + "com.apple.Animoji.StickersApp.MessagesExtension": "memoji", + "com.apple.Jellyfish.Animoji": "memoji", + "com.apple.Stickers.UserGenerated.MessagesExtension": "user", + }.get(bundle, "app" if bundle else "unknown") + label = _sticker_label(_row_value(row, "emoji_description")) or _sticker_label(attribution.get("accessl")) + out = {"source": source, "label": label or ("Memoji" if source == "memoji" else "Sticker")} + name = _sticker_label(attribution.get("name"), 80) + if name: + out["app_name"] = name + placement = {key: _sticker_number(info.get(raw), low, high) for key, raw, low, high in ( + ("x", "sxs", -3, 3), ("y", "sys", -3, 3), + ("scale", "ssa", 0.1, 4), ("rotation", "sro", -math.pi * 2, math.pi * 2), + )} + if all(value is not None for value in placement.values()): + parent_width = _sticker_number(info.get("spw"), 1, 10000) + if parent_width is not None: + placement["parentWidth"] = parent_width + out["placement"] = placement + mime = (_row_value(row, "mime") or "").lower() + out["animated"] = mime in ("image/gif", "image/apng", "image/heics", "image/heic-sequence", "image/heif-sequence") or mime.startswith("video/") + return out + + +def sticker_attachment_columns(con): + """Optional fields were introduced in different macOS releases.""" + names = {_pragma_name(r) for r in con.execute("PRAGMA table_info(attachment)")} + fields = {"is_sticker": "0", "sticker_user_info": "NULL", "attribution_info": "NULL", + "emoji_image_short_description": "NULL"} + return ", ".join( + ("a." + field if field in names else default) + " AS " + + ("emoji_description" if field == "emoji_image_short_description" else field) + for field, default in fields.items() + ) + + +def sticker_flag_sql(con): + return "a.is_sticker" if any(_pragma_name(r) == "is_sticker" for r in con.execute("PRAGMA table_info(attachment)")) else "0" + + def _effect_name(style_id: str | None) -> str | None: """'com.apple.messages.effect.CKConfettiEffect' → 'confetti'.""" if not style_id: @@ -1021,24 +1128,44 @@ def enrich(con, rows, with_names: bool): kept = [r for r in rows if not (r["assoc_type"] and 2000 <= r["assoc_type"] < 4000)] targets = {r["guid"]: r["ROWID"] for r in kept if r["guid"]} extras: dict[int, dict] = {r["ROWID"]: {} for r in kept} + # An associated sticker can be inside the window while its parent is + # outside it. Keep it visible as a standalone sticker in that case. + orphans = {r["ROWID"]: r for r in rows if r["assoc_type"] == 2007 + and _strip_assoc(_row_value(r, "assoc_guid")) not in targets} + orphan_targets = {_strip_assoc(_row_value(r, "assoc_guid")) for r in orphans.values()} + legacy_stickers = [] # Tapbacks: scan every assoc row in the DB (type-filtered), net per sender+target. emoji_col = ", m.associated_message_emoji AS emo" if has_emoji else "" - latest: dict[tuple[str, str], sqlite3.Row] = {} + latest: dict[tuple[str, str, int], sqlite3.Row] = {} for r in con.execute( - f"""SELECT m.associated_message_guid AS ag, m.associated_message_type AS t, + f"""SELECT m.ROWID AS mid, m.guid, m.associated_message_guid AS ag, m.associated_message_type AS t, m.is_from_me, m.date, h.id AS handle{emoji_col} FROM {MESSAGE_SRC} m LEFT JOIN handle h ON h.ROWID = m.handle_id - WHERE m.associated_message_type BETWEEN 2000 AND 3999""" + WHERE m.associated_message_type = 1000 OR m.associated_message_type BETWEEN 2000 AND 3999""" ): tg = _strip_assoc(r["ag"]) - if tg not in targets: + if tg not in targets and tg not in orphan_targets: + continue + if r["t"] == 1000: + # A dragged sticker is additive, not the sender's one tapback + # slot. Several stickers from the same person must all survive. + if tg in targets: + legacy_stickers.append(r) continue who = "me" if r["is_from_me"] else (r["handle"] or "?") - key = (who, tg) - if key not in latest or r["date"] > latest[key]["date"]: + key = (who, tg, _assoc_part(r["ag"])) + if key not in latest or (r["date"], _row_value(r, "mid", 0)) > (latest[key]["date"], _row_value(latest[key], "mid", 0)): latest[key] = r - for (who, tg), r in latest.items(): + sticker_rows = list(legacy_stickers) + for (who, tg, part), r in latest.items(): + if tg not in targets: + # Only the currently active sticker is shown, not a later + # removed/replaced sticker that happened to fall in the window. + if r["t"] == 2007 and _row_value(r, "mid") in orphans: + kept.append(orphans[r["mid"]]) + extras[r["mid"]] = {"sticker_orphan": True} + continue # A rendered reaction (or its removal) is newer activity than the # target bubble. Preserve its timestamp for the client's read boundary. e = extras[targets[tg]] @@ -1046,34 +1173,102 @@ def enrich(con, rows, with_names: bool): e["activity_ts"] = max(e.get("activity_ts", ""), activity) if not (2000 <= r["t"] < 3000): continue # net-removed + if r["t"] == 2007: + sticker_rows.append(r) + continue # a sticker has image bytes, never a made-up star emoji kind = r["t"] - 2000 emoji = (r["emo"] if has_emoji and r["emo"] else None) or TAPBACK_EMOJI.get(kind, "⭐") extras[targets[tg]].setdefault("tapbacks", []).append({ "emoji": emoji, "from_me": bool(r["is_from_me"]), "by": None if r["is_from_me"] else (name_for(r["handle"]) if with_names else None) or r["handle"], + **({"kind": "emoji"} if kind == 6 else {}), }) + # Fold sticker events onto their parent, even when the event itself fell + # outside the query window. The target's display date stays unchanged. + sticker_destinations = {} + for r in sticker_rows: + mid = r["mid"] + target = targets[_strip_assoc(r["ag"])] + if mid == target: + continue # malformed self-reference + sticker_destinations[mid] = (target, r) + e = extras[target] + e["activity_ts"] = max(e.get("activity_ts", ""), fmt_ts(r["date"])) + # A sticker can itself be the target of another sticker. Keep both on + # the surviving root bubble; coordinates relative to a removed sticker + # are not coordinates relative to the root, so those use the strip. + nested_stickers = set() + for mid in list(sticker_destinations): + target, event = sticker_destinations[mid] + seen = {mid} + while target in sticker_destinations and target not in seen: + seen.add(target) + target = sticker_destinations[target][0] + if target in seen: + del sticker_destinations[mid] # malformed cycle: keep the event row + elif target != sticker_destinations[mid][0]: + sticker_destinations[mid] = (target, event) + nested_stickers.add(mid) + extras[target]["activity_ts"] = max(extras[target].get("activity_ts", ""), fmt_ts(event["date"])) + kept = [r for r in kept if r["ROWID"] not in sticker_destinations] + # Preserve the input chronology after adding orphan 2007 rows. + order = {r["ROWID"]: i for i, r in enumerate(rows)} + kept.sort(key=lambda r: order[r["ROWID"]]) + # Attachments. transfer_name is null mid-download; .pluginPayloadAttachment # blobs are rich-link-preview internals (a URL message can carry four of # them), not user files — both are noise to a client. - rowids = [r["ROWID"] for r in kept] + rowids = [r["ROWID"] for r in kept] + list(sticker_destinations) + force_stickers = {r["ROWID"] for r in kept if r["assoc_type"] in (1000, 2007)} | set(sticker_destinations) + attachment_fields = sticker_attachment_columns(con) for a in _chunked_in( con, f"""SELECT maj.message_id AS mid, a.ROWID AS aid, a.transfer_name AS name, - {attachment_mime_sql(con)} AS mime, a.total_bytes AS bytes + {attachment_mime_sql(con)} AS mime, a.total_bytes AS bytes, {attachment_fields} FROM message_attachment_join maj JOIN attachment a ON a.ROWID = maj.attachment_id - WHERE a.transfer_name IS NOT NULL - AND a.transfer_name NOT LIKE '%.pluginPayloadAttachment' + WHERE (a.transfer_name IS NULL OR a.transfer_name NOT LIKE '%.pluginPayloadAttachment') AND maj.message_id IN ({{marks}})""", rowids, ): - extras.setdefault(a["mid"], {}).setdefault("attachments", []).append( - # id as a STRING: attachment ROWIDs are 64-bit and JS number - # consumers lose precision past 2^53. - {"id": str(a["aid"]), "name": a["name"], "mime": a["mime"], "bytes": a["bytes"]} - ) + # id as a STRING: attachment ROWIDs are 64-bit and JS number + # consumers lose precision past 2^53. + attachment = {"id": str(a["aid"]), "name": a["name"] or "Sticker", "mime": a["mime"], "bytes": a["bytes"]} + sticker = sticker_metadata(a, force=a["mid"] in force_stickers) + if a["name"] is None and sticker is None: + continue # unfinished ordinary transfer, not a displayable file + if sticker is not None: + attachment["sticker"] = sticker + extras.setdefault(a["mid"], {}).setdefault("attachments", []).append(attachment) + + for mid in force_stickers: + if not extras.get(mid, {}).get("attachments"): + extras.setdefault(mid, {})["attachments"] = [{"id": "", "name": "Sticker", "mime": None, "bytes": None, + "sticker": {"source": "unknown", "label": "Sticker", "animated": False}}] + + for mid, (target, r) in sticker_destinations.items(): + attachments = extras.get(mid, {}).get("attachments", []) + if not attachments: + attachments = [{"id": "", "name": "Sticker", "mime": None, "bytes": None, + "sticker": {"source": "unknown", "label": "Sticker", "animated": False}}] + for attachment in attachments: + overlay = {"attachment": attachment, "guid": r["guid"] or "", "part": _assoc_part(r["ag"]), + "from_me": bool(r["is_from_me"]), "ts": fmt_ts(r["date"]), + "by": None if r["is_from_me"] else (name_for(r["handle"]) if with_names else None) or r["handle"]} + if r["t"] == 2007: + overlay["reaction"] = True + placement = attachment.get("sticker", {}).get("placement") + if placement is not None and mid not in nested_stickers: + overlay["placement"] = placement + elif mid in nested_stickers: + # Preserve attribution but never apply the old target's + # coordinate system to an unrelated body part. + attachment = dict(attachment) + attachment["sticker"] = {k: v for k, v in attachment.get("sticker", {}).items() if k != "placement"} + overlay["attachment"] = attachment + extras[target].setdefault("stickers", []).append(overlay) # Rich-link cards: the balloon payload names the URL/title/summary; the # preview image is the nth .pluginPayloadAttachment row of the message. @@ -1290,6 +1485,7 @@ def base_select(con) -> str: m.service, h.id AS handle, c.chat_identifier, c.room_name, m.guid, m.date_read, m.is_delivered, m.is_read, m.error, {edit_cols} {sched_cols} m.associated_message_type AS assoc_type, + m.associated_message_guid AS assoc_guid, m.thread_originator_guid AS orig_guid, m.expressive_send_style_id AS style_id, m.is_audio_message, @@ -1659,7 +1855,8 @@ def cmd_chats(con, args): attachments = {} for attachment in _chunked_in( con, - f"""SELECT maj.message_id AS mid, a.transfer_name AS name, {attachment_mime_sql(con)} AS mime + f"""SELECT maj.message_id AS mid, a.transfer_name AS name, {attachment_mime_sql(con)} AS mime, + {sticker_flag_sql(con)} AS is_sticker FROM message_attachment_join maj JOIN attachment a ON a.ROWID = maj.attachment_id WHERE maj.message_id IN ({{marks}}) @@ -1669,6 +1866,7 @@ def cmd_chats(con, args): ): attachments.setdefault(attachment["mid"], { "name": attachment["name"] or "", "mime": attachment["mime"] or "", + **({"sticker": True} if attachment["is_sticker"] == 1 else {}), }) participants = {} @@ -2364,12 +2562,14 @@ def cmd_attachment(con, args): """ if not re.fullmatch(r"[0-9]{1,18}", args.id): sys.exit("error: attachment id must be decimal digits") + sticker_column = sticker_flag_sql(con) + " = 1" row = con.execute( f"""SELECT a.filename, {attachment_mime_sql(con)} AS mime_type FROM attachment a JOIN message_attachment_join maj ON maj.attachment_id = a.ROWID JOIN {MESSAGE_SRC} m ON m.ROWID = maj.message_id - WHERE a.ROWID = ? AND a.transfer_name IS NOT NULL + WHERE a.ROWID = ? AND (a.transfer_name IS NOT NULL OR {sticker_column} + OR m.associated_message_type IN (1000, 2007)) LIMIT 1""", (int(args.id),), ).fetchone() @@ -2401,16 +2601,19 @@ def cmd_attachment(con, args): src = path tmp = None - if args.jpeg and (row["mime_type"] or "").startswith("image/"): + image_format = "png" if getattr(args, "png", False) else "jpeg" + if (getattr(args, "jpeg", False) or getattr(args, "png", False)) and (row["mime_type"] or "").startswith("image/"): import subprocess import tempfile - fd_t, tmp = tempfile.mkstemp(suffix=".jpg") + fd_t, tmp = tempfile.mkstemp(suffix=".png" if image_format == "png" else ".jpg") os.close(fd_t) - cmd = ["sips", "-s", "format", "jpeg"] + cmd = ["sips", "-s", "format", image_format] if getattr(args, "max_dim", 0): # -Z is "resample, longest edge, preserve aspect"; it never upscales. - cmd += ["-Z", str(int(args.max_dim)), "-s", "formatOptions", "80"] + cmd += ["-Z", str(int(args.max_dim))] + if image_format == "jpeg": + cmd += ["-s", "formatOptions", "80"] cmd += [path, "--out", tmp] try: r = subprocess.run(cmd, capture_output=True, timeout=60) @@ -2780,7 +2983,7 @@ def build_parser() -> argparse.ArgumentParser: help="do not resolve handles via Contacts") common.add_argument("--rich", action="store_true", default=argparse.SUPPRESS, help="JSON only: fold tapbacks onto their targets and add " - "read_at/edited/retracted/reply_to/attachments/effect/audio") + "read_at/edited/retracted/reply_to/attachments/stickers/effect/audio") common.add_argument("--hide-spam", action="store_true", default=argparse.SUPPRESS, help="omit chats with is_filtered=2 (iPhone Spam)") common.add_argument("--hide-unknown", action="store_true", default=argparse.SUPPRESS, @@ -2810,10 +3013,13 @@ def build_parser() -> argparse.ArgumentParser: s = sub.add_parser("attachment", parents=[common], help="stream one attachment's bytes to stdout (id from --rich)") s.add_argument("id") - s.add_argument("--jpeg", action="store_true", + conversion = s.add_mutually_exclusive_group() + conversion.add_argument("--jpeg", action="store_true", help="convert image/* (HEIC) to JPEG via sips before streaming") + conversion.add_argument("--png", action="store_true", + help="convert image/* to PNG via sips, preserving sticker transparency") s.add_argument("--max-dim", type=int, default=0, dest="max_dim", - help="with --jpeg, resample to N px on the long edge (inline previews)") + help="with --jpeg/--png, resample to N px on the long edge (inline previews)") s.set_defaults(func=cmd_attachment) s = sub.add_parser("avatar", parents=[common], help="stream a contact photo (JPEG) for a handle, or a group's own photo with --chat") s.add_argument("handle", nargs="?", default="") diff --git a/bridge/mac/test_stickers.py b/bridge/mac/test_stickers.py new file mode 100644 index 00000000..c5c8e511 --- /dev/null +++ b/bridge/mac/test_stickers.py @@ -0,0 +1,266 @@ +#!/usr/bin/env python3 +"""Synthetic sticker rows only. Never opens the user's Messages database.""" +import math +import io +import plistlib +import sqlite3 +import tempfile +import unittest +from importlib.machinery import SourceFileLoader +from importlib.util import module_from_spec, spec_from_loader +from pathlib import Path +from types import SimpleNamespace +from unittest.mock import patch + + +def load_imsg(): + loader = SourceFileLoader("blip_sticker_test", str(Path(__file__).with_name("imsg"))) + module = module_from_spec(spec_from_loader(loader.name, loader)) + loader.exec_module(module) + return module + + +T0 = 800_000_000_000_000_000 + + +class Stickers(unittest.TestCase): + def setUp(self): + self.imsg = load_imsg() + self.con = sqlite3.connect(":memory:") + self.con.row_factory = sqlite3.Row + self.con.executescript(""" + CREATE TABLE message ( + ROWID INTEGER PRIMARY KEY, guid TEXT, date INTEGER, is_from_me INTEGER DEFAULT 0, + handle_id INTEGER DEFAULT 1, text TEXT, attributedBody BLOB, item_type INTEGER DEFAULT 0, + associated_message_type INTEGER DEFAULT 0, associated_message_guid TEXT, + associated_message_emoji TEXT, date_read INTEGER DEFAULT 0, + balloon_bundle_id TEXT, payload_data BLOB, thread_originator_guid TEXT, + expressive_send_style_id TEXT, is_audio_message INTEGER DEFAULT 0); + CREATE TABLE handle (ROWID INTEGER PRIMARY KEY, id TEXT); + CREATE TABLE attachment (ROWID INTEGER PRIMARY KEY, transfer_name TEXT, mime_type TEXT, + uti TEXT, total_bytes INTEGER, is_sticker INTEGER DEFAULT 0, sticker_user_info BLOB, + attribution_info BLOB, emoji_image_short_description TEXT); + CREATE TABLE message_attachment_join (message_id INTEGER, attachment_id INTEGER); + CREATE TABLE chat_recoverable_message_join (message_id INTEGER); + INSERT INTO handle VALUES (1, '+15551234567'); + """) + + def tearDown(self): + self.con.close() + + def message(self, guid, kind=0, target=None, date=T0, emoji=None, mine=False): + cur = self.con.execute("""INSERT INTO message + (guid, date, associated_message_type, associated_message_guid, associated_message_emoji, is_from_me) + VALUES (?, ?, ?, ?, ?, ?)""", (guid, date, kind, target, emoji, mine)) + return cur.lastrowid + + def attachment(self, mid, aid, sticker=True, name="sticker.png", mime="image/png", info=None): + self.con.execute("INSERT INTO attachment VALUES (?, ?, ?, NULL, 128, ?, ?, NULL, NULL)", + (aid, name, mime, sticker, plistlib.dumps(info) if info is not None else None)) + self.con.execute("INSERT INTO message_attachment_join VALUES (?, ?)", (mid, aid)) + + def enrich(self, ids): + marks = ",".join("?" for _ in ids) + rows = list(self.con.execute(f"""SELECT m.*, associated_message_type AS assoc_type, + associated_message_guid AS assoc_guid, thread_originator_guid AS orig_guid, + expressive_send_style_id AS style_id FROM {self.imsg.MESSAGE_SRC} m + WHERE ROWID IN ({marks}) ORDER BY date, ROWID""", ids)) + return self.imsg.enrich(self.con, rows, False) + + def test_standalone_sticker_is_not_a_photo(self): + a, b = self.message("S"), self.message("P") + self.attachment(a, 1) + self.attachment(b, 2, sticker=False) + kept, extra = self.enrich([a, b]) + self.assertEqual(len(kept), 2) + self.assertEqual(extra[a]["attachments"][0]["sticker"]["label"], "Sticker") + self.assertNotIn("sticker", extra[b]["attachments"][0]) + + def test_sticker_without_transfer_name_is_preserved(self): + s = self.message("S") + self.attachment(s, 1, name=None) + self.assertEqual(self.enrich([s])[1][s]["attachments"][0]["name"], "Sticker") + + def test_ordinary_unfinished_and_link_payload_attachments_stay_hidden(self): + m = self.message("M") + self.attachment(m, 1, sticker=False, name=None) + self.attachment(m, 2, sticker=False, name="x.pluginPayloadAttachment") + self.assertNotIn("attachments", self.enrich([m])[1][m]) + + def test_legacy_stickers_fold_additively_onto_parent_even_outside_window(self): + parent = self.message("P") + one = self.message("S1", 1000, "p:0/P", T0 + 1_000_000_000) + two = self.message("S2", 1000, "p:0/P", T0 + 2_000_000_000) + self.attachment(one, 1) + self.attachment(two, 2) + for window in ([parent], [parent, one, two]): + kept, extra = self.enrich(window) + self.assertEqual([r["ROWID"] for r in kept], [parent]) + self.assertEqual([s["attachment"]["id"] for s in extra[parent]["stickers"]], ["1", "2"]) + self.assertTrue(all(not s.get("reaction") for s in extra[parent]["stickers"])) + self.assertEqual(extra[parent]["activity_ts"], self.imsg.fmt_ts(T0 + 2_000_000_000)) + + def test_sticker_tapback_never_becomes_star_and_can_be_removed(self): + parent = self.message("P") + sticker = self.message("S", 2007, "p:0/P", T0 + 1_000_000_000) + self.attachment(sticker, 1) + kept, extra = self.enrich([parent, sticker]) + self.assertEqual(len(kept), 1) + self.assertNotIn("tapbacks", extra[parent]) + self.assertEqual(len(extra[parent]["stickers"]), 1) + self.assertIs(extra[parent]["stickers"][0]["reaction"], True) + self.message("R", 3007, "p:0/P", T0 + 2_000_000_000) + extra = self.enrich([parent, sticker])[1] + self.assertNotIn("stickers", extra[parent]) + self.assertEqual(extra[parent]["activity_ts"], self.imsg.fmt_ts(T0 + 2_000_000_000)) + + def test_emoji_replaces_sticker_slot_but_not_legacy_decorations(self): + p = self.message("P") + legacy = self.message("L", 1000, "p:0/P") + modern = self.message("S", 2007, "p:0/P") + self.attachment(legacy, 1) + self.attachment(modern, 2) + self.message("E", 2006, "p:0/P", T0 + 1_000_000_000, emoji="👍") + extra = self.enrich([p])[1][p] + self.assertEqual([s["guid"] for s in extra["stickers"]], ["L"]) + self.assertEqual(extra["tapbacks"][0]["kind"], "emoji") + self.assertEqual(extra["tapbacks"][0]["emoji"], "👍") + + def test_body_parts_keep_separate_reaction_slots(self): + p = self.message("P") + s = self.message("S", 2007, "p:2/P") + self.attachment(s, 1) + self.message("E", 2001, "p:0/P", T0 + 1_000_000_000) + extra = self.enrich([p])[1][p] + self.assertEqual(extra["stickers"][0]["part"], 2) + self.assertEqual(extra["tapbacks"][0]["emoji"], "👍") + + def test_same_timestamp_later_rowid_wins(self): + p = self.message("P") + s = self.message("S", 2007, "p:0/P") + self.attachment(s, 1) + self.message("R", 3007, "p:0/P") + self.assertNotIn("stickers", self.enrich([p])[1][p]) + + def test_orphan_sticker_stays_visible_until_removed(self): + self.message("P") + s = self.message("S", 2007, "p:0/P") + self.attachment(s, 1) + kept, extra = self.enrich([s]) + self.assertEqual([r["ROWID"] for r in kept], [s]) + self.assertIn("sticker", extra[s]["attachments"][0]) + self.message("R", 3007, "p:0/P", T0 + 1_000_000_000) + self.assertEqual(self.enrich([s])[0], []) + + def test_legacy_orphan_is_retained(self): + s = self.message("S", 1000, "p:0/missing") + self.attachment(s, 1, sticker=False) # association works on older schemas + kept, extra = self.enrich([s]) + self.assertEqual(len(kept), 1) + self.assertIn("sticker", extra[s]["attachments"][0]) + + def test_missing_sticker_media_gets_an_explicit_placeholder(self): + p = self.message("P") + self.message("S", 2007, "p:0/P") + overlay = self.enrich([p])[1][p]["stickers"][0] + self.assertEqual(overlay["attachment"]["id"], "") + self.assertEqual(overlay["attachment"]["sticker"]["label"], "Sticker") + + def test_orphan_missing_media_still_has_a_labeled_attachment(self): + s = self.message("S", 2007, "p:0/missing") + self.assertEqual(self.enrich([s])[1][s]["attachments"][0]["sticker"]["label"], "Sticker") + + def test_nested_sticker_survives_on_root_without_wrong_coordinates(self): + p = self.message("P") + one = self.message("S1", 1000, "p:0/P") + two = self.message("S2", 1000, "p:0/S1", T0 + 1_000_000_000) + geometry = {"sxs": "0.5", "sys": "0.5", "ssa": "1", "sro": "0"} + self.attachment(one, 1, info=geometry) + self.attachment(two, 2, info=geometry) + kept, extra = self.enrich([p, one, two]) + self.assertEqual([r["ROWID"] for r in kept], [p]) + self.assertEqual(len(extra[p]["stickers"]), 2) + nested = next(s for s in extra[p]["stickers"] if s["guid"] == "S2") + self.assertNotIn("placement", nested) + self.assertNotIn("placement", nested["attachment"]["sticker"]) + + def test_cyclic_sticker_association_keeps_one_visible_row(self): + one = self.message("S1", 1000, "p:0/S2") + two = self.message("S2", 1000, "p:0/S1") + self.attachment(one, 1) + self.attachment(two, 2) + kept, extra = self.enrich([one, two]) + self.assertTrue(kept) + self.assertEqual(len(extra[kept[0]["ROWID"]]["attachments"]), 1) + + def test_deleted_sticker_does_not_reappear(self): + p = self.message("P") + s = self.message("S", 1000, "p:0/P") + self.attachment(s, 1) + self.con.execute("INSERT INTO chat_recoverable_message_join VALUES (?)", (s,)) + self.assertNotIn("stickers", self.enrich([p])[1][p]) + + def test_schema_without_sticker_columns_still_reads_ordinary_images(self): + self.con.execute("DROP TABLE attachment") + self.con.execute("CREATE TABLE attachment (ROWID INTEGER PRIMARY KEY, transfer_name TEXT, mime_type TEXT, total_bytes INTEGER)") + p = self.message("P") + self.con.execute("INSERT INTO attachment VALUES (1, 'image.png', 'image/png', 10)") + self.con.execute("INSERT INTO message_attachment_join VALUES (?, 1)", (p,)) + self.assertEqual(self.enrich([p])[1][p]["attachments"], [{"id": "1", "name": "image.png", "mime": "image/png", "bytes": 10}]) + + def test_geometry_and_accessible_label_are_bounded_and_typed(self): + info = {"sxs": "0.75", "sys": "0.1", "ssa": "1.2", "sro": "-0.4", "spw": "164", + "pid": "extension:team:com.apple.messages.genmoji", "untrusted": "not forwarded"} + r = {"is_sticker": 1, "mime": "image/heic", "sticker_user_info": plistlib.dumps(info), + "emoji_description": "Smile\x00", "attribution_info": None} + m = self.imsg.sticker_metadata(r) + self.assertEqual(m["placement"], {"x": .75, "y": .1, "scale": 1.2, "rotation": -.4, "parentWidth": 164}) + self.assertEqual((m["label"], m["source"]), ("Smile", "genmoji")) + self.assertNotIn("untrusted", m) + for bad in ["nan", "inf", "1e999", -100, True]: + r["sticker_user_info"] = plistlib.dumps({**info, "sxs": bad}) + self.assertNotIn("placement", self.imsg.sticker_metadata(r)) + + def test_valid_plist_over_cap_is_rejected(self): + blob = plistlib.dumps({"value": "x" * 65536}) + self.assertGreater(len(blob), 65536) + self.assertEqual(self.imsg._small_plist_dict(blob), {}) + + def test_bad_plists_never_take_down_thread(self): + for bad in [None, b"garbage", b"", + b"&unknown;", b"x" * 65537, plistlib.dumps([1, 2])]: + self.assertEqual(self.imsg._small_plist_dict(bad), {}) + self.assertEqual(self.imsg.sticker_metadata({"is_sticker": 1, "sticker_user_info": bad})["source"], "unknown") + + def test_png_conversion_flag_is_exclusive_with_jpeg(self): + args = self.imsg.build_parser().parse_args(["attachment", "42", "--png"]) + self.assertTrue(args.png) + self.assertFalse(args.jpeg) + + def test_attachment_png_conversion_preserves_stream_and_cleans_temp(self): + m = self.message("S") + self.attachment(m, 1, name=None, mime="image/heic") + self.con.execute("ALTER TABLE attachment ADD COLUMN filename TEXT") + with tempfile.TemporaryDirectory(prefix="blip-sticker-test-") as directory: + source = Path(directory) / "source.heic" + source.write_bytes(b"synthetic heic input") + self.con.execute("UPDATE attachment SET filename=? WHERE ROWID=1", (str(source),)) + output, temporary = io.BytesIO(), [] + + def convert(argv, **kwargs): + self.assertEqual(argv[:4], ["sips", "-s", "format", "png"]) + temporary.append(argv[-1]) + Path(argv[-1]).write_bytes(b"\x89PNG\r\n\x1a\nsynthetic output") + return SimpleNamespace(returncode=0, stderr=b"") + + with patch.object(self.imsg, "ATTACHMENTS_ROOT", directory), \ + patch("subprocess.run", side_effect=convert), \ + patch.object(self.imsg.sys, "stdout", SimpleNamespace(buffer=output)): + self.imsg.cmd_attachment(self.con, SimpleNamespace(id="1", png=True, jpeg=False, max_dim=0)) + self.assertEqual(output.getvalue(), b"\x89PNG\r\n\x1a\nsynthetic output") + self.assertTrue(temporary) + self.assertFalse(Path(temporary[0]).exists()) + + +if __name__ == "__main__": + unittest.main() diff --git a/collector.ts b/collector.ts index efd0d9b2..e90cd53b 100644 --- a/collector.ts +++ b/collector.ts @@ -25,6 +25,7 @@ import { spawn, spawnSync } from "node:child_process"; import { createHash } from "node:crypto"; import { appendFileSync, chmodSync, lstatSync, mkdirSync, readdirSync, readFileSync, renameSync, statSync, unlinkSync, writeFileSync } from "node:fs"; import { basename, dirname, join } from "node:path"; +import { mergeStickers, type StickerOverlay } from "./stickers"; import { parseReadSnapshot, parseReadIntents, queueReadIntent, retryReadIntent, reconcileReadIntents, type ReadSnapshot, type ReadIntents } from "./read-sync"; @@ -47,10 +48,10 @@ export const POLL_WINDOW = 150; export const DEEP_WINDOW = 500; /** One reaction folded onto its target message (imsg --rich). */ -export interface Tapback { emoji: string; from_me: boolean; by: string | null } +export interface Tapback { emoji: string; from_me: boolean; by: string | null; kind?: "emoji" } /** Attachment metadata only — the file itself stays on the Mac until * fetch.ts pulls it by `id` (attachment ROWID as a string: 64-bit safe). */ -export interface AttachmentMeta { id?: string; name: string; mime: string | null; bytes: number | null } +export interface AttachmentMeta { id?: string; name: string; mime: string | null; bytes: number | null; sticker?: import("./stickers").StickerMetadata } export interface ImsgMessage { /** Stable chat.db message ROWID, supplied by bridges that expose it. */ @@ -91,6 +92,7 @@ export interface ImsgMessage { delivered?: boolean | null; tapbacks?: Tapback[] | null; attachments?: AttachmentMeta[] | null; + stickers?: import("./stickers").StickerOverlay[] | null; reply_to?: { text: string; from_me: boolean } | null; edited?: boolean | null; retracted?: boolean | null; @@ -437,11 +439,12 @@ export function prettyHandle(id: string): string { /** Never expose Messages' U+FFFC attachment marker as a dotted OBJ glyph. */ export function messagePreview( text: unknown, - attachment?: { name?: unknown; mime?: unknown } | null, + attachment?: { name?: unknown; mime?: unknown; sticker?: unknown } | null, ): string { const cleaned = String(text ?? "").replace(/\uFFFC/g, "").trim(); if (cleaned) return cleaned; if (!attachment) return ""; + if (attachment.sticker === true || (attachment.sticker && typeof attachment.sticker === "object")) return "Sticker"; const mime = String(attachment.mime ?? "").toLowerCase(); const name = String(attachment.name ?? "").toLowerCase(); if (mime.startsWith("image/") || /\.(?:avif|gif|heic|heif|jpe?g|png|webp)$/.test(name)) return "Photo"; @@ -530,11 +533,15 @@ export function dedupeSelfEcho(msgs: ImsgMessage[], knownSelfChats: string[] = [ // on your own note is seen no matter which twin carried it. const emptySentReceipt = new Map>(); const emptySentTapbacks = new Map(); + const emptySentStickers = new Map(); + const emptySentActivity = new Map(); for (const m of msgs) { if (selfChats.has(chatKey(m)) && m.from_me && m.text === "") { const k = contextKey(m); emptySentReceipt.set(k, sentReceipt(emptySentReceipt.get(k), m)); emptySentTapbacks.set(k, mergeTapbacks(emptySentTapbacks.get(k), m.tapbacks)); + emptySentStickers.set(k, mergeStickers(emptySentStickers.get(k), m.stickers)); + if (m.activity_ts && m.activity_ts > (emptySentActivity.get(k) ?? "")) emptySentActivity.set(k, m.activity_ts); } } const retractedSent = new Set( @@ -564,14 +571,20 @@ export function dedupeSelfEcho(msgs: ImsgMessage[], knownSelfChats: string[] = [ const idx = selfText.get(key); if (idx !== undefined && out[idx]!.from_me !== m.from_me) { const kept = out[idx]!; - out[idx] = { ...kept, ...sentReceipt(kept, m), from_me: kept.from_me || m.from_me, tapbacks: mergeTapbacks(kept.tapbacks, m.tapbacks) }; + const stickers = mergeStickers(kept.stickers, m.stickers); + const activity = [kept.activity_ts ?? "", m.activity_ts ?? ""].sort().at(-1); + out[idx] = { ...kept, ...sentReceipt(kept, m), from_me: kept.from_me || m.from_me, tapbacks: mergeTapbacks(kept.tapbacks, m.tapbacks), + ...(stickers ? { stickers } : {}), ...(activity ? { activity_ts: activity } : {}) }; continue; } selfText.set(key, out.length); } if (emptySent.has(context) && !m.from_me) { - out.push({ ...m, ...emptySentReceipt.get(context), from_me: true, tapbacks: mergeTapbacks(m.tapbacks, emptySentTapbacks.get(context)) }); + const stickers = mergeStickers(m.stickers, emptySentStickers.get(context)); + const activity = [m.activity_ts ?? "", emptySentActivity.get(context) ?? ""].sort().at(-1); + out.push({ ...m, ...emptySentReceipt.get(context), from_me: true, tapbacks: mergeTapbacks(m.tapbacks, emptySentTapbacks.get(context)), + ...(stickers ? { stickers } : {}), ...(activity ? { activity_ts: activity } : {}) }); continue; } out.push(m); @@ -1955,7 +1968,7 @@ export interface ChatInfo { /** Mirrored from Messages' pinning preferences; absent on old bridges. */ pinned: boolean; pin_order: number | null; - last_attachment?: { name: string; mime: string } | null; + last_attachment?: { name: string; mime: string; sticker?: boolean } | null; pin_name: string | null; muted: boolean; } @@ -2013,14 +2026,15 @@ export function fetchChats(runner = spawnSync): ChatInfo[] | null { last_text: messagePreview( r.last_text, r.last_attachment && typeof r.last_attachment === "object" - ? { name: r.last_attachment.name, mime: r.last_attachment.mime } + ? { name: r.last_attachment.name, mime: r.last_attachment.mime, sticker: r.last_attachment.sticker === true } : null, ), last_from_me: r.last_from_me === true, last_handle: String(r.last_handle ?? ""), last_name: typeof r.last_name === "string" ? r.last_name : null, last_attachment: r.last_attachment && typeof r.last_attachment === "object" - ? { name: String(r.last_attachment.name ?? ""), mime: String(r.last_attachment.mime ?? "") } + ? { name: String(r.last_attachment.name ?? ""), mime: String(r.last_attachment.mime ?? ""), + ...(r.last_attachment.sticker === true ? { sticker: true } : {}) } : null, pinned: r.pinned === true || boundedPinOrder !== null, pin_order: boundedPinOrder, diff --git a/docs/STICKERS.md b/docs/STICKERS.md new file mode 100644 index 00000000..6a303dcb --- /dev/null +++ b/docs/STICKERS.md @@ -0,0 +1,168 @@ +# Receiving stickers + +The rich bridge distinguishes a sticker from a photo using +`attachment.is_sticker` or a sticker association (`1000` or `2007`). A small +PNG, transparent photo, filename, or MIME type alone does not make a sticker. +Standalone stickers render without a photo bubble. Associated stickers belong +to their original message rather than appearing as duplicate message rows. + +| Received media | Inline display | +|---|---| +| PNG, JPEG | Original pixels; PNG transparency preserved | +| HEIC / HEIF | PNG bounded to 2048 pixels per edge, converted by the Mac's `sips`; alpha preserved | +| GIF | Original animated file through Qt's `AnimatedImage` | +| Animated WebP | Original animated file through Qt's `AnimatedImage` | +| APNG, including APNG declared `image/png` | Lossless animated WebP preview; original PNG retained for explicit Open, Copy, and Save | +| HEICS / HEIC sequence | Animated color and grayscale alpha tracks combined into lossless WebP when the container layout is recognized | +| Sticker video | Bounded WebP animation; a separate grayscale alpha track is used when present | + +All inline sticker rasters, including static PNG/JPEG and converted HEIC +stills, must have positive dimensions no larger than 2048 pixels per edge. +Larger raw static assets show an unavailable placeholder; HEIC stills are resized on the Mac before transfer. The table's original-pixel and full-size descriptions +apply within that limit. + +HEICS conversion uses the animated pair in a four-track container. FFprobe +checks dimensions, frame count, duration, and track layout before FFmpeg +decodes it. Supported animations have at most 100 frames, eight seconds, a +2048-pixel edge, and 64 million total decoded pixels. Unknown or larger +sequences get a transparent still when available. Missing tools, malformed +media, timeouts, or an oversized converted preview keep the usable still; +the fetch result explicitly reports `animationUnavailable`. HEICS explicit +Open/Copy/Save currently uses the Mac's bounded transparent still, not a +native HEICS export. Video Open still opens the original video externally. + +Qt's PNG reader displays APNG as a single frame on the tested Qt 6 stack. +That is why the inline APNG conversion exists. Animated images do not retain +all decoded frames in the QML cache, and the view pauses playback outside the +visible conversation viewport. + +## Associations and placement + +The wire attachment gains a `sticker` object with a bounded plain-text label, +source (`memoji`, `genmoji`, `user`, `app`, or `unknown`), optional app name, +animation hint, and optional placement. The bridge exposes only selected +fields, never the raw property list or a Mac attachment path. + +Associated media appears in the parent message's `stickers` array: + +```json +{ + "attachment": { + "id": "42", + "name": "sticker.png", + "mime": "image/png", + "bytes": 1024, + "sticker": {"source": "user", "label": "Sticker", "animated": false} + }, + "guid": "EXAMPLE-STICKER-GUID", + "part": 0, + "from_me": false, + "by": null, + "ts": "2026-11-06T12:01:00Z", + "placement": {"x": 0.9, "y": 0.1, "scale": 1, "rotation": -0.2, "parentWidth": 164} +} +``` + +`sticker_user_info` stores the normalized center in `sxs`/`sys`, scale in +`ssa`, clockwise radians in `sro`, and the source parent preview width in +`spw`. Blip validates every value. Its small-panel layout preserves the +relative position and rotation while bounding the rotated rectangle to the +rendered target. This is an approximation, not pixel-perfect reconstruction +of the sender's Messages layout; `parentWidth` is retained as metadata. + +`p:N/GUID` identifies a body part, not an index into the attachment array. +Blip can safely position part 0 on text or the first rendered image. Other +parts, missing geometry, or an unavailable placement target use a compact +attached-sticker strip. Nested associations resolved inside the loaded window +retain their media on the surviving root bubble but use the strip because +their coordinates refer to another sticker. Native sticker effect shaders +are not recreated; Blip displays the decoded media pixels. + +Legacy type `1000` decorations are additive, so several stickers from one +sender survive. A type `2007` sticker occupies that sender's tapback slot for +one body part; a later emoji replaces it, and type `3007` removes it. It never +becomes a fabricated star emoji. Legacy sticker rows hidden by Recently +Deleted stay hidden; other legacy removal encodings have not been verified. +Type `2007` overlays carry `reaction: true` so the client can use a round +reaction badge without putting ordinary placed stickers inside a bubble. +Sticker metadata is fetched for loaded parents even when the associated event +is outside the message window. A current sticker whose parent is outside the +window remains visible on its own. Missing media remains a labeled unavailable +sticker instead of disappearing. + +The decoration's timestamp advances the parent's `seen_ts` without changing +its displayed message time. Self-thread deduplication preserves decorations +and that boundary from either echo. At most 24 distinct associated stickers +are rendered per bubble; an overflow count explains omitted decorations. +Unsent parent messages hide their sticker decoration. + +## Sending and privacy + +This receiving path does not add a native sticker-send command. Sending an +ordinary transparent image through the existing attachment composer remains +an image send; it does not place a native sticker on a selected message. +An Accessibility route through Messages' sticker picker needs separately +verified target and sticker identities before it can safely be offered. + +A separate synthetic feasibility probe found a possible standalone route: +public ImageIO encoded a transparent HEIC with `tiff:DocumentName` and +`dc:description`; AppKit accepted it as `NSAdaptiveImageGlyph`, and its +identifier survived an attributed-string RTFD roundtrip. Plain PNG and +untagged HEIC were not accepted. Apple's +[Genmoji API overview](https://developer.apple.com/videos/play/wwdc2024/10220/) +documents rich-text and pasteboard support, but the probe did not access the +clipboard, open Messages, or send. A native Messages receipt and exact target +handling remain unverified, so no native sticker-send control is shipped. +Bubble placement is a further capability beyond standalone glyph transport. + +Media uses the existing private attachment cache (directory `0700`, files +`0600`, 500 MB LRU). Automatic transfers retain the five-MB output ceiling; +the absolute source/transport ceiling remains 100 MB. Conversion inputs are +private temporary **media** files, always removed, or bounded pipes. FFmpeg +and FFprobe receive fixed arguments, deadlines, output caps, and a restricted +protocol list. No message body enters arguments, files, or logs. Read-state +snapshots remain metadata only, and no Messages database is written. +The sticker render path accepts only PNG, JPEG, GIF, or WebP raster signatures +matching the advertised or converted format, with nonzero dimensions no +larger than 2048 pixels per edge. Cache hits are checked too. SVG and other +resource-bearing formats are never loaded through sticker attribution. + +## Evidence and checks + +The parsers and conversion code are independently implemented from observed +format facts; no upstream implementation was copied or vendored. The sources +used to cross-check those facts are pinned here: + +- [ReagentX/imessage-exporter attachment fields and source metadata](https://github.com/ReagentX/imessage-exporter/blob/47b75148b82e666b25db1211ce1dcecf32ab40a8/imessage-database/src/tables/attachment.rs) + (`is_sticker`, `pid`, Memoji/Genmoji source, sequence media types). +- [Its association type definitions](https://github.com/ReagentX/imessage-exporter/blob/47b75148b82e666b25db1211ce1dcecf32ab40a8/imessage-database/src/tables/messages/message.rs) + (`1000`, `2007`, `3007`). That project is GPL-3.0 licensed. +- [Its documented HEICS track roles](https://github.com/ReagentX/imessage-exporter/blob/47b75148b82e666b25db1211ce1dcecf32ab40a8/imessage-exporter/src/app/compatibility/converters/sticker.rs). + Blip uses one bounded color/alpha filter into lossless WebP rather than that + implementation's temporary PNG sequences and GIF palette workflow. +- [openclaw/imsg sticker geometry metadata](https://github.com/openclaw/imsg/blob/1bc0679663bf3978a744a61a5a9161d9ba518764/Sources/IMsgHelper/StickerAssets.inc) + and [placement fixtures](https://github.com/openclaw/imsg/blob/1bc0679663bf3978a744a61a5a9161d9ba518764/Tests/IMsgHelperTests/StickerPlacementTests.m) + (`sxs`, `sys`, `ssa`, `sro`, `spw`). That project is MIT licensed; its + sending implementation is not used. + +The current gateway schema was checked for the needed column names and types +without selecting message rows or attachment content. All media tests use +generated synthetic fixtures: + +```sh +bun test stickers.test.ts +python3 -m unittest discover -s bridge/mac -p test_stickers.py +python3 scripts/test-sticker-ui.py +``` + +The Qt test runs the shipping `StickerImage.qml`, verifies background showing +through transparent corners, checks that both converted animation types +advance frames, and checks playback pause. It does not open Messages or send. + +In the upstream UI, native sticker reactions are shown in a compact strip attached to the parent message. Known part-0 placements overlay the text or first image surface; unknown placements remain in the strip. The fork’s reaction-balloon restyle is not included. + +![Synthetic sticker examples in the upstream UI](img/stickers.png) + +Legacy type-1000 rows retain upstream unread counting and toast behavior. Decoder output is capped to min(100, floor(64 million / frame pixels)) frames even when container frame counts lie. Failed APNG conversions cache the still fallback. + +Failed video previews are remembered for one hour to avoid repeated decoder work on thread open; explicit Open remains available. diff --git a/docs/img/stickers.png b/docs/img/stickers.png new file mode 100644 index 00000000..ad24440e Binary files /dev/null and b/docs/img/stickers.png differ diff --git a/fetch.ts b/fetch.ts index 027a99c8..be2995ba 100644 --- a/fetch.ts +++ b/fetch.ts @@ -53,6 +53,12 @@ export function wantsJpeg(mime: string): boolean { return mime === "image/heic" || mime === "image/heif"; } +/** HEIC stickers need alpha-preserving PNG, including a still fallback for + * Live Sticker sequences that sips cannot export as an animation. */ +export function wantsStickerPng(mime: string): boolean { + return ["image/heic", "image/heif", "image/heics", "image/heic-sequence", "image/heif-sequence"].includes(String(mime || "").toLowerCase()); +} + /** * Formats whose whole point is that they MOVE. * @@ -79,6 +85,7 @@ export function sanitizeName(name: string): string { * from the sender's filename: "image/png" + "evil.desktop" caches as .png. */ const MIME_EXT: Record = { "image/png": "png", "image/jpeg": "jpg", "image/gif": "gif", "image/webp": "webp", + "image/apng": "png", "image/heic": "jpg", "image/heif": "jpg", "image/tiff": "tiff", "image/bmp": "bmp", "video/mp4": "mp4", "video/quicktime": "mov", "video/x-m4v": "m4v", "audio/mpeg": "mp3", "audio/mp4": "m4a", "audio/x-m4a": "m4a", "audio/aac": "aac", "audio/amr": "amr", @@ -88,18 +95,19 @@ const MIME_EXT: Record = { "audio/x-caf": "ogg", "application/pdf": "pdf", "text/plain": "txt", "text/vcard": "vcf", "text/calendar": "ics", }; -export function cacheFileName(id: string, name: string, mime: string, preview = false): string { +export function cacheFileName(id: string, name: string, mime: string, preview = false, sticker = false): string { // Three transforms, never colliding: `orig` is the untouched file, `jpg` a // sips HEIC→JPEG at full size, `prev` a resampled inline preview. A preview // is ALWAYS JPEG whatever the source was, so its extension is too — // xdg-open dispatches on extension, and a JPEG inside a .png would be a lie // (war room #49). - const transform = preview ? "prev" : wantsJpeg(mime) ? "jpg" : "orig"; + const png = sticker && wantsStickerPng(mime); + const transform = png ? "png" : preview ? "prev" : wantsJpeg(mime) ? "jpg" : "orig"; let base = sanitizeName(name); // The extension is ALWAYS ours. An unmapped type (image/svg+xml, anything // exotic) used to keep the sender's name — "evil.desktop" with an image MIME // passed the opening gate and reached xdg-open by extension (Astra B#1). - const ext = preview ? "jpg" : (MIME_EXT[String(mime || "").toLowerCase()] ?? "bin"); + const ext = png ? "png" : preview ? "jpg" : (MIME_EXT[String(mime || "").toLowerCase()] ?? "bin"); base = base.replace(/\.[^.]{1,8}$/, "") + "." + ext; return `${id}-${transform}-${base}`; } @@ -154,6 +162,21 @@ export function imageMetrics(bytes: Buffer, mime: string): ImageMetrics { }; } + // WebP extended header (including animations), lossless, and lossy. + if (bytes.length >= 30 && bytes.toString("ascii", 0, 4) === "RIFF" && bytes.toString("ascii", 8, 12) === "WEBP") { + const kind = bytes.toString("ascii", 12, 16); + if (kind === "VP8X") return { + pixelWidth: bytes.readUIntLE(24, 3) + 1, pixelHeight: bytes.readUIntLE(27, 3) + 1, pixelRatio: 1, + }; + if (kind === "VP8L" && bytes[20] === 0x2f) return { + pixelWidth: 1 + (bytes.readUInt32LE(21) & 0x3fff), + pixelHeight: 1 + ((bytes.readUInt32LE(21) >>> 14) & 0x3fff), pixelRatio: 1, + }; + if (kind === "VP8 " && bytes[23] === 0x9d && bytes[24] === 0x01 && bytes[25] === 0x2a) return { + pixelWidth: bytes.readUInt16LE(26) & 0x3fff, pixelHeight: bytes.readUInt16LE(28) & 0x3fff, pixelRatio: 1, + }; + } + const png = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]); if (bytes.length >= 24 && bytes.subarray(0, 8).equals(png)) { const pixelWidth = bytes.readUInt32BE(16); @@ -208,22 +231,168 @@ export function imageMetrics(bytes: Buffer, mime: string): ImageMetrics { return { ...EMPTY_IMAGE_METRICS }; } -function cachedImageMetrics(path: string, mime: string): ImageMetrics { +export interface ImageAnimation { animated: boolean; frames: number; format: string } + +/** Sticker images bypass JPEG previewing, so do not let a sender use that + * path to load SVG or another resource-bearing format in the QML renderer. */ +export function stickerRasterMime(bytes: Buffer): string { + if (bytes.length >= 24 && bytes.toString("hex", 0, 8) === "89504e470d0a1a0a" + && bytes.toString("ascii", 12, 16) === "IHDR") return "image/png"; + if (bytes.length >= 4 && bytes[0] === 0xff && bytes[1] === 0xd8 && bytes[2] === 0xff) return "image/jpeg"; + if (bytes.length >= 10 && ["GIF87a", "GIF89a"].includes(bytes.toString("ascii", 0, 6))) return "image/gif"; + if (bytes.length >= 30 && bytes.toString("ascii", 0, 4) === "RIFF" && bytes.toString("ascii", 8, 12) === "WEBP" + && ["VP8X", "VP8L", "VP8 "].includes(bytes.toString("ascii", 12, 16))) return "image/webp"; + return ""; +} + +function stickerRasterValid(bytes: Buffer, mime: string): boolean { + const expected = mime === "image/apng" ? "image/png" : mime; + if (stickerRasterMime(bytes) !== expected) return false; + const metrics = imageMetrics(bytes, expected); + return metrics.pixelWidth > 0 && metrics.pixelHeight > 0 && metrics.pixelWidth <= 2048 && metrics.pixelHeight <= 2048; +} + +/** Only inspect bounded container headers. PNG's acTL precedes IDAT. */ +export function imageAnimation(bytes: Buffer): ImageAnimation { + const plain = { animated: false, frames: 1, format: "" }; + if (bytes.length >= 10 && ["GIF87a", "GIF89a"].includes(bytes.toString("ascii", 0, 6))) + return { animated: true, frames: 0, format: "gif" }; + if (bytes.length >= 30 && bytes.toString("ascii", 0, 4) === "RIFF" && bytes.toString("ascii", 8, 12) === "WEBP") + return { animated: bytes.toString("ascii", 12, 16) === "VP8X" && (bytes[20]! & 2) !== 0, frames: 0, format: "webp" }; + if (bytes.length < 24 || bytes.toString("hex", 0, 8) !== "89504e470d0a1a0a") return plain; + for (let pos = 8; pos + 12 <= bytes.length;) { + const length = bytes.readUInt32BE(pos); + if (length > IMAGE_HEADER_BYTES || pos + 12 + length > bytes.length) break; + const type = bytes.toString("ascii", pos + 4, pos + 8); + if (type === "acTL" && length === 8) { + const frames = bytes.readUInt32BE(pos + 8); + return { animated: frames > 1, frames, format: "png" }; + } + if (type === "IDAT" || type === "IEND") break; + pos += length + 12; + } + return { ...plain, format: "png" }; +} + +/** Qt's PNG reader renders APNG as a still. Convert only the inline sticker + * preview to lossless animated WebP, preserving alpha. Originals still back + * explicit Open/Copy/Save; no conversation text enters files or argv. */ +export function stickerAnimationPreview(bytes: Buffer, runner = spawnSync, dir = CACHE_DIR): { bytes: Buffer; converted: boolean; unavailable: boolean } { + const animation = imageAnimation(bytes.subarray(0, IMAGE_HEADER_BYTES)); + if (animation.format !== "png" || !animation.animated) return { bytes, converted: false, unavailable: false }; + const size = imageMetrics(bytes.subarray(0, IMAGE_HEADER_BYTES), "image/png"); + if (animation.frames > 100 || size.pixelWidth <= 0 || size.pixelHeight <= 0 + || size.pixelWidth > 2048 || size.pixelHeight > 2048 + || size.pixelWidth * size.pixelHeight * animation.frames > 64_000_000) + return { bytes, converted: false, unavailable: true }; + // FFmpeg 6's APNG demuxer asks for input length even with ignore_loop=1; + // a looping APNG on stdin fails with ENOSYS. Keep one bounded, seekable + // private media file, as with HEICS, and remove it on every exit path. + const tmp = join(dir, `.sticker-${process.pid}-${Math.random().toString(36).slice(2, 10)}.apng`); + try { + mkdirSync(dir, { recursive: true, mode: 0o700 }); + const fd = openSync(tmp, "wx", 0o600); + try { writeSync(fd, bytes); } finally { closeSync(fd); } + const res = runner("ffmpeg", ["-nostdin", "-v", "error", "-protocol_whitelist", "file,pipe", "-max_pixels", "4194304", "-f", "apng", "-ignore_loop", "1", "-i", tmp, + "-map_metadata", "-1", "-c:v", "libwebp_anim", "-lossless", "1", "-loop", "0", + "-frames:v", String(Math.min(100, Math.floor(64_000_000 / (size.pixelWidth * size.pixelHeight)))), "-f", "webp", "pipe:1"], + { timeout: 30000, maxBuffer: FETCH_MAX_BYTES + (1 << 20) }); + const out = res.stdout as Buffer; + if (res.status === 0 && Buffer.isBuffer(out) && out.length > 30 && imageAnimation(out).format === "webp") + return { bytes: out, converted: true, unavailable: false }; + } catch { /* A missing ffmpeg or decoder retains a usable transparent still. */ } + finally { try { unlinkSync(tmp); } catch { /* no temp file survived */ } } + return { bytes, converted: false, unavailable: true }; +} + +interface StickerSequenceResult { bytes: Buffer; renderMime: "image/webp" | "image/png"; unavailable: boolean } + +/** Live Stickers use a color track and a separate grayscale alpha track. + * In HEICS the first pair is the still image; tracks 2/3 are the animation. + * Probe only container metadata before allowing bounded decode. A private + * media temp file supplies seekable input (MOV/HEICS often puts moov last). + */ +export function stickerSequencePreview(bytes: Buffer, mime: string, runner = spawnSync, dir = CACHE_DIR): StickerSequenceResult | undefined { + if (!bytes.length || bytes.length > FETCH_MAX_BYTES) return undefined; + const tmp = join(dir, `.sticker-${process.pid}-${Math.random().toString(36).slice(2, 10)}.media`); + try { + mkdirSync(dir, { recursive: true, mode: 0o700 }); + const fd = openSync(tmp, "wx", 0o600); + try { writeSync(fd, bytes); } finally { closeSync(fd); } + const probe = runner("ffprobe", ["-v", "error", "-protocol_whitelist", "file,pipe", + "-show_entries", "stream=index,codec_type,width,height,nb_frames,duration,pix_fmt", + "-of", "json", "-max_pixels", "4194304", "-f", "mov", "-i", tmp], { timeout: 10000, maxBuffer: 65536 }); + if (probe.status !== 0) return undefined; + const data = JSON.parse(String(probe.stdout)); + if (!Array.isArray(data.streams) || data.streams.length > 8) return undefined; + const video = data.streams.filter((s: any) => s.codec_type === "video"); + const heics = wantsStickerPng(mime); + if (!video.length || (heics && video.length < 4)) return undefined; + const color = heics ? video[2] : video[0]; + const mask = heics ? video[3] : video.length === 2 && /^gray/.test(video[1].pix_fmt) ? video[1] : null; + const width = Number(color.width), height = Number(color.height), count = Number(color.nb_frames), seconds = Number(color.duration); + const validSize = (s: any) => Number.isInteger(s.index) && s.index >= 0 && s.index <= 16 + && Number.isInteger(s.width) && Number.isInteger(s.height) && s.width > 0 && s.height > 0 && s.width <= 2048 && s.height <= 2048; + if (!validSize(color) || (mask && (!validSize(mask) || mask.width !== width || mask.height !== height || !/^gray/.test(mask.pix_fmt)))) return undefined; + const animate = Number.isInteger(count) && count > 1 && count <= 100 + && width * height * count <= 64_000_000 && Number.isFinite(seconds) && seconds > 0 && seconds <= 8; + const filter = mask + ? `[0:${color.index}][0:${mask.index}]alphamerge,format=rgba[out]` + : `[0:${color.index}]format=rgba[out]`; + const output = animate + ? ["-c:v", "libwebp_anim", "-lossless", "1", "-loop", "0", "-frames:v", String(Math.min(100, Math.floor(64_000_000 / (width * height)))), "-t", "8", "-f", "webp"] + : ["-c:v", "png", "-frames:v", "1", "-f", "image2pipe"]; + const result = runner("ffmpeg", ["-nostdin", "-v", "error", "-protocol_whitelist", "file,pipe", + "-threads", "1", "-max_pixels", "4194304", "-f", "mov", "-i", tmp, "-filter_complex_threads", "1", "-filter_complex", filter, + "-map", "[out]", "-an", "-map_metadata", "-1", ...output, "pipe:1"], + { timeout: 30000, maxBuffer: FETCH_MAX_BYTES + (1 << 20) }); + const out = result.stdout as Buffer; + if (result.status !== 0 || !Buffer.isBuffer(out) || out.length > FETCH_MAX_BYTES) return undefined; + const format = imageAnimation(out).format; + if ((animate && format !== "webp") || (!animate && format !== "png")) return undefined; + return { bytes: out, renderMime: animate ? "image/webp" : "image/png", unavailable: !animate }; + } catch { return undefined; } + finally { try { unlinkSync(tmp); } catch { /* no temp file survived */ } } +} + +function cachedImageMetrics(path: string, mime: string): ImageMetrics & { animated: boolean; animationUnavailable: boolean; rasterMime: string } { + const empty = { ...EMPTY_IMAGE_METRICS, animated: false, animationUnavailable: false, rasterMime: "" }; let fd = -1; try { fd = openSync(path, constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK); const st = fstatSync(fd); - if (!st.isFile() || st.uid !== process.getuid() || st.size <= 0) return { ...EMPTY_IMAGE_METRICS }; + if (!st.isFile() || st.uid !== process.getuid() || st.size <= 0) return empty; const header = Buffer.alloc(Math.min(st.size, IMAGE_HEADER_BYTES)); const count = readSync(fd, header, 0, header.length, 0); - return imageMetrics(header.subarray(0, count), mime); + const bytes = header.subarray(0, count); + const animation = imageAnimation(bytes); + return { ...imageMetrics(bytes, mime), animated: animation.animated && animation.format !== "png", + animationUnavailable: animation.animated && animation.format === "png", rasterMime: stickerRasterMime(bytes) }; } catch { - return { ...EMPTY_IMAGE_METRICS }; + return empty; } finally { if (fd >= 0) closeSync(fd); } } +function cachedMediaBytes(path: string, cap: number): Buffer | undefined { + let fd = -1; + try { + fd = openSync(path, constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK); + const stat = fstatSync(fd); + if (!stat.isFile() || stat.uid !== process.getuid() || stat.size <= 0 || stat.size > cap) return undefined; + const bytes = Buffer.alloc(stat.size); + let offset = 0; + while (offset < bytes.length) { + const n = readSync(fd, bytes, offset, bytes.length - offset, offset); + if (!n) return undefined; + offset += n; + } + return bytes; + } catch { return undefined; } + finally { if (fd >= 0) closeSync(fd); } +} + function evict(keep: string): void { let entries: { name: string; bytes: number; mtimeMs: number }[] = []; try { @@ -348,6 +517,12 @@ export interface FetchResult { pixelWidth: number; pixelHeight: number; pixelRatio: number; + /** Actual cached bytes, for clipboard MIME and sticker render selection. */ + renderMime: string; + animated: boolean; + /** A transparent still is available, but its source animation could not be decoded. */ + animationUnavailable: boolean; + /** Real decoded envelope, kept in memory; absent for non-audio media. */ } /** An iPhone voice message (CAF, Opus inside) → Ogg Opus, so the default @@ -383,24 +558,56 @@ export function fetchAttachment( mime: string, runner = spawnSync, maxBytes = FETCH_MAX_BYTES, + sticker = false, ): FetchResult { const fail = (error: string, online = true): FetchResult => - ({ ok: false, online, path: "", url: "", error, ...EMPTY_IMAGE_METRICS }); + ({ ok: false, online, path: "", url: "", error, ...EMPTY_IMAGE_METRICS, + renderMime: "", animated: false, animationUnavailable: false }); if (!/^[0-9]{1,18}$/.test(id)) return fail("bad attachment id"); + mime = String(mime || "").toLowerCase(); + if (sticker && !["image/png", "image/apng", "image/jpeg", "image/gif", "image/webp"].includes(mime) + && !wantsStickerPng(mime) && !mime.startsWith("video/")) return fail("unsupported sticker media format"); mkdirSync(CACHE_DIR, { recursive: true, mode: 0o700 }); - const preview = maxBytes < FETCH_MAX_BYTES && isImageMime(mime) && !isAnimatedMime(mime); - const file = join(CACHE_DIR, cacheFileName(id, name, mime, preview)); - try { - const st = lstatSync(file); + const auto = maxBytes < FETCH_MAX_BYTES; + const preview = auto && isImageMime(mime) && !isAnimatedMime(mime) && !sticker; + const png = sticker && wantsStickerPng(mime); + const animatedPreview = sticker && auto && ["image/png", "image/apng"].includes(mime.toLowerCase()); + const sequencePreview = sticker && auto && (mime.toLowerCase().startsWith("video/") + || (png && !["image/heic", "image/heif"].includes(mime.toLowerCase()))); + const rawFile = join(CACHE_DIR, cacheFileName(id, name, mime, preview, sticker)); + const animationFile = join(CACHE_DIR, cacheFileName(id, name, "image/webp").replace(`${id}-orig-`, `${id}-anim-`)); + const stillFile = join(CACHE_DIR, cacheFileName(id, name, "image/png").replace(`${id}-orig-`, `${id}-still-`)); + const failedFile = stillFile + ".failed"; + if (sequencePreview && cachedMediaBytes(failedFile, 16)?.toString() === "unavailable") { + try { if (Date.now() - lstatSync(failedFile).mtimeMs < 3600000) return fail("sticker preview unavailable; open the original attachment"); } catch {} + } + let file = rawFile; + let cachedRaw: Buffer | undefined; + let renderMime = png ? "image/png" : preview || wantsJpeg(mime) ? "image/jpeg" : mime; + const sequenceStill = png && !["image/heic", "image/heif"].includes(mime.toLowerCase()); + const candidates = sequencePreview ? [animationFile, stillFile].concat(png ? [rawFile] : []) + : animatedPreview ? [animationFile, stillFile, rawFile] : [rawFile]; + for (const candidate of candidates) try { + const st = lstatSync(candidate); // A symlink planted in the cache must never be followed (or touched). - if (st.isFile() && st.size > 0) { + if (st.isFile() && st.size > 0 && st.size <= (sticker ? Math.min(maxBytes, FETCH_MAX_BYTES) : FETCH_MAX_BYTES)) { const now = new Date(); - utimesSync(file, now, now); // mtime is the LRU clock + utimesSync(candidate, now, now); // mtime is the LRU clock + const cachedMime = candidate === animationFile ? "image/webp" : candidate === stillFile ? "image/png" : renderMime; + const { rasterMime, ...metrics } = cachedImageMetrics(candidate, cachedMime); + if (sticker && !cachedMime.startsWith("video/") && (rasterMime !== (cachedMime === "image/apng" ? "image/png" : cachedMime) + || metrics.pixelWidth <= 0 || metrics.pixelHeight <= 0 || metrics.pixelWidth > 2048 || metrics.pixelHeight > 2048)) + return fail("sticker media is not a supported bounded raster image"); + if (animatedPreview && candidate === rawFile && metrics.animationUnavailable) { + cachedRaw = cachedMediaBytes(candidate, Math.min(maxBytes, FETCH_MAX_BYTES)); + if (cachedRaw) break; // convert an already cached APNG without another bridge read + } return { - ok: true, online: true, path: file, url: pathToFileURL(file).href, error: "", - ...cachedImageMetrics(file, mime), + ok: true, online: true, path: candidate, url: pathToFileURL(candidate).href, error: "", + renderMime: cachedMime, ...metrics, + animationUnavailable: candidate !== animationFile && (sequenceStill || candidate === stillFile || metrics.animationUnavailable), }; } } catch { /* not cached */ } @@ -415,10 +622,10 @@ export function fetchAttachment( // still fetch the untouched original into `-orig-…`. const args = [ "attachment", id, - ...(wantsJpeg(mime) || preview ? ["--jpeg"] : []), - ...(preview ? ["--max-dim", String(PREVIEW_MAX_DIM)] : []), + ...(png && !sequencePreview ? ["--png"] : !sequencePreview && (wantsJpeg(mime) || preview) ? ["--jpeg"] : []), + ...(png && !sequencePreview ? ["--max-dim", "2048"] : preview ? ["--max-dim", String(PREVIEW_MAX_DIM)] : []), ]; - const res = runner(shimPath("imsg"), args, { + const res = cachedRaw ? { status: 0, stdout: cachedRaw, stderr: "" } : runner(shimPath("imsg"), args, { timeout: 120000, maxBuffer: FETCH_MAX_BYTES + (1 << 20), }); @@ -430,9 +637,52 @@ export function fetchAttachment( const raw = res.stdout as Buffer; if (!raw || raw.length === 0) return fail("empty attachment stream"); if (raw.length > Math.min(maxBytes, FETCH_MAX_BYTES)) return fail("attachment exceeds the fetch ceiling"); - const bytes = String(mime || "").toLowerCase() === "audio/x-caf" + let bytes = String(mime || "").toLowerCase() === "audio/x-caf" ? rewrapCaf(raw, runner) : bakeOrientation(raw, runner); + let animationUnavailable = sequenceStill; + if (sequencePreview) { + const converted = stickerSequencePreview(bytes, mime, runner); + if (converted && converted.bytes.length <= Math.min(maxBytes, FETCH_MAX_BYTES)) { + bytes = converted.bytes; + file = converted.renderMime === "image/webp" ? animationFile : stillFile; + renderMime = converted.renderMime; + animationUnavailable = converted.unavailable; + } else if (png) { + // A missing ffmpeg, unknown HEICS layout, or oversized preview still + // gets the Mac's transparent first image. Never cache raw HEICS as PNG. + const fallback = runner(shimPath("imsg"), ["attachment", id, "--png", "--max-dim", "2048"], + { timeout: 120000, maxBuffer: FETCH_MAX_BYTES + (1 << 20) }); + if (fallback.status === 69 || fallback.status === 255) return fail("Mac unreachable", false); + const still = fallback.stdout as Buffer; + if (fallback.status !== 0 || !Buffer.isBuffer(still) || imageAnimation(still).format !== "png") + return fail("sticker animation and still preview unavailable"); + if (still.length > Math.min(maxBytes, FETCH_MAX_BYTES)) return fail("sticker preview exceeds the fetch ceiling"); + bytes = still; + animationUnavailable = true; + } else { + try { + const fd = openSync(failedFile, constants.O_WRONLY | constants.O_CREAT | constants.O_NOFOLLOW | constants.O_TRUNC, 0o600); + try { writeSync(fd, "unavailable"); } finally { closeSync(fd); } + evict(failedFile.slice(failedFile.lastIndexOf("/") + 1)); + } catch { /* the failure itself must still be reported */ } + return fail("sticker video preview unavailable; open the original attachment"); + } + } + if (animatedPreview) { + const converted = stickerAnimationPreview(bytes, runner); + animationUnavailable = converted.unavailable; + if (converted.converted) { + if (converted.bytes.length <= Math.min(maxBytes, FETCH_MAX_BYTES)) { + bytes = converted.bytes; + file = animationFile; + renderMime = "image/webp"; + } else animationUnavailable = true; + } + } + if (animatedPreview && animationUnavailable) file = stillFile; + if (sticker && !renderMime.startsWith("video/") && !stickerRasterValid(bytes.subarray(0, IMAGE_HEADER_BYTES), renderMime)) + return fail("sticker media is not a supported bounded raster image"); // tmp + fsync + rename: a killed fetch must never leave a cache hit that // looks complete. @@ -446,10 +696,13 @@ export function fetchAttachment( closeSync(fd); } renameSync(tmp, file); - evict(cacheFileName(id, name, mime, preview)); + evict(file.slice(file.lastIndexOf("/") + 1)); + const animation = imageAnimation(bytes.subarray(0, IMAGE_HEADER_BYTES)); return { ok: true, online: true, path: file, url: pathToFileURL(file).href, error: "", - ...imageMetrics(bytes.subarray(0, IMAGE_HEADER_BYTES), mime), + ...imageMetrics(bytes.subarray(0, IMAGE_HEADER_BYTES), renderMime), renderMime, + animated: animation.animated && animation.format !== "png", + animationUnavailable: animationUnavailable || (animation.animated && animation.format === "png"), }; } @@ -457,7 +710,7 @@ if (import.meta.main) { const [id, name, mime] = [process.argv[2] ?? "", process.argv[3] ?? "file", process.argv[4] ?? ""]; const cap = Number(process.argv[5] ?? ""); try { - console.log(JSON.stringify(fetchAttachment(id, name, mime, undefined, cap > 0 ? cap : FETCH_MAX_BYTES))); + console.log(JSON.stringify(fetchAttachment(id, name, mime, undefined, cap > 0 ? cap : FETCH_MAX_BYTES, process.argv.includes("--sticker")))); } catch (e) { console.log(JSON.stringify({ ok: false, online: true, path: "", url: "", error: String(e) })); } diff --git a/save-attachment.ts b/save-attachment.ts index d0356625..698e5aaf 100644 --- a/save-attachment.ts +++ b/save-attachment.ts @@ -26,13 +26,13 @@ import { CACHE_DIR, FETCH_MAX_BYTES } from "./fetch"; const CONTROL = /[\x00-\x1f\x7f-\x9f\u202a-\u202e\u2066-\u2069]/; -/** Only an original (`-orig-…`) or the full-size HEIC→JPEG (`-jpg-…`) +/** Only an original (`-orig-…`) or full-size HEIC→JPEG/PNG (`-jpg-`/`-png-`) * inside the media cache is a valid source: never a preview, never a path a * caller made up. Returns the resolved path. */ export function cachedOriginal(path: string, cacheDir = CACHE_DIR): string { if (typeof path !== "string" || !isAbsolute(path) || CONTROL.test(path)) throw new Error("Invalid attachment path"); const resolved = realpathSync(path); - if (dirname(resolved) !== realpathSync(cacheDir) || !/^[^/]+-(orig|jpg)-[^/]+$/.test(basename(resolved))) + if (dirname(resolved) !== realpathSync(cacheDir) || !/^[^/]+-(orig|jpg|png)-[^/]+$/.test(basename(resolved))) throw new Error("Not a fetched original"); const st = lstatSync(resolved); if (!st.isFile()) throw new Error("Not a fetched original"); diff --git a/scripts/blip-setup b/scripts/blip-setup index d5a02a45..ed33e8f9 100755 --- a/scripts/blip-setup +++ b/scripts/blip-setup @@ -30,6 +30,17 @@ for dep in ssh scp bun jq notify-send wl-copy xdg-open; do [[ " ${pkgs[*]} " == *" ${pkg_for[$dep]} "* ]] || pkgs+=("${pkg_for[$dep]}") fi done +webp_available=0 +for qt_root in /usr/lib/qt6 /usr/lib64/qt6 /usr/lib/x86_64-linux-gnu/qt6 /usr/lib/aarch64-linux-gnu/qt6; do + [[ -f "$qt_root/plugins/imageformats/libqwebp.so" ]] && webp_available=1 + done +IFS=: read -r -a qt_extra <<< "${QT_PLUGIN_PATH:-}" +for qt_path in "${qt_extra[@]}"; do + [[ -f "$qt_path/imageformats/libqwebp.so" ]] && webp_available=1 +done +if (( !webp_available )); then + echo "• optional, for animated stickers: sudo pacman -S --needed qt6-imageformats" +fi if (( ${#missing[@]} )); then echo "✗ missing on this machine: ${missing[*]}" >&2 echo " sudo pacman -S --needed ${pkgs[*]} (Omarchy/Arch names), then re-run blip-setup" >&2 diff --git a/scripts/demo/fake-imsg b/scripts/demo/fake-imsg index 93148815..66a026c9 100755 --- a/scripts/demo/fake-imsg +++ b/scripts/demo/fake-imsg @@ -105,14 +105,15 @@ function conversation(): Row[] { tapback: m.tapback === true, error: 0, read_at: m.read_at ? stamp(Number(m.min) + 2) : null, - tapbacks: null, + tapbacks: m.tapbacks || null, + stickers: Array.isArray(m.stickers) ? (m.stickers as any[]).map(sticker => ({...sticker, ts: stamp(Number(m.min) + 1)})) : [], attachments: Array.isArray(m.attachments) - ? (m.attachments as unknown as Array<{ name: string; mime: string }>).map((a, k) => ({ - id: String(700000 + i * 10 + k), name: a.name, mime: a.mime, bytes: 1843200, + ? (m.attachments as unknown as Array<{ name: string; mime: string; sticker?: unknown }>).map((a, k) => ({ + id: String(700000 + i * 10 + k), name: a.name, mime: a.mime, bytes: a.mime.startsWith("audio/") ? 1152044 : a.sticker ? 12000 : 1843200, ...(a.sticker ? {sticker:a.sticker} : {}), })) : null, reply_to: m.reply_to ? { text: String(m.reply_to), from_me: false } : null, - edited: m.edited === true, retracted: m.retracted === true, effect: null, audio: false, link: null, + edited: m.edited === true, retracted: m.retracted === true, effect: null, audio: m.audio === true, link: null, }; return row; }); @@ -205,12 +206,19 @@ function main(): void { if (sub === "attachment") { const dim = val("--max-dim") || "1600"; const id = argv.find((a) => /^[0-9]+$/.test(a)) ?? "0"; + const isSticker = id === "990001" || id === "990002" || conversation().some(row => (row.attachments as any[] || []).some(a => a.id === id && a.sticker)); + if (isSticker) { + // Procedural transparent art: no real person's image or downloaded media. + const r = spawnSync("magick", ["-size", "192x192", "xc:none", "-fill", "#ffd45c", "-stroke", "#edb742", "-strokewidth", "3", "-draw", "polygon 96,10 118,64 176,68 132,108 146,168 96,138 46,168 60,108 16,68 74,64", "-stroke", "none", "-fill", "#513d32", "-draw", "circle 75,87 79,87 circle 117,87 121,87", "-stroke", "#513d32", "-strokewidth", "5", "-fill", "none", "-draw", "path 'M 76,110 Q 96,130 116,110'", "png:-"], { maxBuffer: 8 << 20 }); + if (r.status !== 0 || !r.stdout?.length) process.exit(1); + process.stdout.write(r.stdout as Buffer); return; + } const hue = (Number(id) % 12) * 30; const r = spawnSync("magick", [ "-size", `${dim}x${Math.round(Number(dim) * 0.75)}`, `gradient:hsl(${hue},60%,42%)-hsl(${(hue + 40) % 360},55%,22%)`, "-gravity", "center", "-font", FONT, "-pointsize", String(Math.round(Number(dim) / 12)), - "-fill", "white", "-annotate", "+0+0", "demo photo", + "-fill", "white", "-annotate", "+0+0", "LAKE TRAIL", "-quality", "80", "jpg:-", ], { maxBuffer: 32 << 20 }); if (r.status !== 0 || !r.stdout?.length) process.exit(1); diff --git a/scripts/demo/fixtures.json b/scripts/demo/fixtures.json index 1869c957..c19b500d 100644 --- a/scripts/demo/fixtures.json +++ b/scripts/demo/fixtures.json @@ -106,7 +106,7 @@ { "min": -181, "from_me": false, - "text": "Loved “7 works. bringing the good thermos”", + "text": "Loved \u201c7 works. bringing the good thermos\u201d", "tapback": true }, { @@ -140,6 +140,78 @@ "from_me": true, "text": "on my way, ten minutes out", "read_at": true + }, + { + "min": -12, + "from_me": false, + "text": "Perfect. I found a new overlook for our coffee stop.", + "stickers": [ + { + "attachment": { + "id": "990001", + "name": "sunshine.png", + "mime": "image/png", + "bytes": 12000, + "sticker": { + "source": "user", + "label": "Sunshine sticker", + "animated": false + } + }, + "guid": "DEMO-STICKER-1", + "part": 0, + "from_me": true, + "by": "You", + "placement": { + "x": 1.0, + "y": 0.0, + "scale": 1.0, + "rotation": -0.12, + "parentWidth": 300 + } + } + ] + }, + { + "min": -10, + "from_me": true, + "text": "", + "attachments": [ + { + "name": "happy-star.png", + "mime": "image/png", + "sticker": { + "source": "user", + "label": "Happy star sticker", + "animated": false + } + } + ] + }, + { + "min": -8, + "from_me": false, + "text": "See you there!", + "stickers": [ + { + "reaction": true, + "attachment": { + "id": "990002", + "name": "reaction-star.png", + "mime": "image/png", + "bytes": 12000, + "sticker": { + "source": "user", + "label": "Star reaction", + "animated": false + } + }, + "guid": "DEMO-REACTION-STICKER", + "part": 0, + "from_me": true, + "by": "You" + } + ] } ] } diff --git a/scripts/test-sticker-ui.py b/scripts/test-sticker-ui.py new file mode 100644 index 00000000..a09a530e --- /dev/null +++ b/scripts/test-sticker-ui.py @@ -0,0 +1,163 @@ +#!/usr/bin/env python3 +"""Decode synthetic sticker media through the shipping fetcher and QML item. + +Requires Bun, ffmpeg/ffprobe, and Qt 6 QtQuick/QtTest. No Mac, network, real +Messages rows, or live app is used. --capture writes a synthetic preview. +""" +import argparse +import json +import os +from pathlib import Path +import shutil +import struct +import subprocess +import tempfile +import zlib + + +ROOT = Path(__file__).resolve().parents[1] + + +def png(width, height, raw, channels): + def chunk(kind, data): + return struct.pack(">I", len(data)) + kind + data + struct.pack(">I", zlib.crc32(kind + data)) + scan = b"".join(b"\0" + raw[y * width * channels:(y + 1) * width * channels] for y in range(height)) + return (b"\x89PNG\r\n\x1a\n" + chunk(b"IHDR", struct.pack(">IIBBBBB", width, height, 8, 6 if channels == 4 else 0, 0, 0, 0)) + + chunk(b"IDAT", zlib.compress(scan)) + chunk(b"IEND", b"")) + + +def run(command, **kwargs): + subprocess.run(command, check=True, timeout=45, **kwargs) + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--capture", type=Path) + args = parser.parse_args() + runner = shutil.which("qmltestrunner") or next((str(p) for p in ( + Path("/usr/lib/qt6/bin/qmltestrunner"), Path("/usr/lib/qt6/libexec/qmltestrunner")) if p.is_file()), None) + if not runner: + raise SystemExit("Qt 6 qmltestrunner not found") + if not shutil.which("ffmpeg") or not shutil.which("ffprobe"): + raise SystemExit("ffmpeg and ffprobe are required for the synthetic codec test") + with tempfile.TemporaryDirectory(prefix="blip-sticker-ui-") as directory: + folder = Path(directory) + width = height = 64 + mask = bytes(128 if (x - 32) ** 2 + (y - 32) ** 2 < 24 ** 2 else 0 + for y in range(height) for x in range(width)) + red = b"".join(bytes((240, 80, 100, a)) for a in mask) + blue = b"".join(bytes((60, 160, 250, a)) for a in mask) + (folder / "still.png").write_bytes(png(width, height, red, 4)) + (folder / "mask.png").write_bytes(png(width, height, mask, 1)) + run(["ffmpeg", "-v", "error", "-f", "rawvideo", "-pixel_format", "rgba", "-video_size", "64x64", + "-framerate", "5", "-i", "pipe:0", "-plays", "0", "-f", "apng", str(folder / "animated.png")], input=red + blue) + # Four independently addressable tracks model HEICS's still pair + # and animated color/alpha pair, without importing someone's sticker. + run(["ffmpeg", "-v", "error", "-f", "lavfi", "-i", "testsrc=size=64x64:rate=5:duration=1", + "-loop", "1", "-framerate", "5", "-i", str(folder / "mask.png"), "-t", "1", + "-map", "0:v", "-map", "1:v", "-map", "0:v", "-map", "1:v", "-c:v", "png", + str(folder / "four-track.mov")]) + prepare = folder / "prepare.ts" + prepare.write_text('''import { readFileSync, writeFileSync } from "node:fs"; +import { spawnSync } from "node:child_process"; +import { stickerAnimationPreview, stickerSequencePreview } from @MODULE@; +const dir = process.argv[2]; +// Synthetic inputs only: expose codec diagnostics here, never in shipping +// message/media logs. CI must explain unsupported decoders rather than skip. +const diagnosticRunner = ((command, args, options) => { + const result = spawnSync(command, args, options); + if (result.status !== 0) console.error(command, "exit", result.status, + String(result.stderr || "").slice(0, 4096), result.error?.message || ""); + return result; +}) as typeof spawnSync; +const apng = stickerAnimationPreview(readFileSync(dir + "/animated.png"), diagnosticRunner, dir); +if (!apng.converted || apng.unavailable) throw Error("APNG conversion failed"); +writeFileSync(dir + "/animated.webp", apng.bytes); +const sequence = stickerSequencePreview(readFileSync(dir + "/four-track.mov"), "image/heic-sequence", diagnosticRunner, dir); +if (!sequence || sequence.unavailable || sequence.renderMime !== "image/webp") throw Error("sequence conversion failed"); +writeFileSync(dir + "/sequence.webp", sequence.bytes); +'''.replace("@MODULE@", json.dumps(str(ROOT / "fetch.ts")))) + run(["bun", str(prepare), str(folder)], env={**os.environ, "XDG_CACHE_HOME": str(folder / "cache")}) + shutil.copy(ROOT / "StickerImage.qml", folder) + shutil.copy(ROOT / "Stickers.mjs", folder) + capture = "" if args.capture is None else str(args.capture.resolve()) + if capture: + args.capture.resolve().parent.mkdir(parents=True, exist_ok=True) + (folder / "tst_sticker.qml").write_text('''import QtQuick +import QtTest +import "Stickers.mjs" as Stickers +Item { + id: root + width: 580; height: 220 + Rectangle { anchors.fill: parent; color: "#202127" } + StickerImage { + id: still; x: 20; y: 30; width: 160; height: 160 + source: "still.png" + attachment: ({sticker:{label:"Synthetic translucent circle"}}) + } + StickerImage { + id: apng; x: 210; y: 30; width: 160; height: 160 + source: "animated.webp"; animated: true + } + StickerImage { + id: sequence; x: 400; y: 30; width: 160; height: 160 + source: "sequence.webp"; animated: true + } + TestCase { + name: "StickerRendering" + when: windowShown + function initTestCase() { + tryCompare(still, "ready", true) + tryCompare(apng, "ready", true) + tryCompare(sequence, "ready", true) + } + function test_alpha_survives_static_and_animated_paths() { + for (var item of [still, apng, sequence]) { + var captured = grabImage(item) + // grabImage includes the parent's opaque background. Transparent + // corners must show that background, never black or an image matte. + compare(captured.red(1, 1), 32) + compare(captured.green(1, 1), 33) + compare(captured.blue(1, 1), 39) + } + var staticImage = grabImage(still) + // The center is 50% red over the background, not opaque source red. + verify(staticImage.red(80, 80) > 130 && staticImage.red(80, 80) < 145) + } + function test_frames_advance_for_both_animation_formats() { + compare(apng.frameCount, 2) + compare(sequence.frameCount, 5) + for (var item of [apng, sequence]) { + var first = item.currentFrame + tryVerify(function() { return item.currentFrame !== first }, 2000) + } + } + function test_pause_releases_animation_work() { + apng.playing = false + wait(250) + var first = apng.currentFrame + wait(400) + compare(apng.currentFrame, first) + apng.playing = true + } + function test_geometry_module_loads_in_qml() { + var g = Stickers.stickerGeometry({x:1,y:0,scale:1,rotation:0.5}, 180, 80, 64, 64) + verify(g.placed) + verify(g.width > 0 && isFinite(g.rotation)) + compare(Stickers.placedStickers([{part:0,placement:{x:0.5,y:0.5,scale:1,rotation:0}}]).length, 1) + } + function cleanupTestCase() { + var path = @CAPTURE@ + if (path !== "") grabImage(root).save(path) + } + } +} +'''.replace("@CAPTURE@", json.dumps(capture))) + result = subprocess.run([runner, "-input", str(folder), "-platform", "offscreen"], timeout=30, + env={**os.environ, "QT_QPA_PLATFORM": "offscreen", "QT_QPA_PLATFORMTHEME": "generic", + "QT_QUICK_BACKEND": "software"}) + return result.returncode + + +if __name__ == "__main__": + raise SystemExit(main()) diff --git a/stickers.test.ts b/stickers.test.ts new file mode 100644 index 00000000..cb855b81 --- /dev/null +++ b/stickers.test.ts @@ -0,0 +1,363 @@ +import { describe, expect, test } from "bun:test"; +import { mkdirSync, mkdtempSync, readdirSync, readFileSync, rmSync, statSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { decorate } from "./thread"; +import { dedupeSelfEcho, messagePreview, type ImsgMessage } from "./collector"; +import { MAX_VISIBLE_STICKERS, normalizeStickerAttachment, normalizeStickerMetadata, + normalizeStickerOverlays, normalizeStickerPlacement, placedStickers, unplacedStickers, reactionStickers, stickerGeometry, + type StickerOverlay } from "./stickers"; +import { CACHE_DIR, FETCH_MAX_BYTES, cacheFileName, fetchAttachment, imageAnimation, imageMetrics, + stickerAnimationPreview, stickerSequencePreview, wantsStickerPng } from "./fetch"; +import { cachedOriginal } from "./save-attachment"; + +const position = { x: 0.9, y: 0.1, scale: 1.2, rotation: -.2, parentWidth: 160 }; +const overlay = (n = 1): StickerOverlay => ({ + attachment: { id: String(n), name: "sticker.png", mime: "image/png", bytes: 128, + sticker: { source: "user", label: "Heart", placement: position } }, + guid: `S${n}`, part: 0, from_me: false, by: "Example", ts: "2026-11-06T10:01:00Z", placement: position, +}); +const message = (overrides: Partial = {}): ImsgMessage => ({ + guid: "P", ts: "2026-11-06T10:00:00Z", text: "Example", from_me: false, + chat: "+15551234567", handle: "+15551234567", name: null, service: "iMessage", ...overrides, +}); + +describe("sticker presentation model", () => { + test("a transparent photo never becomes a sticker based on its filename or MIME", () => { + const a = { name: "sticker.png", mime: "image/png", bytes: 100 }; + expect(normalizeStickerAttachment(a)).toEqual(a); + expect(normalizeStickerMetadata(true)).toBeUndefined(); + expect(normalizeStickerMetadata([])).toBeUndefined(); + expect(messagePreview("\uFFFC", a)).toBe("Photo"); + expect(messagePreview("\uFFFC", { ...a, sticker: true })).toBe("Sticker"); + expect(messagePreview("caption", { ...a, sticker: true })).toBe("caption"); + }); + test("only finite bounded placement numbers reach the renderer", () => { + expect(normalizeStickerPlacement(position)).toEqual(position); + for (const value of [NaN, Infinity, -Infinity, true, "0.5", null, 100]) + expect(normalizeStickerPlacement({ ...position, x: value })).toBeUndefined(); + expect(normalizeStickerPlacement({ ...position, parentWidth: Infinity })).not.toHaveProperty("parentWidth"); + }); + test("labels are bounded plain text; unknown attribution is still a sticker", () => { + expect(normalizeStickerMetadata({ source: "weird", label: "\u202e\0" })).toEqual({ + source: "unknown", label: "", animated: false, + }); + expect(normalizeStickerMetadata({ label: "a".repeat(1000) })!.label.length).toBe(160); + expect(normalizeStickerMetadata({ source: { toString: "not callable" } })!.source).toBe("unknown"); + }); + test("duplicates do not consume the bounded overlay budget; overflow is honest", () => { + const input = Array.from({ length: MAX_VISIBLE_STICKERS + 9 }, (_, i) => overlay(i + 1)); + const result = normalizeStickerOverlays([null, {}, ...input, input[0], input[1]]); + expect(result.stickers.length).toBe(MAX_VISIBLE_STICKERS); + expect(result.overflow).toBe(9); + }); + test("missing media survives as a labeled non-fetchable placeholder", () => { + const s = overlay(); s.attachment.id = "not-an-id"; + const result = normalizeStickerOverlays([s]); + expect(result.stickers[0]!.attachment.id).toBe(""); + expect(result.stickers[0]!.attachment.sticker!.label).toBe("Heart"); + }); + test("unknown body parts and absent geometry use an attached strip", () => { + const first = overlay(1), second = { ...overlay(2), part: 2 }, third = overlay(3); + delete third.placement; + delete third.attachment.sticker!.placement; + expect(placedStickers([first, second, third]).map(s => s.guid)).toEqual(["S1"]); + expect(unplacedStickers([first, second, third]).map(s => s.guid)).toEqual(["S2", "S3"]); + }); + test("decorating preserves sticker activity without moving message time", () => { + const result = decorate([message({ stickers: [overlay()], activity_ts: "2026-11-06T10:01:00Z" })], "2026-11-06")[0]!; + expect(result.ts).toBe("2026-11-06T10:00:00Z"); + expect(result.seen_ts).toBe("2026-11-06T10:01:00Z"); + expect(result.stickers[0]!.placement).toEqual(position); + expect(result.tapbacks).toEqual([]); + }); + test("native sticker reactions remain distinct from additive decorations", () => { + const input = [overlay(1), { ...overlay(2), reaction: true }, + { ...overlay(3), reaction: "true" }, { ...overlay(4), reaction: true, part: 2 }]; + const normalized = normalizeStickerOverlays(input).stickers; + expect(reactionStickers(normalized).map(s => s.guid)).toEqual(["S2", "S4"]); + expect(placedStickers(normalized).map(s => s.guid)).toEqual(["S1", "S3"]); + expect(unplacedStickers(normalized)).toEqual([]); + expect(decorate([message({ stickers: normalized })], "2026-11-06")[0]!.stickers[1]!.reaction).toBe(true); + }); + test("an unsent parent hides sticker decoration", () => { + const b = decorate([message({ stickers: [overlay()], retracted: true })], "2026-11-06")[0]!; + expect(b.stickers).toEqual([]); + expect(b.stickersOverflow).toBe(0); + }); + test("either self-thread twin keeps stickers and their read boundary", () => { + for (const reversed of [false, true]) { + const rows = [message({ from_me: true }), message({ stickers: [overlay()], activity_ts: "2026-11-06T10:01:00Z" })]; + const result = dedupeSelfEcho(reversed ? rows.reverse() : rows, ["+15551234567"]); + expect(result.length).toBe(1); + expect(result[0]!.stickers!.length).toBe(1); + expect(result[0]!.activity_ts).toBe("2026-11-06T10:01:00Z"); + } + }); + test("empty outbound self echo transfers its decorations to the displayed inbound", () => { + const result = dedupeSelfEcho([ + message({ text: "", from_me: true, stickers: [overlay()], activity_ts: "2026-11-06T10:01:00Z" }), + message({ text: "" }), + ], ["+15551234567"]); + expect(result.length).toBe(1); + expect(result[0]!.stickers![0]!.guid).toBe("S1"); + expect(result[0]!.activity_ts).toBe("2026-11-06T10:01:00Z"); + }); + test("geometry preserves image aspect and clamps absurd source dimensions", () => { + const g = stickerGeometry(position, 200, 100, 200, 100); + expect(g.width / g.height).toBe(2); + expect(g.rotation).toBeCloseTo(-.2 * 180 / Math.PI); + expect(g.x).toBeGreaterThanOrEqual(0); + expect(g.x + g.width).toBeLessThanOrEqual(200); + const fallback = stickerGeometry(undefined, NaN, Infinity, 0, -1); + expect(Object.values(fallback).every(v => typeof v !== "number" || Number.isFinite(v))).toBe(true); + expect(fallback.placed).toBe(false); + for (const rotation of [0, Math.PI / 4, Math.PI / 2, Math.PI, -Math.PI / 3]) { + const g = stickerGeometry({ ...position, x: 3, y: -3, scale: 4, rotation }, 80, 30, 200, 100); + const bw = g.width * Math.abs(Math.cos(rotation)) + g.height * Math.abs(Math.sin(rotation)); + const bh = g.height * Math.abs(Math.cos(rotation)) + g.width * Math.abs(Math.sin(rotation)); + expect(g.x + g.width / 2 - bw / 2).toBeGreaterThanOrEqual(-1e-6); + expect(g.y + g.height / 2 - bh / 2).toBeGreaterThanOrEqual(-1e-6); + expect(g.x + g.width / 2 + bw / 2).toBeLessThanOrEqual(80 + 1e-6); + expect(g.y + g.height / 2 + bh / 2).toBeLessThanOrEqual(30 + 1e-6); + } + }); +}); + +// Deliberately tiny container headers for parser/runner tests. Pixel decoding +// is covered by scripts/test-sticker-ui.py with generated, valid media. +function png(frames = 1, width = 64, height = 64): Buffer { + const chunk = (type: string, body: Buffer) => { + const b = Buffer.alloc(12 + body.length); b.writeUInt32BE(body.length); b.write(type, 4); body.copy(b, 8); return b; + }; + const ihdr = Buffer.alloc(13); ihdr.writeUInt32BE(width, 0); ihdr.writeUInt32BE(height, 4); ihdr[8] = 8; ihdr[9] = 6; + const actl = Buffer.alloc(8); actl.writeUInt32BE(frames); + return Buffer.concat([Buffer.from("89504e470d0a1a0a", "hex"), chunk("IHDR", ihdr), + ...(frames > 1 ? [chunk("acTL", actl)] : []), chunk("IDAT", Buffer.alloc(0))]); +} +function webp(): Buffer { + const b = Buffer.alloc(40); b.write("RIFF", 0); b.writeUInt32LE(32, 4); b.write("WEBPVP8X", 8); + b.writeUInt32LE(10, 16); b[20] = 0x12; b.writeUIntLE(63, 24, 3); b.writeUIntLE(31, 27, 3); return b; +} +const cap = 5 * 1024 * 1024; +let nextId = 80000; +const id = () => String(nextId++); + +describe("alpha and animated sticker fetches", () => { + test("HEIC and HEICS sticker transforms have PNG filenames; photos retain JPEG", () => { + expect(wantsStickerPng("image/heic-sequence")).toBe(true); + expect(cacheFileName("1", "evil.desktop", "image/heic", false, true)).toBe("1-png-evil.png"); + expect(cacheFileName("1", "same.heic", "image/heic")).toBe("1-jpg-same.jpg"); + }); + test("sticker attribution never admits SVG or MIME-spoofed external-resource media", () => { + let calls = 0; + const svg = Buffer.from(''); + const runner = (() => { calls++; return { status: 0, stdout: svg }; }) as never; + expect(fetchAttachment(id(), "unsafe.svg", "image/svg+xml", runner, cap, true).ok).toBe(false); + expect(calls).toBe(0); + for (const mime of ["image/png", "image/gif", "image/jpeg", "image/webp", "image/heic"]) + expect(fetchAttachment(id(), "fake.png", mime, runner, cap, true).ok).toBe(false); + expect(fetchAttachment(id(), "wrong.gif", "image/gif", (() => ({ status: 0, stdout: png() })) as never, cap, true).ok).toBe(false); + expect(fetchAttachment(id(), "zero.png", "image/png", (() => ({ status: 0, stdout: png(1, 0, 0) })) as never, cap, true).ok).toBe(false); + }); + test("cached sticker bytes are revalidated instead of trusting their extension", () => { + const aid = id(); + mkdirSync(CACHE_DIR, { recursive: true, mode: 0o700 }); + const file = join(CACHE_DIR, cacheFileName(aid, "cached.png", "image/png")); + writeFileSync(file, '', { mode: 0o600 }); + let called = false; + const result = fetchAttachment(aid, "cached.png", "image/png", (() => { called = true; throw Error("unexpected"); }) as never, cap, true); + expect(result.ok).toBe(false); + expect(result.error).toContain("raster"); + expect(called).toBe(false); + }); + test("transparent PNG stickers avoid sips JPEG preview, on first fetch and cache hit", () => { + const calls: string[][] = []; + const runner = ((_cmd: string, args: string[]) => { calls.push(args); return { status: 0, stdout: png() }; }) as never; + const aid = id(); + const result = fetchAttachment(aid, "sticker.png", "image/png", runner, cap, true); + expect(result.ok).toBe(true); + expect(calls).toEqual([["attachment", aid]]); + expect(result.renderMime).toBe("image/png"); + expect(readFileSync(result.path)).toEqual(png()); + expect(statSync(result.path).mode & 0o777).toBe(0o600); + expect(fetchAttachment(aid, "sticker.png", "image/png", runner, cap, true).url).toBe(result.url); + expect(calls.length).toBe(1); + }); + test("HEIC sticker asks for PNG and can be saved as a full-size converted original", () => { + const calls: string[][] = []; + const runner = ((_cmd: string, args: string[]) => { calls.push(args); return { status: 0, stdout: png() }; }) as never; + const aid = id(); + const result = fetchAttachment(aid, "sticker.heic", "image/heic", runner, cap, true); + expect(calls).toEqual([["attachment", aid, "--png", "--max-dim", "2048"]]); + expect(result.renderMime).toBe("image/png"); + expect(result.path).toContain("-png-"); + expect(cachedOriginal(result.path)).toBe(result.path); + }); + test("APNG auto-preview becomes animated WebP; copy/save retains original PNG", () => { + const calls: any[] = []; + const runner = ((cmd: string, args: string[], options: any) => { + calls.push({ cmd, args, options }); + if (cmd === "ffmpeg") { + const input = args[args.indexOf("-i") + 1]!; + expect(readFileSync(input)).toEqual(png(2)); + expect(statSync(input).mode & 0o777).toBe(0o600); + } + return { status: 0, stdout: cmd === "ffmpeg" ? webp() : png(2) }; + }) as never; + const aid = id(); + const result = fetchAttachment(aid, "animated.png", "image/png", runner, cap, true); + expect(result.renderMime).toBe("image/webp"); expect(result.animated).toBe(true); + expect(result.path).toContain("-anim-"); + expect(calls[1].options.input).toBeUndefined(); + expect(calls[1].args).toContain("-ignore_loop"); + expect(readdirSync(CACHE_DIR).some(n => n.startsWith(".sticker-"))).toBe(false); + expect(calls[1].args).toContain("-lossless"); + expect(() => cachedOriginal(result.path)).toThrow("original"); + const original = fetchAttachment(aid, "animated.png", "image/png", runner, FETCH_MAX_BYTES, true); + expect(original.renderMime).toBe("image/png"); + expect(readFileSync(original.path)).toEqual(png(2)); + expect(cachedOriginal(original.path)).toBe(original.path); + const count = calls.length; + expect(fetchAttachment(aid, "animated.png", "image/png", runner, cap, true).path).toBe(result.path); + expect(calls.length).toBe(count); + }); + test("an APNG cached by copy converts locally without another bridge read", () => { + const aid = id(); + fetchAttachment(aid, "loop.png", "image/png", (() => ({ status: 0, stdout: png(2) })) as never, FETCH_MAX_BYTES, true); + const calls: string[] = []; + const r = fetchAttachment(aid, "loop.png", "image/png", ((cmd: string) => { + calls.push(cmd); return { status: 0, stdout: webp() }; + }) as never, cap, true); + expect(calls).toEqual(["ffmpeg"]); + expect(r.animated).toBe(true); + }); + test("malformed headers cannot make the parser read past the buffer", () => { + for (let end = 0; end < png(2).length; end++) expect(() => imageAnimation(png(2).subarray(0, end))).not.toThrow(); + const malformed = png(2); malformed.writeUInt32BE(0xffffffff, 8); + expect(imageAnimation(malformed).animated).toBe(false); + expect(imageMetrics(webp(), "image/webp")).toEqual({ pixelWidth: 64, pixelHeight: 32, pixelRatio: 1 }); + }); + test("huge APNG dimensions or frame counts never enter a decoder", () => { + let calls = 0; + const runner = (() => { calls++; throw Error("must not run"); }) as never; + for (const bytes of [png(101), png(2, 100000, 100000), png(100, 2048, 2048)]) { + const r = stickerAnimationPreview(bytes, runner); + expect(r.unavailable).toBe(true); expect(r.bytes).toBe(bytes); + } + expect(calls).toBe(0); + }); + test("missing decoder, timeout, corrupt output, and large output preserve a transparent still", () => { + for (const behavior of ["missing", "timeout", "corrupt", "large"]) { + const runner = ((cmd: string, _args: string[], opts: any) => { + if (cmd !== "ffmpeg") return { status: 0, stdout: png(2) }; + expect(opts.timeout).toBe(30000); expect(opts.maxBuffer).toBeLessThanOrEqual(FETCH_MAX_BYTES + (1 << 20)); + if (behavior === "missing") throw Error("ENOENT"); + if (behavior === "timeout") return { status: null, stdout: Buffer.alloc(0), error: Error("ETIMEDOUT") }; + if (behavior === "large") return { status: 0, stdout: Buffer.concat([webp(), Buffer.alloc(cap)]) }; + return { status: 0, stdout: Buffer.from("not an image") }; + }) as never; + const r = fetchAttachment(id(), "loop.png", "image/png", runner, cap, true); + expect(r.ok).toBe(true); expect(r.animationUnavailable).toBe(true); + expect(r.renderMime).toBe("image/png"); expect(readFileSync(r.path)).toEqual(png(2)); + expect(readdirSync(CACHE_DIR).some(n => n.startsWith(".sticker-"))).toBe(false); + } + }); + test("HEICS failure asks the Mac for a transparent still and names the lost motion", () => { + const calls: any[] = []; + const runner = ((cmd: string, args: string[]) => { + calls.push({ cmd, args }); + if (cmd === "ffprobe") return { status: 1, stdout: "" }; + return { status: 0, stdout: args.includes("--png") ? png() : Buffer.from("synthetic sequence") }; + }) as never; + const aid = id(), r = fetchAttachment(aid, "live.heics", "image/heic-sequence", runner, cap, true); + expect(r.ok).toBe(true); expect(r.renderMime).toBe("image/png"); expect(r.animationUnavailable).toBe(true); + expect(calls[0].args).toEqual(["attachment", aid]); + expect(calls.at(-1).args).toEqual(["attachment", aid, "--png", "--max-dim", "2048"]); + expect(readdirSync(CACHE_DIR).some(n => n.startsWith(".sticker-"))).toBe(false); + }); + test("HEICS color and alpha tracks are selected only after bounded metadata validation", () => { + const dir = mkdtempSync(join(tmpdir(), "blip-sticker-sequence-")); + const streams = Array.from({ length: 4 }, (_, index) => ({ index, codec_type: "video", width: 64, height: 32, + pix_fmt: index % 2 ? "gray" : "yuv420p", nb_frames: "5", duration: "1" })); + const calls: any[] = []; + const runner = ((cmd: string, args: string[], opts: any) => { + calls.push({ cmd, args, opts }); + if (cmd === "ffprobe") { expect(statSync(args.at(-1)!).mode & 0o777).toBe(0o600); return { status: 0, stdout: JSON.stringify({ streams }) }; } + return { status: 0, stdout: webp() }; + }) as never; + try { + expect(stickerSequencePreview(Buffer.from("media"), "image/heics", runner, dir)?.renderMime).toBe("image/webp"); + expect(calls[1].args).toContain("[0:2][0:3]alphamerge,format=rgba[out]"); + expect(calls[1].args).toContain("file,pipe"); + expect(readdirSync(dir)).toEqual([]); + streams[2]!.width = 999999; + calls.length = 0; + expect(stickerSequencePreview(Buffer.from("media"), "image/heics", runner, dir)).toBeUndefined(); + expect(calls.length).toBe(1); + expect(readdirSync(dir)).toEqual([]); + } finally { rmSync(dir, { recursive: true, force: true }); } + }); +}); + +test("decoder frame budget does not trust advertised APNG count", () => { + stickerAnimationPreview(png(2, 1024, 1024), ((_cmd: string, args: string[]) => { + expect(args[args.indexOf("-frames:v") + 1]).toBe("61"); + expect(args[args.indexOf("-max_pixels") + 1]).toBe("4194304"); + return {status: 0, stdout: webp()}; + }) as never); +}); +test("MOV input gate, pixel ceiling, and independent sequence limits", () => { + for (const [width, height, count, seconds, animated] of [ + [2048, 2048, 2, 1, true], [2049, 2049, 2, 1, false], + [64, 64, 100, 8, true], [64, 64, 101, 8, false], + [64, 64, 100, 8.01, false], [1024, 1024, 62, 1, false], + ] as const) { + let decoded = false; + const r = stickerSequencePreview(Buffer.from("media"), "video/quicktime", ((cmd: string, args: string[]) => { + expect(args[args.indexOf("-f") + 1]).toBe("mov"); + expect(args.indexOf("-f")).toBeLessThan(args.indexOf("-i")); + expect(args[args.indexOf("-max_pixels") + 1]).toBe("4194304"); + if (cmd === "ffprobe") return {status: 0, stdout: JSON.stringify({streams:[{index:0, codec_type:"video", width,height,nb_frames:String(count),duration:String(seconds)}]})}; + decoded = true; + if (animated) expect(Number(args[args.indexOf("-frames:v") + 1]) * width * height).toBeLessThanOrEqual(64_000_000); + return {status:0, stdout:args.includes("libwebp_anim") ? webp() : png()}; + }) as never); + expect(r?.renderMime === "image/webp").toBe(animated); + expect(decoded).toBe(width <= 2048); + } +}); +test("failed APNG conversion is cached as a still", () => { + const aid=id(); let calls=0; + const runner=((cmd:string)=>{calls++; return {status:cmd === "ffmpeg" ? 1 : 0, stdout:png(2)};}) as never; + expect(fetchAttachment(aid,"failure.png","image/png",runner,cap,true).animationUnavailable).toBe(true); + const before=calls; + expect(fetchAttachment(aid,"failure.png","image/png",runner,cap,true).animationUnavailable).toBe(true); + expect(calls).toBe(before); +}); + +test("a failed video decode is not retried on every thread open", () => { + const aid=id(); let calls=0; + const runner=((cmd:string)=>{calls++; return {status:cmd === "ffprobe" ? 1 : 0, stdout:Buffer.from("ffconcat version 1.0\nfile other.png")};}) as never; + expect(fetchAttachment(aid,"fake.mov","video/quicktime",runner,cap,true).ok).toBe(false); + const before=calls; + expect(fetchAttachment(aid,"fake.mov","video/quicktime",runner,cap,true).ok).toBe(false); + expect(calls).toBe(before); +}); +test("the real MOV demuxer refuses a concat payload", () => { + expect(stickerSequencePreview(Buffer.from("ffconcat version 1.0\nfile other.png"),"video/quicktime")).toBeUndefined(); +}); + +test("a non-sticker GIF cached by Open remains a hit above the auto-fetch ceiling", () => { + const aid=id(); mkdirSync(CACHE_DIR,{recursive:true,mode:0o700}); + const path=join(CACHE_DIR,cacheFileName(aid,"large.gif","image/gif")); + const bytes=Buffer.alloc(6*1024*1024); bytes.write("GIF89a"); bytes.writeUInt16LE(64,6); bytes.writeUInt16LE(64,8); + writeFileSync(path,bytes,{mode:0o600}); + expect(fetchAttachment(aid,"large.gif","image/gif",(()=>{throw Error("cache miss");}) as never,cap).ok).toBe(true); +}); +test("static sticker edge checks are independent of animation and mask checks", () => { + for (const width of [2048,2049]) { + const r=fetchAttachment(id(),"edge.png","image/png",(()=>({status:0,stdout:png(1,width,64)})) as never,cap,true); + expect(r.ok).toBe(width === 2048); + } +}); diff --git a/stickers.ts b/stickers.ts new file mode 100644 index 00000000..840506d2 --- /dev/null +++ b/stickers.ts @@ -0,0 +1,169 @@ +/** Sticker wire normalization and layout. No files, processes, or message state. + * Regenerate the QML module: bun build stickers.ts --target=browser --format=esm --outfile=Stickers.mjs + */ +import type { AttachmentMeta } from "./collector"; + +export interface StickerPlacement { + /** Center as a fraction of the target bubble's dimensions. */ + x: number; + y: number; + scale: number; + /** Clockwise radians, as stored by Messages. */ + rotation: number; + parentWidth?: number; +} +export interface StickerMetadata { + source: "memoji" | "genmoji" | "user" | "app" | "unknown"; + label: string; + app_name?: string; + animated?: boolean; + placement?: StickerPlacement; +} +export interface StickerOverlay { + attachment: AttachmentMeta; + guid: string; + part: number; + from_me: boolean; + by: string | null; + ts: string; + /** Native type 2007 occupies the sender's Tapback slot, unlike decoration 1000. */ + reaction?: boolean; + placement?: StickerPlacement; +} + +/** Protect the render tree from a very long history of decorations on one message. */ +export const MAX_VISIBLE_STICKERS = 24; +const sources = new Set(["memoji", "genmoji", "user", "app", "unknown"]); +const record = (value: unknown): value is Record => + !!value && typeof value === "object" && !Array.isArray(value); +const finite = (value: unknown, min: number, max: number): value is number => + typeof value === "number" && Number.isFinite(value) && value >= min && value <= max; +const label = (value: unknown, max = 160): string => + typeof value === "string" ? value.replace(/[\p{Cc}\p{Cf}]/gu, "").trim().slice(0, max) : ""; + +export function normalizeStickerPlacement(value: unknown): StickerPlacement | undefined { + if (!record(value) || !finite(value.x, -3, 3) || !finite(value.y, -3, 3) + || !finite(value.scale, 0.1, 4) || !finite(value.rotation, -Math.PI * 2, Math.PI * 2)) return undefined; + return Object.assign({ x: value.x, y: value.y, scale: value.scale, rotation: value.rotation }, + finite(value.parentWidth, 1, 10000) ? { parentWidth: value.parentWidth } : {}); +} + +export function normalizeStickerMetadata(value: unknown): StickerMetadata | undefined { + if (!record(value)) return undefined; + const placement = normalizeStickerPlacement(value.placement); + const app = label(value.app_name, 80); + return Object.assign({ + source: typeof value.source === "string" && sources.has(value.source) ? value.source as StickerMetadata["source"] : "unknown", + label: label(value.label) || "Sticker", + animated: value.animated === true, + }, app ? { app_name: app } : {}, placement ? { placement } : {}); +} + +/** Ordinary transparent PNGs remain ordinary images: sticker attribution is explicit. */ +export function normalizeStickerAttachment(attachment: AttachmentMeta): AttachmentMeta { + const sticker = normalizeStickerMetadata(attachment.sticker); + if (sticker) return Object.assign({}, attachment, { sticker }); + const plain = Object.assign({}, attachment); + delete plain.sticker; + return plain; +} + +export function normalizeStickerOverlays(value: unknown): { stickers: StickerOverlay[]; overflow: number } { + if (!Array.isArray(value)) return { stickers: [], overflow: 0 }; + const valid: StickerOverlay[] = []; + const identities = new Set(); + let total = 0; + for (const raw of value) { + if (!record(raw) || !record(raw.attachment)) continue; + const a = raw.attachment; + const sticker = normalizeStickerMetadata(a.sticker); + if (!sticker) continue; + const id = typeof a.id === "string" && /^[0-9]{1,18}$/.test(a.id) ? a.id : ""; + const guid = label(raw.guid, 128); + const key = `${guid}\0${id}`; + // A missing-media placeholder still needs its message identity to avoid + // merging unrelated unloaded stickers. Blank identities are not deduped. + if ((guid || id) && identities.has(key)) continue; + if (guid || id) identities.add(key); + total++; + if (valid.length >= MAX_VISIBLE_STICKERS) continue; + const placement = normalizeStickerPlacement(raw.placement) ?? sticker.placement; + valid.push(Object.assign({ + attachment: { + id, name: label(a.name, 256) || "Sticker", + mime: typeof a.mime === "string" ? a.mime.slice(0, 100) : null, + bytes: finite(a.bytes, 0, Number.MAX_SAFE_INTEGER) ? a.bytes : null, + sticker, + }, + guid, + part: finite(raw.part, 0, 999999) && Number.isInteger(raw.part) ? raw.part : 0, + from_me: raw.from_me === true, + by: label(raw.by, 160) || null, + ts: typeof raw.ts === "string" ? raw.ts : "", + }, raw.reaction === true ? { reaction: true } : {}, placement ? { placement } : {})); + } + return { stickers: valid, overflow: Math.max(0, total - valid.length) }; +} + +/** A self-thread can store decoration on either echo. Preserve both, once. */ +export function mergeStickers(a?: StickerOverlay[] | null, b?: StickerOverlay[] | null): StickerOverlay[] | undefined { + const all = [...(a ?? []), ...(b ?? [])]; + if (!all.length) return undefined; + const seen = new Set(); + return all.filter(s => { + const key = `${s.guid}\0${s.attachment?.id ?? ""}`; + if (seen.has(key)) return false; + seen.add(key); + return true; + }); +} + +/** Body-part 0 is the only safely addressable part without attributed-body + * range mapping. The renderer anchors it to text, or the first attachment. + */ +export function placedStickers(stickers: StickerOverlay[] | undefined): StickerOverlay[] { + return (stickers ?? []).filter(s => s.reaction !== true && s.part === 0 && !!normalizeStickerPlacement(s.placement)); +} + +export function unplacedStickers(stickers: StickerOverlay[] | undefined): StickerOverlay[] { + return (stickers ?? []).filter(s => s.reaction !== true && (s.part !== 0 || !normalizeStickerPlacement(s.placement))); +} + +export function reactionStickers(stickers: StickerOverlay[] | undefined): StickerOverlay[] { + return (stickers ?? []).filter(s => s.reaction === true); +} + +/** A compact decoration stays bounded even when metadata claims huge scaling. + * Unknown placement gets a readable row below the bubble, never a guessed + * position obscuring its text. The renderer chooses the target body part. + */ +export function stickerGeometry(placement: StickerPlacement | undefined, width: number, height: number, + naturalWidth = 64, naturalHeight = 64): + { x: number; y: number; width: number; height: number; rotation: number; placed: boolean } { + const w = finite(width, 1, 10000) ? width : 64; + const h = finite(height, 1, 10000) ? height : 64; + const nw = finite(naturalWidth, 1, 100000) ? naturalWidth : 64; + const nh = finite(naturalHeight, 1, 100000) ? naturalHeight : 64; + const p = normalizeStickerPlacement(placement); + const scale = p?.scale ?? 1; + const longEdge = Math.min(Math.max(40 * scale, 24), 120, w); + let sw = nw >= nh ? longEdge : longEdge * nw / nh; + let sh = nh >= nw ? longEdge : longEdge * nh / nw; + const angle = p?.rotation ?? 0; + const cos = Math.abs(Math.cos(angle)), sin = Math.abs(Math.sin(angle)); + let bw = sw * cos + sh * sin, bh = sh * cos + sw * sin; + const fit = Math.min(1, w / bw, h / bh); + sw *= fit; sh *= fit; bw *= fit; bh *= fit; + const insetX = (bw - sw) / 2, insetY = (bh - sh) / 2; + return { + // Sender offsets can sit outside the native bubble. Clamp to our target + // so they cannot escape the panel or cover another message's actions. + // Rotation is around the image center; clamp its rotated bounding box, + // not just the unrotated image, to the target. parentWidth is preserved + // as provenance but native point-for-point scale is intentionally not + // reproduced in a much narrower Linux bubble. + x: p ? Math.max(insetX, Math.min(w - sw - insetX, p.x * w - sw / 2)) : 0, + y: p ? Math.max(insetY, Math.min(h - sh - insetY, p.y * h - sh / 2)) : 0, + width: sw, height: sh, rotation: p ? p.rotation * 180 / Math.PI : 0, placed: !!p, + }; +} diff --git a/thread.ts b/thread.ts index 430537d5..40fb5c37 100644 --- a/thread.ts +++ b/thread.ts @@ -13,6 +13,7 @@ import { homedir } from "node:os"; import { spawnSync } from "node:child_process"; import { readFileSync } from "node:fs"; +import { normalizeStickerAttachment, normalizeStickerOverlays, type StickerOverlay } from "./stickers"; import { chatKey, dedupeSelfEcho, @@ -80,6 +81,10 @@ export interface Bubble { tapbacks: Tapback[]; /** Attachment chips ("📷 IMG_4502.png"); metadata only, files stay on the Mac. */ attachments: AttachmentMeta[]; + /** Stickers placed on this message, independent of the sender's tapback. */ + stickers: StickerOverlay[]; + /** Additional decorations omitted from the bounded render tree. */ + stickersOverflow: number; /** Inline-reply context: the quoted snippet, "" when not a reply. */ replyText: string; replyMine: boolean; @@ -307,6 +312,7 @@ export function decorate(msgs: ImsgMessage[], today: string, formats = DEFAULT_F localDay(next.ts) !== localDay(m.ts) || !sameSender(m, next) || minutesBetween(m.ts, next.ts) > GROUP_GAP_MINUTES; + const stickerState = normalizeStickerOverlays(m.stickers); out.push({ ts: m.ts, @@ -326,7 +332,9 @@ export function decorate(msgs: ImsgMessage[], today: string, formats = DEFAULT_F // Belt to imsg 1.5.1's suspenders: link-preview payloads are not files. attachments: (m.attachments ?? []).filter( (a) => !String(a.name || "").endsWith(".pluginPayloadAttachment"), - ), + ).map(normalizeStickerAttachment), + stickers: m.retracted ? [] : stickerState.stickers, + stickersOverflow: m.retracted ? 0 : stickerState.overflow, replyText: m.reply_to?.text ?? "", replyMine: m.reply_to?.from_me ?? false, edited: m.edited === true, @@ -420,6 +428,8 @@ export function pendingBubble(prev: Bubble | undefined, send: PendingSend, today receipt: "", tapbacks: [], attachments: [], + stickers: [], + stickersOverflow: 0, replyText: "", replyMine: false, edited: false, diff --git a/ui.test.ts b/ui.test.ts index f5bcc294..da7ad713 100644 --- a/ui.test.ts +++ b/ui.test.ts @@ -1076,7 +1076,7 @@ test("a thread response taken before a local send or failure cannot replace bubb // the picture (or file chip) itself, through the one shared TapbackPill. test("tapbacks on picture-only messages get a pill on the picture", () => { expect(panel).toContain("component TapbackPill: Rectangle {"); - expect(panel.split("TapbackPill {").length - 1).toBe(3); // text bubble, picture, file chip + expect(panel.split("TapbackPill {").length - 1).toBe(4); // text bubble, picture, file chip, standalone sticker expect(panel).toContain('readonly property bool pillHere: index === 0 && String(bubbleRow.modelData.text || "") === ""'); expect(panel).toContain("+ (pillHere ? Style.space(12) : 0)"); });