github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/service/kbfs_favorites_handler.go (about)

     1  // Copyright 2018 Keybase, Inc. All rights reserved. Use of
     2  // this source code is governed by the included BSD license.
     3  
     4  // Handlers for KBFS-favorites-related gregor messages
     5  
     6  package service
     7  
     8  import (
     9  	"fmt"
    10  	"strings"
    11  
    12  	"github.com/keybase/client/go/protocol/keybase1"
    13  
    14  	"golang.org/x/net/context"
    15  
    16  	"github.com/keybase/client/go/gregor"
    17  	"github.com/keybase/client/go/libkb"
    18  	"github.com/keybase/client/go/protocol/gregor1"
    19  )
    20  
    21  const kbfsFavoritesHandlerName = "kbfsFavoritesHandler"
    22  
    23  type kbfsFavoritesHandler struct {
    24  	libkb.Contextified
    25  }
    26  
    27  var _ libkb.GregorInBandMessageHandler = (*kbfsFavoritesHandler)(nil)
    28  
    29  func newKBFSFavoritesHandler(g *libkb.GlobalContext) *kbfsFavoritesHandler {
    30  	return &kbfsFavoritesHandler{
    31  		Contextified: libkb.NewContextified(g),
    32  	}
    33  }
    34  
    35  func (r *kbfsFavoritesHandler) Create(ctx context.Context, cli gregor1.IncomingInterface,
    36  	category string, item gregor.Item) (bool, error) {
    37  	switch category {
    38  	case "kbfs.favorites":
    39  		return true, r.favoritesChanged(ctx, cli, item)
    40  	default:
    41  		if strings.HasPrefix(category, "kbfs.") {
    42  			return false, fmt.Errorf("unknown KBFS category: %q", category)
    43  		}
    44  		return false, nil
    45  	}
    46  }
    47  
    48  func (r *kbfsFavoritesHandler) Dismiss(ctx context.Context, cli gregor1.IncomingInterface, category string, item gregor.Item) (bool, error) {
    49  	return false, nil
    50  }
    51  
    52  func (r *kbfsFavoritesHandler) IsAlive() bool {
    53  	return true
    54  }
    55  
    56  func (r *kbfsFavoritesHandler) Name() string {
    57  	return kbfsFavoritesHandlerName
    58  }
    59  
    60  func (r *kbfsFavoritesHandler) favoritesChanged(ctx context.Context,
    61  	cli gregor1.IncomingInterface,
    62  	item gregor.Item) error {
    63  	r.G().Log.CDebugf(ctx, "kbfsFavoritesHandler: kbfs."+
    64  		"favorites received")
    65  
    66  	// We will locally dismiss for now so that each client only plays them once:
    67  	if err := r.G().GregorState.LocalDismissItem(ctx, item.Metadata().MsgID()); err != nil {
    68  		r.G().Log.CDebugf(ctx,
    69  			"failed to locally dismiss favoritesChanged notification: %s", err)
    70  	}
    71  
    72  	kbUID := keybase1.UID(item.Metadata().UID().String())
    73  	r.Contextified.G().NotifyRouter.HandleFavoritesChanged(kbUID)
    74  	return nil
    75  }