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

     1  package chat
     2  
     3  import (
     4  	"context"
     5  	"strconv"
     6  
     7  	"github.com/keybase/client/go/chat/globals"
     8  	"github.com/keybase/client/go/protocol/chat1"
     9  	"github.com/keybase/client/go/protocol/gregor1"
    10  )
    11  
    12  const disablePlaintextDesktopKey = "disableplaintextdesktop"
    13  
    14  func setPlaintextDesktopDisabled(ctx context.Context, g *globals.Context, disabled bool) error {
    15  	_, err := g.GregorState.UpdateCategory(ctx, disablePlaintextDesktopKey,
    16  		[]byte(strconv.FormatBool(disabled)), gregor1.TimeOrOffset{})
    17  	return err
    18  }
    19  
    20  func getPlaintextDesktopDisabled(ctx context.Context, g *globals.Context) (bool, error) {
    21  	st, err := g.GregorState.State(ctx)
    22  	if err != nil {
    23  		return false, err
    24  	}
    25  	cat, err := gregor1.ObjFactory{}.MakeCategory(disablePlaintextDesktopKey)
    26  	if err != nil {
    27  		return false, err
    28  	}
    29  	items, err := st.ItemsWithCategoryPrefix(cat)
    30  	if err != nil {
    31  		return false, err
    32  	}
    33  	if len(items) > 0 {
    34  		it := items[0]
    35  		body := string(it.Body().Bytes())
    36  		return strconv.ParseBool(body)
    37  	}
    38  	return false, nil
    39  }
    40  
    41  func getGlobalAppNotificationSettings(ctx context.Context, g *globals.Context, ri func() chat1.RemoteInterface) (
    42  	res chat1.GlobalAppNotificationSettings, err error) {
    43  	settings, err := ri().GetGlobalAppNotificationSettings(ctx)
    44  	if err != nil {
    45  		return res, err
    46  	}
    47  	plaintextDesktopDisabled, err := getPlaintextDesktopDisabled(ctx, g)
    48  	if err != nil {
    49  		return res, err
    50  	}
    51  
    52  	settings.Settings[chat1.GlobalAppNotificationSetting_PLAINTEXTDESKTOP] = !plaintextDesktopDisabled
    53  	return settings, nil
    54  }
    55  
    56  func setGlobalAppNotificationSettings(ctx context.Context, g *globals.Context, ri func() chat1.RemoteInterface,
    57  	strSettings map[string]bool) error {
    58  
    59  	var settings chat1.GlobalAppNotificationSettings
    60  	settings.Settings = make(map[chat1.GlobalAppNotificationSetting]bool)
    61  	for k, v := range strSettings {
    62  		key, err := strconv.Atoi(k)
    63  		if err != nil {
    64  			g.Log.CDebugf(ctx, "setGlobalAppNotificationSettings: failed to convert key: %s", err.Error())
    65  			continue
    66  		}
    67  		gkey := chat1.GlobalAppNotificationSetting(key)
    68  		g.Log.CDebugf(ctx, "setGlobalAppNotificationSettings: setting typ: %s enabled: %v",
    69  			chat1.GlobalAppNotificationSettingRevMap[gkey], v)
    70  		switch gkey {
    71  		case chat1.GlobalAppNotificationSetting_PLAINTEXTDESKTOP:
    72  			if err := setPlaintextDesktopDisabled(ctx, g, !v); err != nil {
    73  				return err
    74  			}
    75  		default:
    76  			settings.Settings[gkey] = v
    77  		}
    78  	}
    79  	if len(settings.Settings) == 0 {
    80  		return nil
    81  	}
    82  	return ri().SetGlobalAppNotificationSettings(ctx, settings)
    83  }