github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/avatars/simple.go (about) 1 package avatars 2 3 import ( 4 "fmt" 5 "strings" 6 7 "github.com/keybase/client/go/libkb" 8 "github.com/keybase/client/go/protocol/keybase1" 9 ) 10 11 type apiAvatarRes struct { 12 Status libkb.AppStatus `json:"status"` 13 Pictures []map[keybase1.AvatarFormat]keybase1.AvatarUrl `json:"pictures"` 14 } 15 16 func (a apiAvatarRes) GetAppStatus() *libkb.AppStatus { 17 return &a.Status 18 } 19 20 type SimpleSource struct{} 21 22 func NewSimpleSource() *SimpleSource { 23 return &SimpleSource{} 24 } 25 26 var _ libkb.AvatarLoaderSource = (*SimpleSource)(nil) 27 28 func (s *SimpleSource) StartBackgroundTasks(_ libkb.MetaContext) {} 29 func (s *SimpleSource) StopBackgroundTasks(_ libkb.MetaContext) {} 30 31 func (s *SimpleSource) formatArg(formats []keybase1.AvatarFormat) string { 32 var strs []string 33 for _, f := range formats { 34 strs = append(strs, f.String()) 35 } 36 return strings.Join(strs, ",") 37 } 38 39 func (s *SimpleSource) apiReq(m libkb.MetaContext, endpoint, param string, names []string, 40 formats []keybase1.AvatarFormat) (apiAvatarRes, error) { 41 arg := libkb.NewAPIArg(endpoint) 42 arg.Args = libkb.NewHTTPArgs() 43 arg.SessionType = libkb.APISessionTypeOPTIONAL 44 uarg := strings.Join(names, ",") 45 farg := s.formatArg(formats) 46 arg.Args.Add(param, libkb.S{Val: uarg}) 47 arg.Args.Add("formats", libkb.S{Val: farg}) 48 s.debug(m, "issuing %s avatar req: uarg: %s farg: %s", param, uarg, farg) 49 var apiRes apiAvatarRes 50 if err := m.G().API.GetDecode(m, arg, &apiRes); err != nil { 51 s.debug(m, "apiReq: API fail: %s", err) 52 return apiRes, err 53 } 54 return apiRes, nil 55 } 56 57 func (s *SimpleSource) makeRes(res *keybase1.LoadAvatarsRes, apiRes apiAvatarRes, names []string) error { 58 if len(apiRes.Pictures) != len(names) { 59 return fmt.Errorf("invalid API server response, wrong number of users: %d != %d", 60 len(apiRes.Pictures), len(names)) 61 } 62 allocRes(res, names) 63 for index, rec := range apiRes.Pictures { 64 u := names[index] 65 for format, url := range rec { 66 res.Picmap[u][format] = url 67 } 68 } 69 return nil 70 } 71 72 func (s *SimpleSource) debug(m libkb.MetaContext, msg string, args ...interface{}) { 73 m.Debug("Avatars.SimpleSource: %s", fmt.Sprintf(msg, args...)) 74 } 75 76 func (s *SimpleSource) LoadUsers(m libkb.MetaContext, usernames []string, formats []keybase1.AvatarFormat) (res keybase1.LoadAvatarsRes, err error) { 77 defer m.Trace("SimpleSource.LoadUsers", &err)() 78 apiRes, err := s.apiReq(m, "image/username_pic_lookups", "usernames", usernames, formats) 79 if err != nil { 80 return res, err 81 } 82 if err = s.makeRes(&res, apiRes, usernames); err != nil { 83 return res, err 84 } 85 return res, nil 86 } 87 88 func (s *SimpleSource) LoadTeams(m libkb.MetaContext, teams []string, formats []keybase1.AvatarFormat) (res keybase1.LoadAvatarsRes, err error) { 89 defer m.Trace("SimpleSource.LoadTeams", &err)() 90 apiRes, err := s.apiReq(m, "image/team_avatar_lookups", "team_names", teams, formats) 91 if err != nil { 92 return res, err 93 } 94 if err = s.makeRes(&res, apiRes, teams); err != nil { 95 return res, err 96 } 97 return res, nil 98 } 99 100 func (s *SimpleSource) ClearCacheForName(m libkb.MetaContext, name string, formats []keybase1.AvatarFormat) (err error) { 101 return nil 102 } 103 104 func (s *SimpleSource) OnDbNuke(m libkb.MetaContext) error { return nil }