2017-07-26 15:53:11 +02: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.
|
|
|
|
|
2020-02-13 18:27:33 +01:00
|
|
|
package postgres
|
2017-07-26 15:53:11 +02:00
|
|
|
|
|
|
|
import (
|
2017-09-18 15:15:27 +02:00
|
|
|
"context"
|
2017-07-26 15:53:11 +02:00
|
|
|
"database/sql"
|
2020-06-18 19:36:03 +02:00
|
|
|
"encoding/json"
|
2017-07-26 15:53:11 +02:00
|
|
|
|
2020-05-21 15:40:13 +02:00
|
|
|
"github.com/matrix-org/dendrite/internal"
|
2020-09-24 12:10:14 +02:00
|
|
|
"github.com/matrix-org/dendrite/internal/sqlutil"
|
2022-02-18 14:51:59 +01:00
|
|
|
"github.com/matrix-org/dendrite/userapi/storage/tables"
|
2022-11-11 17:41:37 +01:00
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
2017-07-26 15:53:11 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const accountDataSchema = `
|
|
|
|
-- Stores data about accounts data.
|
2022-10-18 16:59:08 +02:00
|
|
|
CREATE TABLE IF NOT EXISTS userapi_account_datas (
|
2017-07-26 15:53:11 +02:00
|
|
|
-- The Matrix user ID localpart for this account
|
|
|
|
localpart TEXT NOT NULL,
|
2022-11-11 17:41:37 +01:00
|
|
|
server_name TEXT NOT NULL,
|
2017-07-26 15:53:11 +02:00
|
|
|
-- The room ID for this data (empty string if not specific to a room)
|
|
|
|
room_id TEXT,
|
|
|
|
-- The account data type
|
|
|
|
type TEXT NOT NULL,
|
|
|
|
-- The account data content
|
2022-11-11 17:41:37 +01:00
|
|
|
content TEXT NOT NULL
|
2017-07-26 15:53:11 +02:00
|
|
|
);
|
2022-11-11 17:41:37 +01:00
|
|
|
|
|
|
|
CREATE UNIQUE INDEX IF NOT EXISTS userapi_account_datas_idx ON userapi_account_datas(localpart, server_name, room_id, type);
|
2017-07-26 15:53:11 +02:00
|
|
|
`
|
|
|
|
|
|
|
|
const insertAccountDataSQL = `
|
2022-11-11 17:41:37 +01:00
|
|
|
INSERT INTO userapi_account_datas(localpart, server_name, room_id, type, content) VALUES($1, $2, $3, $4, $5)
|
|
|
|
ON CONFLICT (localpart, server_name, room_id, type) DO UPDATE SET content = EXCLUDED.content
|
2017-07-26 15:53:11 +02:00
|
|
|
`
|
|
|
|
|
|
|
|
const selectAccountDataSQL = "" +
|
2022-11-11 17:41:37 +01:00
|
|
|
"SELECT room_id, type, content FROM userapi_account_datas WHERE localpart = $1 AND server_name = $2"
|
2017-07-26 15:53:11 +02:00
|
|
|
|
2017-08-02 17:21:35 +02:00
|
|
|
const selectAccountDataByTypeSQL = "" +
|
2022-11-11 17:41:37 +01:00
|
|
|
"SELECT content FROM userapi_account_datas WHERE localpart = $1 AND server_name = $2 AND room_id = $3 AND type = $4"
|
2017-08-02 17:21:35 +02:00
|
|
|
|
2017-07-26 15:53:11 +02:00
|
|
|
type accountDataStatements struct {
|
2017-08-02 17:21:35 +02:00
|
|
|
insertAccountDataStmt *sql.Stmt
|
|
|
|
selectAccountDataStmt *sql.Stmt
|
|
|
|
selectAccountDataByTypeStmt *sql.Stmt
|
2017-07-26 15:53:11 +02:00
|
|
|
}
|
|
|
|
|
2022-02-18 14:51:59 +01:00
|
|
|
func NewPostgresAccountDataTable(db *sql.DB) (tables.AccountDataTable, error) {
|
|
|
|
s := &accountDataStatements{}
|
|
|
|
_, err := db.Exec(accountDataSchema)
|
2017-07-26 15:53:11 +02:00
|
|
|
if err != nil {
|
2022-02-18 14:51:59 +01:00
|
|
|
return nil, err
|
2017-07-26 15:53:11 +02:00
|
|
|
}
|
2022-02-18 14:51:59 +01:00
|
|
|
return s, sqlutil.StatementList{
|
2021-07-28 19:30:04 +02:00
|
|
|
{&s.insertAccountDataStmt, insertAccountDataSQL},
|
|
|
|
{&s.selectAccountDataStmt, selectAccountDataSQL},
|
|
|
|
{&s.selectAccountDataByTypeStmt, selectAccountDataByTypeSQL},
|
|
|
|
}.Prepare(db)
|
2017-07-26 15:53:11 +02:00
|
|
|
}
|
|
|
|
|
2022-02-18 14:51:59 +01:00
|
|
|
func (s *accountDataStatements) InsertAccountData(
|
2022-11-11 17:41:37 +01:00
|
|
|
ctx context.Context, txn *sql.Tx,
|
|
|
|
localpart string, serverName gomatrixserverlib.ServerName,
|
|
|
|
roomID, dataType string, content json.RawMessage,
|
2017-09-18 15:15:27 +02:00
|
|
|
) (err error) {
|
2020-09-24 12:10:14 +02:00
|
|
|
stmt := sqlutil.TxStmt(txn, s.insertAccountDataStmt)
|
2022-11-11 17:41:37 +01:00
|
|
|
_, err = stmt.ExecContext(ctx, localpart, serverName, roomID, dataType, content)
|
2017-07-26 15:53:11 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-02-18 14:51:59 +01:00
|
|
|
func (s *accountDataStatements) SelectAccountData(
|
2022-11-11 17:41:37 +01:00
|
|
|
ctx context.Context,
|
|
|
|
localpart string, serverName gomatrixserverlib.ServerName,
|
2017-09-18 15:15:27 +02:00
|
|
|
) (
|
2020-06-18 19:36:03 +02:00
|
|
|
/* global */ map[string]json.RawMessage,
|
|
|
|
/* rooms */ map[string]map[string]json.RawMessage,
|
|
|
|
error,
|
2017-07-26 15:53:11 +02:00
|
|
|
) {
|
2022-11-11 17:41:37 +01:00
|
|
|
rows, err := s.selectAccountDataStmt.QueryContext(ctx, localpart, serverName)
|
2017-07-26 15:53:11 +02:00
|
|
|
if err != nil {
|
2020-06-18 19:36:03 +02:00
|
|
|
return nil, nil, err
|
2017-07-26 15:53:11 +02:00
|
|
|
}
|
2020-05-21 15:40:13 +02:00
|
|
|
defer internal.CloseAndLogIfError(ctx, rows, "selectAccountData: rows.close() failed")
|
2017-07-26 15:53:11 +02:00
|
|
|
|
2020-06-18 19:36:03 +02:00
|
|
|
global := map[string]json.RawMessage{}
|
|
|
|
rooms := map[string]map[string]json.RawMessage{}
|
2017-07-26 15:53:11 +02:00
|
|
|
|
|
|
|
for rows.Next() {
|
|
|
|
var roomID string
|
|
|
|
var dataType string
|
|
|
|
var content []byte
|
|
|
|
|
2017-09-18 15:15:27 +02:00
|
|
|
if err = rows.Scan(&roomID, &dataType, &content); err != nil {
|
2020-06-18 19:36:03 +02:00
|
|
|
return nil, nil, err
|
2017-07-26 15:53:11 +02:00
|
|
|
}
|
|
|
|
|
2020-06-18 19:36:03 +02:00
|
|
|
if roomID != "" {
|
|
|
|
if _, ok := rooms[roomID]; !ok {
|
|
|
|
rooms[roomID] = map[string]json.RawMessage{}
|
|
|
|
}
|
|
|
|
rooms[roomID][dataType] = content
|
2017-07-26 15:53:11 +02:00
|
|
|
} else {
|
2020-06-18 19:36:03 +02:00
|
|
|
global[dataType] = content
|
2017-07-26 15:53:11 +02:00
|
|
|
}
|
|
|
|
}
|
2020-06-18 19:36:03 +02:00
|
|
|
|
2020-02-11 15:12:21 +01:00
|
|
|
return global, rooms, rows.Err()
|
2017-07-26 15:53:11 +02:00
|
|
|
}
|
2017-08-02 17:21:35 +02:00
|
|
|
|
2022-02-18 14:51:59 +01:00
|
|
|
func (s *accountDataStatements) SelectAccountDataByType(
|
2022-11-11 17:41:37 +01:00
|
|
|
ctx context.Context,
|
|
|
|
localpart string, serverName gomatrixserverlib.ServerName,
|
|
|
|
roomID, dataType string,
|
2020-06-18 19:36:03 +02:00
|
|
|
) (data json.RawMessage, err error) {
|
|
|
|
var bytes []byte
|
2017-09-18 15:15:27 +02:00
|
|
|
stmt := s.selectAccountDataByTypeStmt
|
2022-11-11 17:41:37 +01:00
|
|
|
if err = stmt.QueryRowContext(ctx, localpart, serverName, roomID, dataType).Scan(&bytes); err != nil {
|
2019-10-07 14:15:58 +02:00
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2017-08-02 17:21:35 +02:00
|
|
|
return
|
|
|
|
}
|
2020-06-18 19:36:03 +02:00
|
|
|
data = json.RawMessage(bytes)
|
2017-08-02 17:21:35 +02:00
|
|
|
return
|
|
|
|
}
|