diff --git a/CHANGELOG.md b/CHANGELOG.md index 5002fc1..b2d956d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,8 +3,9 @@ * Added universal macOS binaries of `nac-relay` and `extract-key` to releases (`macoun-mac-tools-.tar.gz`). The bridge stays source only. * Added bubble and screen effects in both directions, through the - `com.beeper.imessage.effect` content key. Outbound effects are sent on text - messages only. See [docs/effects.md](docs/effects.md). + `com.beeper.imessage.effect` content key, on text and attachments. With a + caption the effect rides on the attachment and the caption follows plain. + Not on SMS. See [docs/effects.md](docs/effects.md). * Fixed members of an iMessage group who never post not appearing in the Matrix room. Every live group message now joins the members on Apple's roster. diff --git a/README.md b/README.md index 5fe7dcf..6d13e63 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ supported by Apple Inc. or the Corten-Matrix Project. | Images, video, audio, files, voice messages | yes | yes | | Replies | yes | yes | | Reactions (tapbacks) | yes | yes | -| Bubble and screen effects ([docs/effects.md](docs/effects.md)) | yes, text only | yes | +| Bubble and screen effects ([docs/effects.md](docs/effects.md)) | yes, not on SMS | yes | | Edits | yes | yes | | Unsend / redaction | yes | yes | | Read receipts | yes | yes | diff --git a/docs/bridge-guide.md b/docs/bridge-guide.md index 3946b57..7fe4667 100644 --- a/docs/bridge-guide.md +++ b/docs/bridge-guide.md @@ -202,8 +202,8 @@ iMessage bubble effects (Slam, Loud, Gentle, Invisible Ink) and screen effects Shooting Star) are bridged both ways through one content key on the message, `com.beeper.imessage.effect`, whose value is Apple's effect id (the key Beeper's iMessage bridge uses). The text is unchanged; invisible ink also arrives as a -Matrix spoiler. To send one, set the key on an outgoing text message. Effects -on outgoing attachments and SMS are not sent. Ids, examples and limits: +Matrix spoiler. To send one, set the key on an outgoing text message or +attachment. Effects on SMS are not sent. Ids, examples and limits: [effects.md](effects.md). ## FaceTime diff --git a/docs/effects.md b/docs/effects.md index e57ac6e..28a2b46 100644 --- a/docs/effects.md +++ b/docs/effects.md @@ -71,8 +71,9 @@ because the bridge reads none from CloudKit. ## Matrix to iMessage -Put the key on an outgoing text message (`m.text`, `m.notice`, `m.emote`) with -an id from the table. `body` is sent unchanged. +Put the key on an outgoing text message (`m.text`, `m.notice`, `m.emote`) or +attachment (`m.image`, `m.video`, `m.audio`, `m.file`) with an id from the +table. `body` is sent unchanged. ```json { @@ -86,7 +87,11 @@ an id from the table. `body` is sent unchanged. `formatted_body` alone is not turned into invisible ink. - An unknown id, or a value that is not a string, is logged and the message goes out without an effect. -- Effects on attachments are not sent. +- On an attachment with a caption (a `body` that differs from `filename`), + the caption goes out as its own iMessage after the attachment, and the + effect rides on the attachment only, so it plays once. A separate `m.text` + sent after the attachment is an ordinary message: put the key on it only if + it should have its own effect. - A message bound for the text-message relay (SMS) goes out without the effect, which SMS cannot carry. - A text long enough to be split carries the effect on its first part only. @@ -94,4 +99,6 @@ an id from the table. `body` is sent unchanged. Inside the bridge the Go connector passes the effect to `send_message` in `pkg/rustpushgo/src/lib.rs` as an envelope ahead of the text, `\x00FX\x01\x00`, next to the existing rich-link envelope. Rust -strips it and sets the message's effect. +strips it and sets the message's effect. For an attachment the envelope alone +is `send_attachment`'s `body` argument, split off the same way +(`attachment_effect_and_subject`). diff --git a/pkg/connector/client.go b/pkg/connector/client.go index b96d9e6..7425ce5 100644 --- a/pkg/connector/client.go +++ b/pkg/connector/client.go @@ -6782,9 +6782,17 @@ func (c *IMClient) handleMatrixFile(ctx context.Context, msg *bridgev2.MatrixMes replyGuid, replyPart := extractReplyInfo(msg.ReplyTo) + // An iMessage effect on the event rides on the attachment. A caption sent + // below as its own iMessage goes out plain, so the effect plays once. + var rawContent map[string]any + if msg.Event != nil { + rawContent = msg.Event.Content.Raw + } + effectBody := outboundAttachmentEffect(ctx, rawContent, conv.IsSms) + // Rust-side send_with_flap_retry handles SendTimedOut retry with a stable // UUID — no Go-side retry here (would orphan delivery receipts). - uuid, err := c.client.SendAttachment(conv, data, mimeType, mimeToUTI(mimeType), fileName, c.handle, replyGuid, replyPart, nil) + uuid, err := c.client.SendAttachment(conv, data, mimeType, mimeToUTI(mimeType), fileName, c.handle, replyGuid, replyPart, effectBody) if err != nil { if errors.Is(err, rustpushgo.ErrWrappedErrorNoSmsRelay) { return nil, errNoCarrierRoute diff --git a/pkg/connector/macoun_effects.go b/pkg/connector/macoun_effects.go index bc8d193..0eaf24d 100644 --- a/pkg/connector/macoun_effects.go +++ b/pkg/connector/macoun_effects.go @@ -118,3 +118,15 @@ func outboundTextWithEffect(ctx context.Context, raw map[string]any, isSms bool, // out any id that holds one. return "\x00FX\x01" + effect + "\x00" + text } + +// outboundAttachmentEffect returns the body argument for SendAttachment: the +// effect envelope alone when the message's content asks for a known effect, +// nil otherwise, under the same rules as outboundTextWithEffect. +// send_attachment splits the envelope off into the message's effect +// (attachment_effect_and_subject in lib.rs). +func outboundAttachmentEffect(ctx context.Context, raw map[string]any, isSms bool) *string { + if body := outboundTextWithEffect(ctx, raw, isSms, ""); body != "" { + return &body + } + return nil +} diff --git a/pkg/connector/macoun_effects_test.go b/pkg/connector/macoun_effects_test.go index 75d4ca6..37338bf 100644 --- a/pkg/connector/macoun_effects_test.go +++ b/pkg/connector/macoun_effects_test.go @@ -149,3 +149,23 @@ func TestOutboundEffectRefusals(t *testing.T) { t.Errorf("SMS outbound text = %q, want plain text", sms) } } + +// A photo or video can carry an effect as well as text: the key on an +// m.image/m.video/m.file event goes to send_attachment as the envelope alone. +func TestOutboundAttachmentEffect(t *testing.T) { + ctx := context.Background() + slam := map[string]any{imessageEffectKey: "com.apple.MobileSMS.expressivesend.impact"} + got := outboundAttachmentEffect(ctx, slam, false) + if got == nil || *got != "\x00FX\x01com.apple.MobileSMS.expressivesend.impact\x00" { + t.Fatalf("outboundAttachmentEffect = %v", got) + } + if got := outboundAttachmentEffect(ctx, slam, true); got != nil { + t.Fatalf("SMS attachment kept the effect: %q", *got) + } + if got := outboundAttachmentEffect(ctx, map[string]any{imessageEffectKey: "com.example.nope"}, false); got != nil { + t.Fatalf("unknown effect kept: %q", *got) + } + if got := outboundAttachmentEffect(ctx, nil, false); got != nil { + t.Fatalf("no effect asked for, got %q", *got) + } +} diff --git a/pkg/rustpushgo/src/lib.rs b/pkg/rustpushgo/src/lib.rs index 12aa054..df6b0b7 100644 --- a/pkg/rustpushgo/src/lib.rs +++ b/pkg/rustpushgo/src/lib.rs @@ -314,6 +314,19 @@ pub(crate) fn split_effect_envelope(text: String) -> (Option, String) { (None, text) } +/// The effect and subject for send_attachment. Its body may start with the +/// effect envelope, which the Go side sends alone for a photo or file that +/// carries an effect; whatever follows the envelope is the subject. +pub(crate) fn attachment_effect_and_subject(body: Option) -> (Option, Option) { + match body { + None => (None, None), + Some(body) => { + let (effect, rest) = split_effect_envelope(body); + (effect, Some(rest).filter(|s| !s.is_empty())) + } + } +} + mod manual_ford { use super::*; use aes_siv::{siv::CmacSiv, KeyInit}; @@ -10599,19 +10612,20 @@ impl Client { }]; // Captions are sent via the subject field in the iMessage plist, - // not as a separate text part in the XML body. - let subject = body.clone().filter(|s| !s.is_empty()); + // not as a separate text part in the XML body. An effect envelope + // ahead of it becomes the message's effect. + let (effect, subject) = attachment_effect_and_subject(body); let mut msg = MessageInst::new( conv.clone(), &handle, Message::Message(NormalMessage { parts: MessageParts(parts), - effect: None, + effect, reply_guid: reply_guid.clone(), reply_part: reply_part.clone(), service, - subject, + subject: subject.clone(), app: None, link_meta: None, voice: is_voice, @@ -10638,7 +10652,8 @@ impl Client { idx: None, ext: None, }]; - let sms_subject = body.filter(|s| !s.is_empty()); + // The text-message relay carries no effects. + let sms_subject = subject; let mut sms_msg = MessageInst::new( conv, &handle, diff --git a/pkg/rustpushgo/src/macoun_tests.rs b/pkg/rustpushgo/src/macoun_tests.rs index bccf2fd..5969074 100644 --- a/pkg/rustpushgo/src/macoun_tests.rs +++ b/pkg/rustpushgo/src/macoun_tests.rs @@ -65,3 +65,19 @@ fn macoun_effect_envelope_absent_or_malformed() { assert_eq!(rest, text); } } + +#[test] +fn macoun_attachment_effect_and_subject() { + use crate::attachment_effect_and_subject; + // The Go side passes the envelope alone as send_attachment's body. + let (effect, subject) = attachment_effect_and_subject(Some("\u{0}FX\u{1}com.apple.MobileSMS.expressivesend.impact\u{0}".to_string())); + assert_eq!(effect.as_deref(), Some("com.apple.MobileSMS.expressivesend.impact")); + assert_eq!(subject, None); + // Text after the envelope stays the subject, as a body without one does. + let (effect, subject) = attachment_effect_and_subject(Some("\u{0}FX\u{1}com.apple.messages.effect.CKHeartEffect\u{0}Hi".to_string())); + assert_eq!(effect.as_deref(), Some("com.apple.messages.effect.CKHeartEffect")); + assert_eq!(subject.as_deref(), Some("Hi")); + assert_eq!(attachment_effect_and_subject(Some("Hi".to_string())), (None, Some("Hi".to_string()))); + assert_eq!(attachment_effect_and_subject(Some(String::new())), (None, None)); + assert_eq!(attachment_effect_and_subject(None), (None, None)); +}