github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/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/chat/utils"
     9  	"github.com/keybase/client/go/protocol/chat1"
    10  )
    11  
    12  func getGlobalAppNotificationSettings(ctx context.Context, g *globals.Context, ri func() chat1.RemoteInterface) (
    13  	res chat1.GlobalAppNotificationSettings, err error) {
    14  	settings, err := ri().GetGlobalAppNotificationSettings(ctx)
    15  	if err != nil {
    16  		return res, err
    17  	}
    18  	plaintextDesktopDisabled, err := utils.GetGregorBool(ctx, g, utils.DisablePlaintextDesktopGregorKey, false)
    19  	if err != nil {
    20  		return res, err
    21  	}
    22  	settings.Settings[chat1.GlobalAppNotificationSetting_PLAINTEXTDESKTOP] = !plaintextDesktopDisabled
    23  
    24  	convertHeic, err := utils.GetGregorBool(ctx, g, utils.ConvertHEICGregorKey, true)
    25  	if err != nil {
    26  		return res, err
    27  	}
    28  	settings.Settings[chat1.GlobalAppNotificationSetting_CONVERTHEIC] = convertHeic
    29  	return settings, nil
    30  }
    31  
    32  func setGlobalAppNotificationSettings(ctx context.Context, g *globals.Context, ri func() chat1.RemoteInterface,
    33  	strSettings map[string]bool) error {
    34  
    35  	var settings chat1.GlobalAppNotificationSettings
    36  	settings.Settings = make(map[chat1.GlobalAppNotificationSetting]bool)
    37  	for k, v := range strSettings {
    38  		key, err := strconv.Atoi(k)
    39  		if err != nil {
    40  			g.Log.CDebugf(ctx, "setGlobalAppNotificationSettings: failed to convert key: %s", err.Error())
    41  			continue
    42  		}
    43  		gkey := chat1.GlobalAppNotificationSetting(key)
    44  		g.Log.CDebugf(ctx, "setGlobalAppNotificationSettings: setting typ: %s enabled: %v",
    45  			chat1.GlobalAppNotificationSettingRevMap[gkey], v)
    46  		switch gkey {
    47  		case chat1.GlobalAppNotificationSetting_PLAINTEXTDESKTOP:
    48  			err = utils.SetGregorBool(ctx, g, utils.DisablePlaintextDesktopGregorKey, !v)
    49  			if err != nil {
    50  				return err
    51  			}
    52  		case chat1.GlobalAppNotificationSetting_CONVERTHEIC:
    53  			err = utils.SetGregorBool(ctx, g, utils.ConvertHEICGregorKey, v)
    54  			if err != nil {
    55  				return err
    56  			}
    57  		default:
    58  			settings.Settings[gkey] = v
    59  		}
    60  	}
    61  	if len(settings.Settings) == 0 {
    62  		return nil
    63  	}
    64  	return ri().SetGlobalAppNotificationSettings(ctx, settings)
    65  }