backfillqueue.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // mautrix-whatsapp - A Matrix-WhatsApp puppeting bridge.
  2. // Copyright (C) 2021 Tulir Asokan, Sumner Evans
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. package main
  17. import (
  18. "time"
  19. log "maunium.net/go/maulogger/v2"
  20. "maunium.net/go/mautrix-whatsapp/database"
  21. )
  22. type BackfillQueue struct {
  23. BackfillQuery *database.BackfillQuery
  24. ImmediateBackfillRequests chan *database.Backfill
  25. DeferredBackfillRequests chan *database.Backfill
  26. ReCheckQueue chan bool
  27. log log.Logger
  28. }
  29. // Immediate backfills should happen first, then deferred backfills and lastly
  30. // media backfills.
  31. func (bq *BackfillQueue) RunLoop(user *User) {
  32. for {
  33. if immediate := bq.BackfillQuery.GetNext(user.MXID, database.BackfillImmediate); immediate != nil {
  34. bq.ImmediateBackfillRequests <- immediate
  35. immediate.MarkDone()
  36. } else if backfill := bq.BackfillQuery.GetNext(user.MXID, database.BackfillDeferred); backfill != nil {
  37. bq.DeferredBackfillRequests <- backfill
  38. backfill.MarkDone()
  39. } else if mediaBackfill := bq.BackfillQuery.GetNext(user.MXID, database.BackfillMedia); mediaBackfill != nil {
  40. bq.DeferredBackfillRequests <- mediaBackfill
  41. mediaBackfill.MarkDone()
  42. } else {
  43. select {
  44. case <-bq.ReCheckQueue:
  45. case <-time.After(time.Minute):
  46. }
  47. }
  48. }
  49. }