github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/client/cmd_chat_addtochannel.go (about) 1 // Copyright 2019 Keybase, Inc. All rights reserved. Use of 2 // this source code is governed by the included BSD license. 3 4 package client 5 6 import ( 7 "errors" 8 "strings" 9 10 "github.com/keybase/cli" 11 "github.com/keybase/client/go/chat/utils" 12 "github.com/keybase/client/go/libcmdline" 13 "github.com/keybase/client/go/libkb" 14 "github.com/keybase/client/go/protocol/chat1" 15 "github.com/keybase/client/go/protocol/keybase1" 16 "golang.org/x/net/context" 17 ) 18 19 type CmdChatAddToChannel struct { 20 libkb.Contextified 21 teamName string 22 channelName string 23 users []string 24 topicType chat1.TopicType 25 } 26 27 func NewCmdChatAddToChannelRunner(g *libkb.GlobalContext) *CmdChatAddToChannel { 28 return &CmdChatAddToChannel{Contextified: libkb.NewContextified(g)} 29 } 30 31 func newCmdChatAddToChannel(cl *libcmdline.CommandLine, g *libkb.GlobalContext) cli.Command { 32 return cli.Command{ 33 Name: "add-to-channel", 34 Usage: "Add one or more users to a channel", 35 ArgumentHelp: "<team> <channel> <usernames>", 36 Action: func(c *cli.Context) { 37 cl.ChooseCommand(NewCmdChatAddToChannelRunner(g), "add-to-channel", c) 38 cl.SetLogForward(libcmdline.LogForwardNone) 39 }, 40 Flags: mustGetChatFlags("topic-type"), 41 Description: chatAddToChannelDoc, 42 } 43 } 44 45 func (c *CmdChatAddToChannel) Run() error { 46 ctx := context.TODO() 47 resolver, err := newChatConversationResolver(c.G()) 48 if err != nil { 49 return err 50 } 51 req := chatConversationResolvingRequest{ 52 TlfName: c.teamName, 53 TopicName: c.channelName, 54 TopicType: c.topicType, 55 MembersType: chat1.ConversationMembersType_TEAM, 56 Visibility: keybase1.TLFVisibility_PRIVATE, 57 } 58 conversation, _, err := resolver.Resolve(ctx, req, chatConversationResolvingBehavior{ 59 CreateIfNotExists: false, 60 MustNotExist: false, 61 Interactive: false, 62 IdentifyBehavior: keybase1.TLFIdentifyBehavior_CHAT_CLI, 63 }) 64 if err != nil { 65 return err 66 } 67 err = resolver.ChatClient.BulkAddToConv(ctx, chat1.BulkAddToConvArg{ 68 Usernames: c.users, 69 ConvID: conversation.GetConvID(), 70 }) 71 if err == nil { 72 dui := c.G().UI.GetDumbOutputUI() 73 dui.Printf("Success!\n") 74 return nil 75 } 76 return err 77 } 78 79 func (c *CmdChatAddToChannel) ParseArgv(ctx *cli.Context) (err error) { 80 if len(ctx.Args()) != 3 { 81 return errors.New("add-to-channel takes three arguments") 82 } 83 84 c.teamName = ctx.Args().Get(0) 85 c.channelName = utils.SanitizeTopicName(ctx.Args().Get(1)) 86 87 userString := ctx.Args().Get(2) 88 if len(userString) == 0 { 89 return errors.New("add-to-channel needs at least one user") 90 } 91 users := strings.Split(userString, ",") 92 for _, user := range users { 93 if len(user) == 0 { 94 return errors.New("cannot specify an empty user") 95 } 96 c.users = append(c.users, user) 97 } 98 99 c.topicType, err = parseConversationTopicType(ctx) 100 if err != nil { 101 return err 102 } 103 104 return nil 105 } 106 107 func (c *CmdChatAddToChannel) GetUsage() libkb.Usage { 108 return libkb.Usage{ 109 Config: true, 110 API: true, 111 KbKeyring: true, 112 } 113 } 114 115 const chatAddToChannelDoc = `"keybase chat add-to-channel" allows you to add one or more users to a channel 116 117 EXAMPLES: 118 119 Add a single keybase user: 120 121 keybase chat add-to-channel acme announcements alice 122 123 Add multiple keybase users: 124 125 keybase chat add-to-channel acme announcements alice,bob,charlie 126 `