mirror of
https://github.com/larksuite/cli.git
synced 2026-07-06 16:18:05 +08:00
* 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.
190 lines
5.6 KiB
Go
190 lines
5.6 KiB
Go
// Copyright (c) 2026 Lark Technologies Pte. Ltd.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
//nolint:forbidigo // intermediate draft calendar patch errors; mail command layer wraps into typed ValidationError.
|
|
package draft
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
)
|
|
|
|
const calendarMediaType = "text/calendar"
|
|
|
|
// applyCalendarSet installs or replaces the text/calendar MIME part in the
|
|
// snapshot. The caller is expected to have pre-built icsData using the
|
|
// snapshot's From/To/Cc addresses.
|
|
func applyCalendarSet(snapshot *DraftSnapshot, icsData []byte) error {
|
|
if len(icsData) == 0 {
|
|
return fmt.Errorf("set_calendar: ICS data is empty (shortcut layer must pre-build it)")
|
|
}
|
|
setCalendarPart(snapshot, icsData)
|
|
return nil
|
|
}
|
|
|
|
// applyCalendarRemove strips the text/calendar part from the snapshot.
|
|
// No-op if no calendar part exists.
|
|
func applyCalendarRemove(snapshot *DraftSnapshot) error {
|
|
removeCalendarPart(snapshot)
|
|
return nil
|
|
}
|
|
|
|
// setCalendarPart places exactly one text/calendar part inside
|
|
// multipart/alternative, matching the Feishu client behavior. Any existing
|
|
// text/calendar parts elsewhere in the tree are removed first.
|
|
func setCalendarPart(snapshot *DraftSnapshot, icsData []byte) {
|
|
newPart := &Part{
|
|
MediaType: calendarMediaType,
|
|
MediaParams: map[string]string{"charset": "UTF-8", "method": "REQUEST"},
|
|
Body: icsData,
|
|
Dirty: true,
|
|
}
|
|
|
|
if snapshot.Body == nil {
|
|
snapshot.Body = newPart
|
|
return
|
|
}
|
|
|
|
// Remove all existing text/calendar parts from everywhere in the tree.
|
|
if strings.EqualFold(snapshot.Body.MediaType, calendarMediaType) {
|
|
snapshot.Body = newPart
|
|
return
|
|
}
|
|
removeAllPartsByMediaType(snapshot.Body, calendarMediaType)
|
|
|
|
// Place inside the existing multipart/alternative.
|
|
if alt := FindPartByMediaType(snapshot.Body, "multipart/alternative"); alt != nil {
|
|
alt.Children = append(alt.Children, newPart)
|
|
alt.Dirty = true
|
|
return
|
|
}
|
|
|
|
// No multipart/alternative exists. If the body is a single leaf,
|
|
// wrap it in multipart/alternative together with the calendar.
|
|
if !snapshot.Body.IsMultipart() {
|
|
original := *snapshot.Body
|
|
// Reset all header-carrying fields so the serializer constructs a fresh
|
|
// Content-Type from MediaType instead of reusing the stale leaf headers.
|
|
snapshot.Body.Headers = nil
|
|
snapshot.Body.MediaType = "multipart/alternative"
|
|
snapshot.Body.MediaParams = nil
|
|
snapshot.Body.ContentDisposition = ""
|
|
snapshot.Body.ContentDispositionArg = nil
|
|
snapshot.Body.ContentID = ""
|
|
snapshot.Body.PartID = ""
|
|
snapshot.Body.Body = nil
|
|
snapshot.Body.TransferEncoding = ""
|
|
snapshot.Body.RawEntity = nil
|
|
snapshot.Body.Preamble = nil
|
|
snapshot.Body.Epilogue = nil
|
|
snapshot.Body.EncodingProblem = false
|
|
snapshot.Body.Children = []*Part{&original, newPart}
|
|
snapshot.Body.Dirty = true
|
|
return
|
|
}
|
|
|
|
// Multipart body without an alternative sub-part (e.g. multipart/mixed
|
|
// with a text/html child). Find the first text/* child and wrap it in
|
|
// a new multipart/alternative that also contains the calendar.
|
|
for i, child := range snapshot.Body.Children {
|
|
if child != nil && strings.HasPrefix(strings.ToLower(child.MediaType), "text/") {
|
|
alt := &Part{
|
|
MediaType: "multipart/alternative",
|
|
Children: []*Part{child, newPart},
|
|
Dirty: true,
|
|
}
|
|
snapshot.Body.Children[i] = alt
|
|
snapshot.Body.Dirty = true
|
|
return
|
|
}
|
|
}
|
|
|
|
// Fallback: append to the root multipart container.
|
|
snapshot.Body.Children = append(snapshot.Body.Children, newPart)
|
|
snapshot.Body.Dirty = true
|
|
}
|
|
|
|
func removeCalendarPart(snapshot *DraftSnapshot) {
|
|
if snapshot.Body == nil {
|
|
return
|
|
}
|
|
if strings.EqualFold(snapshot.Body.MediaType, calendarMediaType) {
|
|
snapshot.Body = nil
|
|
return
|
|
}
|
|
removeAllPartsByMediaType(snapshot.Body, calendarMediaType)
|
|
}
|
|
|
|
// FindPartByMediaType walks the MIME tree and returns the first part with
|
|
// the given media type, or nil when not found.
|
|
func FindPartByMediaType(root *Part, mediaType string) *Part {
|
|
if root == nil {
|
|
return nil
|
|
}
|
|
if strings.EqualFold(root.MediaType, mediaType) {
|
|
return root
|
|
}
|
|
for _, child := range root.Children {
|
|
if found := FindPartByMediaType(child, mediaType); found != nil {
|
|
return found
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// findAllPartsByMediaType walks the MIME tree and returns every part with
|
|
// the given media type. Used in tests to assert tree contents.
|
|
func findAllPartsByMediaType(root *Part, mediaType string) []*Part {
|
|
if root == nil {
|
|
return nil
|
|
}
|
|
var result []*Part
|
|
if strings.EqualFold(root.MediaType, mediaType) {
|
|
result = append(result, root)
|
|
}
|
|
for _, child := range root.Children {
|
|
result = append(result, findAllPartsByMediaType(child, mediaType)...)
|
|
}
|
|
return result
|
|
}
|
|
|
|
// removePartByMediaType removes the first part with the given media type from
|
|
// the MIME tree. The parent is marked dirty when a removal happens.
|
|
func removePartByMediaType(root *Part, mediaType string) {
|
|
if root == nil {
|
|
return
|
|
}
|
|
for i, child := range root.Children {
|
|
if child != nil && strings.EqualFold(child.MediaType, mediaType) {
|
|
root.Children = append(root.Children[:i], root.Children[i+1:]...)
|
|
root.Dirty = true
|
|
return
|
|
}
|
|
removePartByMediaType(child, mediaType)
|
|
}
|
|
}
|
|
|
|
// removeAllPartsByMediaType removes every part with the given media type from
|
|
// the MIME tree, at all nesting levels.
|
|
func removeAllPartsByMediaType(root *Part, mediaType string) {
|
|
if root == nil {
|
|
return
|
|
}
|
|
var kept []*Part
|
|
removed := false
|
|
for _, child := range root.Children {
|
|
if child != nil && strings.EqualFold(child.MediaType, mediaType) {
|
|
removed = true
|
|
continue
|
|
}
|
|
kept = append(kept, child)
|
|
}
|
|
if removed {
|
|
root.Children = kept
|
|
root.Dirty = true
|
|
}
|
|
for _, child := range root.Children {
|
|
removeAllPartsByMediaType(child, mediaType)
|
|
}
|
|
}
|