2022-02-18 07:51:59 -06:00
|
|
|
// Copyright 2017 Vector Creations Ltd
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package shared
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/rand"
|
|
|
|
"database/sql"
|
|
|
|
"encoding/base64"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
2022-03-23 07:55:34 -05:00
|
|
|
"strings"
|
2022-02-18 07:51:59 -06:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
2023-04-06 03:55:01 -05:00
|
|
|
"github.com/matrix-org/gomatrixserverlib/fclient"
|
2022-02-18 07:51:59 -06:00
|
|
|
"golang.org/x/crypto/bcrypt"
|
|
|
|
|
|
|
|
"github.com/matrix-org/dendrite/clientapi/auth/authtypes"
|
2022-03-03 05:40:53 -06:00
|
|
|
"github.com/matrix-org/dendrite/internal/pushrules"
|
2022-02-18 07:51:59 -06:00
|
|
|
"github.com/matrix-org/dendrite/internal/sqlutil"
|
|
|
|
"github.com/matrix-org/dendrite/userapi/api"
|
|
|
|
"github.com/matrix-org/dendrite/userapi/storage/tables"
|
2022-11-02 05:18:11 -05:00
|
|
|
"github.com/matrix-org/dendrite/userapi/types"
|
2022-02-18 07:51:59 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
// Database represents an account database
|
|
|
|
type Database struct {
|
|
|
|
DB *sql.DB
|
|
|
|
Writer sqlutil.Writer
|
|
|
|
Accounts tables.AccountsTable
|
|
|
|
Profiles tables.ProfileTable
|
|
|
|
AccountDatas tables.AccountDataTable
|
|
|
|
ThreePIDs tables.ThreePIDTable
|
|
|
|
OpenIDTokens tables.OpenIDTable
|
|
|
|
KeyBackups tables.KeyBackupTable
|
|
|
|
KeyBackupVersions tables.KeyBackupVersionTable
|
|
|
|
Devices tables.DevicesTable
|
|
|
|
LoginTokens tables.LoginTokenTable
|
2022-03-03 05:40:53 -06:00
|
|
|
Notifications tables.NotificationTable
|
|
|
|
Pushers tables.PusherTable
|
2022-05-04 12:04:28 -05:00
|
|
|
Stats tables.StatsTable
|
2022-02-18 07:51:59 -06:00
|
|
|
LoginTokenLifetime time.Duration
|
|
|
|
ServerName gomatrixserverlib.ServerName
|
|
|
|
BcryptCost int
|
|
|
|
OpenIDTokenLifetimeMS int64
|
|
|
|
}
|
|
|
|
|
2023-02-20 07:58:03 -06:00
|
|
|
type KeyDatabase struct {
|
|
|
|
OneTimeKeysTable tables.OneTimeKeys
|
|
|
|
DeviceKeysTable tables.DeviceKeys
|
|
|
|
KeyChangesTable tables.KeyChanges
|
|
|
|
StaleDeviceListsTable tables.StaleDeviceLists
|
|
|
|
CrossSigningKeysTable tables.CrossSigningKeys
|
|
|
|
CrossSigningSigsTable tables.CrossSigningSigs
|
|
|
|
DB *sql.DB
|
|
|
|
Writer sqlutil.Writer
|
|
|
|
}
|
|
|
|
|
2022-02-18 07:51:59 -06:00
|
|
|
const (
|
|
|
|
// The length of generated device IDs
|
|
|
|
deviceIDByteLength = 6
|
|
|
|
loginTokenByteLength = 32
|
|
|
|
)
|
|
|
|
|
|
|
|
// GetAccountByPassword returns the account associated with the given localpart and password.
|
|
|
|
// Returns sql.ErrNoRows if no account exists which matches the given localpart.
|
|
|
|
func (d *Database) GetAccountByPassword(
|
2022-11-11 10:41:37 -06:00
|
|
|
ctx context.Context, localpart string, serverName gomatrixserverlib.ServerName,
|
|
|
|
plaintextPassword string,
|
2022-02-18 07:51:59 -06:00
|
|
|
) (*api.Account, error) {
|
2022-11-11 10:41:37 -06:00
|
|
|
hash, err := d.Accounts.SelectPasswordHash(ctx, localpart, serverName)
|
2022-02-18 07:51:59 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-10-10 05:14:16 -05:00
|
|
|
if len(hash) == 0 && len(plaintextPassword) > 0 {
|
2022-10-10 04:39:29 -05:00
|
|
|
return nil, bcrypt.ErrHashTooShort
|
|
|
|
}
|
2022-02-18 07:51:59 -06:00
|
|
|
if err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(plaintextPassword)); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-11-11 10:41:37 -06:00
|
|
|
return d.Accounts.SelectAccountByLocalpart(ctx, localpart, serverName)
|
2022-02-18 07:51:59 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetProfileByLocalpart returns the profile associated with the given localpart.
|
|
|
|
// Returns sql.ErrNoRows if no profile exists which matches the given localpart.
|
|
|
|
func (d *Database) GetProfileByLocalpart(
|
2022-11-11 10:41:37 -06:00
|
|
|
ctx context.Context,
|
|
|
|
localpart string, serverName gomatrixserverlib.ServerName,
|
2022-02-18 07:51:59 -06:00
|
|
|
) (*authtypes.Profile, error) {
|
2022-11-11 10:41:37 -06:00
|
|
|
return d.Profiles.SelectProfileByLocalpart(ctx, localpart, serverName)
|
2022-02-18 07:51:59 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetAvatarURL updates the avatar URL of the profile associated with the given
|
|
|
|
// localpart. Returns an error if something went wrong with the SQL query
|
|
|
|
func (d *Database) SetAvatarURL(
|
2022-11-11 10:41:37 -06:00
|
|
|
ctx context.Context,
|
|
|
|
localpart string, serverName gomatrixserverlib.ServerName,
|
|
|
|
avatarURL string,
|
2022-10-21 03:48:25 -05:00
|
|
|
) (profile *authtypes.Profile, changed bool, err error) {
|
|
|
|
err = d.Writer.Do(d.DB, nil, func(txn *sql.Tx) error {
|
2022-11-11 10:41:37 -06:00
|
|
|
profile, changed, err = d.Profiles.SetAvatarURL(ctx, txn, localpart, serverName, avatarURL)
|
2022-10-21 03:48:25 -05:00
|
|
|
return err
|
2022-02-18 07:51:59 -06:00
|
|
|
})
|
2022-10-21 03:48:25 -05:00
|
|
|
return
|
2022-02-18 07:51:59 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetDisplayName updates the display name of the profile associated with the given
|
|
|
|
// localpart. Returns an error if something went wrong with the SQL query
|
|
|
|
func (d *Database) SetDisplayName(
|
2022-11-11 10:41:37 -06:00
|
|
|
ctx context.Context,
|
|
|
|
localpart string, serverName gomatrixserverlib.ServerName,
|
|
|
|
displayName string,
|
2022-10-21 03:48:25 -05:00
|
|
|
) (profile *authtypes.Profile, changed bool, err error) {
|
|
|
|
err = d.Writer.Do(d.DB, nil, func(txn *sql.Tx) error {
|
2022-11-11 10:41:37 -06:00
|
|
|
profile, changed, err = d.Profiles.SetDisplayName(ctx, txn, localpart, serverName, displayName)
|
2022-10-21 03:48:25 -05:00
|
|
|
return err
|
2022-02-18 07:51:59 -06:00
|
|
|
})
|
2022-10-21 03:48:25 -05:00
|
|
|
return
|
2022-02-18 07:51:59 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetPassword sets the account password to the given hash.
|
|
|
|
func (d *Database) SetPassword(
|
2022-11-11 10:41:37 -06:00
|
|
|
ctx context.Context, localpart string, serverName gomatrixserverlib.ServerName,
|
|
|
|
plaintextPassword string,
|
2022-02-18 07:51:59 -06:00
|
|
|
) error {
|
|
|
|
hash, err := d.hashPassword(plaintextPassword)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return d.Writer.Do(nil, nil, func(txn *sql.Tx) error {
|
2022-11-11 10:41:37 -06:00
|
|
|
return d.Accounts.UpdatePassword(ctx, localpart, serverName, hash)
|
2022-02-18 07:51:59 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateAccount makes a new account with the given login name and password, and creates an empty profile
|
|
|
|
// for this account. If no password is supplied, the account will be a passwordless account. If the
|
|
|
|
// account already exists, it will return nil, ErrUserExists.
|
|
|
|
func (d *Database) CreateAccount(
|
2022-11-11 10:41:37 -06:00
|
|
|
ctx context.Context, localpart string, serverName gomatrixserverlib.ServerName,
|
|
|
|
plaintextPassword, appserviceID string, accountType api.AccountType,
|
2022-02-18 07:51:59 -06:00
|
|
|
) (acc *api.Account, err error) {
|
|
|
|
err = d.Writer.Do(d.DB, nil, func(txn *sql.Tx) error {
|
|
|
|
// For guest accounts, we create a new numeric local part
|
|
|
|
if accountType == api.AccountTypeGuest {
|
|
|
|
var numLocalpart int64
|
2022-11-11 10:41:37 -06:00
|
|
|
numLocalpart, err = d.Accounts.SelectNewNumericLocalpart(ctx, txn, serverName)
|
2022-02-18 07:51:59 -06:00
|
|
|
if err != nil {
|
2022-11-11 10:41:37 -06:00
|
|
|
return fmt.Errorf("d.Accounts.SelectNewNumericLocalpart: %w", err)
|
2022-02-18 07:51:59 -06:00
|
|
|
}
|
|
|
|
localpart = strconv.FormatInt(numLocalpart, 10)
|
|
|
|
plaintextPassword = ""
|
|
|
|
appserviceID = ""
|
|
|
|
}
|
2022-11-11 10:41:37 -06:00
|
|
|
acc, err = d.createAccount(ctx, txn, localpart, serverName, plaintextPassword, appserviceID, accountType)
|
2022-02-18 07:51:59 -06:00
|
|
|
return err
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// WARNING! This function assumes that the relevant mutexes have already
|
|
|
|
// been taken out by the caller (e.g. CreateAccount or CreateGuestAccount).
|
|
|
|
func (d *Database) createAccount(
|
2022-11-11 10:41:37 -06:00
|
|
|
ctx context.Context, txn *sql.Tx,
|
|
|
|
localpart string, serverName gomatrixserverlib.ServerName,
|
|
|
|
plaintextPassword, appserviceID string, accountType api.AccountType,
|
2022-02-18 07:51:59 -06:00
|
|
|
) (*api.Account, error) {
|
|
|
|
var err error
|
|
|
|
var account *api.Account
|
|
|
|
// Generate a password hash if this is not a password-less user
|
|
|
|
hash := ""
|
|
|
|
if plaintextPassword != "" {
|
|
|
|
hash, err = d.hashPassword(plaintextPassword)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2022-11-11 10:41:37 -06:00
|
|
|
if account, err = d.Accounts.InsertAccount(ctx, txn, localpart, serverName, hash, appserviceID, accountType); err != nil {
|
2022-02-18 07:51:59 -06:00
|
|
|
return nil, sqlutil.ErrUserExists
|
|
|
|
}
|
2022-11-11 10:41:37 -06:00
|
|
|
if err = d.Profiles.InsertProfile(ctx, txn, localpart, serverName); err != nil {
|
|
|
|
return nil, fmt.Errorf("d.Profiles.InsertProfile: %w", err)
|
2022-02-18 07:51:59 -06:00
|
|
|
}
|
2022-11-11 10:41:37 -06:00
|
|
|
pushRuleSets := pushrules.DefaultAccountRuleSets(localpart, serverName)
|
2022-03-03 05:40:53 -06:00
|
|
|
prbs, err := json.Marshal(pushRuleSets)
|
|
|
|
if err != nil {
|
2022-11-11 10:41:37 -06:00
|
|
|
return nil, fmt.Errorf("json.Marshal: %w", err)
|
2022-03-03 05:40:53 -06:00
|
|
|
}
|
2022-11-11 10:41:37 -06:00
|
|
|
if err = d.AccountDatas.InsertAccountData(ctx, txn, localpart, serverName, "", "m.push_rules", json.RawMessage(prbs)); err != nil {
|
|
|
|
return nil, fmt.Errorf("d.AccountDatas.InsertAccountData: %w", err)
|
2022-02-18 07:51:59 -06:00
|
|
|
}
|
|
|
|
return account, nil
|
|
|
|
}
|
|
|
|
|
2022-09-09 06:56:33 -05:00
|
|
|
func (d *Database) QueryPushRules(
|
|
|
|
ctx context.Context,
|
2022-11-11 10:41:37 -06:00
|
|
|
localpart string, serverName gomatrixserverlib.ServerName,
|
2022-09-09 06:56:33 -05:00
|
|
|
) (*pushrules.AccountRuleSets, error) {
|
2022-11-11 10:41:37 -06:00
|
|
|
data, err := d.AccountDatas.SelectAccountDataByType(ctx, localpart, serverName, "", "m.push_rules")
|
2022-09-09 06:56:33 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we didn't find any default push rules then we should just generate some
|
|
|
|
// fresh ones.
|
|
|
|
if len(data) == 0 {
|
2022-11-11 10:41:37 -06:00
|
|
|
pushRuleSets := pushrules.DefaultAccountRuleSets(localpart, serverName)
|
2022-09-09 06:56:33 -05:00
|
|
|
prbs, err := json.Marshal(pushRuleSets)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to marshal default push rules: %w", err)
|
|
|
|
}
|
|
|
|
err = d.Writer.Do(d.DB, nil, func(txn *sql.Tx) error {
|
2022-11-11 10:41:37 -06:00
|
|
|
if dbErr := d.AccountDatas.InsertAccountData(ctx, txn, localpart, serverName, "", "m.push_rules", prbs); dbErr != nil {
|
2022-09-09 06:56:33 -05:00
|
|
|
return fmt.Errorf("failed to save default push rules: %w", dbErr)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return pushRuleSets, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var pushRules pushrules.AccountRuleSets
|
|
|
|
if err := json.Unmarshal(data, &pushRules); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &pushRules, nil
|
|
|
|
}
|
|
|
|
|
2022-02-18 07:51:59 -06:00
|
|
|
// SaveAccountData saves new account data for a given user and a given room.
|
|
|
|
// If the account data is not specific to a room, the room ID should be an empty string
|
|
|
|
// If an account data already exists for a given set (user, room, data type), it will
|
|
|
|
// update the corresponding row with the new content
|
|
|
|
// Returns a SQL error if there was an issue with the insertion/update
|
|
|
|
func (d *Database) SaveAccountData(
|
2022-11-11 10:41:37 -06:00
|
|
|
ctx context.Context, localpart string, serverName gomatrixserverlib.ServerName,
|
|
|
|
roomID, dataType string, content json.RawMessage,
|
2022-02-18 07:51:59 -06:00
|
|
|
) error {
|
|
|
|
return d.Writer.Do(d.DB, nil, func(txn *sql.Tx) error {
|
2022-11-11 10:41:37 -06:00
|
|
|
return d.AccountDatas.InsertAccountData(ctx, txn, localpart, serverName, roomID, dataType, content)
|
2022-02-18 07:51:59 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetAccountData returns account data related to a given localpart
|
|
|
|
// If no account data could be found, returns an empty arrays
|
|
|
|
// Returns an error if there was an issue with the retrieval
|
2022-11-11 10:41:37 -06:00
|
|
|
func (d *Database) GetAccountData(ctx context.Context, localpart string, serverName gomatrixserverlib.ServerName) (
|
2022-02-18 07:51:59 -06:00
|
|
|
global map[string]json.RawMessage,
|
|
|
|
rooms map[string]map[string]json.RawMessage,
|
|
|
|
err error,
|
|
|
|
) {
|
2022-11-11 10:41:37 -06:00
|
|
|
return d.AccountDatas.SelectAccountData(ctx, localpart, serverName)
|
2022-02-18 07:51:59 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetAccountDataByType returns account data matching a given
|
|
|
|
// localpart, room ID and type.
|
|
|
|
// If no account data could be found, returns nil
|
|
|
|
// Returns an error if there was an issue with the retrieval
|
|
|
|
func (d *Database) GetAccountDataByType(
|
2022-11-11 10:41:37 -06:00
|
|
|
ctx context.Context, localpart string, serverName gomatrixserverlib.ServerName,
|
|
|
|
roomID, dataType string,
|
2022-02-18 07:51:59 -06:00
|
|
|
) (data json.RawMessage, err error) {
|
|
|
|
return d.AccountDatas.SelectAccountDataByType(
|
2022-11-11 10:41:37 -06:00
|
|
|
ctx, localpart, serverName, roomID, dataType,
|
2022-02-18 07:51:59 -06:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetNewNumericLocalpart generates and returns a new unused numeric localpart
|
|
|
|
func (d *Database) GetNewNumericLocalpart(
|
2022-11-11 10:41:37 -06:00
|
|
|
ctx context.Context, serverName gomatrixserverlib.ServerName,
|
2022-02-18 07:51:59 -06:00
|
|
|
) (int64, error) {
|
2022-11-11 10:41:37 -06:00
|
|
|
return d.Accounts.SelectNewNumericLocalpart(ctx, nil, serverName)
|
2022-02-18 07:51:59 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Database) hashPassword(plaintext string) (hash string, err error) {
|
|
|
|
hashBytes, err := bcrypt.GenerateFromPassword([]byte(plaintext), d.BcryptCost)
|
|
|
|
return string(hashBytes), err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Err3PIDInUse is the error returned when trying to save an association involving
|
|
|
|
// a third-party identifier which is already associated to a local user.
|
|
|
|
var Err3PIDInUse = errors.New("this third-party identifier is already in use")
|
|
|
|
|
|
|
|
// SaveThreePIDAssociation saves the association between a third party identifier
|
|
|
|
// and a local Matrix user (identified by the user's ID's local part).
|
|
|
|
// If the third-party identifier is already part of an association, returns Err3PIDInUse.
|
|
|
|
// Returns an error if there was a problem talking to the database.
|
|
|
|
func (d *Database) SaveThreePIDAssociation(
|
2022-11-11 10:41:37 -06:00
|
|
|
ctx context.Context, threepid string,
|
|
|
|
localpart string, serverName gomatrixserverlib.ServerName,
|
|
|
|
medium string,
|
2022-02-18 07:51:59 -06:00
|
|
|
) (err error) {
|
|
|
|
return d.Writer.Do(d.DB, nil, func(txn *sql.Tx) error {
|
2022-11-11 10:41:37 -06:00
|
|
|
user, _, err := d.ThreePIDs.SelectLocalpartForThreePID(
|
2022-02-18 07:51:59 -06:00
|
|
|
ctx, txn, threepid, medium,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(user) > 0 {
|
|
|
|
return Err3PIDInUse
|
|
|
|
}
|
|
|
|
|
2022-11-11 10:41:37 -06:00
|
|
|
return d.ThreePIDs.InsertThreePID(ctx, txn, threepid, medium, localpart, serverName)
|
2022-02-18 07:51:59 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveThreePIDAssociation removes the association involving a given third-party
|
|
|
|
// identifier.
|
|
|
|
// If no association exists involving this third-party identifier, returns nothing.
|
|
|
|
// If there was a problem talking to the database, returns an error.
|
|
|
|
func (d *Database) RemoveThreePIDAssociation(
|
|
|
|
ctx context.Context, threepid string, medium string,
|
|
|
|
) (err error) {
|
|
|
|
return d.Writer.Do(d.DB, nil, func(txn *sql.Tx) error {
|
|
|
|
return d.ThreePIDs.DeleteThreePID(ctx, txn, threepid, medium)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetLocalpartForThreePID looks up the localpart associated with a given third-party
|
|
|
|
// identifier.
|
|
|
|
// If no association involves the given third-party idenfitier, returns an empty
|
|
|
|
// string.
|
|
|
|
// Returns an error if there was a problem talking to the database.
|
|
|
|
func (d *Database) GetLocalpartForThreePID(
|
|
|
|
ctx context.Context, threepid string, medium string,
|
2022-11-11 10:41:37 -06:00
|
|
|
) (localpart string, serverName gomatrixserverlib.ServerName, err error) {
|
2022-02-18 07:51:59 -06:00
|
|
|
return d.ThreePIDs.SelectLocalpartForThreePID(ctx, nil, threepid, medium)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetThreePIDsForLocalpart looks up the third-party identifiers associated with
|
|
|
|
// a given local user.
|
|
|
|
// If no association is known for this user, returns an empty slice.
|
|
|
|
// Returns an error if there was an issue talking to the database.
|
|
|
|
func (d *Database) GetThreePIDsForLocalpart(
|
2022-11-11 10:41:37 -06:00
|
|
|
ctx context.Context,
|
|
|
|
localpart string, serverName gomatrixserverlib.ServerName,
|
2022-02-18 07:51:59 -06:00
|
|
|
) (threepids []authtypes.ThreePID, err error) {
|
2022-11-11 10:41:37 -06:00
|
|
|
return d.ThreePIDs.SelectThreePIDsForLocalpart(ctx, localpart, serverName)
|
2022-02-18 07:51:59 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// CheckAccountAvailability checks if the username/localpart is already present
|
|
|
|
// in the database.
|
|
|
|
// If the DB returns sql.ErrNoRows the Localpart isn't taken.
|
2022-11-11 10:41:37 -06:00
|
|
|
func (d *Database) CheckAccountAvailability(ctx context.Context, localpart string, serverName gomatrixserverlib.ServerName) (bool, error) {
|
|
|
|
_, err := d.Accounts.SelectAccountByLocalpart(ctx, localpart, serverName)
|
2022-02-18 07:51:59 -06:00
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetAccountByLocalpart returns the account associated with the given localpart.
|
|
|
|
// This function assumes the request is authenticated or the account data is used only internally.
|
|
|
|
// Returns sql.ErrNoRows if no account exists which matches the given localpart.
|
2022-11-11 10:41:37 -06:00
|
|
|
func (d *Database) GetAccountByLocalpart(ctx context.Context, localpart string, serverName gomatrixserverlib.ServerName,
|
2022-02-18 07:51:59 -06:00
|
|
|
) (*api.Account, error) {
|
2022-03-23 07:55:34 -05:00
|
|
|
// try to get the account with lowercase localpart (majority)
|
2022-11-11 10:41:37 -06:00
|
|
|
acc, err := d.Accounts.SelectAccountByLocalpart(ctx, strings.ToLower(localpart), serverName)
|
2022-03-23 07:55:34 -05:00
|
|
|
if err == sql.ErrNoRows {
|
2022-11-11 10:41:37 -06:00
|
|
|
acc, err = d.Accounts.SelectAccountByLocalpart(ctx, localpart, serverName) // try with localpart as passed by the request
|
2022-03-23 07:55:34 -05:00
|
|
|
}
|
|
|
|
return acc, err
|
2022-02-18 07:51:59 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// SearchProfiles returns all profiles where the provided localpart or display name
|
|
|
|
// match any part of the profiles in the database.
|
|
|
|
func (d *Database) SearchProfiles(ctx context.Context, searchString string, limit int,
|
|
|
|
) ([]authtypes.Profile, error) {
|
|
|
|
return d.Profiles.SelectProfilesBySearch(ctx, searchString, limit)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeactivateAccount deactivates the user's account, removing all ability for the user to login again.
|
2022-11-11 10:41:37 -06:00
|
|
|
func (d *Database) DeactivateAccount(ctx context.Context, localpart string, serverName gomatrixserverlib.ServerName) (err error) {
|
2022-02-18 07:51:59 -06:00
|
|
|
return d.Writer.Do(nil, nil, func(txn *sql.Tx) error {
|
2022-11-11 10:41:37 -06:00
|
|
|
return d.Accounts.DeactivateAccount(ctx, localpart, serverName)
|
2022-02-18 07:51:59 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateOpenIDToken persists a new token that was issued for OpenID Connect
|
|
|
|
func (d *Database) CreateOpenIDToken(
|
|
|
|
ctx context.Context,
|
2022-11-11 10:41:37 -06:00
|
|
|
token, userID string,
|
2022-02-18 07:51:59 -06:00
|
|
|
) (int64, error) {
|
2022-11-11 10:41:37 -06:00
|
|
|
localpart, domain, err := gomatrixserverlib.SplitID('@', userID)
|
|
|
|
if err != nil {
|
|
|
|
return 0, nil
|
|
|
|
}
|
2022-02-18 07:51:59 -06:00
|
|
|
expiresAtMS := time.Now().UnixNano()/int64(time.Millisecond) + d.OpenIDTokenLifetimeMS
|
2022-11-11 10:41:37 -06:00
|
|
|
err = d.Writer.Do(d.DB, nil, func(txn *sql.Tx) error {
|
|
|
|
return d.OpenIDTokens.InsertOpenIDToken(ctx, txn, token, localpart, domain, expiresAtMS)
|
2022-02-18 07:51:59 -06:00
|
|
|
})
|
|
|
|
return expiresAtMS, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetOpenIDTokenAttributes gets the attributes of issued an OIDC auth token
|
|
|
|
func (d *Database) GetOpenIDTokenAttributes(
|
|
|
|
ctx context.Context,
|
|
|
|
token string,
|
|
|
|
) (*api.OpenIDTokenAttributes, error) {
|
|
|
|
return d.OpenIDTokens.SelectOpenIDTokenAtrributes(ctx, token)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Database) CreateKeyBackup(
|
|
|
|
ctx context.Context, userID, algorithm string, authData json.RawMessage,
|
|
|
|
) (version string, err error) {
|
|
|
|
err = d.Writer.Do(d.DB, nil, func(txn *sql.Tx) error {
|
|
|
|
version, err = d.KeyBackupVersions.InsertKeyBackup(ctx, txn, userID, algorithm, authData, "")
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Database) UpdateKeyBackupAuthData(
|
|
|
|
ctx context.Context, userID, version string, authData json.RawMessage,
|
|
|
|
) (err error) {
|
|
|
|
err = d.Writer.Do(d.DB, nil, func(txn *sql.Tx) error {
|
|
|
|
return d.KeyBackupVersions.UpdateKeyBackupAuthData(ctx, txn, userID, version, authData)
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Database) DeleteKeyBackup(
|
|
|
|
ctx context.Context, userID, version string,
|
|
|
|
) (exists bool, err error) {
|
|
|
|
err = d.Writer.Do(d.DB, nil, func(txn *sql.Tx) error {
|
|
|
|
exists, err = d.KeyBackupVersions.DeleteKeyBackup(ctx, txn, userID, version)
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Database) GetKeyBackup(
|
|
|
|
ctx context.Context, userID, version string,
|
|
|
|
) (versionResult, algorithm string, authData json.RawMessage, etag string, deleted bool, err error) {
|
|
|
|
err = d.Writer.Do(d.DB, nil, func(txn *sql.Tx) error {
|
|
|
|
versionResult, algorithm, authData, etag, deleted, err = d.KeyBackupVersions.SelectKeyBackup(ctx, txn, userID, version)
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Database) GetBackupKeys(
|
|
|
|
ctx context.Context, version, userID, filterRoomID, filterSessionID string,
|
|
|
|
) (result map[string]map[string]api.KeyBackupSession, err error) {
|
|
|
|
err = d.Writer.Do(d.DB, nil, func(txn *sql.Tx) error {
|
|
|
|
if filterSessionID != "" {
|
|
|
|
result, err = d.KeyBackups.SelectKeysByRoomIDAndSessionID(ctx, txn, userID, version, filterRoomID, filterSessionID)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if filterRoomID != "" {
|
|
|
|
result, err = d.KeyBackups.SelectKeysByRoomID(ctx, txn, userID, version, filterRoomID)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
result, err = d.KeyBackups.SelectKeys(ctx, txn, userID, version)
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Database) CountBackupKeys(
|
|
|
|
ctx context.Context, version, userID string,
|
|
|
|
) (count int64, err error) {
|
|
|
|
err = d.Writer.Do(d.DB, nil, func(txn *sql.Tx) error {
|
|
|
|
count, err = d.KeyBackups.CountKeys(ctx, txn, userID, version)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// nolint:nakedret
|
|
|
|
func (d *Database) UpsertBackupKeys(
|
|
|
|
ctx context.Context, version, userID string, uploads []api.InternalKeyBackupSession,
|
|
|
|
) (count int64, etag string, err error) {
|
|
|
|
// wrap the following logic in a txn to ensure we atomically upload keys
|
|
|
|
err = d.Writer.Do(d.DB, nil, func(txn *sql.Tx) error {
|
|
|
|
_, _, _, oldETag, deleted, err := d.KeyBackupVersions.SelectKeyBackup(ctx, txn, userID, version)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if deleted {
|
|
|
|
return fmt.Errorf("backup was deleted")
|
|
|
|
}
|
|
|
|
// pull out all keys for this (user_id, version)
|
|
|
|
existingKeys, err := d.KeyBackups.SelectKeys(ctx, txn, userID, version)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
changed := false
|
|
|
|
// loop over all the new keys (which should be smaller than the set of backed up keys)
|
|
|
|
for _, newKey := range uploads {
|
|
|
|
// if we have a matching (room_id, session_id), we may need to update the key if it meets some rules, check them.
|
|
|
|
existingRoom := existingKeys[newKey.RoomID]
|
|
|
|
if existingRoom != nil {
|
|
|
|
existingSession, ok := existingRoom[newKey.SessionID]
|
|
|
|
if ok {
|
|
|
|
if existingSession.ShouldReplaceRoomKey(&newKey.KeyBackupSession) {
|
|
|
|
err = d.KeyBackups.UpdateBackupKey(ctx, txn, userID, version, newKey)
|
|
|
|
changed = true
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("d.KeyBackups.UpdateBackupKey: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// if we shouldn't replace the key we do nothing with it
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// if we're here, either the room or session are new, either way, we insert
|
|
|
|
err = d.KeyBackups.InsertBackupKey(ctx, txn, userID, version, newKey)
|
|
|
|
changed = true
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("d.KeyBackups.InsertBackupKey: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
count, err = d.KeyBackups.CountKeys(ctx, txn, userID, version)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if changed {
|
|
|
|
// update the etag
|
|
|
|
var newETag string
|
|
|
|
if oldETag == "" {
|
|
|
|
newETag = "1"
|
|
|
|
} else {
|
|
|
|
oldETagInt, err := strconv.ParseInt(oldETag, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to parse old etag: %s", err)
|
|
|
|
}
|
|
|
|
newETag = strconv.FormatInt(oldETagInt+1, 10)
|
|
|
|
}
|
|
|
|
etag = newETag
|
|
|
|
return d.KeyBackupVersions.UpdateKeyBackupETag(ctx, txn, userID, version, newETag)
|
|
|
|
} else {
|
|
|
|
etag = oldETag
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetDeviceByAccessToken returns the device matching the given access token.
|
|
|
|
// Returns sql.ErrNoRows if no matching device was found.
|
|
|
|
func (d *Database) GetDeviceByAccessToken(
|
|
|
|
ctx context.Context, token string,
|
|
|
|
) (*api.Device, error) {
|
|
|
|
return d.Devices.SelectDeviceByToken(ctx, token)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetDeviceByID returns the device matching the given ID.
|
|
|
|
// Returns sql.ErrNoRows if no matching device was found.
|
|
|
|
func (d *Database) GetDeviceByID(
|
2022-11-11 10:41:37 -06:00
|
|
|
ctx context.Context,
|
|
|
|
localpart string, serverName gomatrixserverlib.ServerName,
|
|
|
|
deviceID string,
|
2022-02-18 07:51:59 -06:00
|
|
|
) (*api.Device, error) {
|
2022-11-11 10:41:37 -06:00
|
|
|
return d.Devices.SelectDeviceByID(ctx, localpart, serverName, deviceID)
|
2022-02-18 07:51:59 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetDevicesByLocalpart returns the devices matching the given localpart.
|
|
|
|
func (d *Database) GetDevicesByLocalpart(
|
2022-11-11 10:41:37 -06:00
|
|
|
ctx context.Context,
|
|
|
|
localpart string, serverName gomatrixserverlib.ServerName,
|
2022-02-18 07:51:59 -06:00
|
|
|
) ([]api.Device, error) {
|
2022-11-11 10:41:37 -06:00
|
|
|
return d.Devices.SelectDevicesByLocalpart(ctx, nil, localpart, serverName, "")
|
2022-02-18 07:51:59 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Database) GetDevicesByID(ctx context.Context, deviceIDs []string) ([]api.Device, error) {
|
|
|
|
return d.Devices.SelectDevicesByID(ctx, deviceIDs)
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateDevice makes a new device associated with the given user ID localpart.
|
|
|
|
// If there is already a device with the same device ID for this user, that access token will be revoked
|
|
|
|
// and replaced with the given accessToken. If the given accessToken is already in use for another device,
|
|
|
|
// an error will be returned.
|
|
|
|
// If no device ID is given one is generated.
|
|
|
|
// Returns the device on success.
|
|
|
|
func (d *Database) CreateDevice(
|
2022-11-11 10:41:37 -06:00
|
|
|
ctx context.Context, localpart string, serverName gomatrixserverlib.ServerName,
|
|
|
|
deviceID *string, accessToken string, displayName *string, ipAddr, userAgent string,
|
2022-02-18 07:51:59 -06:00
|
|
|
) (dev *api.Device, returnErr error) {
|
|
|
|
if deviceID != nil {
|
2023-02-17 04:39:46 -06:00
|
|
|
_, ok := d.Writer.(*sqlutil.ExclusiveWriter)
|
|
|
|
if ok { // we're using most likely using SQLite, so do things a little different
|
|
|
|
returnErr = d.Writer.Do(d.DB, nil, func(txn *sql.Tx) error {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
devices, err := d.Devices.SelectDevicesByLocalpart(ctx, txn, localpart, serverName, "")
|
|
|
|
if err != nil && !errors.Is(err, sql.ErrNoRows) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// No devices yet, only create a new one
|
|
|
|
if len(devices) == 0 {
|
|
|
|
dev, err = d.Devices.InsertDevice(ctx, txn, *deviceID, localpart, serverName, accessToken, displayName, ipAddr, userAgent)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
sessionID := devices[0].SessionID + 1
|
|
|
|
|
|
|
|
// Revoke existing tokens for this device
|
|
|
|
if err = d.Devices.DeleteDevice(ctx, txn, *deviceID, localpart, serverName); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Create a new device with the session ID incremented
|
|
|
|
dev, err = d.Devices.InsertDeviceWithSessionID(ctx, txn, *deviceID, localpart, serverName, accessToken, displayName, ipAddr, userAgent, sessionID)
|
2022-02-18 07:51:59 -06:00
|
|
|
return err
|
2023-02-17 04:39:46 -06:00
|
|
|
})
|
|
|
|
} else {
|
|
|
|
returnErr = d.Writer.Do(d.DB, nil, func(txn *sql.Tx) error {
|
|
|
|
var err error
|
|
|
|
// Revoke existing tokens for this device
|
|
|
|
if err = d.Devices.DeleteDevice(ctx, txn, *deviceID, localpart, serverName); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-02-18 07:51:59 -06:00
|
|
|
|
2023-02-17 04:39:46 -06:00
|
|
|
dev, err = d.Devices.InsertDevice(ctx, txn, *deviceID, localpart, serverName, accessToken, displayName, ipAddr, userAgent)
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
}
|
2022-02-18 07:51:59 -06:00
|
|
|
} else {
|
|
|
|
// We generate device IDs in a loop in case its already taken.
|
|
|
|
// We cap this at going round 5 times to ensure we don't spin forever
|
|
|
|
var newDeviceID string
|
|
|
|
for i := 1; i <= 5; i++ {
|
|
|
|
newDeviceID, returnErr = generateDeviceID()
|
|
|
|
if returnErr != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
returnErr = d.Writer.Do(d.DB, nil, func(txn *sql.Tx) error {
|
|
|
|
var err error
|
2022-11-11 10:41:37 -06:00
|
|
|
dev, err = d.Devices.InsertDevice(ctx, txn, newDeviceID, localpart, serverName, accessToken, displayName, ipAddr, userAgent)
|
2022-02-18 07:51:59 -06:00
|
|
|
return err
|
|
|
|
})
|
|
|
|
if returnErr == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-02-17 04:39:46 -06:00
|
|
|
return dev, returnErr
|
2022-02-18 07:51:59 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// generateDeviceID creates a new device id. Returns an error if failed to generate
|
|
|
|
// random bytes.
|
|
|
|
func generateDeviceID() (string, error) {
|
|
|
|
b := make([]byte, deviceIDByteLength)
|
|
|
|
_, err := rand.Read(b)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
// url-safe no padding
|
|
|
|
return base64.RawURLEncoding.EncodeToString(b), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateDevice updates the given device with the display name.
|
|
|
|
// Returns SQL error if there are problems and nil on success.
|
|
|
|
func (d *Database) UpdateDevice(
|
2022-11-11 10:41:37 -06:00
|
|
|
ctx context.Context,
|
|
|
|
localpart string, serverName gomatrixserverlib.ServerName,
|
|
|
|
deviceID string, displayName *string,
|
2022-02-18 07:51:59 -06:00
|
|
|
) error {
|
|
|
|
return d.Writer.Do(d.DB, nil, func(txn *sql.Tx) error {
|
2022-11-11 10:41:37 -06:00
|
|
|
return d.Devices.UpdateDeviceName(ctx, txn, localpart, serverName, deviceID, displayName)
|
2022-02-18 07:51:59 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveDevices revokes one or more devices by deleting the entry in the database
|
|
|
|
// matching with the given device IDs and user ID localpart.
|
|
|
|
// If the devices don't exist, it will not return an error
|
|
|
|
// If something went wrong during the deletion, it will return the SQL error.
|
|
|
|
func (d *Database) RemoveDevices(
|
2022-11-11 10:41:37 -06:00
|
|
|
ctx context.Context,
|
|
|
|
localpart string, serverName gomatrixserverlib.ServerName,
|
|
|
|
devices []string,
|
2022-02-18 07:51:59 -06:00
|
|
|
) error {
|
|
|
|
return d.Writer.Do(d.DB, nil, func(txn *sql.Tx) error {
|
2022-11-11 10:41:37 -06:00
|
|
|
if err := d.Devices.DeleteDevices(ctx, txn, localpart, serverName, devices); err != sql.ErrNoRows {
|
2022-02-18 07:51:59 -06:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveAllDevices revokes devices by deleting the entry in the
|
|
|
|
// database matching the given user ID localpart.
|
|
|
|
// If something went wrong during the deletion, it will return the SQL error.
|
|
|
|
func (d *Database) RemoveAllDevices(
|
2022-11-11 10:41:37 -06:00
|
|
|
ctx context.Context,
|
|
|
|
localpart string, serverName gomatrixserverlib.ServerName,
|
|
|
|
exceptDeviceID string,
|
2022-02-18 07:51:59 -06:00
|
|
|
) (devices []api.Device, err error) {
|
|
|
|
err = d.Writer.Do(d.DB, nil, func(txn *sql.Tx) error {
|
2022-11-11 10:41:37 -06:00
|
|
|
devices, err = d.Devices.SelectDevicesByLocalpart(ctx, txn, localpart, serverName, exceptDeviceID)
|
2022-02-18 07:51:59 -06:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-11-11 10:41:37 -06:00
|
|
|
if err := d.Devices.DeleteDevicesByLocalpart(ctx, txn, localpart, serverName, exceptDeviceID); err != sql.ErrNoRows {
|
2022-02-18 07:51:59 -06:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-05-04 12:04:28 -05:00
|
|
|
// UpdateDeviceLastSeen updates a last seen timestamp and the ip address.
|
2022-11-11 10:41:37 -06:00
|
|
|
func (d *Database) UpdateDeviceLastSeen(ctx context.Context, localpart string, serverName gomatrixserverlib.ServerName, deviceID, ipAddr, userAgent string) error {
|
2022-02-18 07:51:59 -06:00
|
|
|
return d.Writer.Do(d.DB, nil, func(txn *sql.Tx) error {
|
2022-11-11 10:41:37 -06:00
|
|
|
return d.Devices.UpdateDeviceLastSeen(ctx, txn, localpart, serverName, deviceID, ipAddr, userAgent)
|
2022-02-18 07:51:59 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateLoginToken generates a token, stores and returns it. The lifetime is
|
|
|
|
// determined by the loginTokenLifetime given to the Database constructor.
|
|
|
|
func (d *Database) CreateLoginToken(ctx context.Context, data *api.LoginTokenData) (*api.LoginTokenMetadata, error) {
|
|
|
|
tok, err := generateLoginToken()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
meta := &api.LoginTokenMetadata{
|
|
|
|
Token: tok,
|
|
|
|
Expiration: time.Now().Add(d.LoginTokenLifetime),
|
|
|
|
}
|
|
|
|
|
|
|
|
err = d.Writer.Do(d.DB, nil, func(txn *sql.Tx) error {
|
|
|
|
return d.LoginTokens.InsertLoginToken(ctx, txn, meta, data)
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return meta, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateLoginToken() (string, error) {
|
|
|
|
b := make([]byte, loginTokenByteLength)
|
|
|
|
_, err := rand.Read(b)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return base64.RawURLEncoding.EncodeToString(b), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveLoginToken removes the named token (and may clean up other expired tokens).
|
|
|
|
func (d *Database) RemoveLoginToken(ctx context.Context, token string) error {
|
|
|
|
return d.Writer.Do(d.DB, nil, func(txn *sql.Tx) error {
|
|
|
|
return d.LoginTokens.DeleteLoginToken(ctx, txn, token)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetLoginTokenDataByToken returns the data associated with the given token.
|
|
|
|
// May return sql.ErrNoRows.
|
|
|
|
func (d *Database) GetLoginTokenDataByToken(ctx context.Context, token string) (*api.LoginTokenData, error) {
|
|
|
|
return d.LoginTokens.SelectLoginToken(ctx, token)
|
|
|
|
}
|
2022-03-03 05:40:53 -06:00
|
|
|
|
2022-11-11 10:41:37 -06:00
|
|
|
func (d *Database) InsertNotification(ctx context.Context, localpart string, serverName gomatrixserverlib.ServerName, eventID string, pos uint64, tweaks map[string]interface{}, n *api.Notification) error {
|
2022-03-03 05:40:53 -06:00
|
|
|
return d.Writer.Do(d.DB, nil, func(txn *sql.Tx) error {
|
2022-11-11 10:41:37 -06:00
|
|
|
return d.Notifications.Insert(ctx, txn, localpart, serverName, eventID, pos, pushrules.BoolTweakOr(tweaks, pushrules.HighlightTweak, false), n)
|
2022-03-03 05:40:53 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-11-11 10:41:37 -06:00
|
|
|
func (d *Database) DeleteNotificationsUpTo(ctx context.Context, localpart string, serverName gomatrixserverlib.ServerName, roomID string, pos uint64) (affected bool, err error) {
|
2022-03-03 05:40:53 -06:00
|
|
|
err = d.Writer.Do(d.DB, nil, func(txn *sql.Tx) error {
|
2022-11-11 10:41:37 -06:00
|
|
|
affected, err = d.Notifications.DeleteUpTo(ctx, txn, localpart, serverName, roomID, pos)
|
2022-03-03 05:40:53 -06:00
|
|
|
return err
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-11 10:41:37 -06:00
|
|
|
func (d *Database) SetNotificationsRead(ctx context.Context, localpart string, serverName gomatrixserverlib.ServerName, roomID string, pos uint64, b bool) (affected bool, err error) {
|
2022-03-03 05:40:53 -06:00
|
|
|
err = d.Writer.Do(d.DB, nil, func(txn *sql.Tx) error {
|
2022-11-11 10:41:37 -06:00
|
|
|
affected, err = d.Notifications.UpdateRead(ctx, txn, localpart, serverName, roomID, pos, b)
|
2022-03-03 05:40:53 -06:00
|
|
|
return err
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-11 10:41:37 -06:00
|
|
|
func (d *Database) GetNotifications(ctx context.Context, localpart string, serverName gomatrixserverlib.ServerName, fromID int64, limit int, filter tables.NotificationFilter) ([]*api.Notification, int64, error) {
|
|
|
|
return d.Notifications.Select(ctx, nil, localpart, serverName, fromID, limit, filter)
|
2022-03-03 05:40:53 -06:00
|
|
|
}
|
|
|
|
|
2022-11-11 10:41:37 -06:00
|
|
|
func (d *Database) GetNotificationCount(ctx context.Context, localpart string, serverName gomatrixserverlib.ServerName, filter tables.NotificationFilter) (int64, error) {
|
|
|
|
return d.Notifications.SelectCount(ctx, nil, localpart, serverName, filter)
|
2022-03-03 05:40:53 -06:00
|
|
|
}
|
|
|
|
|
2022-11-11 10:41:37 -06:00
|
|
|
func (d *Database) GetRoomNotificationCounts(ctx context.Context, localpart string, serverName gomatrixserverlib.ServerName, roomID string) (total int64, highlight int64, _ error) {
|
|
|
|
return d.Notifications.SelectRoomCounts(ctx, nil, localpart, serverName, roomID)
|
2022-03-03 05:40:53 -06:00
|
|
|
}
|
|
|
|
|
2022-03-03 10:45:06 -06:00
|
|
|
func (d *Database) DeleteOldNotifications(ctx context.Context) error {
|
2022-09-09 06:59:28 -05:00
|
|
|
return d.Writer.Do(d.DB, nil, func(txn *sql.Tx) error {
|
|
|
|
return d.Notifications.Clean(ctx, txn)
|
|
|
|
})
|
2022-03-03 10:45:06 -06:00
|
|
|
}
|
|
|
|
|
2022-03-03 05:40:53 -06:00
|
|
|
func (d *Database) UpsertPusher(
|
2022-11-11 10:41:37 -06:00
|
|
|
ctx context.Context, p api.Pusher,
|
|
|
|
localpart string, serverName gomatrixserverlib.ServerName,
|
2022-03-03 05:40:53 -06:00
|
|
|
) error {
|
|
|
|
data, err := json.Marshal(p.Data)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return d.Writer.Do(d.DB, nil, func(txn *sql.Tx) error {
|
|
|
|
return d.Pushers.InsertPusher(
|
|
|
|
ctx, txn,
|
|
|
|
p.SessionID,
|
|
|
|
p.PushKey,
|
|
|
|
p.PushKeyTS,
|
|
|
|
p.Kind,
|
|
|
|
p.AppID,
|
|
|
|
p.AppDisplayName,
|
|
|
|
p.DeviceDisplayName,
|
|
|
|
p.ProfileTag,
|
|
|
|
p.Language,
|
|
|
|
string(data),
|
2022-11-11 10:41:37 -06:00
|
|
|
localpart,
|
|
|
|
serverName)
|
2022-03-03 05:40:53 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetPushers returns the pushers matching the given localpart.
|
|
|
|
func (d *Database) GetPushers(
|
2022-11-11 10:41:37 -06:00
|
|
|
ctx context.Context, localpart string, serverName gomatrixserverlib.ServerName,
|
2022-03-03 05:40:53 -06:00
|
|
|
) ([]api.Pusher, error) {
|
2022-11-11 10:41:37 -06:00
|
|
|
return d.Pushers.SelectPushers(ctx, nil, localpart, serverName)
|
2022-03-03 05:40:53 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// RemovePusher deletes one pusher
|
|
|
|
// Invoked when `append` is true and `kind` is null in
|
|
|
|
// https://matrix.org/docs/spec/client_server/r0.6.1#post-matrix-client-r0-pushers-set
|
|
|
|
func (d *Database) RemovePusher(
|
2022-11-11 10:41:37 -06:00
|
|
|
ctx context.Context, appid, pushkey, localpart string, serverName gomatrixserverlib.ServerName,
|
2022-03-03 05:40:53 -06:00
|
|
|
) error {
|
2022-09-26 03:45:35 -05:00
|
|
|
return d.Writer.Do(d.DB, nil, func(txn *sql.Tx) error {
|
2022-11-11 10:41:37 -06:00
|
|
|
err := d.Pushers.DeletePusher(ctx, txn, appid, pushkey, localpart, serverName)
|
2022-03-03 05:40:53 -06:00
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemovePushers deletes all pushers that match given App Id and Push Key pair.
|
|
|
|
// Invoked when `append` parameter is false in
|
|
|
|
// https://matrix.org/docs/spec/client_server/r0.6.1#post-matrix-client-r0-pushers-set
|
|
|
|
func (d *Database) RemovePushers(
|
|
|
|
ctx context.Context, appid, pushkey string,
|
|
|
|
) error {
|
2022-09-26 03:45:35 -05:00
|
|
|
return d.Writer.Do(d.DB, nil, func(txn *sql.Tx) error {
|
2022-03-03 05:40:53 -06:00
|
|
|
return d.Pushers.DeletePushers(ctx, txn, appid, pushkey)
|
|
|
|
})
|
|
|
|
}
|
2022-05-04 12:04:28 -05:00
|
|
|
|
|
|
|
// UserStatistics populates types.UserStatistics, used in reports.
|
|
|
|
func (d *Database) UserStatistics(ctx context.Context) (*types.UserStatistics, *types.DatabaseEngine, error) {
|
|
|
|
return d.Stats.UserStatistics(ctx, nil)
|
|
|
|
}
|
2022-11-02 05:18:11 -05:00
|
|
|
|
|
|
|
func (d *Database) UpsertDailyRoomsMessages(ctx context.Context, serverName gomatrixserverlib.ServerName, stats types.MessageStats, activeRooms, activeE2EERooms int64) error {
|
|
|
|
return d.Writer.Do(d.DB, nil, func(txn *sql.Tx) error {
|
|
|
|
return d.Stats.UpsertDailyStats(ctx, txn, serverName, stats, activeRooms, activeE2EERooms)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Database) DailyRoomsMessages(
|
|
|
|
ctx context.Context, serverName gomatrixserverlib.ServerName,
|
|
|
|
) (stats types.MessageStats, activeRooms, activeE2EERooms int64, err error) {
|
|
|
|
return d.Stats.DailyRoomsMessages(ctx, nil, serverName)
|
|
|
|
}
|
2023-02-20 07:58:03 -06:00
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
func (d *KeyDatabase) ExistingOneTimeKeys(ctx context.Context, userID, deviceID string, keyIDsWithAlgorithms []string) (map[string]json.RawMessage, error) {
|
|
|
|
return d.OneTimeKeysTable.SelectOneTimeKeys(ctx, userID, deviceID, keyIDsWithAlgorithms)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *KeyDatabase) StoreOneTimeKeys(ctx context.Context, keys api.OneTimeKeys) (counts *api.OneTimeKeysCount, err error) {
|
|
|
|
_ = d.Writer.Do(d.DB, nil, func(txn *sql.Tx) error {
|
|
|
|
counts, err = d.OneTimeKeysTable.InsertOneTimeKeys(ctx, txn, keys)
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *KeyDatabase) OneTimeKeysCount(ctx context.Context, userID, deviceID string) (*api.OneTimeKeysCount, error) {
|
|
|
|
return d.OneTimeKeysTable.CountOneTimeKeys(ctx, userID, deviceID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *KeyDatabase) DeviceKeysJSON(ctx context.Context, keys []api.DeviceMessage) error {
|
|
|
|
return d.DeviceKeysTable.SelectDeviceKeysJSON(ctx, keys)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *KeyDatabase) PrevIDsExists(ctx context.Context, userID string, prevIDs []int64) (bool, error) {
|
|
|
|
count, err := d.DeviceKeysTable.CountStreamIDsForUser(ctx, userID, prevIDs)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return count == len(prevIDs), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *KeyDatabase) StoreRemoteDeviceKeys(ctx context.Context, keys []api.DeviceMessage, clearUserIDs []string) error {
|
|
|
|
return d.Writer.Do(d.DB, nil, func(txn *sql.Tx) error {
|
|
|
|
for _, userID := range clearUserIDs {
|
|
|
|
err := d.DeviceKeysTable.DeleteAllDeviceKeys(ctx, txn, userID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return d.DeviceKeysTable.InsertDeviceKeys(ctx, txn, keys)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *KeyDatabase) StoreLocalDeviceKeys(ctx context.Context, keys []api.DeviceMessage) error {
|
|
|
|
// work out the latest stream IDs for each user
|
|
|
|
userIDToStreamID := make(map[string]int64)
|
|
|
|
for _, k := range keys {
|
|
|
|
userIDToStreamID[k.UserID] = 0
|
|
|
|
}
|
|
|
|
return d.Writer.Do(d.DB, nil, func(txn *sql.Tx) error {
|
|
|
|
for userID := range userIDToStreamID {
|
|
|
|
streamID, err := d.DeviceKeysTable.SelectMaxStreamIDForUser(ctx, txn, userID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
userIDToStreamID[userID] = streamID
|
|
|
|
}
|
|
|
|
// set the stream IDs for each key
|
|
|
|
for i := range keys {
|
|
|
|
k := keys[i]
|
|
|
|
userIDToStreamID[k.UserID]++ // start stream from 1
|
|
|
|
k.StreamID = userIDToStreamID[k.UserID]
|
|
|
|
keys[i] = k
|
|
|
|
}
|
|
|
|
return d.DeviceKeysTable.InsertDeviceKeys(ctx, txn, keys)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *KeyDatabase) DeviceKeysForUser(ctx context.Context, userID string, deviceIDs []string, includeEmpty bool) ([]api.DeviceMessage, error) {
|
|
|
|
return d.DeviceKeysTable.SelectBatchDeviceKeys(ctx, userID, deviceIDs, includeEmpty)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *KeyDatabase) ClaimKeys(ctx context.Context, userToDeviceToAlgorithm map[string]map[string]string) ([]api.OneTimeKeys, error) {
|
|
|
|
var result []api.OneTimeKeys
|
|
|
|
err := d.Writer.Do(d.DB, nil, func(txn *sql.Tx) error {
|
|
|
|
for userID, deviceToAlgo := range userToDeviceToAlgorithm {
|
|
|
|
for deviceID, algo := range deviceToAlgo {
|
|
|
|
keyJSON, err := d.OneTimeKeysTable.SelectAndDeleteOneTimeKey(ctx, txn, userID, deviceID, algo)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if keyJSON != nil {
|
|
|
|
result = append(result, api.OneTimeKeys{
|
|
|
|
UserID: userID,
|
|
|
|
DeviceID: deviceID,
|
|
|
|
KeyJSON: keyJSON,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *KeyDatabase) StoreKeyChange(ctx context.Context, userID string) (id int64, err error) {
|
|
|
|
err = d.Writer.Do(nil, nil, func(_ *sql.Tx) error {
|
|
|
|
id, err = d.KeyChangesTable.InsertKeyChange(ctx, userID)
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *KeyDatabase) KeyChanges(ctx context.Context, fromOffset, toOffset int64) (userIDs []string, latestOffset int64, err error) {
|
|
|
|
return d.KeyChangesTable.SelectKeyChanges(ctx, fromOffset, toOffset)
|
|
|
|
}
|
|
|
|
|
|
|
|
// StaleDeviceLists returns a list of user IDs ending with the domains provided who have stale device lists.
|
|
|
|
// If no domains are given, all user IDs with stale device lists are returned.
|
|
|
|
func (d *KeyDatabase) StaleDeviceLists(ctx context.Context, domains []gomatrixserverlib.ServerName) ([]string, error) {
|
|
|
|
return d.StaleDeviceListsTable.SelectUserIDsWithStaleDeviceLists(ctx, domains)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarkDeviceListStale sets the stale bit for this user to isStale.
|
|
|
|
func (d *KeyDatabase) MarkDeviceListStale(ctx context.Context, userID string, isStale bool) error {
|
|
|
|
return d.Writer.Do(nil, nil, func(_ *sql.Tx) error {
|
|
|
|
return d.StaleDeviceListsTable.InsertStaleDeviceList(ctx, userID, isStale)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteDeviceKeys removes the device keys for a given user/device, and any accompanying
|
|
|
|
// cross-signing signatures relating to that device.
|
|
|
|
func (d *KeyDatabase) DeleteDeviceKeys(ctx context.Context, userID string, deviceIDs []gomatrixserverlib.KeyID) error {
|
|
|
|
return d.Writer.Do(d.DB, nil, func(txn *sql.Tx) error {
|
|
|
|
for _, deviceID := range deviceIDs {
|
|
|
|
if err := d.CrossSigningSigsTable.DeleteCrossSigningSigsForTarget(ctx, txn, userID, deviceID); err != nil && err != sql.ErrNoRows {
|
|
|
|
return fmt.Errorf("d.CrossSigningSigsTable.DeleteCrossSigningSigsForTarget: %w", err)
|
|
|
|
}
|
|
|
|
if err := d.DeviceKeysTable.DeleteDeviceKeys(ctx, txn, userID, string(deviceID)); err != nil && err != sql.ErrNoRows {
|
|
|
|
return fmt.Errorf("d.DeviceKeysTable.DeleteDeviceKeys: %w", err)
|
|
|
|
}
|
|
|
|
if err := d.OneTimeKeysTable.DeleteOneTimeKeys(ctx, txn, userID, string(deviceID)); err != nil && err != sql.ErrNoRows {
|
|
|
|
return fmt.Errorf("d.OneTimeKeysTable.DeleteOneTimeKeys: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// CrossSigningKeysForUser returns the latest known cross-signing keys for a user, if any.
|
2023-04-06 03:55:01 -05:00
|
|
|
func (d *KeyDatabase) CrossSigningKeysForUser(ctx context.Context, userID string) (map[fclient.CrossSigningKeyPurpose]fclient.CrossSigningKey, error) {
|
2023-02-20 07:58:03 -06:00
|
|
|
keyMap, err := d.CrossSigningKeysTable.SelectCrossSigningKeysForUser(ctx, nil, userID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("d.CrossSigningKeysTable.SelectCrossSigningKeysForUser: %w", err)
|
|
|
|
}
|
2023-04-06 03:55:01 -05:00
|
|
|
results := map[fclient.CrossSigningKeyPurpose]fclient.CrossSigningKey{}
|
2023-02-20 07:58:03 -06:00
|
|
|
for purpose, key := range keyMap {
|
|
|
|
keyID := gomatrixserverlib.KeyID("ed25519:" + key.Encode())
|
2023-04-06 03:55:01 -05:00
|
|
|
result := fclient.CrossSigningKey{
|
2023-02-20 07:58:03 -06:00
|
|
|
UserID: userID,
|
2023-04-06 03:55:01 -05:00
|
|
|
Usage: []fclient.CrossSigningKeyPurpose{purpose},
|
2023-02-20 07:58:03 -06:00
|
|
|
Keys: map[gomatrixserverlib.KeyID]gomatrixserverlib.Base64Bytes{
|
|
|
|
keyID: key,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
sigMap, err := d.CrossSigningSigsTable.SelectCrossSigningSigsForTarget(ctx, nil, userID, userID, keyID)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for sigUserID, forSigUserID := range sigMap {
|
|
|
|
if userID != sigUserID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if result.Signatures == nil {
|
|
|
|
result.Signatures = map[string]map[gomatrixserverlib.KeyID]gomatrixserverlib.Base64Bytes{}
|
|
|
|
}
|
|
|
|
if _, ok := result.Signatures[sigUserID]; !ok {
|
|
|
|
result.Signatures[sigUserID] = map[gomatrixserverlib.KeyID]gomatrixserverlib.Base64Bytes{}
|
|
|
|
}
|
|
|
|
for sigKeyID, sigBytes := range forSigUserID {
|
|
|
|
result.Signatures[sigUserID][sigKeyID] = sigBytes
|
|
|
|
}
|
|
|
|
}
|
|
|
|
results[purpose] = result
|
|
|
|
}
|
|
|
|
return results, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CrossSigningKeysForUser returns the latest known cross-signing keys for a user, if any.
|
|
|
|
func (d *KeyDatabase) CrossSigningKeysDataForUser(ctx context.Context, userID string) (types.CrossSigningKeyMap, error) {
|
|
|
|
return d.CrossSigningKeysTable.SelectCrossSigningKeysForUser(ctx, nil, userID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// CrossSigningSigsForTarget returns the signatures for a given user's key ID, if any.
|
|
|
|
func (d *KeyDatabase) CrossSigningSigsForTarget(ctx context.Context, originUserID, targetUserID string, targetKeyID gomatrixserverlib.KeyID) (types.CrossSigningSigMap, error) {
|
|
|
|
return d.CrossSigningSigsTable.SelectCrossSigningSigsForTarget(ctx, nil, originUserID, targetUserID, targetKeyID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// StoreCrossSigningKeysForUser stores the latest known cross-signing keys for a user.
|
|
|
|
func (d *KeyDatabase) StoreCrossSigningKeysForUser(ctx context.Context, userID string, keyMap types.CrossSigningKeyMap) error {
|
|
|
|
return d.Writer.Do(d.DB, nil, func(txn *sql.Tx) error {
|
|
|
|
for keyType, keyData := range keyMap {
|
|
|
|
if err := d.CrossSigningKeysTable.UpsertCrossSigningKeysForUser(ctx, txn, userID, keyType, keyData); err != nil {
|
|
|
|
return fmt.Errorf("d.CrossSigningKeysTable.InsertCrossSigningKeysForUser: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// StoreCrossSigningSigsForTarget stores a signature for a target user ID and key/dvice.
|
|
|
|
func (d *KeyDatabase) StoreCrossSigningSigsForTarget(
|
|
|
|
ctx context.Context,
|
|
|
|
originUserID string, originKeyID gomatrixserverlib.KeyID,
|
|
|
|
targetUserID string, targetKeyID gomatrixserverlib.KeyID,
|
|
|
|
signature gomatrixserverlib.Base64Bytes,
|
|
|
|
) error {
|
|
|
|
return d.Writer.Do(d.DB, nil, func(txn *sql.Tx) error {
|
|
|
|
if err := d.CrossSigningSigsTable.UpsertCrossSigningSigsForTarget(ctx, nil, originUserID, originKeyID, targetUserID, targetKeyID, signature); err != nil {
|
|
|
|
return fmt.Errorf("d.CrossSigningSigsTable.InsertCrossSigningSigsForTarget: %w", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteStaleDeviceLists deletes stale device list entries for users we don't share a room with anymore.
|
|
|
|
func (d *KeyDatabase) DeleteStaleDeviceLists(
|
|
|
|
ctx context.Context,
|
|
|
|
userIDs []string,
|
|
|
|
) error {
|
|
|
|
return d.Writer.Do(d.DB, nil, func(txn *sql.Tx) error {
|
|
|
|
return d.StaleDeviceListsTable.DeleteStaleDeviceLists(ctx, txn, userIDs)
|
|
|
|
})
|
|
|
|
}
|