]> git.giacomolli.uk - onechat/blobdiff - whatsapp_find.go
Order model and chat declarations
[onechat] / whatsapp_find.go
index 545e01c9d63e8127d2a6dbc2f7496a654ad70f57..5c19ae7c0959f825012bb51bdc79cd2a8b0608ef 100644 (file)
@@ -23,6 +23,13 @@ const (
        whatsappNamesNotReady = "The names of the contacts and groups have not arrived from the phone yet. They come some minutes after the link. Try again later."
 )
 
        whatsappNamesNotReady = "The names of the contacts and groups have not arrived from the phone yet. They come some minutes after the link. Try again later."
 )
 
+// WhatsAppMatch is one contact or group of whatsappFind.
+type WhatsAppMatch struct {
+       id   string
+       name string
+       kind string
+}
+
 // whatsappFind returns the contacts and the groups whose name contains name,
 // with no regard to case and accents, as lines "ID | name | contact" or "ID |
 // name | group", sorted by name. It gives at most whatsappFindMatches lines and
 // whatsappFind returns the contacts and the groups whose name contains name,
 // with no regard to case and accents, as lines "ID | name | contact" or "ID |
 // name | group", sorted by name. It gives at most whatsappFindMatches lines and
@@ -61,6 +68,7 @@ func whatsappFind(ctx context.Context, link WhatsAppLink, groups *WhatsAppGroups
        }
 
        var matches []WhatsAppMatch
        }
 
        var matches []WhatsAppMatch
+
        for jid, contact := range contacts {
                names := []string{contact.FullName, contact.FirstName, contact.BusinessName, contact.PushName}
                shown := slices.IndexFunc(names, func(name string) bool {
        for jid, contact := range contacts {
                names := []string{contact.FullName, contact.FirstName, contact.BusinessName, contact.PushName}
                shown := slices.IndexFunc(names, func(name string) bool {
@@ -93,13 +101,6 @@ func whatsappFind(ctx context.Context, link WhatsAppLink, groups *WhatsAppGroups
        return whatsappFindAnswer(name, matches, read || len(names) > 0), nil
 }
 
        return whatsappFindAnswer(name, matches, read || len(names) > 0), nil
 }
 
-// WhatsAppMatch is one contact or group of whatsappFind.
-type WhatsAppMatch struct {
-       id   string
-       name string
-       kind string
-}
-
 // whatsappFold returns text in lower case with no accents and no space at its
 // ends, so that names match with no regard to case and accents.
 func whatsappFold(text string) string {
 // whatsappFold returns text in lower case with no accents and no space at its
 // ends, so that names match with no regard to case and accents.
 func whatsappFold(text string) string {
@@ -111,14 +112,15 @@ func whatsappFold(text string) string {
        return strings.ToLower(strings.TrimSpace(folded))
 }
 
        return strings.ToLower(strings.TrimSpace(folded))
 }
 
-// whatsappFindAnswer returns the answer of whatsappFind for name with matches. Without
-// groups, the answer says that it has no groups.
+// whatsappFindAnswer returns the answer of whatsappFind for name with matches.
+// Without groups, the answer says that it has no groups.
 func whatsappFindAnswer(name string, matches []WhatsAppMatch, groups bool) string {
        slices.SortFunc(matches, func(a, b WhatsAppMatch) int {
                return cmp.Or(strings.Compare(whatsappFold(a.name), whatsappFold(b.name)), strings.Compare(a.id, b.id))
        })
 
        var lines []string
 func whatsappFindAnswer(name string, matches []WhatsAppMatch, groups bool) string {
        slices.SortFunc(matches, func(a, b WhatsAppMatch) int {
                return cmp.Or(strings.Compare(whatsappFold(a.name), whatsappFold(b.name)), strings.Compare(a.id, b.id))
        })
 
        var lines []string
+
        for _, match := range matches[:min(len(matches), whatsappFindMatches)] {
                lines = append(lines, match.id+" | "+match.name+" | "+match.kind)
        }
        for _, match := range matches[:min(len(matches), whatsappFindMatches)] {
                lines = append(lines, match.id+" | "+match.name+" | "+match.kind)
        }