diff --git a/commands.go b/commands.go index 7921082..5a90f31 100644 --- a/commands.go +++ b/commands.go @@ -96,6 +96,8 @@ func (handler *CommandHandler) Handle(roomID types.MatrixRoomID, user *User, mes handler.CommandDeleteSession(ce) case "delete-portal": handler.CommandDeletePortal(ce) + case "delete-all-portals": + handler.CommandDeleteAllPortals(ce) case "login-matrix", "logout", "sync", "list", "open", "pm": if ce.User.Conn == nil { ce.Reply("You are not logged in. Use the `login` command to log into WhatsApp.") @@ -290,20 +292,22 @@ func (handler *CommandHandler) CommandHelp(ce *CommandEvent) { }, "\n* ")) } -const cmdSyncHelp = `sync [--create] - Synchronize contacts from phone and optionally create portals for group chats.` +const cmdSyncHelp = `sync [--create-all] - Synchronize contacts from phone and optionally create portals for group chats.` // CommandSync handles sync command func (handler *CommandHandler) CommandSync(ce *CommandEvent) { user := ce.User - create := len(ce.Args) > 0 && ce.Args[0] == "--create" + create := len(ce.Args) > 0 && ce.Args[0] == "--create-all" - handler.log.Debugln("Importing all contacts of", user) + ce.Reply("Updating contact and chat list...") + handler.log.Debugln("Importing contacts of", user.MXID) _, err := user.Conn.Contacts() if err != nil { user.log.Errorln("Error updating contacts:", err) ce.Reply("Failed to sync contact list (see logs for details)") return } + handler.log.Debugln("Importing chats of", user.MXID) _, err = user.Conn.Chats() if err != nil { user.log.Errorln("Error updating chats:", err) @@ -311,10 +315,12 @@ func (handler *CommandHandler) CommandSync(ce *CommandEvent) { return } + ce.Reply("Syncing contacts...") user.syncPuppets() + ce.Reply("Syncing chats...") user.syncPortals(create) - ce.Reply("Imported contacts successfully.") + ce.Reply("Sync complete.") } func (handler *CommandHandler) CommandDeletePortal(ce *CommandEvent) { @@ -334,6 +340,49 @@ func (handler *CommandHandler) CommandDeletePortal(ce *CommandEvent) { portal.Cleanup(false) } +func (handler *CommandHandler) CommandDeleteAllPortals(ce *CommandEvent) { + portals := ce.User.GetPortals() + portalsToDelete := make([]*Portal, 0, len(portals)) + for _, portal := range portals { + users := portal.GetUserIDs() + if len(users) == 1 && users[0] == ce.User.MXID { + portalsToDelete = append(portalsToDelete, portal) + } + } + leave := func(portal *Portal) { + if len(portal.MXID) > 0 { + _, _ = portal.MainIntent().KickUser(portal.MXID, &mautrix.ReqKickUser{ + Reason: "Deleting portal", + UserID: ce.User.MXID, + }) + } + } + customPuppet := handler.bridge.GetPuppetByCustomMXID(ce.User.MXID) + if customPuppet != nil { + intent := customPuppet.CustomIntent() + leave = func(portal *Portal) { + if len(portal.MXID) > 0 { + _, _ = intent.LeaveRoom(portal.MXID) + _, _ = intent.ForgetRoom(portal.MXID) + } + } + } + ce.Reply("Found %d portals with no other users, deleting...", len(portalsToDelete)) + for _, portal := range portalsToDelete { + portal.Delete() + leave(portal) + } + ce.Reply("Finished deleting portal info. Now cleaning up rooms in background. " + + "You may already continue using the bridge. Use `sync` to recreate portals.") + + go func() { + for _, portal := range portalsToDelete { + portal.Cleanup(false) + } + ce.Reply("Finished background cleanup of deleted portal rooms.") + }() +} + const cmdListHelp = `list - Get a list of all contacts and groups.` func (handler *CommandHandler) CommandList(ce *CommandEvent) { diff --git a/portal.go b/portal.go index c692226..9ee4c98 100644 --- a/portal.go +++ b/portal.go @@ -416,14 +416,12 @@ func (portal *Portal) ensureUserInvited(user *User) { } func (portal *Portal) Sync(user *User, contact whatsapp.Contact) { - if portal.IsPrivateChat() { - portal.ensureUserInvited(user) - return - } portal.log.Infoln("Syncing portal for", user.MXID) if len(portal.MXID) == 0 { - portal.Name = contact.Name + if !portal.IsPrivateChat() { + portal.Name = contact.Name + } err := portal.CreateMatrixRoom(user) if err != nil { portal.log.Errorln("Failed to create portal room:", err) @@ -433,6 +431,10 @@ func (portal *Portal) Sync(user *User, contact whatsapp.Contact) { portal.ensureUserInvited(user) } + if portal.IsPrivateChat() { + return + } + update := false update = portal.UpdateMetadata(user) || update if !portal.IsStatusBroadcastRoom() {