diff --git a/commands.go b/commands.go index 6d9099d..709b0ff 100644 --- a/commands.go +++ b/commands.go @@ -31,6 +31,7 @@ import ( "github.com/tidwall/gjson" "maunium.net/go/maulogger/v2" + "maunium.net/go/mautrix-whatsapp/database" "go.mau.fi/whatsmeow" "go.mau.fi/whatsmeow/appstate" @@ -140,7 +141,7 @@ func (handler *CommandHandler) CommandMux(ce *CommandEvent) { handler.CommandLogout(ce) case "toggle": handler.CommandToggle(ce) - case "set-relay", "unset-relay", "login-matrix", "sync", "list", "search", "open", "pm", "invite-link", "resolve", "resolve-link", "join", "create", "accept": + case "set-relay", "unset-relay", "login-matrix", "sync", "list", "search", "open", "pm", "invite-link", "resolve", "resolve-link", "join", "create", "accept", "backfill": if !ce.User.HasSession() { ce.Reply("You are not logged in. Use the `login` command to log into WhatsApp.") return @@ -176,6 +177,8 @@ func (handler *CommandHandler) CommandMux(ce *CommandEvent) { handler.CommandCreate(ce) case "accept": handler.CommandAccept(ce) + case "backfill": + handler.CommandBackfill(ce) } default: ce.Reply("Unknown command, use the `help` command for help.") @@ -603,6 +606,9 @@ func (handler *CommandHandler) CommandLogout(ce *CommandEvent) { ce.User.removeFromJIDMap(BridgeState{StateEvent: StateLoggedOut}) ce.User.DeleteConnection() ce.User.DeleteSession() + ce.Bridge.DB.BackfillQuery.DeleteAll(ce.User.MXID) + ce.Bridge.DB.HistorySyncQuery.DeleteAllConversations(ce.User.MXID) + ce.Bridge.DB.HistorySyncQuery.DeleteAllMessages(ce.User.MXID) ce.Reply("Logged out successfully.") } @@ -745,6 +751,7 @@ func (handler *CommandHandler) CommandHelp(ce *CommandEvent) { cmdPrefix + cmdSetPowerLevelHelp, cmdPrefix + cmdDeletePortalHelp, cmdPrefix + cmdDeleteAllPortalsHelp, + cmdPrefix + cmdBackfillHelp, }, "\n* ")) } @@ -835,6 +842,43 @@ func (handler *CommandHandler) CommandDeleteAllPortals(ce *CommandEvent) { }() } +const cmdBackfillHelp = `backfill [batch size] [batch delay] - Backfill all messages the portal.` + +func (handler *CommandHandler) CommandBackfill(ce *CommandEvent) { + if ce.Portal == nil { + ce.Reply("This is not a portal room") + return + } + if !ce.Bridge.Config.Bridge.HistorySync.Backfill { + ce.Bot.SendMessageEvent(ce.RoomID, event.EventMessage, &event.MessageEventContent{ + MsgType: event.MsgNotice, + Body: "Backfill is not enabled for this bridge.", + }) + return + } + batchSize := 100 + batchDelay := 5 + if len(ce.Args) >= 1 { + var err error + batchSize, err = strconv.Atoi(ce.Args[0]) + if err != nil || batchSize < 1 { + ce.Reply("\"%s\" isn't a valid batch size", ce.Args[0]) + return + } + } + if len(ce.Args) >= 2 { + var err error + batchDelay, err = strconv.Atoi(ce.Args[0]) + if err != nil || batchSize < 0 { + ce.Reply("\"%s\" isn't a valid batch delay", ce.Args[1]) + return + } + } + backfill := ce.Portal.bridge.DB.BackfillQuery.NewWithValues(ce.User.MXID, database.BackfillImmediate, 0, &ce.Portal.Key, nil, nil, batchSize, -1, batchDelay) + backfill.Insert() + ce.User.BackfillQueue.ReCheckQueue <- true +} + const cmdListHelp = `list [page] [items per page] - Get a list of all contacts and groups.` func matchesQuery(str string, query string) bool {