List and Open commands

This commit is contained in:
Karmanyaah Malhotra 2021-02-28 14:59:03 -05:00
parent 3e75ffaf05
commit a46326cedd
2 changed files with 115 additions and 92 deletions

View File

@ -20,14 +20,18 @@ import (
// "errors" // "errors"
"context" "context"
"fmt" "fmt"
"math"
"sort"
// "math" // "math"
"strconv" "strconv"
"strings" "strings"
"github.com/Rhymen/go-whatsapp"
"maunium.net/go/maulogger/v2" "maunium.net/go/maulogger/v2"
"maunium.net/go/mautrix-whatsapp/database"
"maunium.net/go/mautrix-whatsapp/types"
whatsappExt "maunium.net/go/mautrix-whatsapp/whatsapp-ext"
"maunium.net/go/mautrix" "maunium.net/go/mautrix"
"maunium.net/go/mautrix/appservice" "maunium.net/go/mautrix/appservice"
@ -752,109 +756,124 @@ 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(contacts bool, input map[string]whatsapp.Contact) (result []string) { func formatContacts(contacts bool, input map[string]string) (result []string) {
// for jid, contact := range input { for jid, contact := range input {
// if strings.HasSuffix(jid, whatsappExt.NewUserSuffix) != contacts { if strings.HasSuffix(jid, whatsappExt.NewUserSuffix) != contacts {
// continue continue
// } }
//
// if contacts { if contacts {
// result = append(result, fmt.Sprintf("* %s / %s - `%s`", contact.Name, contact.Notify, contact.Jid[:len(contact.Jid)-len(whatsappExt.NewUserSuffix)])) result = append(result, fmt.Sprintf("* %s / %s - `%s`", contact, jid))
// } else { } else {
// result = append(result, fmt.Sprintf("* %s - `%s`", contact.Name, contact.Jid)) result = append(result, fmt.Sprintf("* %s - `%s`", contact, jid))
// } }
// } }
// sort.Sort(sort.StringSlice(result)) sort.Sort(sort.StringSlice(result))
return return
} }
func (handler *CommandHandler) CommandList(ce *CommandEvent) { func (handler *CommandHandler) CommandList(ce *CommandEvent) {
// if len(ce.Args) == 0 { if len(ce.Args) == 0 {
// ce.Reply("**Usage:** `list <contacts|groups> [page] [items per page]`") ce.Reply("**Usage:** `list <contacts|groups> [page] [items per page]`")
// return return
// } }
// mode := strings.ToLower(ce.Args[0]) mode := strings.ToLower(ce.Args[0])
// if mode[0] != 'g' && mode[0] != 'c' { if mode[0] != 'g' && mode[0] != 'c' {
// ce.Reply("**Usage:** `list <contacts|groups> [page] [items per page]`") ce.Reply("**Usage:** `list <contacts|groups> [page] [items per page]`")
// return return
// } }
// var err error var err error
// page := 1 page := 1
// max := 100 max := 100
// if len(ce.Args) > 1 { if len(ce.Args) > 1 {
// page, err = strconv.Atoi(ce.Args[1]) page, err = strconv.Atoi(ce.Args[1])
// if err != nil || page <= 0 { if err != nil || page <= 0 {
// ce.Reply("\"%s\" isn't a valid page number", ce.Args[1]) ce.Reply("\"%s\" isn't a valid page number", ce.Args[1])
// return return
// } }
// } }
// if len(ce.Args) > 2 { if len(ce.Args) > 2 {
// max, err = strconv.Atoi(ce.Args[2]) max, err = strconv.Atoi(ce.Args[2])
// if err != nil || max <= 0 { if err != nil || max <= 0 {
// ce.Reply("\"%s\" isn't a valid number of items per page", ce.Args[2]) ce.Reply("\"%s\" isn't a valid number of items per page", ce.Args[2])
// return return
// } else if max > 400 { } else if max > 400 {
// ce.Reply("Warning: a high number of items per page may fail to send a reply") ce.Reply("Warning: a high number of items per page may fail to send a reply")
// } }
// } }
// contacts := mode[0] == 'c' contacts := mode[0] == 'c'
// typeName := "Groups" typeName := "Groups"
// if contacts { if contacts {
// typeName = "Contacts" typeName = "Contacts"
// } }
// result := formatContacts(contacts, ce.User.Conn.Store.Contacts)
// if len(result) == 0 { //real deal
// ce.Reply("No %s found", strings.ToLower(typeName)) v := make(map[types.GroupMeID]string)
// return
// } if contacts {
// pages := int(math.Ceil(float64(len(result)) / float64(max))) for i, j := range ce.User.ChatList {
// if (page-1)*max >= len(result) { v[i] = j.OtherUser.Name
// if pages == 1 { }
// ce.Reply("There is only 1 page of %s", strings.ToLower(typeName)) } else {
// } else { for i, j := range ce.User.GroupList {
// ce.Reply("There are only %d pages of %s", pages, strings.ToLower(typeName)) v[i] = j.Name
// } }
// return
// } }
// lastIndex := page * max result := formatContacts(contacts, v)
// if lastIndex > len(result) {
// lastIndex = len(result) if len(result) == 0 {
// } ce.Reply("No %s found", strings.ToLower(typeName))
// result = result[(page-1)*max : lastIndex] return
// ce.Reply("### %s (page %d of %d)\n\n%s", typeName, page, pages, strings.Join(result, "\n")) }
pages := int(math.Ceil(float64(len(result)) / float64(max)))
if (page-1)*max >= len(result) {
if pages == 1 {
ce.Reply("There is only 1 page of %s", strings.ToLower(typeName))
} else {
ce.Reply("There are only %d pages of %s", pages, strings.ToLower(typeName))
}
return
}
lastIndex := page * max
if lastIndex > len(result) {
lastIndex = len(result)
}
result = result[(page-1)*max : lastIndex]
ce.Reply("### %s (page %d of %d)\n\n%s", typeName, page, pages, strings.Join(result, "\n"))
} }
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) {
// if len(ce.Args) == 0 { if len(ce.Args) == 0 {
// ce.Reply("**Usage:** `open <group JID>`") ce.Reply("**Usage:** `open <group JID>`")
// return return
// } }
// user := ce.User user := ce.User
// jid := ce.Args[0] jid := ce.Args[0]
// if strings.HasSuffix(jid, whatsappExt.NewUserSuffix) { if strings.HasSuffix(jid, whatsappExt.NewUserSuffix) {
// ce.Reply("That looks like a user JID. Did you mean `pm %s`?", jid[:len(jid)-len(whatsappExt.NewUserSuffix)]) ce.Reply("That looks like a user JID. Did you mean `pm %s`?", jid[:len(jid)-len(whatsappExt.NewUserSuffix)])
// return return
// } }
// contact, ok := user.Conn.Store.Contacts[jid] contact, ok := user.GroupList[jid]
// if !ok { if !ok {
// ce.Reply("Group JID not found in contacts. Try syncing contacts with `sync` first.") ce.Reply("Group JID not found in contacts. Try syncing contacts with `sync` first.")
// return return
// } }
// handler.log.Debugln("Importing", jid, "for", user) handler.log.Debugln("Importing", jid, "for", user)
// portal := user.bridge.GetPortalByJID(database.GroupPortalKey(jid)) portal := user.bridge.GetPortalByJID(database.GroupPortalKey(jid))
// if len(portal.MXID) > 0 { if len(portal.MXID) > 0 {
// portal.Sync(user, contact) portal.Sync(user, contact)
// ce.Reply("Portal room synced.") ce.Reply("Portal room synced.")
// } else { } else {
// portal.Sync(user, contact) portal.Sync(user, contact)
// ce.Reply("Portal room created.") ce.Reply("Portal room created.")
// } }
// _, _ = portal.MainIntent().InviteUser(portal.MXID, &mautrix.ReqInviteUser{UserID: user.MXID}) _, _ = portal.MainIntent().InviteUser(portal.MXID, &mautrix.ReqInviteUser{UserID: user.MXID})
} }
const cmdPMHelp = `pm [--force] <_international phone number_> - Open a private chat with the given phone number.` const cmdPMHelp = `pm [--force] <_international phone number_> - Open a private chat with the given phone number.`

View File

@ -58,6 +58,9 @@ type User struct {
ConnectionErrors int ConnectionErrors int
CommunityID string CommunityID string
ChatList map[types.GroupMeID]groupme.Chat
GroupList map[types.GroupMeID]groupme.Group
cleanDisconnection bool cleanDisconnection bool
batteryWarningsSent int batteryWarningsSent int
lastReconnection int64 lastReconnection int64
@ -535,6 +538,7 @@ func (user *User) HandleChatList() {
} }
user.chatListReceived <- struct{}{} user.chatListReceived <- struct{}{}
user.log.Infoln("Chat list received") user.log.Infoln("Chat list received")
user.GroupList = chatMap
go user.syncPortals(chatMap, false) go user.syncPortals(chatMap, false)
} }