2018-08-13 22:24:44 +02:00
|
|
|
// mautrix-whatsapp - A Matrix-WhatsApp puppeting bridge.
|
2024-03-11 21:27:10 +01:00
|
|
|
// Copyright (C) 2024 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 (
|
2024-03-11 21:27:10 +01:00
|
|
|
"context"
|
2018-08-26 21:53:13 +02:00
|
|
|
"database/sql"
|
2021-12-29 20:40:08 +01:00
|
|
|
"sync"
|
|
|
|
"time"
|
2019-01-11 20:17:31 +01:00
|
|
|
|
2023-08-05 17:59:17 +02:00
|
|
|
"go.mau.fi/whatsmeow/types"
|
2020-05-08 21:32:22 +02:00
|
|
|
"maunium.net/go/mautrix/id"
|
2024-03-11 21:27:10 +01:00
|
|
|
|
|
|
|
"go.mau.fi/util/dbutil"
|
2018-08-13 22:24:44 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type UserQuery struct {
|
2024-03-11 21:27:10 +01:00
|
|
|
*dbutil.QueryHelper[*User]
|
2018-08-13 22:24:44 +02:00
|
|
|
}
|
|
|
|
|
2024-03-11 21:27:10 +01:00
|
|
|
func newUser(qh *dbutil.QueryHelper[*User]) *User {
|
2018-08-13 22:24:44 +02:00
|
|
|
return &User{
|
2024-03-11 21:27:10 +01:00
|
|
|
qh: qh,
|
2021-12-29 20:40:08 +01:00
|
|
|
|
|
|
|
lastReadCache: make(map[PortalKey]time.Time),
|
|
|
|
inSpaceCache: make(map[PortalKey]bool),
|
2018-08-13 22:24:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-11 21:27:10 +01:00
|
|
|
const (
|
|
|
|
getAllUsersQuery = `SELECT mxid, username, agent, device, management_room, space_room, phone_last_seen, phone_last_pinged, timezone FROM "user"`
|
|
|
|
getUserByMXIDQuery = getAllUsersQuery + ` WHERE mxid=$1`
|
|
|
|
getUserByUsernameQuery = getAllUsersQuery + ` WHERE username=$1`
|
|
|
|
insertUserQuery = `
|
|
|
|
INSERT INTO "user" (
|
|
|
|
mxid, username, agent, device,
|
|
|
|
management_room, space_room,
|
|
|
|
phone_last_seen, phone_last_pinged, timezone
|
|
|
|
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
|
|
|
|
`
|
|
|
|
updateUserQuery = `
|
|
|
|
UPDATE "user"
|
2024-03-12 19:58:00 +01:00
|
|
|
SET username=$2, agent=$3, device=$4,
|
|
|
|
management_room=$5, space_room=$6,
|
|
|
|
phone_last_seen=$7, phone_last_pinged=$8, timezone=$9
|
|
|
|
WHERE mxid=$1
|
2024-03-11 21:27:10 +01:00
|
|
|
`
|
|
|
|
getUserLastAppStateKeyIDQuery = "SELECT key_id FROM whatsmeow_app_state_sync_keys WHERE jid=$1 ORDER BY timestamp DESC LIMIT 1"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (uq *UserQuery) GetAll(ctx context.Context) ([]*User, error) {
|
|
|
|
return uq.QueryMany(ctx, getAllUsersQuery)
|
2018-08-13 22:24:44 +02:00
|
|
|
}
|
|
|
|
|
2024-03-11 21:27:10 +01:00
|
|
|
func (uq *UserQuery) GetByMXID(ctx context.Context, userID id.UserID) (*User, error) {
|
|
|
|
return uq.QueryOne(ctx, getUserByMXIDQuery, userID)
|
2018-08-13 22:24:44 +02:00
|
|
|
}
|
|
|
|
|
2024-03-11 21:27:10 +01:00
|
|
|
func (uq *UserQuery) GetByUsername(ctx context.Context, username string) (*User, error) {
|
|
|
|
return uq.QueryOne(ctx, getUserByUsernameQuery, username)
|
2018-08-28 23:40:54 +02:00
|
|
|
}
|
|
|
|
|
2018-08-13 22:24:44 +02:00
|
|
|
type User struct {
|
2024-03-11 21:27:10 +01:00
|
|
|
qh *dbutil.QueryHelper[*User]
|
2018-08-13 22:24:44 +02:00
|
|
|
|
2022-02-18 11:12:15 +01:00
|
|
|
MXID id.UserID
|
|
|
|
JID types.JID
|
|
|
|
ManagementRoom id.RoomID
|
|
|
|
SpaceRoom id.RoomID
|
|
|
|
PhoneLastSeen time.Time
|
|
|
|
PhoneLastPinged time.Time
|
2022-05-11 23:37:30 +02:00
|
|
|
Timezone string
|
2021-12-29 20:40:08 +01:00
|
|
|
|
|
|
|
lastReadCache map[PortalKey]time.Time
|
|
|
|
lastReadCacheLock sync.Mutex
|
|
|
|
inSpaceCache map[PortalKey]bool
|
|
|
|
inSpaceCacheLock sync.Mutex
|
2018-08-13 22:24:44 +02:00
|
|
|
}
|
|
|
|
|
2024-03-11 21:27:10 +01:00
|
|
|
func (user *User) Scan(row dbutil.Scannable) (*User, error) {
|
2022-05-12 19:51:37 +02:00
|
|
|
var username, timezone sql.NullString
|
2024-03-11 21:27:10 +01:00
|
|
|
var device, agent sql.NullInt16
|
2022-02-18 11:12:15 +01:00
|
|
|
var phoneLastSeen, phoneLastPinged sql.NullInt64
|
2022-05-12 19:51:37 +02:00
|
|
|
err := row.Scan(&user.MXID, &username, &agent, &device, &user.ManagementRoom, &user.SpaceRoom, &phoneLastSeen, &phoneLastPinged, &timezone)
|
2018-08-13 22:24:44 +02:00
|
|
|
if err != nil {
|
2024-03-11 21:27:10 +01:00
|
|
|
return nil, err
|
2018-08-13 22:24:44 +02:00
|
|
|
}
|
2022-05-12 19:51:37 +02:00
|
|
|
user.Timezone = timezone.String
|
2021-10-22 19:14:34 +02:00
|
|
|
if len(username.String) > 0 {
|
2024-03-11 21:27:10 +01:00
|
|
|
user.JID = types.JID{
|
|
|
|
User: username.String,
|
|
|
|
Device: uint16(device.Int16),
|
|
|
|
Server: types.DefaultUserServer,
|
|
|
|
}
|
2018-08-16 14:59:18 +02:00
|
|
|
}
|
2022-01-25 13:26:24 +01:00
|
|
|
if phoneLastSeen.Valid {
|
|
|
|
user.PhoneLastSeen = time.Unix(phoneLastSeen.Int64, 0)
|
|
|
|
}
|
2022-02-18 11:12:15 +01:00
|
|
|
if phoneLastPinged.Valid {
|
|
|
|
user.PhoneLastPinged = time.Unix(phoneLastPinged.Int64, 0)
|
|
|
|
}
|
2024-03-11 21:27:10 +01:00
|
|
|
return user, nil
|
2018-08-13 22:24:44 +02:00
|
|
|
}
|
|
|
|
|
2024-03-11 21:27:10 +01:00
|
|
|
func (user *User) sqlVariables() []any {
|
|
|
|
var username *string
|
|
|
|
var agent, device *uint16
|
2021-10-22 19:14:34 +02:00
|
|
|
if !user.JID.IsEmpty() {
|
2024-03-11 21:27:10 +01:00
|
|
|
username = dbutil.StrPtr(user.JID.User)
|
|
|
|
var zero uint16
|
|
|
|
agent = &zero
|
|
|
|
device = dbutil.NumPtr(user.JID.Device)
|
2018-08-30 00:10:26 +02:00
|
|
|
}
|
2024-03-11 21:27:10 +01:00
|
|
|
return []any{
|
2024-03-12 19:59:46 +01:00
|
|
|
user.MXID, username, agent, device, user.ManagementRoom, user.SpaceRoom,
|
2024-03-11 21:27:10 +01:00
|
|
|
dbutil.UnixPtr(user.PhoneLastSeen), dbutil.UnixPtr(user.PhoneLastPinged),
|
2024-03-12 19:59:46 +01:00
|
|
|
user.Timezone,
|
2018-09-01 22:38:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-11 21:27:10 +01:00
|
|
|
func (user *User) Insert(ctx context.Context) error {
|
|
|
|
return user.qh.Exec(ctx, insertUserQuery, user.sqlVariables()...)
|
2022-01-25 13:26:24 +01:00
|
|
|
}
|
|
|
|
|
2024-03-11 21:27:10 +01:00
|
|
|
func (user *User) Update(ctx context.Context) error {
|
|
|
|
return user.qh.Exec(ctx, updateUserQuery, user.sqlVariables()...)
|
2022-02-18 11:12:15 +01:00
|
|
|
}
|
|
|
|
|
2024-03-11 21:27:10 +01:00
|
|
|
func (user *User) GetLastAppStateKeyID(ctx context.Context) (keyID []byte, err error) {
|
|
|
|
err = user.qh.GetDB().QueryRow(ctx, getUserLastAppStateKeyIDQuery, user.JID).Scan(&keyID)
|
|
|
|
return
|
2022-05-16 10:22:41 +02:00
|
|
|
}
|