--- a/pkg/igconnector/chatinfo.go +++ b/pkg/igconnector/chatinfo.go @@ -18,6 +18,7 @@ import ( "context" + "errors" "fmt" "net/url" "path" @@ -43,7 +44,18 @@ if err != nil { return nil, err } - return ic.wrapChatInfo(info.ThreadInfo.AsIGDirectThread), nil + return ic.threadChatInfo(info.ThreadInfo) +} + +// threadChatInfo is the info of a thread as GetThread answers it. Xessenger: a thread Instagram answers +// without its direct thread (as_ig_direct_thread null) has no info to give, which errors.ErrUnsupported +// tells a caller rather than wrapChatInfo reading the missing thread (a nil pointer): a history move then +// moves it with what the bridge holds (services/bridgekit/migrate moveChatInfo). +func (ic *IGClient) threadChatInfo(thread slidetypes.WrappedThreadInfo) (*bridgev2.ChatInfo, error) { + if thread.AsIGDirectThread == nil { + return nil, fmt.Errorf("the thread came without its direct thread: %w", errors.ErrUnsupported) + } + return ic.wrapChatInfo(thread.AsIGDirectThread), nil } func (ic *IGClient) GetUserInfo(ctx context.Context, ghost *bridgev2.Ghost) (*bridgev2.UserInfo, error) { --- /dev/null +++ b/pkg/igconnector/xessenger_chatinfo_test.go @@ -0,0 +1,28 @@ +package igconnector + +import ( + "context" + "errors" + "testing" + + "maunium.net/go/mautrix/bridgev2" + "maunium.net/go/mautrix/bridgev2/database" + "maunium.net/go/mautrix/bridgev2/networkid" + + "go.mau.fi/mautrix-meta/pkg/instameow/slidetypes" + "go.mau.fi/mautrix-meta/pkg/metaid" +) + +// A thread Instagram answers without its direct thread has no info to give: the connector says the request +// is unsupported (errors.ErrUnsupported), which a history move takes as its cue to use what the bridge +// holds, instead of reading the missing thread. A request that could not be made stays a failure. +func TestXessengerChatInfoWithoutDirectThreadIsUnsupported(t *testing.T) { + info, err := (&IGClient{}).threadChatInfo(slidetypes.WrappedThreadInfo{ID: "1"}) + if info != nil || !errors.Is(err, errors.ErrUnsupported) { + t.Fatalf("a thread without its direct thread answered %v, %v; want errors.ErrUnsupported", info, err) + } + portal := &bridgev2.Portal{Portal: &database.Portal{PortalKey: networkid.PortalKey{ID: metaid.MakeFBPortalID(1)}}} + if info, err = (&IGClient{}).GetChatInfo(context.Background(), portal); info != nil || err == nil || errors.Is(err, errors.ErrUnsupported) { + t.Fatalf("a client that could not ask answered %v, %v; want a failure, not errors.ErrUnsupported", info, err) + } +}