Files
larksuite-cli/shortcuts/mail/mail_share_to_chat.go
chanthuang 2e7a11a8e8 feat(mail): support sharing emails to IM chats (#637)
* feat(mail): add +share-to-chat shortcut to share emails as IM cards

Two-step API (create share token → send card) wrapped in a single
shortcut. Supports message-id/thread-id, five receive-id-type variants
(chat_id, open_id, user_id, union_id, email), and dry-run mode.

Change-Id: Ic7b8c01c0d25fef262f35be92555f1fd019bd679
Co-Authored-By: AI

* fix(mail): regenerate SKILL.md from skill-template instead of manual edit

Add missing safety rule 8 (draft link rule) to skill-template/domains/mail.md
so it survives regeneration. SKILL.md is now produced by `make gen-skills`
in the registry repo rather than hand-edited.

Change-Id: I9cf3605deae8b6de2042e40819fedc304967e78e
Co-Authored-By: AI

* fix(mail): add docstrings and use real validation path in tests

- Add Go doc comments to exported symbols for docstring coverage
- Rewrite tests to exercise MailShareToChat.Validate via RuntimeContext
  instead of duplicating validation logic
- Replace hand-rolled containsStr with strings.Contains
- Add httpmock stubs for execute and error path tests

Change-Id: Ic781494f61e9e844224185844bce7b0c48e8e200
Co-Authored-By: AI

* test(mail): add dry-run E2E test for +share-to-chat

Validate request shape (method, URL, mailbox path) under --dry-run
with fake credentials. Covers message-id, thread-id, and custom
mailbox variants.

Change-Id: Iae87bf141cbe4f312d3e9b1fca4ba175052c5c35
Co-Authored-By: AI

* fix(mail): include request body and params in dry-run output

DryRun now mirrors Execute: the share-token POST shows message_id or
thread_id, and the send POST shows receive_id_type and receive_id.
E2E test updated to assert these fields. Also fix strconv.Itoa usage.

Change-Id: I00f8770fd5a12b7354986c5e5077f97cfe5d6653

* style(mail): gofmt dry-run test file

Change-Id: I47dc6a9a47252dcfb7853737f88dfdaef65a0ae7

* test(mail): assert exact API call count in dry-run test

Change-Id: I9f4a1a183b55d03f5248eb4adddfddb08037ca95
2026-04-24 21:11:48 +08:00

119 lines
4.0 KiB
Go

// Copyright (c) 2026 Lark Technologies Pte. Ltd.
// SPDX-License-Identifier: MIT
package mail
import (
"context"
"fmt"
"github.com/larksuite/cli/internal/output"
"github.com/larksuite/cli/shortcuts/common"
)
// validReceiveIDTypes enumerates accepted --receive-id-type values.
var validReceiveIDTypes = map[string]bool{
"chat_id": true,
"open_id": true,
"user_id": true,
"union_id": true,
"email": true,
}
// MailShareToChat shares an email or thread as a card to a Lark IM chat.
var MailShareToChat = common.Shortcut{
Service: "mail",
Command: "+share-to-chat",
Description: "Share an email or thread as a card to a Lark IM chat.",
Risk: "write",
Scopes: []string{
"mail:user_mailbox.message:readonly",
"im:message",
"im:message.send_as_user",
},
AuthTypes: []string{"user"},
HasFormat: true,
Flags: []common.Flag{
{Name: "message-id", Desc: "Message ID to share (mutually exclusive with --thread-id)"},
{Name: "thread-id", Desc: "Thread ID to share (mutually exclusive with --message-id)"},
{Name: "receive-id", Desc: "Receiver ID. Type determined by --receive-id-type.", Required: true},
{Name: "receive-id-type", Default: "chat_id", Desc: "Receiver ID type: chat_id (default), open_id, user_id, union_id, email"},
{Name: "mailbox", Default: "me", Desc: "Mailbox email address (default: me)"},
},
DryRun: func(ctx context.Context, runtime *common.RuntimeContext) *common.DryRunAPI {
mailboxID := resolveMailboxID(runtime)
msgID := runtime.Str("message-id")
threadID := runtime.Str("thread-id")
receiveID := runtime.Str("receive-id")
receiveIDType := runtime.Str("receive-id-type")
var createBody map[string]interface{}
if threadID != "" {
createBody = map[string]interface{}{"thread_id": threadID}
} else {
createBody = map[string]interface{}{"message_id": msgID}
}
return common.NewDryRunAPI().
Desc("Share email card: create share token → send card to IM chat").
POST(mailboxPath(mailboxID, "messages", "share_token")).
Body(createBody).
POST(mailboxPath(mailboxID, "share_tokens", "<card_id>", "send")).
Params(map[string]interface{}{"receive_id_type": receiveIDType}).
Body(map[string]interface{}{"receive_id": receiveID})
},
Validate: func(ctx context.Context, runtime *common.RuntimeContext) error {
msgID := runtime.Str("message-id")
threadID := runtime.Str("thread-id")
if msgID == "" && threadID == "" {
return output.ErrValidation("either --message-id or --thread-id is required")
}
if msgID != "" && threadID != "" {
return output.ErrValidation("--message-id and --thread-id are mutually exclusive")
}
idType := runtime.Str("receive-id-type")
if !validReceiveIDTypes[idType] {
return output.ErrValidation("--receive-id-type must be one of: chat_id, open_id, user_id, union_id, email")
}
return nil
},
Execute: func(ctx context.Context, runtime *common.RuntimeContext) error {
msgID := runtime.Str("message-id")
threadID := runtime.Str("thread-id")
receiveID := runtime.Str("receive-id")
receiveIDType := runtime.Str("receive-id-type")
mailboxID := resolveMailboxID(runtime)
var createBody map[string]interface{}
if threadID != "" {
createBody = map[string]interface{}{"thread_id": threadID}
} else {
createBody = map[string]interface{}{"message_id": msgID}
}
createResp, err := runtime.CallAPI("POST",
mailboxPath(mailboxID, "messages", "share_token"),
nil, createBody)
if err != nil {
return fmt.Errorf("create share token: %w", err)
}
cardID, _ := createResp["card_id"].(string)
if cardID == "" {
return fmt.Errorf("create share token: response missing card_id")
}
sendResp, err := runtime.CallAPI("POST",
mailboxPath(mailboxID, "share_tokens", cardID, "send"),
map[string]interface{}{"receive_id_type": receiveIDType},
map[string]interface{}{"receive_id": receiveID})
if err != nil {
return fmt.Errorf("share token created (card_id=%s) but send failed: %w", cardID, err)
}
runtime.Out(map[string]interface{}{
"card_id": cardID,
"im_message_id": sendResp["message_id"],
}, nil)
return nil
},
}