diff --git a/pkg/msgconv/from-whatsapp.go b/pkg/msgconv/from-whatsapp.go index 6dac5a2..0a24579 100644 --- a/pkg/msgconv/from-whatsapp.go +++ b/pkg/msgconv/from-whatsapp.go @@ -244,6 +244,7 @@ func (mc *MessageConverter) ToMatrix( } part.Extra["fi.mau.whatsapp.source_broadcast_list"] = info.Chat.String() } + markXessengerReceivedAlbum(part, waMsg, rawWaMsg) mc.addMentions(ctx, contextInfo.GetMentionedJID(), part.Content) if contextInfo.GetNonJIDMentions() == 1 { part.Content.Mentions.Room = true diff --git a/pkg/msgconv/xessenger_album_receive.go b/pkg/msgconv/xessenger_album_receive.go new file mode 100644 index 0000000..95a13f8 --- /dev/null +++ b/pkg/msgconv/xessenger_album_receive.go @@ -0,0 +1,56 @@ +package msgconv + +import ( + "go.mau.fi/whatsmeow/proto/waE2E" + "maunium.net/go/mautrix/bridgev2" +) + +// xessengerAlbumKey marks a photo or video that came as part of a WhatsApp album, so that Xessenger shows the album's +// items together the way WhatsApp does. The album send patch reads the same key on messages from Matrix. +const xessengerAlbumKey = "com.xessenger.album" + +// xessengerAlbumParentID is the album message a WhatsApp photo or video belongs to. WhatsApp sends an album as an album +// message ("Sent an album with N images:", a notice Xessenger doesn't show) followed by each photo or video as a +// message of its own, attached to the album message (MEDIA_ALBUM). A message sent from the user's phone or in a +// disappearing chat comes wrapped, and the attachment can be on any level, so every level is checked. +func xessengerAlbumParentID(msgs ...*waE2E.Message) string { + for _, msg := range msgs { + for msg != nil { + assoc := msg.GetMessageContextInfo().GetMessageAssociation() + if assoc.GetAssociationType() == waE2E.MessageAssociation_MEDIA_ALBUM && assoc.GetParentMessageKey().GetID() != "" { + return assoc.GetParentMessageKey().GetID() + } + msg = xessengerInnerMessage(msg) + } + } + return "" +} + +func xessengerInnerMessage(msg *waE2E.Message) *waE2E.Message { + for _, inner := range []*waE2E.Message{ + msg.GetDeviceSentMessage().GetMessage(), + msg.GetEphemeralMessage().GetMessage(), + msg.GetViewOnceMessage().GetMessage(), + msg.GetViewOnceMessageV2().GetMessage(), + msg.GetDocumentWithCaptionMessage().GetMessage(), + } { + if inner != nil { + return inner + } + } + return nil +} + +// markXessengerReceivedAlbum puts the album mark, the album message's ID, on a photo or video from a WhatsApp album. +// WhatsApp doesn't say the item's place in the album, so the mark carries only the id. +func markXessengerReceivedAlbum(part *bridgev2.ConvertedMessagePart, msgs ...*waE2E.Message) { + if part == nil || part.Content == nil || !part.Content.MsgType.IsMedia() { + return + } + if id := xessengerAlbumParentID(msgs...); id != "" { + if part.Extra == nil { + part.Extra = map[string]any{} + } + part.Extra[xessengerAlbumKey] = map[string]any{"id": id} + } +} diff --git a/pkg/msgconv/xessenger_album_receive_test.go b/pkg/msgconv/xessenger_album_receive_test.go new file mode 100644 index 0000000..2fa7858 --- /dev/null +++ b/pkg/msgconv/xessenger_album_receive_test.go @@ -0,0 +1,71 @@ +package msgconv + +import ( + "reflect" + "testing" + + "go.mau.fi/whatsmeow/proto/waCommon" + "go.mau.fi/whatsmeow/proto/waE2E" + "google.golang.org/protobuf/proto" + "maunium.net/go/mautrix/bridgev2" + "maunium.net/go/mautrix/event" +) + +func albumItem(assocType waE2E.MessageAssociation_AssociationType, parentID string) *waE2E.Message { + return &waE2E.Message{ + ImageMessage: &waE2E.ImageMessage{Mimetype: proto.String("image/jpeg")}, + MessageContextInfo: &waE2E.MessageContextInfo{MessageAssociation: &waE2E.MessageAssociation{ + AssociationType: assocType.Enum(), + ParentMessageKey: &waCommon.MessageKey{ID: proto.String(parentID), RemoteJID: proto.String("123@s.whatsapp.net")}, + }}, + } +} + +func photoPart() *bridgev2.ConvertedMessagePart { + return &bridgev2.ConvertedMessagePart{ + Type: event.EventMessage, + Content: &event.MessageEventContent{MsgType: event.MsgImage, Body: "photo.jpg"}, + } +} + +func TestReceivedAlbumItemsCarryTheAlbumMark(t *testing.T) { + item := albumItem(waE2E.MessageAssociation_MEDIA_ALBUM, "3EB0ALBUM") + part := photoPart() + markXessengerReceivedAlbum(part, item, item) + if got := part.Extra[xessengerAlbumKey]; !reflect.DeepEqual(got, map[string]any{"id": "3EB0ALBUM"}) { + t.Fatalf("album item mark = %#v", got) + } + + // Sent from the user's phone: the item comes inside a device-sent wrapper, and the unwrapped message is what's + // converted. + fromPhone := &waE2E.Message{DeviceSentMessage: &waE2E.DeviceSentMessage{ + DestinationJID: proto.String("123@s.whatsapp.net"), + Message: &waE2E.Message{EphemeralMessage: &waE2E.FutureProofMessage{Message: item}}, + }} + part = photoPart() + markXessengerReceivedAlbum(part, &waE2E.Message{ImageMessage: item.ImageMessage}, fromPhone) + if got := part.Extra[xessengerAlbumKey]; !reflect.DeepEqual(got, map[string]any{"id": "3EB0ALBUM"}) { + t.Fatalf("wrapped album item mark = %#v", got) + } + + // An HD copy of a photo is attached to the photo, not to an album. + part = photoPart() + hd := albumItem(waE2E.MessageAssociation_HD_IMAGE_DUAL_UPLOAD, "3EB0PHOTO") + markXessengerReceivedAlbum(part, hd, hd) + if _, ok := part.Extra[xessengerAlbumKey]; ok { + t.Fatal("an HD copy was marked as an album item") + } + + text := &bridgev2.ConvertedMessagePart{Type: event.EventMessage, Content: &event.MessageEventContent{MsgType: event.MsgText, Body: "hi"}} + markXessengerReceivedAlbum(text, item, item) + if _, ok := text.Extra[xessengerAlbumKey]; ok { + t.Fatal("a part without media was marked") + } + + part = photoPart() + plain := &waE2E.Message{ImageMessage: &waE2E.ImageMessage{}} + markXessengerReceivedAlbum(part, plain, plain) + if _, ok := part.Extra[xessengerAlbumKey]; ok { + t.Fatal("a photo outside an album was marked") + } +}