--- a/pkg/igconnector/chatinfo.go +++ b/pkg/igconnector/chatinfo.go @@ -75,7 +75,9 @@ } func (ic *IGClient) wrapUserInfo(info *slidetypes.User) *bridgev2.UserInfo { - var identifiers []string + // Xessenger: a profile without a username clears the one kept, as updateGhostIGID clears it from the + // metadata; nil would keep it (QA-289). + identifiers := []string{} if info.Username != "" { identifiers = []string{fmt.Sprintf("instagram:%s", info.Username)} } @@ -93,7 +95,8 @@ } func (ic *IGClient) wrapSearchResultInfo(info *slidetypes.SearchResult) *bridgev2.UserInfo { - var identifiers []string + // Xessenger: as in wrapUserInfo. + identifiers := []string{} if info.Username != "" { identifiers = []string{fmt.Sprintf("instagram:%s", info.Username)} } --- /dev/null +++ b/pkg/igconnector/xessenger_identifiers.go @@ -0,0 +1,21 @@ +package igconnector + +import ( + "context" + + "maunium.net/go/mautrix/bridgev2" + + "go.mau.fi/mautrix-meta/pkg/metaid" +) + +// GhostIdentifiers names a ghost's identifiers from what the bridge stored of it, for Xessenger's +// bridgekit (QA-289, services/bridgekit/identifiers.go), which fills in those of the ghosts made +// before bridgev2 kept them: the Instagram username wrapUserInfo gives, which updateGhostIGID keeps in +// the ghost's metadata. Reading it does not fail. +func (ic *IGConnector) GhostIdentifiers(_ context.Context, ghost *bridgev2.Ghost) ([]string, error) { + meta, ok := ghost.Metadata.(*metaid.GhostMetadata) + if !ok || meta.Username == "" { + return nil, nil + } + return []string{"instagram:" + meta.Username}, nil +} --- /dev/null +++ b/pkg/igconnector/xessenger_identifiers_test.go @@ -0,0 +1,49 @@ +package igconnector + +import ( + "context" + "slices" + "testing" + + "maunium.net/go/mautrix/bridgev2" + "maunium.net/go/mautrix/bridgev2/database" + + "go.mau.fi/mautrix-meta/pkg/instameow/slidetypes" + "go.mau.fi/mautrix-meta/pkg/metaid" +) + +// A ghost's Instagram username, kept in its metadata, is its identifier, as wrapUserInfo names it; a +// ghost whose profile never named one has none. +func TestXessengerGhostIdentifiers(t *testing.T) { + ic := &IGConnector{} + ghost := func(meta any) *bridgev2.Ghost { return &bridgev2.Ghost{Ghost: &database.Ghost{Metadata: meta}} } + got, err := ic.GhostIdentifiers(context.Background(), ghost(&metaid.GhostMetadata{Username: "kim.minsu", IGID: "17841400000000000"})) + if err != nil || !slices.Equal(got, []string{"instagram:kim.minsu"}) { + t.Errorf("a ghost with its username: %v (%v)", got, err) + } + for _, meta := range []any{&metaid.GhostMetadata{IGID: "17841400000000000"}, nil, map[string]any{}} { + if got, err := ic.GhostIdentifiers(context.Background(), ghost(meta)); err != nil || got != nil { + t.Errorf("%#v: %v (%v)", meta, got, err) + } + } +} + +// A profile names its username as the identifier. One without a username names an empty list, which +// clears the one kept (bridgev2 keeps them for nil), as updateGhostIGID clears it from the metadata. +func TestXessengerProfileWithoutUsername(t *testing.T) { + ic := &IGClient{Main: &IGConnector{Config: Config{DisplaynameTemplate: "{{.DisplayName}}"}}} + if err := ic.Main.Config.PostProcess(); err != nil { + t.Fatal(err) + } + if got := ic.wrapUserInfo(&slidetypes.User{Username: "kim.minsu"}).Identifiers; !slices.Equal(got, []string{"instagram:kim.minsu"}) { + t.Errorf("a profile with its username: %v", got) + } + for what, got := range map[string][]string{ + "a profile": ic.wrapUserInfo(&slidetypes.User{}).Identifiers, + "a search result": ic.wrapSearchResultInfo(&slidetypes.SearchResult{}).Identifiers, + } { + if got == nil || len(got) != 0 { + t.Errorf("%s without a username: %#v", what, got) + } + } +}