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

     1  package avatars
     2  
     3  import (
     4  	"fmt"
     5  	"time"
     6  
     7  	"github.com/keybase/client/go/kbhttp/manager"
     8  	"github.com/keybase/client/go/libkb"
     9  	"github.com/keybase/client/go/protocol/keybase1"
    10  )
    11  
    12  const (
    13  	// When changing staleThreshold here, serverside avatar
    14  	// `client_avatar_stale_threshold` should be adjusted to match.
    15  	staleThreshold = 24 * time.Hour
    16  )
    17  
    18  func CreateSourceFromEnvAndInstall(g *libkb.GlobalContext) {
    19  	var s libkb.AvatarLoaderSource
    20  	typ := g.Env.GetAvatarSource()
    21  	switch typ {
    22  	case "simple":
    23  		s = NewSimpleSource()
    24  	case "url":
    25  		s = NewURLCachingSource(staleThreshold, 20000)
    26  	case "full":
    27  		maxSize := 10000
    28  		if g.IsMobileAppType() {
    29  			maxSize = 2000
    30  		}
    31  		s = NewFullCachingSource(g, staleThreshold, maxSize)
    32  	}
    33  	g.AddDbNukeHook(s, fmt.Sprintf("AvatarLoader[%s]", typ))
    34  	g.SetAvatarLoader(s)
    35  }
    36  
    37  func ServiceInit(g *libkb.GlobalContext, httpSrv *manager.Srv, source libkb.AvatarLoaderSource) *Srv {
    38  	m := libkb.NewMetaContextBackground(g)
    39  	source.StartBackgroundTasks(m)
    40  	s := NewSrv(g, httpSrv, source) // start the http srv up
    41  	g.PushShutdownHook(func(mctx libkb.MetaContext) error {
    42  		source.StopBackgroundTasks(mctx)
    43  		return nil
    44  	})
    45  	return s
    46  }
    47  
    48  func allocRes(res *keybase1.LoadAvatarsRes, usernames []string) {
    49  	res.Picmap = make(map[string]map[keybase1.AvatarFormat]keybase1.AvatarUrl)
    50  	for _, u := range usernames {
    51  		res.Picmap[u] = make(map[keybase1.AvatarFormat]keybase1.AvatarUrl)
    52  	}
    53  }