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