Add command to sync DM rooms into space

This commit is contained in:
Tulir Asokan 2021-12-29 21:40:58 +02:00
parent 7e5c2769c6
commit ba536f17e7
2 changed files with 38 additions and 2 deletions

View file

@ -1072,13 +1072,14 @@ const cmdSyncHelp = `sync <appstate/contacts/groups> [--create-portals] - Synchr
func (handler *CommandHandler) CommandSync(ce *CommandEvent) { func (handler *CommandHandler) CommandSync(ce *CommandEvent) {
if len(ce.Args) == 0 { if len(ce.Args) == 0 {
ce.Reply("**Usage:** `sync <appstate/contacts/groups> [--create-portals]`") ce.Reply("**Usage:** `sync <appstate/contacts/groups/space> [--create-portals]`")
return return
} }
args := strings.ToLower(strings.Join(ce.Args, " ")) args := strings.ToLower(strings.Join(ce.Args, " "))
contacts := strings.Contains(args, "contacts") contacts := strings.Contains(args, "contacts")
appState := strings.Contains(args, "appstate") appState := strings.Contains(args, "appstate")
groups := strings.Contains(args, "groups") space := strings.Contains(args, "space")
groups := strings.Contains(args, "groups") || space
createPortals := strings.Contains(args, "--create-portals") createPortals := strings.Contains(args, "--create-portals")
if appState { if appState {
@ -1100,6 +1101,20 @@ func (handler *CommandHandler) CommandSync(ce *CommandEvent) {
ce.Reply("Resynced contacts") ce.Reply("Resynced contacts")
} }
} }
if space {
keys := ce.Bridge.DB.Portal.FindPrivateChatsNotInSpace(ce.User.JID)
count := 0
for _, key := range keys {
portal := ce.Bridge.GetPortalByJID(key)
portal.addToSpace(ce.User)
count++
}
plural := "s"
if count == 1 {
plural = ""
}
ce.Reply("Added %d DM room%s to space", count, plural)
}
if groups { if groups {
err := ce.User.ResyncGroups(createPortals) err := ce.User.ResyncGroups(createPortals)
if err != nil { if err != nil {

View file

@ -82,6 +82,27 @@ func (pq *PortalQuery) FindPrivateChats(receiver types.JID) []*Portal {
return pq.getAll("SELECT * FROM portal WHERE receiver=$1 AND jid LIKE '%@s.whatsapp.net'", receiver.ToNonAD()) return pq.getAll("SELECT * FROM portal WHERE receiver=$1 AND jid LIKE '%@s.whatsapp.net'", receiver.ToNonAD())
} }
func (pq *PortalQuery) FindPrivateChatsNotInSpace(receiver types.JID) (keys []PortalKey) {
receiver = receiver.ToNonAD()
rows, err := pq.db.Query(`
SELECT jid FROM portal
LEFT JOIN user_portal ON portal.jid=user_portal.portal_jid AND portal.receiver=user_portal.portal_receiver
WHERE mxid<>'' AND receiver=$1 AND (in_space=false OR in_space IS NULL)
`, receiver)
if err != nil || rows == nil {
return
}
for rows.Next() {
var key PortalKey
key.Receiver = receiver
err = rows.Scan(&key.JID)
if err == nil {
keys = append(keys, key)
}
}
return
}
func (pq *PortalQuery) getAll(query string, args ...interface{}) (portals []*Portal) { func (pq *PortalQuery) getAll(query string, args ...interface{}) (portals []*Portal) {
rows, err := pq.db.Query(query, args...) rows, err := pq.db.Query(query, args...)
if err != nil || rows == nil { if err != nil || rows == nil {