--- /dev/null +++ b/pkg/connector/xessenger_identifiers.go @@ -0,0 +1,32 @@ +package connector + +import ( + "context" + "fmt" + + "go.mau.fi/whatsmeow/types" + "maunium.net/go/mautrix/bridgev2" + + "go.mau.fi/mautrix-whatsapp/pkg/waid" +) + +// 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 phone number contactToUserInfo gives (tel:+), the one the +// ghost's ID is, or for a ghost known by its LID the one the bridge learnt for that LID (whatsmeow's +// LID map). A bot has none, nor a LID with no number learnt. A LID map that could not be read is an +// error, for the ghost to go again. +func (wa *WhatsAppConnector) GhostIdentifiers(ctx context.Context, ghost *bridgev2.Ghost) ([]string, error) { + jid := waid.ParseUserID(ghost.ID) + if jid.Server == types.HiddenUserServer && wa.DeviceStore != nil && wa.DeviceStore.LIDMap != nil { + pn, err := wa.DeviceStore.LIDMap.GetPNForLID(ctx, jid) + if err != nil { + return nil, fmt.Errorf("failed to read the number learnt for the LID: %w", err) + } + jid = pn + } + if jid.Server != types.DefaultUserServer || jid.User == "" { + return nil, nil + } + return []string{"tel:+" + jid.User}, nil +} --- /dev/null +++ b/pkg/connector/xessenger_identifiers_test.go @@ -0,0 +1,60 @@ +package connector + +import ( + "context" + "database/sql" + "path/filepath" + "slices" + "testing" + + _ "github.com/mattn/go-sqlite3" + "go.mau.fi/whatsmeow/store/sqlstore" + "go.mau.fi/whatsmeow/types" + "maunium.net/go/mautrix/bridgev2" + "maunium.net/go/mautrix/bridgev2/database" + "maunium.net/go/mautrix/bridgev2/networkid" +) + +// A ghost named by its phone number has that number as its identifier, as contactToUserInfo names it; a +// ghost known by its LID has the number the LID map learnt for it, and none when it learnt none or the +// bridge keeps no LID map; a bot has none. A LID map that could not be read is an error, not a ghost +// without a number. +func TestXessengerGhostIdentifiers(t *testing.T) { + ctx := context.Background() + wa := &WhatsAppConnector{} + ghost := func(ghostID networkid.UserID) *bridgev2.Ghost { + return &bridgev2.Ghost{Ghost: &database.Ghost{ID: ghostID}} + } + if got, err := wa.GhostIdentifiers(ctx, ghost("821012345678")); err != nil || !slices.Equal(got, []string{"tel:+821012345678"}) { + t.Errorf("a ghost named by its number: %v (%v)", got, err) + } + for _, ghostID := range []networkid.UserID{"lid-123456789012345", "bot-867051314767696", ""} { + if got, err := wa.GhostIdentifiers(ctx, ghost(ghostID)); err != nil || got != nil { + t.Errorf("%q: %v (%v)", ghostID, got, err) + } + } + + raw, err := sql.Open("sqlite3", "file:"+filepath.Join(t.TempDir(), "whatsmeow.db")+"?_foreign_keys=on") + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { _ = raw.Close() }) + wa.DeviceStore = sqlstore.NewWithDB(raw, "sqlite3", nil) + // Before its tables are made, the LID map cannot be read. + if got, err := wa.GhostIdentifiers(ctx, ghost("lid-123456789012345")); err == nil { + t.Errorf("an unreadable LID map: %v", got) + } + if err = wa.DeviceStore.Upgrade(ctx); err != nil { + t.Fatal(err) + } + lid, pn := types.NewJID("123456789012345", types.HiddenUserServer), types.NewJID("821098765432", types.DefaultUserServer) + if err = wa.DeviceStore.LIDMap.PutLIDMapping(ctx, lid, pn); err != nil { + t.Fatal(err) + } + if got, err := wa.GhostIdentifiers(ctx, ghost("lid-123456789012345")); err != nil || !slices.Equal(got, []string{"tel:+821098765432"}) { + t.Errorf("a ghost known by its LID, its number learnt: %v (%v)", got, err) + } + if got, err := wa.GhostIdentifiers(ctx, ghost("lid-999999999999999")); err != nil || got != nil { + t.Errorf("a ghost known by its LID, no number learnt: %v (%v)", got, err) + } +}