+// whatsappContactName returns the name of the person jid: the full name, the
+// first name, the business name, or the push name of the contact in the session
+// of device, then push, the push name of a message, then the phone number. A
+// LID gets its phone number from the LID map of the session.
+func whatsappContactName(ctx context.Context, device *store.Device, jid types.JID, push string) string {
+ jid = jid.ToNonAD()
+ candidates := []types.JID{jid}
+ phone := types.EmptyJID
+ switch jid.Server {
+ case types.DefaultUserServer:
+ phone = jid
+ case types.HiddenUserServer:
+ mapped, err := device.LIDs.GetPNForLID(ctx, jid)
+ if err == nil && !mapped.IsEmpty() {
+ phone = mapped
+ candidates = append(candidates, mapped)
+ }
+ }
+
+ for _, candidate := range candidates {
+ contact, err := device.Contacts.GetContact(ctx, candidate)
+ if err != nil || !contact.Found {
+ continue
+ }
+
+ for _, name := range []string{contact.FullName, contact.FirstName, contact.BusinessName, contact.PushName} {
+ if name != "" {
+ return name
+ }
+ }
+ }
+
+ switch {
+ case push != "":
+ return push
+ case !phone.IsEmpty():
+ return "+" + phone.User
+ default:
+ return jid.User
+ }
+}
+