Skip to content

Commit 5602105

Browse files
author
Peter Steinberger
committed
fix(chat): block consumer accounts
1 parent 89ea31c commit 5602105

6 files changed

Lines changed: 63 additions & 0 deletions

File tree

internal/cmd/chat_dm.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ func (c *ChatDMSendCmd) Run(ctx context.Context, flags *RootFlags) error {
2929
if err != nil {
3030
return err
3131
}
32+
if err := requireWorkspaceAccount(account); err != nil {
33+
return err
34+
}
3235

3336
email := strings.TrimSpace(c.Email)
3437
if email == "" {
@@ -100,6 +103,9 @@ func (c *ChatDMSpaceCmd) Run(ctx context.Context, flags *RootFlags) error {
100103
if err != nil {
101104
return err
102105
}
106+
if err := requireWorkspaceAccount(account); err != nil {
107+
return err
108+
}
103109

104110
email := strings.TrimSpace(c.Email)
105111
if email == "" {

internal/cmd/chat_helpers.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,28 @@ func normalizeUser(resource string) string {
3333
return "users/" + user
3434
}
3535

36+
func requireWorkspaceAccount(account string) error {
37+
if isConsumerAccount(account) {
38+
return usage("chat requires a Google Workspace account (non-gmail.com)")
39+
}
40+
return nil
41+
}
42+
43+
func isConsumerAccount(account string) bool {
44+
account = strings.TrimSpace(strings.ToLower(account))
45+
at := strings.LastIndex(account, "@")
46+
if at == -1 {
47+
return false
48+
}
49+
domain := account[at+1:]
50+
switch domain {
51+
case "gmail.com", "googlemail.com":
52+
return true
53+
default:
54+
return false
55+
}
56+
}
57+
3658
func normalizeThread(space, resource string) (string, error) {
3759
thread := strings.TrimSpace(resource)
3860
if thread == "" {

internal/cmd/chat_messages.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ func (c *ChatMessagesListCmd) Run(ctx context.Context, flags *RootFlags) error {
3232
if err != nil {
3333
return err
3434
}
35+
if err := requireWorkspaceAccount(account); err != nil {
36+
return err
37+
}
3538

3639
space, err := normalizeSpace(c.Space)
3740
if err != nil {
@@ -140,6 +143,9 @@ func (c *ChatMessagesSendCmd) Run(ctx context.Context, flags *RootFlags) error {
140143
if err != nil {
141144
return err
142145
}
146+
if err := requireWorkspaceAccount(account); err != nil {
147+
return err
148+
}
143149

144150
space, err := normalizeSpace(c.Space)
145151
if err != nil {

internal/cmd/chat_spaces.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ func (c *ChatSpacesListCmd) Run(ctx context.Context, flags *RootFlags) error {
2929
if err != nil {
3030
return err
3131
}
32+
if err := requireWorkspaceAccount(account); err != nil {
33+
return err
34+
}
3235

3336
svc, err := newChatService(ctx, account)
3437
if err != nil {
@@ -103,6 +106,9 @@ func (c *ChatSpacesFindCmd) Run(ctx context.Context, flags *RootFlags) error {
103106
if err != nil {
104107
return err
105108
}
109+
if err := requireWorkspaceAccount(account); err != nil {
110+
return err
111+
}
106112

107113
displayName := strings.TrimSpace(c.DisplayName)
108114
if displayName == "" {
@@ -193,6 +199,9 @@ func (c *ChatSpacesCreateCmd) Run(ctx context.Context, flags *RootFlags) error {
193199
if err != nil {
194200
return err
195201
}
202+
if err := requireWorkspaceAccount(account); err != nil {
203+
return err
204+
}
196205

197206
displayName := strings.TrimSpace(c.DisplayName)
198207
if displayName == "" {

internal/cmd/chat_threads.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ func (c *ChatThreadsListCmd) Run(ctx context.Context, flags *RootFlags) error {
2727
if err != nil {
2828
return err
2929
}
30+
if err := requireWorkspaceAccount(account); err != nil {
31+
return err
32+
}
3033

3134
space, err := normalizeSpace(c.Space)
3235
if err != nil {

internal/cmd/execute_chat_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,23 @@ func TestExecute_ChatSpacesList_Text(t *testing.T) {
5858
}
5959
}
6060

61+
func TestExecute_ChatSpacesList_ConsumerBlocked(t *testing.T) {
62+
origNew := newChatService
63+
t.Cleanup(func() { newChatService = origNew })
64+
newChatService = func(context.Context, string) (*chat.Service, error) {
65+
t.Fatalf("unexpected chat service call")
66+
return nil, nil
67+
}
68+
69+
err := Execute([]string{"--account", "user@gmail.com", "chat", "spaces", "list"})
70+
if err == nil {
71+
t.Fatalf("expected error")
72+
}
73+
if !strings.Contains(err.Error(), "Workspace") {
74+
t.Fatalf("unexpected error: %v", err)
75+
}
76+
}
77+
6178
func TestExecute_ChatSpacesFind_JSON(t *testing.T) {
6279
origNew := newChatService
6380
t.Cleanup(func() { newChatService = origNew })

0 commit comments

Comments
 (0)