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

     1  package invitefriends
     2  
     3  import (
     4  	"sync"
     5  
     6  	"github.com/keybase/client/go/libkb"
     7  	"github.com/keybase/client/go/protocol/keybase1"
     8  )
     9  
    10  var (
    11  	countsCache   *keybase1.InviteCounts
    12  	countsCacheMu sync.Mutex // mutex isn't rw due to low call frequency
    13  )
    14  
    15  func GetCounts(mctx libkb.MetaContext) (counts keybase1.InviteCounts, err error) {
    16  	type apiRes struct {
    17  		NumInvites       int     `json:"numInvites"`
    18  		PercentageChange float64 `json:"percentageChange"`
    19  		ShowNumInvites   bool    `json:"showNumInvites"`
    20  		ShowFire         bool    `json:"showFire"`
    21  		TooltipMarkdown  string  `json:"tooltipMarkdown"`
    22  		libkb.AppStatusEmbed
    23  	}
    24  	apiArg := libkb.APIArg{
    25  		Endpoint:    "invite_friends/num_invites",
    26  		SessionType: libkb.APISessionTypeNONE,
    27  	}
    28  	var res apiRes
    29  	err = mctx.G().API.GetDecode(mctx, apiArg, &res)
    30  	if err != nil {
    31  		return counts, err
    32  	}
    33  	newCounts := keybase1.InviteCounts{
    34  		InviteCount:      res.NumInvites,
    35  		PercentageChange: res.PercentageChange,
    36  		ShowNumInvites:   res.ShowNumInvites,
    37  		ShowFire:         res.ShowFire,
    38  		TooltipMarkdown:  res.TooltipMarkdown,
    39  	}
    40  	countsCacheMu.Lock()
    41  	countsCache = &newCounts
    42  	countsCacheMu.Unlock()
    43  	return newCounts, nil
    44  }
    45  
    46  func RequestNotification(mctx libkb.MetaContext) error {
    47  	// noop if there's no home ui present
    48  	if ui, err := mctx.G().UIRouter.GetHomeUI(); ui == nil || err != nil {
    49  		return nil
    50  	}
    51  
    52  	// we only need to grab the pointer during the lock
    53  	countsCacheMu.Lock()
    54  	counts := countsCache
    55  	countsCacheMu.Unlock()
    56  
    57  	if counts == nil {
    58  		freshCounts, err := GetCounts(mctx)
    59  		if err != nil {
    60  			return err
    61  		}
    62  		counts = &freshCounts
    63  	}
    64  	mctx.G().NotifyRouter.HandleUpdateInviteCounts(mctx.Ctx(), *counts)
    65  	return nil
    66  }