2022-03-24 21:21:23 +01:00
|
|
|
// mautrix-whatsapp - A Matrix-WhatsApp puppeting bridge.
|
|
|
|
// Copyright (C) 2021 Tulir Asokan, Sumner Evans
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
log "maunium.net/go/maulogger/v2"
|
|
|
|
"maunium.net/go/mautrix-whatsapp/database"
|
|
|
|
)
|
|
|
|
|
|
|
|
type BackfillQueue struct {
|
|
|
|
BackfillQuery *database.BackfillQuery
|
|
|
|
ImmediateBackfillRequests chan *database.Backfill
|
|
|
|
DeferredBackfillRequests chan *database.Backfill
|
|
|
|
ReCheckQueue chan bool
|
|
|
|
|
|
|
|
log log.Logger
|
|
|
|
}
|
|
|
|
|
2022-04-28 23:37:51 +02:00
|
|
|
// Immediate backfills should happen first, then deferred backfills and lastly
|
|
|
|
// media backfills.
|
|
|
|
func (bq *BackfillQueue) RunLoop(user *User) {
|
2022-03-24 21:21:23 +01:00
|
|
|
for {
|
|
|
|
if immediate := bq.BackfillQuery.GetNext(user.MXID, database.BackfillImmediate); immediate != nil {
|
2022-04-28 23:37:51 +02:00
|
|
|
bq.ImmediateBackfillRequests <- immediate
|
|
|
|
immediate.MarkDone()
|
2022-04-19 04:50:21 +02:00
|
|
|
} else if backfill := bq.BackfillQuery.GetNext(user.MXID, database.BackfillDeferred); backfill != nil {
|
2022-03-24 21:21:23 +01:00
|
|
|
bq.DeferredBackfillRequests <- backfill
|
2022-04-05 21:12:57 +02:00
|
|
|
backfill.MarkDone()
|
2022-04-19 04:50:21 +02:00
|
|
|
} else if mediaBackfill := bq.BackfillQuery.GetNext(user.MXID, database.BackfillMedia); mediaBackfill != nil {
|
|
|
|
bq.DeferredBackfillRequests <- mediaBackfill
|
|
|
|
mediaBackfill.MarkDone()
|
2022-03-24 21:21:23 +01:00
|
|
|
} else {
|
2022-04-28 20:23:34 +02:00
|
|
|
select {
|
|
|
|
case <-bq.ReCheckQueue:
|
|
|
|
case <-time.After(time.Minute):
|
|
|
|
}
|
2022-03-24 21:21:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|