--- a/pkg/connector/client.go +++ b/pkg/connector/client.go @@ -412,22 +412,17 @@ if err != nil { zerolog.Ctx(ctx).Err(err).Msg("Failed to get own ghost to sync remote profile") return - } else if ghost == nil { - return } } - if ghost.ID != ownID { + if ghost != nil && ghost.ID != ownID { return } - name := wa.GetStore().BusinessName - if name == "" { - name = wa.GetStore().PushName - } - if name == "" || wa.UserLogin.RemoteProfile.Name == name && wa.UserLogin.RemoteProfile.Avatar == ghost.AvatarMXC { + // Xessenger: a fresh login has no ghost of its own, yet its name is known (xessenger_profile.go). + profile, changed := xessengerOwnProfile(wa.UserLogin.RemoteProfile, wa.GetStore().BusinessName, wa.GetStore().PushName, ghost) + if !changed { return } - wa.UserLogin.RemoteProfile.Name = name - wa.UserLogin.RemoteProfile.Avatar = ghost.AvatarMXC + wa.UserLogin.RemoteProfile = profile err := wa.UserLogin.Save(ctx) if err != nil { zerolog.Ctx(ctx).Err(err).Msg("Failed to save remote profile") --- /dev/null +++ b/pkg/connector/xessenger_profile.go @@ -0,0 +1,27 @@ +package connector + +import ( + "maunium.net/go/mautrix/bridgev2" + "maunium.net/go/mautrix/bridgev2/status" +) + +// xessengerOwnProfile is the login's profile carrying the account's own WhatsApp name. +// The first full app state sync after login stores the push name without emitting its +// change, so the account's own ghost is never made and the upstream sync skipped the +// name for good. The name is kept without a ghost too; the avatar then stays as it was. +func xessengerOwnProfile(current status.RemoteProfile, businessName, pushName string, ghost *bridgev2.Ghost) (status.RemoteProfile, bool) { + name := businessName + if name == "" { + name = pushName + } + avatar := current.Avatar + if ghost != nil { + avatar = ghost.AvatarMXC + } + if name == "" || current.Name == name && current.Avatar == avatar { + return current, false + } + current.Name = name + current.Avatar = avatar + return current, true +} --- /dev/null +++ b/pkg/connector/xessenger_profile_test.go @@ -0,0 +1,30 @@ +package connector + +import ( + "testing" + + "maunium.net/go/mautrix/bridgev2" + "maunium.net/go/mautrix/bridgev2/database" + "maunium.net/go/mautrix/bridgev2/status" +) + +func TestXessengerOwnProfileKeepsNameWithoutOwnGhost(t *testing.T) { + fresh := status.RemoteProfile{Phone: "+15550000001"} + named, changed := xessengerOwnProfile(fresh, "", "Fixture Name", nil) + if !changed || named.Name != "Fixture Name" || named.Phone != fresh.Phone || named.Avatar != "" { + t.Fatalf("a fresh login keeps its push name and number: %+v", named) + } + if _, changed := xessengerOwnProfile(named, "", "Fixture Name", nil); changed { + t.Fatal("an unchanged profile is not saved again") + } + if business, _ := xessengerOwnProfile(fresh, "Fixture Shop", "Fixture Name", nil); business.Name != "Fixture Shop" { + t.Fatalf("a business name wins over the push name: %+v", business) + } + if _, changed := xessengerOwnProfile(fresh, "", "", nil); changed { + t.Fatal("without a name nothing changes") + } + ghost := &bridgev2.Ghost{Ghost: &database.Ghost{AvatarMXC: "mxc://xessenger.test/own"}} + if withAvatar, _ := xessengerOwnProfile(named, "", "Fixture Name", ghost); withAvatar.Avatar != "mxc://xessenger.test/own" { + t.Fatalf("the own ghost's avatar is still used when it exists: %+v", withAvatar) + } +}