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

     1  // Handlers for ephemeral-related gregor messages
     2  
     3  package service
     4  
     5  import (
     6  	"encoding/json"
     7  	"fmt"
     8  	"strings"
     9  
    10  	"golang.org/x/net/context"
    11  
    12  	"github.com/keybase/client/go/ephemeral"
    13  	"github.com/keybase/client/go/gregor"
    14  	"github.com/keybase/client/go/libkb"
    15  	"github.com/keybase/client/go/protocol/gregor1"
    16  	"github.com/keybase/client/go/protocol/keybase1"
    17  )
    18  
    19  const ekHandlerName = "ephemeralHandler"
    20  
    21  type ekHandler struct {
    22  	libkb.Contextified
    23  }
    24  
    25  var _ libkb.GregorInBandMessageHandler = (*ekHandler)(nil)
    26  
    27  func newEKHandler(g *libkb.GlobalContext) *ekHandler {
    28  	return &ekHandler{
    29  		Contextified: libkb.NewContextified(g),
    30  	}
    31  }
    32  
    33  func (r *ekHandler) Create(ctx context.Context, cli gregor1.IncomingInterface, category string, item gregor.Item) (bool, error) {
    34  	switch category {
    35  	case "ephemeral.new_team_ek":
    36  		return true, r.newTeamEK(ctx, cli, item)
    37  	case "ephemeral.new_teambot_key":
    38  		return true, r.newTeambotEK(ctx, cli, item)
    39  	case "ephemeral.teambot_key_needed":
    40  		return true, r.teambotEKNeeded(ctx, cli, item)
    41  	default:
    42  		if strings.HasPrefix(category, "ephemeral.") {
    43  			return false, fmt.Errorf("unknown ekHandler category: %q", category)
    44  		}
    45  		return false, nil
    46  	}
    47  }
    48  
    49  func (r *ekHandler) Dismiss(ctx context.Context, cli gregor1.IncomingInterface, category string, item gregor.Item) (bool, error) {
    50  	return false, nil
    51  }
    52  
    53  func (r *ekHandler) IsAlive() bool {
    54  	return true
    55  }
    56  
    57  func (r *ekHandler) Name() string {
    58  	return ekHandlerName
    59  }
    60  
    61  func (r *ekHandler) newTeamEK(ctx context.Context, cli gregor1.IncomingInterface, item gregor.Item) error {
    62  	r.G().Log.CDebugf(ctx, "ekHandler: ephemeral.new_team_ek received")
    63  	var msg keybase1.NewTeamEkArg
    64  	if err := json.Unmarshal(item.Body().Bytes(), &msg); err != nil {
    65  		r.G().Log.CDebugf(ctx, "error unmarshaling ephemeral.new_team_ek item: %s", err)
    66  		return err
    67  	}
    68  	r.G().Log.CDebugf(ctx, "ephemeral.new_team_ek unmarshaled: %+v", msg)
    69  
    70  	if err := ephemeral.HandleNewTeamEK(r.MetaContext(ctx), msg.Id, msg.Generation); err != nil {
    71  		return err
    72  	}
    73  
    74  	r.G().Log.CDebugf(ctx, "dismissing ephemeral.new_team_ek item since action succeeded")
    75  	return r.G().GregorState.DismissItem(ctx, cli, item.Metadata().MsgID())
    76  }
    77  
    78  func (r *ekHandler) newTeambotEK(ctx context.Context, cli gregor1.IncomingInterface, item gregor.Item) error {
    79  	r.G().Log.CDebugf(ctx, "ekHandler: ephemeral.new_teambot_key received")
    80  	var msg keybase1.NewTeambotEkArg
    81  	if err := json.Unmarshal(item.Body().Bytes(), &msg); err != nil {
    82  		r.G().Log.CDebugf(ctx, "error unmarshaling ephemeral.new_teambot_key item: %s", err)
    83  		return err
    84  	}
    85  	r.G().Log.CDebugf(ctx, "ephemeral.new_teambot_key unmarshaled: %+v", msg)
    86  
    87  	if err := ephemeral.HandleNewTeambotEK(r.MetaContext(ctx), msg.Id, msg.Generation); err != nil {
    88  		return err
    89  	}
    90  
    91  	r.G().Log.CDebugf(ctx, "dismissing ephemeral.new_teambot_key since action succeeded")
    92  	return r.G().GregorState.DismissItem(ctx, cli, item.Metadata().MsgID())
    93  }
    94  
    95  func (r *ekHandler) teambotEKNeeded(ctx context.Context, cli gregor1.IncomingInterface, item gregor.Item) error {
    96  	r.G().Log.CDebugf(ctx, "ekHandler: ephemeral.teambot_key_needed received")
    97  	var msg keybase1.TeambotEkNeededArg
    98  	if err := json.Unmarshal(item.Body().Bytes(), &msg); err != nil {
    99  		r.G().Log.CDebugf(ctx, "error unmarshaling ephemeral.teambot_key_needed item: %s", err)
   100  		return err
   101  	}
   102  	r.G().Log.CDebugf(ctx, "ephemeral.teambot_key_needed unmarshaled: %+v", msg)
   103  
   104  	if err := ephemeral.HandleTeambotEKNeeded(r.MetaContext(ctx), msg.Id, msg.Uid, msg.Generation, msg.ForceCreateGeneration); err != nil {
   105  		return err
   106  	}
   107  
   108  	r.G().Log.CDebugf(ctx, "dismissing ephemeral.teambot_key_needed item since action succeeded")
   109  	return r.G().GregorState.DismissItem(ctx, cli, item.Metadata().MsgID())
   110  }