2023-07-02 14:21:55 +00:00
|
|
|
// mautrix-gmessages - A Matrix-Google Messages puppeting bridge.
|
2024-02-24 22:18:06 +00:00
|
|
|
// Copyright (C) 2024 Tulir Asokan
|
2023-07-02 14:21:55 +00: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 (
|
|
|
|
"context"
|
2023-09-04 22:18:01 +00:00
|
|
|
"time"
|
2023-07-02 14:21:55 +00:00
|
|
|
|
2023-08-08 13:13:44 +00:00
|
|
|
"go.mau.fi/util/dbutil"
|
2023-07-02 14:21:55 +00:00
|
|
|
"maunium.net/go/mautrix/id"
|
|
|
|
)
|
|
|
|
|
|
|
|
type PuppetQuery struct {
|
2024-02-24 22:18:06 +00:00
|
|
|
*dbutil.QueryHelper[*Puppet]
|
2023-07-02 14:21:55 +00:00
|
|
|
}
|
|
|
|
|
2024-02-24 22:18:06 +00:00
|
|
|
func newPuppet(qh *dbutil.QueryHelper[*Puppet]) *Puppet {
|
|
|
|
return &Puppet{qh: qh}
|
2023-07-02 14:21:55 +00:00
|
|
|
}
|
|
|
|
|
2023-09-04 22:46:56 +00:00
|
|
|
const (
|
|
|
|
deleteAllPuppetsForUserQuery = "DELETE FROM puppet WHERE receiver=$1"
|
|
|
|
getPuppetQuery = "SELECT id, receiver, phone, contact_id, name, name_set, avatar_hash, avatar_mxc, avatar_set, avatar_update_ts, contact_info_set FROM puppet WHERE id=$1 AND receiver=$2"
|
|
|
|
insertPuppetQuery = `
|
|
|
|
INSERT INTO puppet (id, receiver, phone, contact_id, name, name_set, avatar_hash, avatar_mxc, avatar_set, avatar_update_ts, contact_info_set)
|
|
|
|
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)
|
|
|
|
`
|
|
|
|
updatePuppetQuery = `
|
|
|
|
UPDATE puppet
|
|
|
|
SET phone=$3, contact_id=$4, name=$5, name_set=$6, avatar_hash=$7, avatar_mxc=$8, avatar_set=$9, avatar_update_ts=$10, contact_info_set=$11
|
|
|
|
WHERE id=$1 AND receiver=$2
|
|
|
|
`
|
|
|
|
)
|
|
|
|
|
2023-08-10 12:55:33 +00:00
|
|
|
func (pq *PuppetQuery) DeleteAllForUser(ctx context.Context, userID int) error {
|
2024-02-24 22:18:06 +00:00
|
|
|
return pq.Exec(ctx, deleteAllPuppetsForUserQuery, userID)
|
2023-08-10 12:55:33 +00:00
|
|
|
}
|
|
|
|
|
2023-07-02 14:21:55 +00:00
|
|
|
func (pq *PuppetQuery) Get(ctx context.Context, key Key) (*Puppet, error) {
|
2024-02-24 22:18:06 +00:00
|
|
|
return pq.QueryOne(ctx, getPuppetQuery, key.ID, key.Receiver)
|
2023-07-02 14:21:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Puppet struct {
|
2024-02-24 22:18:06 +00:00
|
|
|
qh *dbutil.QueryHelper[*Puppet]
|
2023-07-02 14:21:55 +00:00
|
|
|
|
|
|
|
Key
|
|
|
|
Phone string
|
2023-09-04 22:18:01 +00:00
|
|
|
ContactID string
|
2023-07-02 14:21:55 +00:00
|
|
|
Name string
|
|
|
|
NameSet bool
|
2023-09-04 22:18:01 +00:00
|
|
|
AvatarHash [32]byte
|
2023-07-02 14:21:55 +00:00
|
|
|
AvatarMXC id.ContentURI
|
|
|
|
AvatarSet bool
|
2023-09-04 22:18:01 +00:00
|
|
|
AvatarUpdateTS time.Time
|
2023-07-02 14:21:55 +00:00
|
|
|
ContactInfoSet bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (puppet *Puppet) Scan(row dbutil.Scannable) (*Puppet, error) {
|
2023-09-04 22:18:01 +00:00
|
|
|
var avatarHash []byte
|
|
|
|
var avatarUpdateTS int64
|
2024-02-24 22:18:06 +00:00
|
|
|
err := row.Scan(
|
|
|
|
&puppet.ID, &puppet.Receiver, &puppet.Phone, &puppet.ContactID, &puppet.Name, &puppet.NameSet,
|
|
|
|
&avatarHash, &puppet.AvatarMXC, &puppet.AvatarSet, &avatarUpdateTS, &puppet.ContactInfoSet,
|
|
|
|
)
|
|
|
|
if err != nil {
|
2023-07-02 14:21:55 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2023-09-04 22:18:01 +00:00
|
|
|
if len(avatarHash) == 32 {
|
|
|
|
puppet.AvatarHash = *(*[32]byte)(avatarHash)
|
|
|
|
}
|
|
|
|
puppet.AvatarUpdateTS = time.UnixMilli(avatarUpdateTS)
|
2023-07-02 14:21:55 +00:00
|
|
|
return puppet, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (puppet *Puppet) sqlVariables() []any {
|
2024-02-24 22:18:06 +00:00
|
|
|
return []any{
|
|
|
|
puppet.ID, puppet.Receiver, puppet.Phone, puppet.ContactID, puppet.Name, puppet.NameSet,
|
|
|
|
puppet.AvatarHash[:], &puppet.AvatarMXC, puppet.AvatarSet, puppet.AvatarUpdateTS.UnixMilli(), puppet.ContactInfoSet,
|
|
|
|
}
|
2023-07-02 14:21:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (puppet *Puppet) Insert(ctx context.Context) error {
|
2024-02-24 22:18:06 +00:00
|
|
|
return puppet.qh.Exec(ctx, insertPuppetQuery, puppet.sqlVariables()...)
|
2023-07-02 14:21:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (puppet *Puppet) Update(ctx context.Context) error {
|
2024-02-24 22:18:06 +00:00
|
|
|
return puppet.qh.Exec(ctx, updatePuppetQuery, puppet.sqlVariables()...)
|
2023-07-02 14:21:55 +00:00
|
|
|
}
|