2018-08-13 22:24:44 +02:00
|
|
|
// mautrix-whatsapp - A Matrix-WhatsApp puppeting bridge.
|
2022-02-23 13:30:21 +01:00
|
|
|
// Copyright (C) 2022 Tulir Asokan
|
2018-08-13 22:24:44 +02:00
|
|
|
//
|
|
|
|
// 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 database
|
|
|
|
|
|
|
|
import (
|
2022-04-29 18:38:44 +02:00
|
|
|
"errors"
|
|
|
|
"net"
|
2022-02-23 13:30:21 +01:00
|
|
|
"time"
|
2018-08-26 21:53:13 +02:00
|
|
|
|
2021-10-26 16:01:10 +02:00
|
|
|
"github.com/lib/pq"
|
2019-03-06 22:57:38 +01:00
|
|
|
_ "github.com/mattn/go-sqlite3"
|
2023-08-05 17:59:17 +02:00
|
|
|
"go.mau.fi/util/dbutil"
|
2022-04-29 18:38:44 +02:00
|
|
|
"go.mau.fi/whatsmeow/store"
|
|
|
|
"go.mau.fi/whatsmeow/store/sqlstore"
|
2022-08-14 18:26:42 +02:00
|
|
|
"maunium.net/go/maulogger/v2"
|
2022-04-29 18:38:44 +02:00
|
|
|
|
2019-05-16 19:14:32 +02:00
|
|
|
"maunium.net/go/mautrix-whatsapp/database/upgrades"
|
2018-08-13 22:24:44 +02:00
|
|
|
)
|
|
|
|
|
2021-10-26 16:01:10 +02:00
|
|
|
func init() {
|
|
|
|
sqlstore.PostgresArrayWrapper = pq.Array
|
|
|
|
}
|
|
|
|
|
2018-08-13 22:24:44 +02:00
|
|
|
type Database struct {
|
2022-05-22 00:06:30 +02:00
|
|
|
*dbutil.Database
|
2018-08-13 22:24:44 +02:00
|
|
|
|
2022-03-05 20:22:31 +01:00
|
|
|
User *UserQuery
|
|
|
|
Portal *PortalQuery
|
|
|
|
Puppet *PuppetQuery
|
|
|
|
Message *MessageQuery
|
|
|
|
Reaction *ReactionQuery
|
2022-01-07 13:32:00 +01:00
|
|
|
|
2022-05-10 22:28:30 +02:00
|
|
|
DisappearingMessage *DisappearingMessageQuery
|
|
|
|
Backfill *BackfillQuery
|
|
|
|
HistorySync *HistorySyncQuery
|
|
|
|
MediaBackfillRequest *MediaBackfillRequestQuery
|
2018-08-13 22:24:44 +02:00
|
|
|
}
|
|
|
|
|
2022-08-14 18:26:42 +02:00
|
|
|
func New(baseDB *dbutil.Database, log maulogger.Logger) *Database {
|
2022-05-22 00:06:30 +02:00
|
|
|
db := &Database{Database: baseDB}
|
|
|
|
db.UpgradeTable = upgrades.Table
|
2018-08-13 22:24:44 +02:00
|
|
|
db.User = &UserQuery{
|
|
|
|
db: db,
|
2022-08-14 18:26:42 +02:00
|
|
|
log: log.Sub("User"),
|
2018-08-13 22:24:44 +02:00
|
|
|
}
|
2018-08-16 14:59:18 +02:00
|
|
|
db.Portal = &PortalQuery{
|
|
|
|
db: db,
|
2022-08-14 18:26:42 +02:00
|
|
|
log: log.Sub("Portal"),
|
2018-08-16 14:59:18 +02:00
|
|
|
}
|
|
|
|
db.Puppet = &PuppetQuery{
|
|
|
|
db: db,
|
2022-08-14 18:26:42 +02:00
|
|
|
log: log.Sub("Puppet"),
|
2018-08-16 14:59:18 +02:00
|
|
|
}
|
2018-08-23 00:12:26 +02:00
|
|
|
db.Message = &MessageQuery{
|
|
|
|
db: db,
|
2022-08-14 18:26:42 +02:00
|
|
|
log: log.Sub("Message"),
|
2018-08-23 00:12:26 +02:00
|
|
|
}
|
2022-03-05 20:22:31 +01:00
|
|
|
db.Reaction = &ReactionQuery{
|
|
|
|
db: db,
|
2022-08-14 18:26:42 +02:00
|
|
|
log: log.Sub("Reaction"),
|
2022-03-05 20:22:31 +01:00
|
|
|
}
|
2022-01-07 13:32:00 +01:00
|
|
|
db.DisappearingMessage = &DisappearingMessageQuery{
|
|
|
|
db: db,
|
2022-08-14 18:26:42 +02:00
|
|
|
log: log.Sub("DisappearingMessage"),
|
2022-01-07 13:32:00 +01:00
|
|
|
}
|
2022-05-10 22:28:30 +02:00
|
|
|
db.Backfill = &BackfillQuery{
|
2022-03-24 21:14:08 +01:00
|
|
|
db: db,
|
2022-08-14 18:26:42 +02:00
|
|
|
log: log.Sub("Backfill"),
|
2022-03-24 21:14:08 +01:00
|
|
|
}
|
2022-05-10 22:28:30 +02:00
|
|
|
db.HistorySync = &HistorySyncQuery{
|
2022-03-24 21:14:08 +01:00
|
|
|
db: db,
|
2022-08-14 18:26:42 +02:00
|
|
|
log: log.Sub("HistorySync"),
|
2022-03-24 21:14:08 +01:00
|
|
|
}
|
2022-05-10 22:28:30 +02:00
|
|
|
db.MediaBackfillRequest = &MediaBackfillRequestQuery{
|
|
|
|
db: db,
|
2022-08-14 18:26:42 +02:00
|
|
|
log: log.Sub("MediaBackfillRequest"),
|
2022-02-23 13:30:21 +01:00
|
|
|
}
|
2022-05-22 00:06:30 +02:00
|
|
|
return db
|
2018-08-13 22:24:44 +02:00
|
|
|
}
|
2022-04-29 18:38:44 +02:00
|
|
|
|
|
|
|
func isRetryableError(err error) bool {
|
|
|
|
if pqError := (&pq.Error{}); errors.As(err, &pqError) {
|
|
|
|
switch pqError.Code.Class() {
|
|
|
|
case "08", // Connection Exception
|
|
|
|
"53", // Insufficient Resources (e.g. too many connections)
|
|
|
|
"57": // Operator Intervention (e.g. server restart)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
} else if netError := (&net.OpError{}); errors.As(err, &netError) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *Database) HandleSignalStoreError(device *store.Device, action string, attemptIndex int, err error) (retry bool) {
|
2022-05-22 00:06:30 +02:00
|
|
|
if db.Dialect != dbutil.SQLite && isRetryableError(err) {
|
2022-04-29 18:38:44 +02:00
|
|
|
sleepTime := time.Duration(attemptIndex*2) * time.Second
|
|
|
|
device.Log.Warnf("Failed to %s (attempt #%d): %v - retrying in %v", action, attemptIndex+1, err, sleepTime)
|
|
|
|
time.Sleep(sleepTime)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
device.Log.Errorf("Failed to %s: %v", action, err)
|
|
|
|
return false
|
|
|
|
}
|