github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/contacts/lookup.go (about)

     1  // Copyright 2019 Keybase, Inc. All rights reserved. Use of
     2  // this source code is governed by the included BSD license.
     3  
     4  package contacts
     5  
     6  import (
     7  	"time"
     8  
     9  	"github.com/keybase/client/go/libkb"
    10  	"github.com/keybase/client/go/protocol/keybase1"
    11  )
    12  
    13  func BulkLookupContacts(mctx libkb.MetaContext, emailsContacts []keybase1.EmailAddress,
    14  	phoneNumberContacts []keybase1.RawPhoneNumber, token Token) (res ContactLookupResults, err error) {
    15  
    16  	type lookupArg struct {
    17  		Email       string `json:"e,omitempty"`
    18  		PhoneNumber string `json:"p,omitempty"`
    19  	}
    20  
    21  	type lookupRes struct {
    22  		libkb.AppStatusEmbed
    23  		Resolutions           map[ContactLookupKey]ContactLookupResult `json:"resolutions"`
    24  		ResolvedFreshnessMs   int                                      `json:"resolved_freshness_ms"`
    25  		UnresolvedFreshnessMs int                                      `json:"unresolved_freshness_ms"`
    26  		Token                 Token                                    `json:"token"`
    27  		TokenValidateErr      string                                   `json:"validate_err"`
    28  		TokenValidateSC       int                                      `json:"validate_err_sc"`
    29  	}
    30  
    31  	lookups := make([]lookupArg, 0, len(phoneNumberContacts)+len(emailsContacts))
    32  	for _, phoneNumber := range phoneNumberContacts {
    33  		lookups = append(lookups, lookupArg{PhoneNumber: string(phoneNumber)})
    34  	}
    35  	for _, email := range emailsContacts {
    36  		lookups = append(lookups, lookupArg{Email: string(email)})
    37  	}
    38  
    39  	payload := make(libkb.JSONPayload)
    40  	payload["contacts"] = lookups
    41  	payload["token"] = token
    42  	payload["supports_tokens"] = 1
    43  
    44  	arg := libkb.APIArg{
    45  		Endpoint:    "contacts/lookup",
    46  		JSONPayload: payload,
    47  		SessionType: libkb.APISessionTypeREQUIRED,
    48  	}
    49  	var resp lookupRes
    50  	err = mctx.G().API.PostDecode(mctx, arg, &resp)
    51  	if err != nil {
    52  		return res, err
    53  	}
    54  	if resp.TokenValidateErr != "" {
    55  		mctx.Debug("Error in validation of contact token: %s, code %d", resp.TokenValidateErr, resp.TokenValidateSC)
    56  	}
    57  	res = NewContactLookupResults()
    58  	res.Results = resp.Resolutions
    59  	res.ResolvedFreshness = time.Duration(resp.ResolvedFreshnessMs) * time.Millisecond
    60  	res.UnresolvedFreshness = time.Duration(resp.UnresolvedFreshnessMs) * time.Millisecond
    61  	res.Token = resp.Token
    62  	mctx.Debug(
    63  		"BulkLookupContacts: server said we should cache resolved entries for %.2f s and unresolved for %.2f s",
    64  		res.ResolvedFreshness.Seconds(), res.UnresolvedFreshness.Seconds())
    65  	return res, nil
    66  }