database.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // mautrix-whatsapp - A Matrix-WhatsApp puppeting bridge.
  2. // Copyright (C) 2022 Tulir Asokan
  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 database
  17. import (
  18. "errors"
  19. "net"
  20. "time"
  21. "github.com/lib/pq"
  22. _ "github.com/mattn/go-sqlite3"
  23. "go.mau.fi/util/dbutil"
  24. "go.mau.fi/whatsmeow/store"
  25. "go.mau.fi/whatsmeow/store/sqlstore"
  26. "maunium.net/go/maulogger/v2"
  27. "maunium.net/go/mautrix-whatsapp/database/upgrades"
  28. )
  29. func init() {
  30. sqlstore.PostgresArrayWrapper = pq.Array
  31. }
  32. type Database struct {
  33. *dbutil.Database
  34. User *UserQuery
  35. Portal *PortalQuery
  36. Puppet *PuppetQuery
  37. Message *MessageQuery
  38. Reaction *ReactionQuery
  39. DisappearingMessage *DisappearingMessageQuery
  40. Backfill *BackfillQuery
  41. HistorySync *HistorySyncQuery
  42. MediaBackfillRequest *MediaBackfillRequestQuery
  43. }
  44. func New(baseDB *dbutil.Database, log maulogger.Logger) *Database {
  45. db := &Database{Database: baseDB}
  46. db.UpgradeTable = upgrades.Table
  47. db.User = &UserQuery{
  48. db: db,
  49. log: log.Sub("User"),
  50. }
  51. db.Portal = &PortalQuery{
  52. db: db,
  53. log: log.Sub("Portal"),
  54. }
  55. db.Puppet = &PuppetQuery{
  56. db: db,
  57. log: log.Sub("Puppet"),
  58. }
  59. db.Message = &MessageQuery{
  60. db: db,
  61. log: log.Sub("Message"),
  62. }
  63. db.Reaction = &ReactionQuery{
  64. db: db,
  65. log: log.Sub("Reaction"),
  66. }
  67. db.DisappearingMessage = &DisappearingMessageQuery{
  68. db: db,
  69. log: log.Sub("DisappearingMessage"),
  70. }
  71. db.Backfill = &BackfillQuery{
  72. db: db,
  73. log: log.Sub("Backfill"),
  74. }
  75. db.HistorySync = &HistorySyncQuery{
  76. db: db,
  77. log: log.Sub("HistorySync"),
  78. }
  79. db.MediaBackfillRequest = &MediaBackfillRequestQuery{
  80. db: db,
  81. log: log.Sub("MediaBackfillRequest"),
  82. }
  83. return db
  84. }
  85. func isRetryableError(err error) bool {
  86. if pqError := (&pq.Error{}); errors.As(err, &pqError) {
  87. switch pqError.Code.Class() {
  88. case "08", // Connection Exception
  89. "53", // Insufficient Resources (e.g. too many connections)
  90. "57": // Operator Intervention (e.g. server restart)
  91. return true
  92. }
  93. } else if netError := (&net.OpError{}); errors.As(err, &netError) {
  94. return true
  95. }
  96. return false
  97. }
  98. func (db *Database) HandleSignalStoreError(device *store.Device, action string, attemptIndex int, err error) (retry bool) {
  99. if db.Dialect != dbutil.SQLite && isRetryableError(err) {
  100. sleepTime := time.Duration(attemptIndex*2) * time.Second
  101. device.Log.Warnf("Failed to %s (attempt #%d): %v - retrying in %v", action, attemptIndex+1, err, sleepTime)
  102. time.Sleep(sleepTime)
  103. return true
  104. }
  105. device.Log.Errorf("Failed to %s: %v", action, err)
  106. return false
  107. }