business: fix contact projection and phone sharing

(cherry picked from commit c0a0e5b52240ed415d3b43ba77659821887bf50b)
This commit is contained in:
A 2026-06-07 23:36:06 +08:00
parent d84fa6e126
commit 860e581d06
18 changed files with 703 additions and 36 deletions

View file

@ -117,6 +117,48 @@ func TestServiceByIDDoesNotReloadSelf(t *testing.T) {
}
}
func TestServiceProjectsUsersForViewerContacts(t *testing.T) {
ctx := context.Background()
userStore := memory.NewUserStore()
contacts := memory.NewContactStore()
owner, err := userStore.Create(ctx, domain.User{AccessHash: 1, Phone: "15550000001", FirstName: "Owner"})
if err != nil {
t.Fatalf("create owner: %v", err)
}
friend, err := userStore.Create(ctx, domain.User{AccessHash: 2, Phone: "15550000002", FirstName: "Public", LastName: "Name"})
if err != nil {
t.Fatalf("create friend: %v", err)
}
stranger, err := userStore.Create(ctx, domain.User{AccessHash: 3, Phone: "15550000003", FirstName: "Stranger"})
if err != nil {
t.Fatalf("create stranger: %v", err)
}
if _, err := contacts.Upsert(ctx, owner.ID, domain.ContactInput{
ContactUserID: friend.ID,
Phone: "15550000002",
FirstName: "Remark",
LastName: "Friend",
}); err != nil {
t.Fatalf("upsert contact: %v", err)
}
svc := NewService(userStore, WithContactStore(contacts))
contactUser, found, err := svc.ByID(ctx, owner.ID, friend.ID)
if err != nil || !found {
t.Fatalf("ByID contact found=%v err=%v", found, err)
}
if !contactUser.Contact || contactUser.FirstName != "Remark" || contactUser.LastName != "Friend" || contactUser.Phone != "15550000002" {
t.Fatalf("projected contact = %+v, want contact remark and phone", contactUser)
}
nonContact, found, err := svc.ByID(ctx, owner.ID, stranger.ID)
if err != nil || !found {
t.Fatalf("ByID non-contact found=%v err=%v", found, err)
}
if nonContact.Contact || nonContact.Phone != "" || nonContact.FirstName != "Stranger" {
t.Fatalf("projected non-contact = %+v, want name with hidden phone", nonContact)
}
}
type countingUserStore struct {
*memory.UserStore
byIDCalls int