-
Notifications
You must be signed in to change notification settings - Fork 1.4k
feat(mail): add message citations #2713
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
yangr-happy
wants to merge
1
commit into
larksuite:main
Choose a base branch
from
yangr-happy:feat/a081a8b
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| // Copyright (c) 2026 Lark Technologies Pte. Ltd. | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| package citation | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "strconv" | ||
| "strings" | ||
| "time" | ||
| ) | ||
|
|
||
| // SourceType identifies the product resource represented by a citation. | ||
| type SourceType int | ||
|
|
||
| const ( | ||
| SourceUnknown SourceType = 0 | ||
| SourceMail SourceType = 12 | ||
| ) | ||
|
|
||
| // Citation is the JSON shape attached to successful command envelopes. | ||
| type Citation struct { | ||
| SourceType SourceType `json:"source_type"` | ||
| URL string `json:"url"` | ||
| Title string `json:"title"` | ||
| Snippet string `json:"snippet,omitempty"` | ||
| PublishTime string `json:"publish_time,omitempty"` | ||
| } | ||
|
|
||
| // Filter drops citations that cannot be opened by a client. | ||
| func Filter(items []Citation) []Citation { | ||
| out := make([]Citation, 0, len(items)) | ||
| for _, item := range items { | ||
| if strings.TrimSpace(item.URL) == "" { | ||
| continue | ||
| } | ||
| out = append(out, item) | ||
| } | ||
| return out | ||
| } | ||
|
|
||
| // Time normalizes OpenAPI timestamp values into RFC3339. Mail APIs commonly | ||
| // return Unix milliseconds as either numbers or decimal strings. | ||
| func Time(value interface{}) string { | ||
| switch v := value.(type) { | ||
| case nil: | ||
| return "" | ||
| case time.Time: | ||
| if v.IsZero() { | ||
| return "" | ||
| } | ||
| return v.UTC().Format(time.RFC3339) | ||
| case string: | ||
| return timeString(v) | ||
| case jsonNumber: | ||
| return timeString(v.String()) | ||
| case int: | ||
| return unixTimestamp(int64(v)) | ||
| case int64: | ||
| return unixTimestamp(v) | ||
| case float64: | ||
| return unixTimestamp(int64(v)) | ||
| case float32: | ||
| return unixTimestamp(int64(v)) | ||
| default: | ||
| return timeString(fmt.Sprint(value)) | ||
| } | ||
| } | ||
|
|
||
| type jsonNumber interface { | ||
| String() string | ||
| } | ||
|
|
||
| func timeString(raw string) string { | ||
| value := strings.TrimSpace(raw) | ||
| if value == "" { | ||
| return "" | ||
| } | ||
| if t, err := time.Parse(time.RFC3339, value); err == nil { | ||
| return t.UTC().Format(time.RFC3339) | ||
| } | ||
| if n, err := strconv.ParseInt(value, 10, 64); err == nil { | ||
| return unixTimestamp(n) | ||
| } | ||
| return "" | ||
| } | ||
|
|
||
| func unixTimestamp(value int64) string { | ||
| if value <= 0 { | ||
| return "" | ||
| } | ||
| if value > 1_000_000_000_000 { | ||
| return time.UnixMilli(value).UTC().Format(time.RFC3339) | ||
| } | ||
| return time.Unix(value, 0).UTC().Format(time.RFC3339) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| // Copyright (c) 2026 Lark Technologies Pte. Ltd. | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| package mail | ||
|
|
||
| import ( | ||
| "github.com/larksuite/cli/internal/citation" | ||
| "github.com/larksuite/cli/shortcuts/common" | ||
| ) | ||
|
|
||
| func buildMailMessageCitation(msg map[string]interface{}) citation.Citation { | ||
| return citation.Citation{ | ||
| SourceType: citation.SourceMail, | ||
| URL: strVal(msg["applink"]), | ||
| Title: strVal(msg["subject"]), | ||
| Snippet: mailCitationSnippet(msg), | ||
| PublishTime: mailCitationPublishTime(msg), | ||
| } | ||
| } | ||
|
|
||
| func buildMailMessageCitations(messages []map[string]interface{}) []citation.Citation { | ||
| out := make([]citation.Citation, 0, len(messages)) | ||
| for _, msg := range messages { | ||
| out = append(out, buildMailMessageCitation(msg)) | ||
| } | ||
| return out | ||
| } | ||
|
|
||
| func mailMessageCitationDefinition() *common.CitationDefinition { | ||
| return &common.CitationDefinition{ | ||
| SourceTypes: []citation.SourceType{citation.SourceMail}, | ||
| Build: func(data interface{}) []citation.Citation { | ||
| msg, ok := data.(map[string]interface{}) | ||
| if !ok { | ||
| return nil | ||
| } | ||
| return []citation.Citation{buildMailMessageCitation(msg)} | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| func mailMessagesCitationDefinition() *common.CitationDefinition { | ||
| return &common.CitationDefinition{ | ||
| SourceTypes: []citation.SourceType{citation.SourceMail}, | ||
| Build: func(data interface{}) []citation.Citation { | ||
| switch v := data.(type) { | ||
| case mailMessagesOutput: | ||
| return buildMailMessageCitations(v.Messages) | ||
| case *mailMessagesOutput: | ||
| if v == nil { | ||
| return nil | ||
| } | ||
| return buildMailMessageCitations(v.Messages) | ||
| default: | ||
| return nil | ||
| } | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| func mailTriageCitationDefinition() *common.CitationDefinition { | ||
| return &common.CitationDefinition{ | ||
| SourceTypes: []citation.SourceType{citation.SourceMail}, | ||
| Build: func(data interface{}) []citation.Citation { | ||
| out, ok := data.(map[string]interface{}) | ||
| if !ok { | ||
| return nil | ||
| } | ||
| return buildMailMessageCitations(mapSlice(out["messages"])) | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| func mailCitationSnippet(msg map[string]interface{}) string { | ||
| for _, key := range []string{"body_preview", "preview", "summary"} { | ||
| if value := strVal(msg[key]); value != "" { | ||
| return value | ||
| } | ||
| } | ||
| return "" | ||
| } | ||
|
|
||
| func mailCitationPublishTime(msg map[string]interface{}) string { | ||
| for _, key := range []string{"internal_date", "create_time", "date"} { | ||
| if value := citation.Time(msg[key]); value != "" { | ||
| return value | ||
| } | ||
| } | ||
| return "" | ||
| } | ||
|
|
||
| func mapSlice(raw interface{}) []map[string]interface{} { | ||
| switch v := raw.(type) { | ||
| case []map[string]interface{}: | ||
| return v | ||
| case []interface{}: | ||
| out := make([]map[string]interface{}, 0, len(v)) | ||
| for _, item := range v { | ||
| if msg, ok := item.(map[string]interface{}); ok { | ||
| out = append(out, msg) | ||
| } | ||
| } | ||
| return out | ||
| default: | ||
| return nil | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
Correct the Unix millisecond threshold.
The current threshold treats every millisecond timestamp before September 9, 2001 as Unix seconds. For example,
946684800000represents January 1, 2000 in milliseconds, but this code formats it as a far-future date.Use a threshold that distinguishes normal 10-digit seconds from 12-digit milliseconds. Add a regression test with a pre-2001 millisecond timestamp.
Proposed fix
📝 Committable suggestion
🤖 Prompt for AI Agents