commands/backfill: add command to manually backfill a portal

This commit is contained in:
Sumner Evans 2022-03-24 14:21:57 -06:00
parent 12a23e2ca5
commit e702bf39d2
No known key found for this signature in database
GPG key ID: 8904527AB50022FD

View file

@ -31,6 +31,7 @@ import (
"github.com/tidwall/gjson" "github.com/tidwall/gjson"
"maunium.net/go/maulogger/v2" "maunium.net/go/maulogger/v2"
"maunium.net/go/mautrix-whatsapp/database"
"go.mau.fi/whatsmeow" "go.mau.fi/whatsmeow"
"go.mau.fi/whatsmeow/appstate" "go.mau.fi/whatsmeow/appstate"
@ -140,7 +141,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", "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() { 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
@ -176,6 +177,8 @@ func (handler *CommandHandler) CommandMux(ce *CommandEvent) {
handler.CommandCreate(ce) handler.CommandCreate(ce)
case "accept": case "accept":
handler.CommandAccept(ce) handler.CommandAccept(ce)
case "backfill":
handler.CommandBackfill(ce)
} }
default: default:
ce.Reply("Unknown command, use the `help` command for help.") 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.removeFromJIDMap(BridgeState{StateEvent: StateLoggedOut})
ce.User.DeleteConnection() ce.User.DeleteConnection()
ce.User.DeleteSession() 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.") ce.Reply("Logged out successfully.")
} }
@ -745,6 +751,7 @@ func (handler *CommandHandler) CommandHelp(ce *CommandEvent) {
cmdPrefix + cmdSetPowerLevelHelp, cmdPrefix + cmdSetPowerLevelHelp,
cmdPrefix + cmdDeletePortalHelp, cmdPrefix + cmdDeletePortalHelp,
cmdPrefix + cmdDeleteAllPortalsHelp, cmdPrefix + cmdDeleteAllPortalsHelp,
cmdPrefix + cmdBackfillHelp,
}, "\n* ")) }, "\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 <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 matchesQuery(str string, query string) bool { func matchesQuery(str string, query string) bool {