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"
|
|
|
|
"database/sql"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2023-07-19 17:32:01 +00:00
|
|
|
"sync"
|
2023-07-02 14:21:55 +00:00
|
|
|
|
2023-08-08 13:13:44 +00:00
|
|
|
"go.mau.fi/util/dbutil"
|
2023-07-19 17:32:01 +00:00
|
|
|
"golang.org/x/exp/slices"
|
2023-07-02 14:21:55 +00:00
|
|
|
"maunium.net/go/mautrix/id"
|
|
|
|
|
2023-07-09 11:16:52 +00:00
|
|
|
"go.mau.fi/mautrix-gmessages/libgm"
|
2023-08-30 16:35:02 +00:00
|
|
|
"go.mau.fi/mautrix-gmessages/libgm/gmproto"
|
2023-07-02 14:21:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type UserQuery struct {
|
2024-02-24 22:18:06 +00:00
|
|
|
*dbutil.QueryHelper[*User]
|
2023-07-02 14:21:55 +00:00
|
|
|
}
|
|
|
|
|
2024-02-24 22:18:06 +00:00
|
|
|
func newUser(qh *dbutil.QueryHelper[*User]) *User {
|
|
|
|
return &User{qh: qh}
|
2023-07-02 14:21:55 +00:00
|
|
|
}
|
|
|
|
|
2023-09-04 22:46:56 +00:00
|
|
|
const (
|
2024-06-19 03:44:53 +00:00
|
|
|
getUserBaseQuery = `SELECT rowid, mxid, phone_id, session, self_participant_ids, sim_metadata, settings, management_room, space_room, access_token, disable_notify_battery, disable_notify_verbose FROM "user"`
|
2023-09-04 22:46:56 +00:00
|
|
|
getAllUsersWithSessionQuery = getUserBaseQuery + " WHERE session IS NOT NULL"
|
|
|
|
getAllUsersWithDoublePuppetQuery = getUserBaseQuery + " WHERE access_token<>''"
|
|
|
|
getUserByRowIDQuery = getUserBaseQuery + " WHERE rowid=$1"
|
|
|
|
getUserByMXIDQuery = getUserBaseQuery + " WHERE mxid=$1"
|
|
|
|
|
|
|
|
insertUserQuery = `
|
|
|
|
INSERT INTO "user" (mxid, phone_id, session, self_participant_ids, sim_metadata, settings, management_room, space_room, access_token)
|
|
|
|
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9) RETURNING rowid
|
|
|
|
`
|
|
|
|
updateUserQuery = `
|
|
|
|
UPDATE "user"
|
|
|
|
SET phone_id=$2, session=$3, self_participant_ids=$4, sim_metadata=$5, settings=$6,
|
2024-06-19 03:44:53 +00:00
|
|
|
management_room=$7, space_room=$8, access_token=$9,
|
|
|
|
disable_notify_battery=$10, disable_notify_verbose=$11
|
2023-09-04 22:46:56 +00:00
|
|
|
WHERE mxid=$1
|
|
|
|
`
|
2024-02-24 22:18:06 +00:00
|
|
|
updateuserParticipantIDsQuery = `UPDATE "user" SET self_participant_ids=$2 WHERE mxid=$1`
|
2023-09-04 22:46:56 +00:00
|
|
|
)
|
|
|
|
|
2023-07-02 14:21:55 +00:00
|
|
|
func (uq *UserQuery) GetAllWithSession(ctx context.Context) ([]*User, error) {
|
2024-02-24 22:18:06 +00:00
|
|
|
return uq.QueryMany(ctx, getAllUsersWithSessionQuery)
|
2023-07-02 14:21:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (uq *UserQuery) GetAllWithDoublePuppet(ctx context.Context) ([]*User, error) {
|
2024-02-24 22:18:06 +00:00
|
|
|
return uq.QueryMany(ctx, getAllUsersWithDoublePuppetQuery)
|
2023-07-02 14:21:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (uq *UserQuery) GetByRowID(ctx context.Context, rowID int) (*User, error) {
|
2024-02-24 22:18:06 +00:00
|
|
|
return uq.QueryOne(ctx, getUserByRowIDQuery, rowID)
|
2023-07-02 14:21:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (uq *UserQuery) GetByMXID(ctx context.Context, userID id.UserID) (*User, error) {
|
2024-02-24 22:18:06 +00:00
|
|
|
return uq.QueryOne(ctx, getUserByMXIDQuery, userID)
|
2023-08-30 17:45:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Settings struct {
|
2023-09-04 11:40:33 +00:00
|
|
|
SettingsReceived bool `json:"settings_received"`
|
2023-08-30 17:45:14 +00:00
|
|
|
RCSEnabled bool `json:"rcs_enabled"`
|
|
|
|
ReadReceipts bool `json:"read_receipts"`
|
|
|
|
TypingNotifications bool `json:"typing_notifications"`
|
|
|
|
IsDefaultSMSApp bool `json:"is_default_sms_app"`
|
2023-07-02 14:21:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type User struct {
|
2024-02-24 22:18:06 +00:00
|
|
|
qh *dbutil.QueryHelper[*User]
|
2023-07-02 14:21:55 +00:00
|
|
|
|
|
|
|
RowID int
|
|
|
|
MXID id.UserID
|
2023-07-19 17:32:01 +00:00
|
|
|
PhoneID string
|
2023-07-09 11:16:52 +00:00
|
|
|
Session *libgm.AuthData
|
2023-07-02 14:21:55 +00:00
|
|
|
|
|
|
|
ManagementRoom id.RoomID
|
|
|
|
SpaceRoom id.RoomID
|
|
|
|
|
2023-07-19 17:32:01 +00:00
|
|
|
SelfParticipantIDs []string
|
|
|
|
selfParticipantIDsLock sync.RWMutex
|
|
|
|
|
2023-08-30 16:35:02 +00:00
|
|
|
simMetadata map[string]*gmproto.SIMCard
|
|
|
|
simMetadataLock sync.RWMutex
|
|
|
|
|
2023-08-30 17:45:14 +00:00
|
|
|
Settings Settings
|
|
|
|
|
2023-07-02 14:21:55 +00:00
|
|
|
AccessToken string
|
2024-06-19 03:44:53 +00:00
|
|
|
|
|
|
|
DisableNotifyBattery bool
|
|
|
|
DisableNotifyVerbose bool
|
2023-07-02 14:21:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (user *User) Scan(row dbutil.Scannable) (*User, error) {
|
2023-07-19 17:32:01 +00:00
|
|
|
var phoneID, session, managementRoom, spaceRoom, accessToken sql.NullString
|
2023-08-30 17:45:14 +00:00
|
|
|
var selfParticipantIDs, simMetadata, settings string
|
2024-02-24 22:18:06 +00:00
|
|
|
err := row.Scan(
|
|
|
|
&user.RowID, &user.MXID, &phoneID, &session, &selfParticipantIDs, &simMetadata,
|
|
|
|
&settings, &managementRoom, &spaceRoom, &accessToken,
|
2024-06-19 03:44:53 +00:00
|
|
|
&user.DisableNotifyBattery, &user.DisableNotifyVerbose,
|
2024-02-24 22:18:06 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
2023-07-02 14:21:55 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if session.String != "" {
|
2023-07-09 11:16:52 +00:00
|
|
|
var sess libgm.AuthData
|
2023-07-02 14:21:55 +00:00
|
|
|
err = json.Unmarshal([]byte(session.String), &sess)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to parse session: %w", err)
|
|
|
|
}
|
|
|
|
user.Session = &sess
|
|
|
|
}
|
2023-07-19 17:32:01 +00:00
|
|
|
user.selfParticipantIDsLock.Lock()
|
|
|
|
err = json.Unmarshal([]byte(selfParticipantIDs), &user.SelfParticipantIDs)
|
|
|
|
user.selfParticipantIDsLock.Unlock()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to parse self participant IDs: %w", err)
|
|
|
|
}
|
2023-08-30 16:35:02 +00:00
|
|
|
user.simMetadataLock.Lock()
|
|
|
|
err = json.Unmarshal([]byte(simMetadata), &user.simMetadata)
|
|
|
|
user.simMetadataLock.Unlock()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to parse SIM metadata: %w", err)
|
|
|
|
}
|
2023-08-30 17:45:14 +00:00
|
|
|
err = json.Unmarshal([]byte(settings), &user.Settings)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to parse settings: %w", err)
|
|
|
|
}
|
2023-07-19 17:32:01 +00:00
|
|
|
user.PhoneID = phoneID.String
|
2023-07-02 14:21:55 +00:00
|
|
|
user.AccessToken = accessToken.String
|
|
|
|
user.ManagementRoom = id.RoomID(managementRoom.String)
|
|
|
|
user.SpaceRoom = id.RoomID(spaceRoom.String)
|
|
|
|
return user, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (user *User) sqlVariables() []any {
|
2024-02-24 22:18:06 +00:00
|
|
|
var session *string
|
2023-07-02 14:21:55 +00:00
|
|
|
if user.Session != nil {
|
|
|
|
data, _ := json.Marshal(user.Session)
|
|
|
|
strData := string(data)
|
|
|
|
session = &strData
|
|
|
|
}
|
2023-07-19 17:32:01 +00:00
|
|
|
user.selfParticipantIDsLock.RLock()
|
|
|
|
selfParticipantIDs, _ := json.Marshal(user.SelfParticipantIDs)
|
|
|
|
user.selfParticipantIDsLock.RUnlock()
|
2023-08-30 16:35:02 +00:00
|
|
|
user.simMetadataLock.RLock()
|
|
|
|
simMetadata, err := json.Marshal(user.simMetadata)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
user.simMetadataLock.RUnlock()
|
2023-08-30 17:45:14 +00:00
|
|
|
settings, err := json.Marshal(&user.Settings)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2024-02-24 22:18:06 +00:00
|
|
|
return []any{
|
|
|
|
user.MXID, dbutil.StrPtr(user.PhoneID), session, string(selfParticipantIDs), string(simMetadata),
|
|
|
|
string(settings), dbutil.StrPtr(user.ManagementRoom), dbutil.StrPtr(user.SpaceRoom), dbutil.StrPtr(user.AccessToken),
|
2024-06-19 03:44:53 +00:00
|
|
|
user.DisableNotifyBattery, user.DisableNotifyVerbose,
|
2024-02-24 22:18:06 +00:00
|
|
|
}
|
2023-07-19 17:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (user *User) IsSelfParticipantID(id string) bool {
|
|
|
|
user.selfParticipantIDsLock.RLock()
|
|
|
|
defer user.selfParticipantIDsLock.RUnlock()
|
|
|
|
return slices.Contains(user.SelfParticipantIDs, id)
|
|
|
|
}
|
|
|
|
|
2023-08-30 16:35:02 +00:00
|
|
|
type bridgeStateSIMMeta struct {
|
|
|
|
CarrierName string `json:"carrier_name"`
|
|
|
|
ColorHex string `json:"color_hex"`
|
|
|
|
ParticipantID string `json:"participant_id"`
|
|
|
|
RCSEnabled bool `json:"rcs_enabled"`
|
|
|
|
}
|
|
|
|
|
2023-09-01 10:49:27 +00:00
|
|
|
func (user *User) SIMCount() int {
|
|
|
|
user.simMetadataLock.RLock()
|
|
|
|
defer user.simMetadataLock.RUnlock()
|
|
|
|
return len(user.simMetadata)
|
|
|
|
}
|
|
|
|
|
2023-08-30 16:35:02 +00:00
|
|
|
func (user *User) GetSIMsForBridgeState() []bridgeStateSIMMeta {
|
|
|
|
user.simMetadataLock.RLock()
|
|
|
|
data := make([]bridgeStateSIMMeta, 0, len(user.simMetadata))
|
|
|
|
for _, sim := range user.simMetadata {
|
|
|
|
data = append(data, bridgeStateSIMMeta{
|
|
|
|
CarrierName: sim.GetSIMData().GetCarrierName(),
|
|
|
|
ColorHex: sim.GetSIMData().GetHexHash(),
|
|
|
|
ParticipantID: sim.GetSIMParticipant().GetID(),
|
|
|
|
RCSEnabled: sim.GetRCSChats().GetEnabled(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
user.simMetadataLock.RUnlock()
|
|
|
|
return data
|
|
|
|
}
|
|
|
|
|
|
|
|
func (user *User) GetSIM(participantID string) *gmproto.SIMCard {
|
|
|
|
user.simMetadataLock.Lock()
|
|
|
|
defer user.simMetadataLock.Unlock()
|
|
|
|
return user.simMetadata[participantID]
|
|
|
|
}
|
|
|
|
|
|
|
|
func simsAreEqualish(a, b *gmproto.SIMCard) bool {
|
|
|
|
return a.GetRCSChats().GetEnabled() != b.GetRCSChats().GetEnabled() ||
|
|
|
|
a.GetSIMData().GetCarrierName() != b.GetSIMData().GetCarrierName() ||
|
|
|
|
a.GetSIMData().GetSIMPayload().GetSIMNumber() != b.GetSIMData().GetSIMPayload().GetSIMNumber() ||
|
|
|
|
a.GetSIMData().GetSIMPayload().GetTwo() != b.GetSIMData().GetSIMPayload().GetTwo()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (user *User) SetSIMs(sims []*gmproto.SIMCard) bool {
|
|
|
|
user.simMetadataLock.Lock()
|
|
|
|
defer user.simMetadataLock.Unlock()
|
|
|
|
user.selfParticipantIDsLock.Lock()
|
|
|
|
defer user.selfParticipantIDsLock.Unlock()
|
|
|
|
newMap := make(map[string]*gmproto.SIMCard)
|
|
|
|
participantIDsChanged := false
|
|
|
|
for _, sim := range sims {
|
|
|
|
participantID := sim.GetSIMParticipant().GetID()
|
|
|
|
newMap[sim.GetSIMParticipant().GetID()] = sim
|
|
|
|
if !slices.Contains(user.SelfParticipantIDs, participantID) {
|
|
|
|
user.SelfParticipantIDs = append(user.SelfParticipantIDs, participantID)
|
|
|
|
participantIDsChanged = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
oldMap := user.simMetadata
|
|
|
|
user.simMetadata = newMap
|
|
|
|
if participantIDsChanged || len(newMap) != len(oldMap) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
for participantID, sim := range newMap {
|
|
|
|
existing, ok := oldMap[participantID]
|
|
|
|
if !ok || !simsAreEqualish(existing, sim) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-07-19 17:32:01 +00:00
|
|
|
func (user *User) AddSelfParticipantID(ctx context.Context, id string) error {
|
|
|
|
user.selfParticipantIDsLock.Lock()
|
|
|
|
defer user.selfParticipantIDsLock.Unlock()
|
|
|
|
if !slices.Contains(user.SelfParticipantIDs, id) {
|
|
|
|
user.SelfParticipantIDs = append(user.SelfParticipantIDs, id)
|
|
|
|
selfParticipantIDs, _ := json.Marshal(user.SelfParticipantIDs)
|
2024-02-24 22:18:06 +00:00
|
|
|
return user.qh.Exec(ctx, updateuserParticipantIDsQuery, user.MXID, selfParticipantIDs)
|
2023-07-19 17:32:01 +00:00
|
|
|
}
|
|
|
|
return nil
|
2023-07-02 14:21:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (user *User) Insert(ctx context.Context) error {
|
2024-02-24 22:18:06 +00:00
|
|
|
err := user.qh.GetDB().
|
|
|
|
QueryRow(ctx, insertUserQuery, user.sqlVariables()...).
|
2023-07-02 14:21:55 +00:00
|
|
|
Scan(&user.RowID)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (user *User) Update(ctx context.Context) error {
|
2024-02-24 22:18:06 +00:00
|
|
|
return user.qh.Exec(ctx, updateUserQuery, user.sqlVariables()...)
|
2023-07-02 14:21:55 +00:00
|
|
|
}
|