diff --git a/pkg/connector/handle_message.go b/pkg/connector/handle_message.go index 57640ea..39889d5 100644 --- a/pkg/connector/handle_message.go +++ b/pkg/connector/handle_message.go @@ -426,9 +426,13 @@ func (lc *LineClient) convertLineMessage(ctx context.Context, portal *bridgev2.P // Dispatch to content-type-specific handlers switch ContentType(data.ContentType) { case ContentImage: - return h.ConvertImage(ctx, portal, intent, data, decryptedBody, replyRelatesTo) + converted, err := h.ConvertImage(ctx, portal, intent, data, decryptedBody, replyRelatesTo) + markXessengerImageGroup(converted, data.ContentMetadata) + return converted, err case ContentVideo: - return h.ConvertVideo(ctx, portal, intent, data, decryptedBody, replyRelatesTo) + converted, err := h.ConvertVideo(ctx, portal, intent, data, decryptedBody, replyRelatesTo) + markXessengerImageGroup(converted, data.ContentMetadata) + return converted, err case ContentAudio: return h.ConvertAudio(ctx, portal, intent, data, decryptedBody, replyRelatesTo) case ContentFile: diff --git a/pkg/connector/xessenger_image_group.go b/pkg/connector/xessenger_image_group.go new file mode 100644 index 0000000..13abd64 --- /dev/null +++ b/pkg/connector/xessenger_image_group.go @@ -0,0 +1,42 @@ +package connector + +import ( + "strconv" + "strings" + + "maunium.net/go/mautrix/bridgev2" +) + +// xessengerAlbumKey marks a photo that came as part of a group of photos, so that Xessenger shows the group's photos +// together the way LINE does. +const xessengerAlbumKey = "com.xessenger.album" + +// xessengerImageGroup is the album mark of a photo LINE sent as part of a group of photos. LINE sends each photo of +// the group as a message of its own and shows them together as one grid. Each message's metadata carries the group's +// id (GID), the photo's place from 1 (GSEQ) and the group's size (GTOTAL). A group id of "0" (not given yet), a group +// of one or a place outside the group isn't a group. +func xessengerImageGroup(metadata map[string]string) map[string]any { + id := strings.TrimSpace(metadata["GID"]) + total, totalErr := strconv.Atoi(strings.TrimSpace(metadata["GTOTAL"])) + seq, seqErr := strconv.Atoi(strings.TrimSpace(metadata["GSEQ"])) + if id == "" || id == "0" || totalErr != nil || seqErr != nil || total < 2 || seq < 1 || seq > total { + return nil + } + return map[string]any{"id": id, "index": seq - 1, "count": total} +} + +// markXessengerImageGroup puts the album mark on the photo or video of a message from a LINE group of photos. +func markXessengerImageGroup(converted *bridgev2.ConvertedMessage, metadata map[string]string) { + if converted == nil || xessengerImageGroup(metadata) == nil { + return + } + for _, part := range converted.Parts { + if part == nil || part.Content == nil || !part.Content.MsgType.IsMedia() { + continue + } + if part.Extra == nil { + part.Extra = map[string]any{} + } + part.Extra[xessengerAlbumKey] = xessengerImageGroup(metadata) + } +} diff --git a/pkg/connector/xessenger_image_group_test.go b/pkg/connector/xessenger_image_group_test.go new file mode 100644 index 0000000..274ce75 --- /dev/null +++ b/pkg/connector/xessenger_image_group_test.go @@ -0,0 +1,64 @@ +package connector + +import ( + "reflect" + "testing" + + "maunium.net/go/mautrix/bridgev2" + "maunium.net/go/mautrix/event" +) + +func TestImageGroupMark(t *testing.T) { + cases := []struct { + name string + metadata map[string]string + want map[string]any + }{ + {"second of three", map[string]string{"GID": "620513338829365251", "GSEQ": "2", "GTOTAL": "3", "OID": "x"}, map[string]any{"id": "620513338829365251", "index": 1, "count": 3}}, + {"first", map[string]string{"GID": "620513338829365251", "GSEQ": "1", "GTOTAL": "2"}, map[string]any{"id": "620513338829365251", "index": 0, "count": 2}}, + {"no group", map[string]string{"OID": "x"}, nil}, + {"group id not given yet", map[string]string{"GID": "0", "GSEQ": "1", "GTOTAL": "3"}, nil}, + {"group of one", map[string]string{"GID": "6205", "GSEQ": "1", "GTOTAL": "1"}, nil}, + {"place before the group", map[string]string{"GID": "6205", "GSEQ": "0", "GTOTAL": "3"}, nil}, + {"place after the group", map[string]string{"GID": "6205", "GSEQ": "4", "GTOTAL": "3"}, nil}, + {"not a number", map[string]string{"GID": "6205", "GSEQ": "two", "GTOTAL": "3"}, nil}, + } + for _, c := range cases { + got := xessengerImageGroup(c.metadata) + if c.want == nil && got != nil || c.want != nil && !reflect.DeepEqual(got, c.want) { + t.Errorf("%s: mark = %#v, want %#v", c.name, got, c.want) + } + } +} + +func TestImageGroupMarkGoesOnTheMedia(t *testing.T) { + grouped := map[string]string{"GID": "6205", "GSEQ": "3", "GTOTAL": "3"} + photo := &bridgev2.ConvertedMessage{Parts: []*bridgev2.ConvertedMessagePart{{ + Type: event.EventMessage, + Content: &event.MessageEventContent{MsgType: event.MsgImage, Body: "image.jpg"}, + }}} + markXessengerImageGroup(photo, grouped) + if got := photo.Parts[0].Extra[xessengerAlbumKey]; !reflect.DeepEqual(got, map[string]any{"id": "6205", "index": 2, "count": 3}) { + t.Fatalf("photo mark = %#v", got) + } + + notice := &bridgev2.ConvertedMessage{Parts: []*bridgev2.ConvertedMessagePart{{ + Type: event.EventMessage, + Content: &event.MessageEventContent{MsgType: event.MsgNotice, Body: "[Unable to decrypt LINE message.]"}, + }}} + markXessengerImageGroup(notice, grouped) + if _, ok := notice.Parts[0].Extra[xessengerAlbumKey]; ok { + t.Fatal("a part without media was marked") + } + + single := &bridgev2.ConvertedMessage{Parts: []*bridgev2.ConvertedMessagePart{{ + Type: event.EventMessage, + Content: &event.MessageEventContent{MsgType: event.MsgImage, Body: "image.jpg"}, + }}} + markXessengerImageGroup(single, map[string]string{"OID": "x"}) + if _, ok := single.Parts[0].Extra[xessengerAlbumKey]; ok { + t.Fatal("a photo outside a group was marked") + } + + markXessengerImageGroup(nil, grouped) +}