-
Notifications
You must be signed in to change notification settings - Fork 5
feat: add global search command #23
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
dnikolaev
wants to merge
1
commit into
chatwoot:main
Choose a base branch
from
summeet-co:feat/global-search
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
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,266 @@ | ||
| package cmd | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "strconv" | ||
| "strings" | ||
|
|
||
| "github.com/chatwoot/cli/internal/sdk" | ||
| ) | ||
|
|
||
| type SearchCmd struct { | ||
| Query string `arg:"" help:"Search query."` | ||
| Page int `short:"p" default:"1" help:"Page number (applies to all result buckets)."` | ||
| Only string `help:"Restrict to one bucket: conversations, contacts, messages, or articles." enum:",conversations,contacts,messages,articles" default:""` | ||
| } | ||
|
|
||
| func (c *SearchCmd) Run(app *App) error { | ||
| resp, err := app.Client.Search().Global(sdk.SearchOptions{ | ||
| Query: c.Query, | ||
| Page: c.Page, | ||
| }) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if app.Printer.Format == "json" && !app.Printer.Quiet { | ||
| app.Printer.PrintJSON(payloadOnly(resp, c.Only)) | ||
| return nil | ||
| } | ||
|
|
||
| if app.Printer.Format == "csv" { | ||
| if c.Only == "" { | ||
| return fmt.Errorf("csv output needs a single bucket; use --only=<bucket> or -o json") | ||
| } | ||
| return printSearchSection(app, resp, c.Only, false) | ||
|
scmmishra marked this conversation as resolved.
|
||
| } | ||
|
|
||
| if app.Printer.Quiet { | ||
| return printSearchQuiet(app, resp, c.Only) | ||
| } | ||
|
|
||
| return printSearchText(app, c.Query, resp, c.Only) | ||
| } | ||
|
|
||
| func printSearchText(app *App, query string, resp *sdk.SearchResponse, only string) error { | ||
| p := resp.Payload | ||
| total := len(p.Conversations) + len(p.Contacts) + len(p.Messages) + len(p.Articles) | ||
|
|
||
| if only != "" { | ||
| return printSearchSection(app, resp, only, false) | ||
| } | ||
|
|
||
| if total == 0 { | ||
| _, _ = fmt.Fprintf(app.Printer.Writer, "No results for %q.\n", query) | ||
| return nil | ||
| } | ||
|
|
||
| first := true | ||
| if len(p.Conversations) > 0 { | ||
| first = printSectionHeader(app, first, "CONVERSATIONS") | ||
| printConversationRows(app, p.Conversations) | ||
| } | ||
| if len(p.Contacts) > 0 { | ||
| first = printSectionHeader(app, first, "CONTACTS") | ||
| printContactRows(app, p.Contacts) | ||
| } | ||
| if len(p.Messages) > 0 { | ||
| first = printSectionHeader(app, first, "MESSAGES") | ||
| printMessageRows(app, p.Messages) | ||
| } | ||
| if len(p.Articles) > 0 { | ||
| _ = printSectionHeader(app, first, "ARTICLES") | ||
| printArticleRows(app, p.Articles) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| // payloadOnly returns a response restricted to a single bucket so that --only is | ||
| // honored consistently across every output format (text, csv, quiet, and json). | ||
| // An empty bucket returns the response unchanged. | ||
| func payloadOnly(resp *sdk.SearchResponse, only string) *sdk.SearchResponse { | ||
| if only == "" { | ||
| return resp | ||
| } | ||
| out := &sdk.SearchResponse{} | ||
| switch only { | ||
| case "conversations": | ||
| out.Payload.Conversations = resp.Payload.Conversations | ||
| case "contacts": | ||
| out.Payload.Contacts = resp.Payload.Contacts | ||
| case "messages": | ||
| out.Payload.Messages = resp.Payload.Messages | ||
| case "articles": | ||
| out.Payload.Articles = resp.Payload.Articles | ||
| } | ||
| return out | ||
| } | ||
|
|
||
| func printSearchSection(app *App, resp *sdk.SearchResponse, only string, prefixIDs bool) error { | ||
| _ = prefixIDs // reserved for future use; quiet mode handles prefixing separately | ||
| // In CSV mode an empty bucket must still emit a header row (valid CSV) rather | ||
| // than the human-readable "No X found." message, so scripts get parseable output. | ||
| prose := app.Printer.Format != "csv" | ||
| switch only { | ||
| case "conversations": | ||
| if len(resp.Payload.Conversations) == 0 && prose { | ||
| _, _ = fmt.Fprintln(app.Printer.Writer, "No conversations found.") | ||
| return nil | ||
| } | ||
| printConversationRows(app, resp.Payload.Conversations) | ||
| case "contacts": | ||
| if len(resp.Payload.Contacts) == 0 && prose { | ||
| _, _ = fmt.Fprintln(app.Printer.Writer, "No contacts found.") | ||
| return nil | ||
| } | ||
| printContactRows(app, resp.Payload.Contacts) | ||
| case "messages": | ||
| if len(resp.Payload.Messages) == 0 && prose { | ||
| _, _ = fmt.Fprintln(app.Printer.Writer, "No messages found.") | ||
| return nil | ||
| } | ||
| printMessageRows(app, resp.Payload.Messages) | ||
| case "articles": | ||
| if len(resp.Payload.Articles) == 0 && prose { | ||
| _, _ = fmt.Fprintln(app.Printer.Writer, "No articles found.") | ||
| return nil | ||
| } | ||
| printArticleRows(app, resp.Payload.Articles) | ||
| default: | ||
| return fmt.Errorf("unknown bucket: %q", only) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func printSearchQuiet(app *App, resp *sdk.SearchResponse, only string) error { | ||
| w := app.Printer.Writer | ||
| p := resp.Payload | ||
|
|
||
| if only != "" { | ||
| switch only { | ||
| case "conversations": | ||
| for _, c := range p.Conversations { | ||
| _, _ = fmt.Fprintln(w, c.ID) | ||
| } | ||
| case "contacts": | ||
| for _, c := range p.Contacts { | ||
| _, _ = fmt.Fprintln(w, c.ID) | ||
| } | ||
| case "messages": | ||
| for _, m := range p.Messages { | ||
| _, _ = fmt.Fprintln(w, m.ID) | ||
| } | ||
| case "articles": | ||
| for _, a := range p.Articles { | ||
| _, _ = fmt.Fprintln(w, a.ID) | ||
| } | ||
| default: | ||
| return fmt.Errorf("unknown bucket: %q", only) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| for _, c := range p.Conversations { | ||
| _, _ = fmt.Fprintf(w, "conversation:%d\n", c.ID) | ||
| } | ||
| for _, c := range p.Contacts { | ||
| _, _ = fmt.Fprintf(w, "contact:%d\n", c.ID) | ||
| } | ||
| for _, m := range p.Messages { | ||
| _, _ = fmt.Fprintf(w, "message:%d\n", m.ID) | ||
| } | ||
| for _, a := range p.Articles { | ||
| _, _ = fmt.Fprintf(w, "article:%d\n", a.ID) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func printSectionHeader(app *App, first bool, title string) bool { | ||
| if !first { | ||
| _, _ = fmt.Fprintln(app.Printer.Writer) | ||
| } | ||
| _, _ = fmt.Fprintln(app.Printer.Writer, title) | ||
| return false | ||
| } | ||
|
|
||
| func printConversationRows(app *App, convs []sdk.ConversationSearchResult) { | ||
| headers := []string{"ID", "Contact", "Inbox", "Channel", "Created"} | ||
| rows := make([][]string, 0, len(convs)) | ||
| for _, c := range convs { | ||
| contact := "" | ||
| if c.Contact != nil { | ||
| contact = c.Contact.Name | ||
| } | ||
| inboxName := "" | ||
| channel := "" | ||
| if c.Inbox != nil { | ||
| inboxName = c.Inbox.Name | ||
| channel = c.Inbox.ChannelType | ||
| } | ||
| rows = append(rows, []string{ | ||
| strconv.Itoa(c.ID), | ||
| contact, | ||
| inboxName, | ||
| channel, | ||
| formatTimestamp(c.CreatedAt), | ||
| }) | ||
| } | ||
| app.Printer.PrintTable(headers, rows) | ||
| } | ||
|
|
||
| func printContactRows(app *App, contacts []sdk.ContactSearchResult) { | ||
| headers := []string{"ID", "Name", "Email", "Phone", "Last Activity"} | ||
| rows := make([][]string, 0, len(contacts)) | ||
| for _, c := range contacts { | ||
| rows = append(rows, []string{ | ||
| strconv.Itoa(c.ID), | ||
| c.Name, | ||
| c.Email, | ||
| c.PhoneNumber, | ||
| formatTimestamp(c.LastActivityAt), | ||
| }) | ||
| } | ||
| app.Printer.PrintTable(headers, rows) | ||
| } | ||
|
|
||
| func printMessageRows(app *App, messages []sdk.MessageSearchResult) { | ||
| headers := []string{"ID", "Conv", "Sender", "Time", "Content"} | ||
| rows := make([][]string, 0, len(messages)) | ||
| for _, m := range messages { | ||
| sender := "" | ||
| if m.Sender != nil { | ||
| sender = m.Sender.Name | ||
| } | ||
| content := truncate(sanitizeCell(m.Content), 60) | ||
| rows = append(rows, []string{ | ||
| strconv.Itoa(m.ID), | ||
| strconv.Itoa(m.ConversationID), | ||
| sender, | ||
| formatTimestamp(m.CreatedAt), | ||
| content, | ||
| }) | ||
| } | ||
| app.Printer.PrintTable(headers, rows) | ||
| } | ||
|
|
||
| func printArticleRows(app *App, articles []sdk.ArticleSearchResult) { | ||
| headers := []string{"ID", "Title", "Locale", "Status", "Updated"} | ||
| rows := make([][]string, 0, len(articles)) | ||
| for _, a := range articles { | ||
| rows = append(rows, []string{ | ||
| strconv.Itoa(a.ID), | ||
| truncate(sanitizeCell(a.Title), 60), | ||
| a.Locale, | ||
| a.Status, | ||
| formatTimestamp(a.UpdatedAt), | ||
| }) | ||
| } | ||
| app.Printer.PrintTable(headers, rows) | ||
| } | ||
|
|
||
| // sanitizeCell collapses newlines, tabs, and carriage returns into single | ||
| // spaces so that tabwriter alignment stays intact. | ||
| func sanitizeCell(s string) string { | ||
| r := strings.NewReplacer("\n", " ", "\r", " ", "\t", " ") | ||
| return strings.Join(strings.Fields(r.Replace(s)), " ") | ||
| } | ||
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.
When
--onlyis provided this still calls the global/searchendpoint, which Chatwoot'sSearchController#indexhandles assearch('all'); the bucket routes (/search/messages,/search/contacts, etc.) are the ones that avoid running the other searches. On large accounts, or when an unrelated bucket is slow/errors,chatwoot search --only=messages ...can time out or fail even though the requested messages search would succeed, soOnlyshould affect the SDK request rather than just filtering the rendered payload.Useful? React with 👍 / 👎.