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

     1  package service
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"strings"
     7  
     8  	"github.com/keybase/client/go/gregor"
     9  	"github.com/keybase/client/go/libkb"
    10  	gregor1 "github.com/keybase/client/go/protocol/gregor1"
    11  	keybase1 "github.com/keybase/client/go/protocol/keybase1"
    12  	"github.com/keybase/go-framed-msgpack-rpc/rpc"
    13  
    14  	"golang.org/x/net/context"
    15  )
    16  
    17  type AvatarHandler struct {
    18  	libkb.Contextified
    19  	*BaseHandler
    20  	source libkb.AvatarLoaderSource
    21  }
    22  
    23  func NewAvatarHandler(xp rpc.Transporter, g *libkb.GlobalContext, source libkb.AvatarLoaderSource) *AvatarHandler {
    24  	handler := &AvatarHandler{
    25  		Contextified: libkb.NewContextified(g),
    26  		BaseHandler:  NewBaseHandler(g, xp),
    27  		source:       source,
    28  	}
    29  	return handler
    30  }
    31  
    32  var _ keybase1.AvatarsInterface = (*AvatarHandler)(nil)
    33  
    34  func (h *AvatarHandler) LoadUserAvatars(ctx context.Context, arg keybase1.LoadUserAvatarsArg) (keybase1.LoadAvatarsRes, error) {
    35  	mctx := libkb.NewMetaContext(ctx, h.G()).WithLogTag("UAVTR")
    36  	return h.source.LoadUsers(mctx, arg.Names, arg.Formats)
    37  }
    38  
    39  func (h *AvatarHandler) LoadTeamAvatars(ctx context.Context, arg keybase1.LoadTeamAvatarsArg) (keybase1.LoadAvatarsRes, error) {
    40  	mctx := libkb.NewMetaContext(ctx, h.G()).WithLogTag("TAVTR")
    41  	return h.source.LoadTeams(mctx, arg.Names, arg.Formats)
    42  }
    43  
    44  const avatarGregorHandlerName = "avatarHandler"
    45  
    46  type avatarGregorHandler struct {
    47  	libkb.Contextified
    48  	source libkb.AvatarLoaderSource
    49  }
    50  
    51  var _ libkb.GregorInBandMessageHandler = (*avatarGregorHandler)(nil)
    52  
    53  func newAvatarGregorHandler(g *libkb.GlobalContext, source libkb.AvatarLoaderSource) *avatarGregorHandler {
    54  	return &avatarGregorHandler{
    55  		Contextified: libkb.NewContextified(g),
    56  		source:       source,
    57  	}
    58  }
    59  
    60  func (r *avatarGregorHandler) Create(ctx context.Context, cli gregor1.IncomingInterface, category string, item gregor.Item) (bool, error) {
    61  	switch category {
    62  	case "avatar.clear_cache_for_name":
    63  		return true, r.clearName(ctx, cli, item)
    64  	default:
    65  		if strings.HasPrefix(category, "avatar.") {
    66  			return false, fmt.Errorf("unknown avatarGregorHandler category: %q", category)
    67  		}
    68  		return false, nil
    69  	}
    70  }
    71  
    72  func (r *avatarGregorHandler) Dismiss(ctx context.Context, cli gregor1.IncomingInterface, category string, item gregor.Item) (bool, error) {
    73  	return false, nil
    74  }
    75  
    76  func (r *avatarGregorHandler) IsAlive() bool {
    77  	return true
    78  }
    79  
    80  func (r *avatarGregorHandler) Name() string {
    81  	return avatarGregorHandlerName
    82  }
    83  
    84  func (r *avatarGregorHandler) clearName(ctx context.Context, cli gregor1.IncomingInterface, item gregor.Item) error {
    85  	m := libkb.NewMetaContext(ctx, r.G())
    86  	m.Debug("avatarGregorHandler: avatar.clear_cache_for_name received")
    87  	var msgs []keybase1.AvatarClearCacheMsg
    88  	if err := json.Unmarshal(item.Body().Bytes(), &msgs); err != nil {
    89  		m.Debug("error unmarshaling avatar.clear_cache_for_name item: %s", err)
    90  		return err
    91  	}
    92  
    93  	m.Debug("avatar.clear_cache_for_name unmarshaled: %+v", msgs)
    94  
    95  	for _, msg := range msgs {
    96  		if err := r.source.ClearCacheForName(m, msg.Name, msg.Formats); err != nil {
    97  			return err
    98  		}
    99  		r.G().NotifyRouter.HandleAvatarUpdated(ctx, msg.Name, msg.Formats, msg.Typ)
   100  	}
   101  
   102  	return r.G().GregorState.LocalDismissItem(ctx, item.Metadata().MsgID())
   103  }