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
72
commands.go
72
commands.go
|
@ -135,7 +135,7 @@ func (handler *CommandHandler) CommandMux(ce *CommandEvent) {
|
||||||
handler.CommandLogout(ce)
|
handler.CommandLogout(ce)
|
||||||
case "toggle":
|
case "toggle":
|
||||||
handler.CommandToggle(ce)
|
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() {
|
if !ce.User.HasSession() {
|
||||||
ce.Reply("You are not logged in. Use the `login` command to log into WhatsApp.")
|
ce.Reply("You are not logged in. Use the `login` command to log into WhatsApp.")
|
||||||
return
|
return
|
||||||
|
@ -155,6 +155,8 @@ func (handler *CommandHandler) CommandMux(ce *CommandEvent) {
|
||||||
handler.CommandSync(ce)
|
handler.CommandSync(ce)
|
||||||
case "list":
|
case "list":
|
||||||
handler.CommandList(ce)
|
handler.CommandList(ce)
|
||||||
|
case "search":
|
||||||
|
handler.CommandSearch(ce)
|
||||||
case "open":
|
case "open":
|
||||||
handler.CommandOpen(ce)
|
handler.CommandOpen(ce)
|
||||||
case "pm":
|
case "pm":
|
||||||
|
@ -708,6 +710,7 @@ func (handler *CommandHandler) CommandHelp(ce *CommandEvent) {
|
||||||
cmdPrefix + cmdLogoutMatrixHelp,
|
cmdPrefix + cmdLogoutMatrixHelp,
|
||||||
cmdPrefix + cmdToggleHelp,
|
cmdPrefix + cmdToggleHelp,
|
||||||
cmdPrefix + cmdListHelp,
|
cmdPrefix + cmdListHelp,
|
||||||
|
cmdPrefix + cmdSearchHelp,
|
||||||
cmdPrefix + cmdSyncHelp,
|
cmdPrefix + cmdSyncHelp,
|
||||||
cmdPrefix + cmdOpenHelp,
|
cmdPrefix + cmdOpenHelp,
|
||||||
cmdPrefix + cmdPMHelp,
|
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.`
|
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 {
|
for jid, contact := range input {
|
||||||
if len(contact.FullName) == 0 {
|
if len(contact.FullName) == 0 {
|
||||||
continue
|
continue
|
||||||
|
@ -820,15 +831,21 @@ func formatContacts(bridge *Bridge, input map[types.JID]types.ContactInfo) (resu
|
||||||
if len(pushName) == 0 {
|
if len(pushName) == 0 {
|
||||||
pushName = contact.FullName
|
pushName = contact.FullName
|
||||||
}
|
}
|
||||||
result = append(result, fmt.Sprintf("* %s / [%s](https://matrix.to/#/%s) - `+%s`", contact.FullName, pushName, puppet.MXID, jid.User))
|
|
||||||
|
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))
|
sort.Sort(sort.StringSlice(result))
|
||||||
return
|
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 {
|
for _, group := range input {
|
||||||
result = append(result, fmt.Sprintf("* %s - `%s`", group.GroupName.Name, group.JID.User))
|
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))
|
sort.Sort(sort.StringSlice(result))
|
||||||
return
|
return
|
||||||
|
@ -874,14 +891,14 @@ func (handler *CommandHandler) CommandList(ce *CommandEvent) {
|
||||||
ce.Reply("Failed to get contacts: %s", err)
|
ce.Reply("Failed to get contacts: %s", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
result = formatContacts(ce.User.bridge, contactList)
|
result = formatContacts(ce.User.bridge, contactList, "")
|
||||||
} else {
|
} else {
|
||||||
groupList, err := ce.User.Client.GetJoinedGroups()
|
groupList, err := ce.User.Client.GetJoinedGroups()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ce.Reply("Failed to get groups: %s", err)
|
ce.Reply("Failed to get groups: %s", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
result = formatGroups(groupList)
|
result = formatGroups(groupList, "")
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(result) == 0 {
|
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"))
|
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.`
|
const cmdOpenHelp = `open <_group JID_> - Open a group chat portal.`
|
||||||
|
|
||||||
func (handler *CommandHandler) CommandOpen(ce *CommandEvent) {
|
func (handler *CommandHandler) CommandOpen(ce *CommandEvent) {
|
||||||
|
|
Loading…
Reference in a new issue