forked from keybase/client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd_show_notifications.go
110 lines (90 loc) · 2.82 KB
/
cmd_show_notifications.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
// Copyright 2015 Keybase, Inc. All rights reserved. Use of
// this source code is governed by the included BSD license.
package client
import (
"time"
"golang.org/x/net/context"
"github.com/keybase/cli"
"github.com/keybase/client/go/libcmdline"
"github.com/keybase/client/go/libkb"
keybase1 "github.com/keybase/client/go/protocol"
rpc "github.com/keybase/go-framed-msgpack-rpc"
)
type CmdShowNotifications struct {
libkb.Contextified
}
func (c *CmdShowNotifications) ParseArgv(ctx *cli.Context) error {
return nil
}
func (c *CmdShowNotifications) Run() error {
_, err := c.G().UI.GetTerminalUI().Printf("Showing notifications:\n")
if err != nil {
return err
}
display := newNotificationDisplay(c.G())
// NB: Make sure to edit both of these at the same time.
protocols := []rpc.Protocol{
keybase1.NotifySessionProtocol(display),
keybase1.NotifyUsersProtocol(display),
keybase1.NotifyFSProtocol(display),
keybase1.NotifyTrackingProtocol(display),
}
channels := keybase1.NotificationChannels{
Session: true,
Users: true,
Kbfs: true,
Tracking: true,
}
if err := RegisterProtocols(protocols); err != nil {
return err
}
cli, err := GetNotifyCtlClient(c.G())
if err != nil {
return err
}
if err := cli.SetNotifications(context.TODO(), channels); err != nil {
return err
}
_, err = c.G().UI.GetTerminalUI().Printf("waiting for notifications...\n")
if err != nil {
return err
}
for {
time.Sleep(time.Second)
}
}
func NewCmdShowNotifications(cl *libcmdline.CommandLine, g *libkb.GlobalContext) cli.Command {
return cli.Command{
Name: "show-notifications",
Usage: "Display all notifications sent by daemon in real-time",
Action: func(c *cli.Context) {
cl.ChooseCommand(&CmdShowNotifications{Contextified: libkb.NewContextified(g)}, "show-notifications", c)
},
Flags: []cli.Flag{},
}
}
func (c *CmdShowNotifications) GetUsage() libkb.Usage {
return libkb.Usage{}
}
type notificationDisplay struct {
libkb.Contextified
}
func newNotificationDisplay(g *libkb.GlobalContext) *notificationDisplay {
return ¬ificationDisplay{Contextified: libkb.NewContextified(g)}
}
func (d *notificationDisplay) printf(fmt string, args ...interface{}) error {
_, err := d.G().UI.GetTerminalUI().Printf(fmt, args...)
return err
}
func (d *notificationDisplay) LoggedOut(_ context.Context) error {
return d.printf("Logged out\n")
}
func (d *notificationDisplay) UserChanged(_ context.Context, uid keybase1.UID) error {
return d.printf("User %s changed\n", uid)
}
func (d *notificationDisplay) FSActivity(_ context.Context, notification keybase1.FSNotification) error {
return d.printf("KBFS notification: %+v\n", notification)
}
func (d *notificationDisplay) TrackingChanged(_ context.Context, arg keybase1.TrackingChangedArg) error {
return d.printf("Tracking changed for %s (%s)\n", arg.Username, arg.Uid)
}