mirror of
https://github.com/tulir/mautrix-whatsapp
synced 2024-12-14 01:14:29 +01:00
Implement search command
This commit is contained in:
parent
5c1b57d7b8
commit
dc9a075bc9
1 changed files with 65 additions and 7 deletions
68
commands.go
68
commands.go
|
@ -135,7 +135,7 @@ func (handler *CommandHandler) CommandMux(ce *CommandEvent) {
|
|||
handler.CommandLogout(ce)
|
||||
case "toggle":
|
||||
handler.CommandToggle(ce)
|
||||
case "set-relay", "unset-relay", "login-matrix", "sync", "list", "open", "pm", "invite-link", "check-invite", "join", "create", "accept":
|
||||
case "set-relay", "unset-relay", "login-matrix", "sync", "list", "search", "open", "pm", "invite-link", "check-invite", "join", "create", "accept":
|
||||
if !ce.User.HasSession() {
|
||||
ce.Reply("You are not logged in. Use the `login` command to log into WhatsApp.")
|
||||
return
|
||||
|
@ -155,6 +155,8 @@ func (handler *CommandHandler) CommandMux(ce *CommandEvent) {
|
|||
handler.CommandSync(ce)
|
||||
case "list":
|
||||
handler.CommandList(ce)
|
||||
case "search":
|
||||
handler.CommandSearch(ce)
|
||||
case "open":
|
||||
handler.CommandOpen(ce)
|
||||
case "pm":
|
||||
|
@ -708,6 +710,7 @@ func (handler *CommandHandler) CommandHelp(ce *CommandEvent) {
|
|||
cmdPrefix + cmdLogoutMatrixHelp,
|
||||
cmdPrefix + cmdToggleHelp,
|
||||
cmdPrefix + cmdListHelp,
|
||||
cmdPrefix + cmdSearchHelp,
|
||||
cmdPrefix + cmdSyncHelp,
|
||||
cmdPrefix + cmdOpenHelp,
|
||||
cmdPrefix + cmdPMHelp,
|
||||
|
@ -810,7 +813,15 @@ func (handler *CommandHandler) CommandDeleteAllPortals(ce *CommandEvent) {
|
|||
|
||||
const cmdListHelp = `list <contacts|groups> [page] [items per page] - Get a list of all contacts and groups.`
|
||||
|
||||
func formatContacts(bridge *Bridge, input map[types.JID]types.ContactInfo) (result []string) {
|
||||
func matchesQuery(str string, query string) bool {
|
||||
if query == "" {
|
||||
return true
|
||||
}
|
||||
return strings.Contains(strings.ToLower(str), query)
|
||||
}
|
||||
|
||||
func formatContacts(bridge *Bridge, input map[types.JID]types.ContactInfo, query string) (result []string) {
|
||||
hasQuery := len(query) > 0
|
||||
for jid, contact := range input {
|
||||
if len(contact.FullName) == 0 {
|
||||
continue
|
||||
|
@ -820,16 +831,22 @@ func formatContacts(bridge *Bridge, input map[types.JID]types.ContactInfo) (resu
|
|||
if len(pushName) == 0 {
|
||||
pushName = contact.FullName
|
||||
}
|
||||
|
||||
if !hasQuery || matchesQuery(pushName, query) || matchesQuery(contact.FullName, query) || matchesQuery(jid.User, query) {
|
||||
result = append(result, fmt.Sprintf("* %s / [%s](https://matrix.to/#/%s) - `+%s`", contact.FullName, pushName, puppet.MXID, jid.User))
|
||||
}
|
||||
}
|
||||
sort.Sort(sort.StringSlice(result))
|
||||
return
|
||||
}
|
||||
|
||||
func formatGroups(input []*types.GroupInfo) (result []string) {
|
||||
func formatGroups(input []*types.GroupInfo, query string) (result []string) {
|
||||
hasQuery := len(query) > 0
|
||||
for _, group := range input {
|
||||
if !hasQuery || matchesQuery(group.GroupName.Name, query) || matchesQuery(group.JID.User, query) {
|
||||
result = append(result, fmt.Sprintf("* %s - `%s`", group.GroupName.Name, group.JID.User))
|
||||
}
|
||||
}
|
||||
sort.Sort(sort.StringSlice(result))
|
||||
return
|
||||
}
|
||||
|
@ -874,14 +891,14 @@ func (handler *CommandHandler) CommandList(ce *CommandEvent) {
|
|||
ce.Reply("Failed to get contacts: %s", err)
|
||||
return
|
||||
}
|
||||
result = formatContacts(ce.User.bridge, contactList)
|
||||
result = formatContacts(ce.User.bridge, contactList, "")
|
||||
} else {
|
||||
groupList, err := ce.User.Client.GetJoinedGroups()
|
||||
if err != nil {
|
||||
ce.Reply("Failed to get groups: %s", err)
|
||||
return
|
||||
}
|
||||
result = formatGroups(groupList)
|
||||
result = formatGroups(groupList, "")
|
||||
}
|
||||
|
||||
if len(result) == 0 {
|
||||
|
@ -905,6 +922,47 @@ func (handler *CommandHandler) CommandList(ce *CommandEvent) {
|
|||
ce.Reply("### %s (page %d of %d)\n\n%s", typeName, page, pages, strings.Join(result, "\n"))
|
||||
}
|
||||
|
||||
const cmdSearchHelp = `search <search query> - Search for a contact or group using a query.`
|
||||
|
||||
func (handler *CommandHandler) CommandSearch(ce *CommandEvent) {
|
||||
if len(ce.Args) == 0 {
|
||||
ce.Reply("**Usage:** `search <name query>`")
|
||||
return
|
||||
}
|
||||
query := strings.ToLower(strings.TrimSpace(strings.Join(ce.Args, " ")))
|
||||
|
||||
var err error
|
||||
|
||||
contactList, err := ce.User.Client.Store.Contacts.GetAllContacts()
|
||||
if err != nil {
|
||||
ce.Reply("Failed to get contacts: %s", err)
|
||||
return
|
||||
}
|
||||
formatedContacts := formatContacts(ce.User.bridge, contactList, query)
|
||||
|
||||
groupList, err := ce.User.Client.GetJoinedGroups()
|
||||
if err != nil {
|
||||
ce.Reply("Failed to get groups: %s", err)
|
||||
return
|
||||
}
|
||||
formatedGroups := formatGroups(groupList, query)
|
||||
|
||||
var result string
|
||||
if len(formatedContacts) > 0 {
|
||||
result = "\n\n#### Contacts: \n\n" + strings.Join(formatedContacts, "\n")
|
||||
}
|
||||
if len(formatedGroups) > 0 {
|
||||
result += "\n\n#### Groups: \n\n" + strings.Join(formatedGroups, "\n")
|
||||
}
|
||||
|
||||
if len(result) == 0 {
|
||||
ce.Reply("No contacts or groups found")
|
||||
return
|
||||
}
|
||||
|
||||
ce.Reply("### Search results:%s", result)
|
||||
}
|
||||
|
||||
const cmdOpenHelp = `open <_group JID_> - Open a group chat portal.`
|
||||
|
||||
func (handler *CommandHandler) CommandOpen(ce *CommandEvent) {
|
||||
|
|
Loading…
Reference in a new issue