Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion plugins/platforms/photon/sidecar/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,13 @@ async function normalizeContent(content) {
targetText: reactionTargetText(target),
};
}
// spectrum-ts converts Apple's URL balloon to a richlink content object
// with the URL in content.url. Extract it as plain text so the Python
// adapter sees a text message containing the link, not an opaque
// "richlink" content type it can't handle.
if (content.type === "richlink") {
return { type: "text", text: content.url || "" };
}
return { type: content.type || "unknown" };
}

Expand Down Expand Up @@ -732,7 +739,26 @@ const server = http.createServer(async (req, res) => {
// readable plain text on platforms that don't.
const builder =
format === "markdown" ? spectrumMarkdown(text) : spectrumText(text);
const result = await space.send(builder);
let result;
try {
result = await space.send(builder);
} catch (err) {
// If Apple's IMAgentKit rejects enable_data_detection (set by
// spectrum-ts when the text contains links), strip protocol
// prefixes and retry. Without "https://" the markdown link
// detector won't fire, so data detection stays off and the
// message delivers. iOS still renders domain-like text as
// a tappable link on the receiving end.
const msg = err && (err.message || String(err));
if (msg && msg.includes("enable_data_detection")) {
const stripped = text.replace(/https?:\/\//g, "");
const fallbackBuilder =
format === "markdown" ? spectrumMarkdown(stripped) : spectrumText(stripped);
result = await space.send(fallbackBuilder);
} else {
throw err;
}
}
return ok(res, { messageId: result?.id || null });
}
if (req.url === "/send-attachment") {
Expand Down
16 changes: 16 additions & 0 deletions tests/plugins/platforms/photon/test_spectrum_patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,22 @@ def test_sidecar_labels_catchup_internal_errors_as_upstream_photon() -> None:
assert "PHOTON_ALLOWED_USERS" in index


def test_sidecar_send_falls_back_on_enable_data_detection_error() -> None:
"""When Apple rejects enable_data_detection, the /send handler must
catch the error and retry with protocol prefixes stripped."""
index = Path("plugins/platforms/photon/sidecar/index.mjs").read_text(encoding="utf-8")
assert "enable_data_detection" in index
assert "https?:\\/\\/" in index
assert "throw err" in index # non-data-detection errors re-thrown


def test_sidecar_normalize_content_handles_richlink() -> None:
"""Inbound richlink content must be normalized to text with the URL."""
index = Path("plugins/platforms/photon/sidecar/index.mjs").read_text(encoding="utf-8")
assert 'content.type === "richlink"' in index
assert "content.url" in index


def _tabify(src: str) -> str:
"""Convert the fixture's two-space indentation to the tab indentation that
spectrum-ts ships in `@spectrum-ts/imessage/dist`, so the patch anchors
Expand Down