/
cmd_chat_delete_history.go
200 lines (179 loc) · 5.4 KB
/
cmd_chat_delete_history.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// Copyright 2017 Keybase, Inc. All rights reserved. Use of
// this source code is governed by the included BSD license.
package client
import (
"fmt"
"os"
"strconv"
"time"
"github.com/keybase/cli"
"github.com/keybase/client/go/libcmdline"
"github.com/keybase/client/go/libkb"
"github.com/keybase/client/go/protocol/chat1"
gregor1 "github.com/keybase/client/go/protocol/gregor1"
"github.com/keybase/client/go/protocol/keybase1"
isatty "github.com/mattn/go-isatty"
"golang.org/x/net/context"
)
type CmdChatDeleteHistory struct {
libkb.Contextified
resolvingRequest chatConversationResolvingRequest
age gregor1.DurationSec
ageDesc string
hasTTY bool
}
func NewCmdChatDeleteHistoryRunner(g *libkb.GlobalContext) *CmdChatDeleteHistory {
return &CmdChatDeleteHistory{
Contextified: libkb.NewContextified(g),
}
}
func newCmdChatDeleteHistory(cl *libcmdline.CommandLine, g *libkb.GlobalContext) cli.Command {
return cli.Command{
Name: "delete-history",
Usage: "Delete chat history in a conversation",
ArgumentHelp: "[conversation] --age=<interval>",
Action: func(c *cli.Context) {
cl.ChooseCommand(NewCmdChatDeleteHistoryRunner(g), "delete-history", c)
cl.SetNoStandalone()
},
Flags: append(getConversationResolverFlags(), []cli.Flag{
cli.StringFlag{
Name: "age",
Usage: `Only delete messages older than e.g. 2h, 3d, 1w `,
},
}...),
}
}
func (c *CmdChatDeleteHistory) Run() (err error) {
if c.resolvingRequest.TlfName != "" {
err = annotateResolvingRequest(c.G(), &c.resolvingRequest)
if err != nil {
return err
}
}
// TLFVisibility_ANY doesn't make any sense for send, so switch that to PRIVATE:
if c.resolvingRequest.Visibility == keybase1.TLFVisibility_ANY {
c.resolvingRequest.Visibility = keybase1.TLFVisibility_PRIVATE
}
if c.G().Standalone {
switch c.resolvingRequest.MembersType {
case chat1.ConversationMembersType_TEAM, chat1.ConversationMembersType_IMPTEAMNATIVE,
chat1.ConversationMembersType_IMPTEAMUPGRADE:
c.G().StartStandaloneChat()
default:
err = CantRunInStandaloneError{}
return err
}
}
return c.chatSendDeleteHistory(context.TODO())
}
func (c *CmdChatDeleteHistory) ParseArgv(ctx *cli.Context) (err error) {
c.hasTTY = isatty.IsTerminal(os.Stdin.Fd())
var tlfName string
// Get the TLF name from the first position arg
if len(ctx.Args()) >= 1 {
tlfName = ctx.Args().Get(0)
}
if c.resolvingRequest, err = parseConversationResolvingRequest(ctx, tlfName); err != nil {
return err
}
if timeStr := ctx.String("age"); len(timeStr) > 0 {
c.age, c.ageDesc, err = c.parseAge(timeStr)
if err != nil {
return err
}
}
return nil
}
func (c *CmdChatDeleteHistory) GetUsage() libkb.Usage {
return libkb.Usage{
Config: true,
API: true,
}
}
// Returns a duration and English string like "2 days".
func (c *CmdChatDeleteHistory) parseAge(s string) (gregor1.DurationSec, string, error) {
generalErr := fmt.Errorf("duration must be an integer and suffix [s,h,d,w,m] like: 10d")
if len(s) < 2 {
return 0, "", generalErr
}
factor := time.Second
unitName := ""
switch s[len(s)-1] {
case 's':
factor = time.Second
unitName = "second"
case 'h':
factor = time.Hour
unitName = "hour"
case 'd':
factor = 24 * time.Hour
unitName = "day"
case 'w':
factor = 7 * 24 * time.Hour
unitName = "week"
case 'm':
factor = 30 * 24 * time.Hour
unitName = "month"
default:
return 0, "", generalErr
}
base, err := strconv.Atoi(s[:len(s)-1])
if err != nil {
return 0, "", generalErr
}
if base < 0 {
return 0, "", fmt.Errorf("age cannot be negative")
}
d := time.Duration(base) * factor
if unitName == "month" {
base *= 30
unitName = "day"
}
plural := "s"
if base == 1 {
plural = ""
}
desc := fmt.Sprintf("%v %v%v", base, unitName, plural)
return gregor1.DurationSec(d.Seconds()), desc, nil
}
// Like chatSend but uses PostDeleteHistoryByAge.
func (c *CmdChatDeleteHistory) chatSendDeleteHistory(ctx context.Context) error {
resolver, err := newChatConversationResolver(c.G())
if err != nil {
return err
}
conversation, _, err := resolver.Resolve(ctx, c.resolvingRequest, chatConversationResolvingBehavior{
CreateIfNotExists: false,
MustNotExist: false,
Interactive: c.hasTTY,
IdentifyBehavior: keybase1.TLFIdentifyBehavior_CHAT_CLI,
})
if err != nil {
return err
}
conversationInfo := conversation.Info
arg := chat1.PostDeleteHistoryByAgeArg{
ConversationID: conversationInfo.Id,
TlfName: conversationInfo.TlfName,
TlfPublic: (conversationInfo.Visibility == keybase1.TLFVisibility_PUBLIC),
IdentifyBehavior: keybase1.TLFIdentifyBehavior_CHAT_CLI,
Age: c.age,
}
chatFullName := conversationInfo.TlfName
if conversationInfo.TopicName != "" {
chatFullName = fmt.Sprintf("%s#%s", conversationInfo.TlfName, conversationInfo.TopicName)
}
// Ask for confirmation on this destructive operation.
promptText := fmt.Sprintf("Permanently delete ALL chat history of [%s]?\nHit Enter to confirm, or Ctrl-C to cancel.", chatFullName)
if c.age != gregor1.DurationSec(0) {
promptText = fmt.Sprintf("Permanently delete all chat messages in [%s] older than %v?\nHit Enter to confirm, or Ctrl-C to cancel.",
chatFullName, c.ageDesc)
}
_, err = c.G().UI.GetTerminalUI().Prompt(PromptDescriptorChatDeleteHistory, promptText)
if err != nil {
return err
}
_, err = resolver.ChatClient.PostDeleteHistoryByAge(ctx, arg)
return err
}