github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/contacts/interface.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 "fmt" 8 "time" 9 10 "github.com/keybase/client/go/libkb" 11 "github.com/keybase/client/go/protocol/keybase1" 12 ) 13 14 type ContactLookupResult struct { 15 UID keybase1.UID `json:"uid,omitempty"` 16 Coerced string `json:"coerced,omitempty"` 17 Error string `json:"err,omitempty"` 18 } 19 20 type ContactLookupKey string 21 type ContactLookupResults struct { 22 Results map[ContactLookupKey]ContactLookupResult 23 // Results provided - or not provided - by this provider 24 // should are valid for the following amount of time: 25 ResolvedFreshness time.Duration 26 UnresolvedFreshness time.Duration 27 Token Token 28 } 29 30 func NewContactLookupResults() ContactLookupResults { 31 return ContactLookupResults{ 32 Results: make(map[ContactLookupKey]ContactLookupResult), 33 } 34 } 35 36 func (r *ContactLookupResults) FindComponent(component keybase1.ContactComponent) (res ContactLookupResult, found bool) { 37 var key ContactLookupKey 38 switch { 39 case component.Email != nil: 40 key = MakeEmailLookupKey(*component.Email) 41 case component.PhoneNumber != nil: 42 key = MakePhoneLookupKey(*component.PhoneNumber) 43 default: 44 return res, false 45 } 46 res, found = r.Results[key] 47 return res, found 48 } 49 50 func MakeEmailLookupKey(e keybase1.EmailAddress) ContactLookupKey { 51 return ContactLookupKey(fmt.Sprintf("e:%s", string(e))) 52 } 53 54 func MakePhoneLookupKey(p keybase1.RawPhoneNumber) ContactLookupKey { 55 return ContactLookupKey(fmt.Sprintf("p:%s", string(p))) 56 } 57 58 type ContactUsernameAndFullName struct { 59 Username string 60 Fullname string 61 } 62 63 type Token string 64 65 const NoneToken Token = "" 66 67 type ContactsProvider interface { 68 LookupAllWithToken(libkb.MetaContext, []keybase1.EmailAddress, []keybase1.RawPhoneNumber, Token) (ContactLookupResults, error) 69 LookupAll(libkb.MetaContext, []keybase1.EmailAddress, []keybase1.RawPhoneNumber) (ContactLookupResults, error) 70 FindUsernames(libkb.MetaContext, []keybase1.UID) (map[keybase1.UID]ContactUsernameAndFullName, error) 71 FindFollowing(libkb.MetaContext, []keybase1.UID) (map[keybase1.UID]bool, error) 72 FindServiceMaps(libkb.MetaContext, []keybase1.UID) (map[keybase1.UID]libkb.UserServiceSummary, error) 73 }