2018-08-16 14:59:18 +02:00
|
|
|
// mautrix-whatsapp - A Matrix-WhatsApp puppeting bridge.
|
2021-10-22 19:14:34 +02:00
|
|
|
// Copyright (C) 2021 Tulir Asokan
|
2018-08-16 14:59:18 +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 (
|
2018-08-26 21:53:13 +02:00
|
|
|
"database/sql"
|
2022-06-28 13:37:49 +02:00
|
|
|
"time"
|
2018-08-26 21:53:13 +02:00
|
|
|
|
2019-01-11 20:17:31 +01:00
|
|
|
log "maunium.net/go/maulogger/v2"
|
2022-05-22 00:06:30 +02:00
|
|
|
|
2021-02-17 00:21:30 +01:00
|
|
|
"maunium.net/go/mautrix/id"
|
2022-05-22 00:06:30 +02:00
|
|
|
"maunium.net/go/mautrix/util/dbutil"
|
2021-10-22 19:14:34 +02:00
|
|
|
|
|
|
|
"go.mau.fi/whatsmeow/types"
|
2018-08-16 14:59:18 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type PuppetQuery struct {
|
|
|
|
db *Database
|
2018-08-16 18:20:07 +02:00
|
|
|
log log.Logger
|
2018-08-16 14:59:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (pq *PuppetQuery) New() *Puppet {
|
|
|
|
return &Puppet{
|
|
|
|
db: pq.db,
|
|
|
|
log: pq.log,
|
2020-07-10 13:53:18 +02:00
|
|
|
|
|
|
|
EnablePresence: true,
|
2020-07-10 15:26:55 +02:00
|
|
|
EnableReceipts: true,
|
2018-08-16 14:59:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-28 23:40:54 +02:00
|
|
|
func (pq *PuppetQuery) GetAll() (puppets []*Puppet) {
|
2022-06-28 13:37:49 +02:00
|
|
|
rows, err := pq.db.Query("SELECT username, avatar, avatar_url, displayname, name_quality, name_set, avatar_set, last_sync, custom_mxid, access_token, next_batch, enable_presence, enable_receipts FROM puppet")
|
2018-08-16 14:59:18 +02:00
|
|
|
if err != nil || rows == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
for rows.Next() {
|
|
|
|
puppets = append(puppets, pq.New().Scan(rows))
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-10-22 19:14:34 +02:00
|
|
|
func (pq *PuppetQuery) Get(jid types.JID) *Puppet {
|
2022-06-28 13:37:49 +02:00
|
|
|
row := pq.db.QueryRow("SELECT username, avatar, avatar_url, displayname, name_quality, name_set, avatar_set, last_sync, custom_mxid, access_token, next_batch, enable_presence, enable_receipts FROM puppet WHERE username=$1", jid.User)
|
2018-08-16 14:59:18 +02:00
|
|
|
if row == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return pq.New().Scan(row)
|
|
|
|
}
|
|
|
|
|
2020-05-08 21:32:22 +02:00
|
|
|
func (pq *PuppetQuery) GetByCustomMXID(mxid id.UserID) *Puppet {
|
2022-06-28 13:37:49 +02:00
|
|
|
row := pq.db.QueryRow("SELECT username, avatar, avatar_url, displayname, name_quality, name_set, avatar_set, last_sync, custom_mxid, access_token, next_batch, enable_presence, enable_receipts FROM puppet WHERE custom_mxid=$1", mxid)
|
2019-05-24 01:33:26 +02:00
|
|
|
if row == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return pq.New().Scan(row)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pq *PuppetQuery) GetAllWithCustomMXID() (puppets []*Puppet) {
|
2022-06-28 13:37:49 +02:00
|
|
|
rows, err := pq.db.Query("SELECT username, avatar, avatar_url, displayname, name_quality, name_set, avatar_set, last_sync, custom_mxid, access_token, next_batch, enable_presence, enable_receipts FROM puppet WHERE custom_mxid<>''")
|
2019-05-24 01:33:26 +02:00
|
|
|
if err != nil || rows == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
for rows.Next() {
|
|
|
|
puppets = append(puppets, pq.New().Scan(rows))
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-08-16 14:59:18 +02:00
|
|
|
type Puppet struct {
|
|
|
|
db *Database
|
2018-08-16 18:20:07 +02:00
|
|
|
log log.Logger
|
2018-08-16 14:59:18 +02:00
|
|
|
|
2021-10-22 19:14:34 +02:00
|
|
|
JID types.JID
|
2018-08-16 14:59:18 +02:00
|
|
|
Avatar string
|
2020-05-08 21:32:22 +02:00
|
|
|
AvatarURL id.ContentURI
|
2022-06-28 13:37:49 +02:00
|
|
|
AvatarSet bool
|
2018-09-01 22:38:03 +02:00
|
|
|
Displayname string
|
|
|
|
NameQuality int8
|
2022-06-28 13:37:49 +02:00
|
|
|
NameSet bool
|
|
|
|
LastSync time.Time
|
2019-05-24 01:33:26 +02:00
|
|
|
|
2020-07-10 13:53:18 +02:00
|
|
|
CustomMXID id.UserID
|
|
|
|
AccessToken string
|
|
|
|
NextBatch string
|
|
|
|
EnablePresence bool
|
2020-07-10 15:26:55 +02:00
|
|
|
EnableReceipts bool
|
2018-08-16 14:59:18 +02:00
|
|
|
}
|
|
|
|
|
2022-05-22 00:06:30 +02:00
|
|
|
func (puppet *Puppet) Scan(row dbutil.Scannable) *Puppet {
|
2019-06-01 19:03:29 +02:00
|
|
|
var displayname, avatar, avatarURL, customMXID, accessToken, nextBatch sql.NullString
|
2022-06-28 13:37:49 +02:00
|
|
|
var quality, lastSync sql.NullInt64
|
|
|
|
var enablePresence, enableReceipts, nameSet, avatarSet sql.NullBool
|
2021-10-22 19:14:34 +02:00
|
|
|
var username string
|
2022-06-28 13:37:49 +02:00
|
|
|
err := row.Scan(&username, &avatar, &avatarURL, &displayname, &quality, &nameSet, &avatarSet, &lastSync, &customMXID, &accessToken, &nextBatch, &enablePresence, &enableReceipts)
|
2018-08-16 14:59:18 +02:00
|
|
|
if err != nil {
|
2018-08-18 21:57:08 +02:00
|
|
|
if err != sql.ErrNoRows {
|
2018-08-26 00:55:21 +02:00
|
|
|
puppet.log.Errorln("Database scan failed:", err)
|
2018-08-18 21:57:08 +02:00
|
|
|
}
|
|
|
|
return nil
|
2018-08-16 14:59:18 +02:00
|
|
|
}
|
2021-10-22 19:14:34 +02:00
|
|
|
puppet.JID = types.NewJID(username, types.DefaultUserServer)
|
2018-08-30 00:10:26 +02:00
|
|
|
puppet.Displayname = displayname.String
|
|
|
|
puppet.Avatar = avatar.String
|
2020-05-08 21:32:22 +02:00
|
|
|
puppet.AvatarURL, _ = id.ParseContentURI(avatarURL.String)
|
2018-09-01 23:44:10 +02:00
|
|
|
puppet.NameQuality = int8(quality.Int64)
|
2022-06-28 13:37:49 +02:00
|
|
|
puppet.NameSet = nameSet.Bool
|
|
|
|
puppet.AvatarSet = avatarSet.Bool
|
|
|
|
if lastSync.Int64 > 0 {
|
|
|
|
puppet.LastSync = time.Unix(lastSync.Int64, 0)
|
|
|
|
}
|
2020-05-08 21:32:22 +02:00
|
|
|
puppet.CustomMXID = id.UserID(customMXID.String)
|
2019-05-24 01:33:26 +02:00
|
|
|
puppet.AccessToken = accessToken.String
|
|
|
|
puppet.NextBatch = nextBatch.String
|
2020-07-10 13:53:18 +02:00
|
|
|
puppet.EnablePresence = enablePresence.Bool
|
2020-07-10 15:26:55 +02:00
|
|
|
puppet.EnableReceipts = enableReceipts.Bool
|
2018-08-16 14:59:18 +02:00
|
|
|
return puppet
|
|
|
|
}
|
|
|
|
|
2019-01-21 22:55:16 +01:00
|
|
|
func (puppet *Puppet) Insert() {
|
2021-10-22 19:14:34 +02:00
|
|
|
if puppet.JID.Server != types.DefaultUserServer {
|
|
|
|
puppet.log.Warnfln("Not inserting %s: not a user", puppet.JID)
|
|
|
|
return
|
|
|
|
}
|
2022-06-28 13:37:49 +02:00
|
|
|
var lastSyncTs int64
|
|
|
|
if !puppet.LastSync.IsZero() {
|
|
|
|
lastSyncTs = puppet.LastSync.Unix()
|
|
|
|
}
|
|
|
|
_, err := puppet.db.Exec(`
|
|
|
|
INSERT INTO puppet (username, avatar, avatar_url, avatar_set, displayname, name_quality, name_set, last_sync,
|
|
|
|
custom_mxid, access_token, next_batch, enable_presence, enable_receipts)
|
|
|
|
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13)
|
|
|
|
`, puppet.JID.User, puppet.Avatar, puppet.AvatarURL.String(), puppet.AvatarSet, puppet.Displayname,
|
|
|
|
puppet.NameQuality, puppet.NameSet, lastSyncTs, puppet.CustomMXID, puppet.AccessToken, puppet.NextBatch,
|
|
|
|
puppet.EnablePresence, puppet.EnableReceipts,
|
|
|
|
)
|
2018-08-18 21:57:08 +02:00
|
|
|
if err != nil {
|
2018-09-01 22:38:03 +02:00
|
|
|
puppet.log.Warnfln("Failed to insert %s: %v", puppet.JID, err)
|
2018-08-18 21:57:08 +02:00
|
|
|
}
|
2018-08-16 14:59:18 +02:00
|
|
|
}
|
|
|
|
|
2019-01-21 22:55:16 +01:00
|
|
|
func (puppet *Puppet) Update() {
|
2022-06-28 13:37:49 +02:00
|
|
|
var lastSyncTs int64
|
|
|
|
if !puppet.LastSync.IsZero() {
|
|
|
|
lastSyncTs = puppet.LastSync.Unix()
|
|
|
|
}
|
|
|
|
_, err := puppet.db.Exec(`
|
|
|
|
UPDATE puppet
|
|
|
|
SET displayname=$1, name_quality=$2, name_set=$3, avatar=$4, avatar_url=$5, avatar_set=$6, last_sync=$7,
|
|
|
|
custom_mxid=$8, access_token=$9, next_batch=$10, enable_presence=$11, enable_receipts=$12
|
|
|
|
WHERE username=$13
|
|
|
|
`, puppet.Displayname, puppet.NameQuality, puppet.NameSet, puppet.Avatar, puppet.AvatarURL.String(), puppet.AvatarSet,
|
|
|
|
lastSyncTs, puppet.CustomMXID, puppet.AccessToken, puppet.NextBatch, puppet.EnablePresence, puppet.EnableReceipts,
|
|
|
|
puppet.JID.User)
|
2018-08-18 21:57:08 +02:00
|
|
|
if err != nil {
|
2021-11-05 11:17:56 +01:00
|
|
|
puppet.log.Warnfln("Failed to update %s: %v", puppet.JID, err)
|
2018-08-18 21:57:08 +02:00
|
|
|
}
|
2018-08-16 14:59:18 +02:00
|
|
|
}
|