-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
cmd_chat_notification_settings.go
114 lines (101 loc) · 3.1 KB
/
cmd_chat_notification_settings.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package client
import (
"strconv"
"github.com/keybase/cli"
"github.com/keybase/client/go/libcmdline"
"github.com/keybase/client/go/libkb"
"github.com/keybase/client/go/protocol/chat1"
"golang.org/x/net/context"
)
type CmdChatSetNotificationSettings struct {
libkb.Contextified
settings chat1.GlobalAppNotificationSettings
}
func NewCmdChatSetNotificationSettingsRunner(g *libkb.GlobalContext) *CmdChatSetNotificationSettings {
return &CmdChatSetNotificationSettings{
Contextified: libkb.NewContextified(g),
}
}
func newCmdChatSetNotificationSettings(cl *libcmdline.CommandLine, g *libkb.GlobalContext) cli.Command {
flags := []cli.Flag{}
for _, setting := range chat1.GlobalAppNotificationSettingsSorted() {
flags = append(flags, cli.BoolFlag{
Name: setting.FlagName(),
Usage: setting.Usage(),
})
}
return cli.Command{
Name: "notification-settings",
Usage: "Manage personal notification settings",
Examples: `
View the current settings:
keybase chat notification-settings
Enable plaintext notifications:
keybase chat notification-settings --plaintext-mobile
Disable plaintext notifications:
keybase chat notification-settings --plaintext-mobile=0
`,
ArgumentHelp: "[options]",
Action: func(c *cli.Context) {
cl.ChooseCommand(NewCmdChatSetNotificationSettingsRunner(g), "notification-settings", c)
},
Flags: flags,
}
}
func (c *CmdChatSetNotificationSettings) Run() error {
if len(c.settings.Settings) > 0 {
if err := c.setGlobalAppNotificationSettings(context.TODO()); err != nil {
return err
}
}
return c.getGlobalAppNotificationSettings(context.TODO())
}
func (c *CmdChatSetNotificationSettings) ParseArgv(ctx *cli.Context) (err error) {
if len(ctx.Args()) > 0 {
return UnexpectedArgsError("notification-settings")
}
c.settings.Settings = make(map[chat1.GlobalAppNotificationSetting]bool)
for _, setting := range chat1.GlobalAppNotificationSettingsSorted() {
flagName := setting.FlagName()
if ctx.IsSet(flagName) {
c.settings.Settings[setting] = ctx.Bool(flagName)
}
}
return nil
}
func (c *CmdChatSetNotificationSettings) GetUsage() libkb.Usage {
return libkb.Usage{
Config: true,
API: true,
}
}
func (c *CmdChatSetNotificationSettings) setGlobalAppNotificationSettings(ctx context.Context) error {
lcli, err := GetChatLocalClient(c.G())
if err != nil {
return err
}
strSettings := map[string]bool{}
for setting, enabled := range c.settings.Settings {
strSettings[strconv.FormatInt(int64(setting), 10)] = enabled
}
return lcli.SetGlobalAppNotificationSettingsLocal(ctx, strSettings)
}
func (c *CmdChatSetNotificationSettings) getGlobalAppNotificationSettings(ctx context.Context) error {
lcli, err := GetChatLocalClient(c.G())
if err != nil {
return err
}
settings, err := lcli.GetGlobalAppNotificationSettingsLocal(ctx)
if err != nil {
return err
}
dui := c.G().UI.GetDumbOutputUI()
for _, setting := range chat1.GlobalAppNotificationSettingsSorted() {
enabledStr := "disabled"
if settings.Settings[setting] {
enabledStr = "enabled"
}
dui.Printf("%v (%v)\n\t%v\n", setting.FlagName(), enabledStr, setting.Usage())
}
return nil
}