Files
larksuite__cli/shortcuts/mail/mail_share_to_chat.go
evandance 5e6a3eb857 feat(mail): return typed error envelopes across the mail domain (#1250)
* feat(mail): return typed error envelopes across the mail domain

Replace every produced error path in shortcuts/mail with typed errs.* envelopes, so consumers get stable category, subtype, param/params, hint, retryable, and log_id metadata for classification and recovery instead of free-form message text.

- Locally constructed mail errors move from output.Err* / output.Errorf / final fmt.Errorf / common legacy helpers to errs.* builders, with structured params on multi-flag validation and failed-precondition states kept non-retryable.

- API-call failures move from runtime.CallAPI / DoAPIJSON legacy boundaries to runtime.CallAPITyped or runtime.ClassifyAPIResponse, and mail-specific enrichers read errs.ProblemOf so typed code, subtype, hint, and log_id metadata are preserved.

- Batch draft-send partial failures now use runtime.OutPartialFailure so successful and failed draft sends stay in stdout while the command exits through a typed multi-status signal.

- Add mail-domain typed helpers, mail API code metadata, and guard wiring to keep shortcuts/mail from reintroducing legacy envelopes or legacy API calls.

- Keep genuine intermediate fmt.Errorf wraps in parser/builder layers annotated with nolint comments; command-facing paths wrap them into typed validation, API, network, or internal errors.

* fix(mail): report aborted draft-send batches as a single failure result

When an account-level failure interrupts a batch send after some drafts
already went out, the command previously produced two machine-readable
failure results: the partial-failure ledger on stdout and a second error
envelope on stderr. Consumers could not tell which one to recover from.

The batch ledger is now the only failure result for that case: it gains
aborted and abort_error fields carrying the typed cause, so callers can
see which drafts were sent, which failed, why the batch stopped, and how
to recover — all from stdout. A --stop-on-error stop keeps these fields
unset because stopping early there is the caller's own choice.
2026-06-04 21:02:20 +08:00

125 lines
4.4 KiB
Go

// Copyright (c) 2026 Lark Technologies Pte. Ltd.
// SPDX-License-Identifier: MIT
package mail
import (
"context"
"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 mailValidationError("either --message-id or --thread-id is required").
WithParams(
mailInvalidParam("--message-id", "required when --thread-id is empty"),
mailInvalidParam("--thread-id", "required when --message-id is empty"),
)
}
if msgID != "" && threadID != "" {
return mailValidationError("--message-id and --thread-id are mutually exclusive").
WithParams(
mailInvalidParam("--message-id", "mutually exclusive with --thread-id"),
mailInvalidParam("--thread-id", "mutually exclusive with --message-id"),
)
}
idType := runtime.Str("receive-id-type")
if !validReceiveIDTypes[idType] {
return mailValidationParamError("--receive-id-type", "--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.CallAPITyped("POST",
mailboxPath(mailboxID, "messages", "share_token"),
nil, createBody)
if err != nil {
return mailDecorateProblemMessage(err, "create share token")
}
cardID, _ := createResp["card_id"].(string)
if cardID == "" {
return mailInvalidResponseError("create share token: response missing card_id")
}
sendResp, err := runtime.CallAPITyped("POST",
mailboxPath(mailboxID, "share_tokens", cardID, "send"),
map[string]interface{}{"receive_id_type": receiveIDType},
map[string]interface{}{"receive_id": receiveID})
if err != nil {
return mailDecorateProblemMessage(err, "share token created (card_id=%s) but send failed", cardID)
}
runtime.Out(map[string]interface{}{
"card_id": cardID,
"im_message_id": sendResp["message_id"],
}, nil)
return nil
},
}