2017-10-10 15:28:49 +02:00
|
|
|
// Copyright 2017 Jan Christian Grünhage
|
|
|
|
//
|
|
|
|
// 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 sqlite3
|
2017-10-10 15:28:49 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2017-10-10 16:17:29 +02:00
|
|
|
"database/sql"
|
2019-07-24 18:08:51 +02:00
|
|
|
"encoding/json"
|
2020-02-20 10:28:03 +01:00
|
|
|
"fmt"
|
2017-11-14 10:56:23 +01:00
|
|
|
|
2022-09-28 11:18:03 +02:00
|
|
|
"github.com/matrix-org/dendrite/internal/sqlutil"
|
2020-06-26 16:34:41 +02:00
|
|
|
"github.com/matrix-org/dendrite/syncapi/storage/tables"
|
2017-11-14 10:56:23 +01:00
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
2017-10-10 15:28:49 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const filterSchema = `
|
|
|
|
-- Stores data about filters
|
2020-06-26 16:34:41 +02:00
|
|
|
CREATE TABLE IF NOT EXISTS syncapi_filter (
|
2017-10-10 15:28:49 +02:00
|
|
|
-- The filter
|
|
|
|
filter TEXT NOT NULL,
|
|
|
|
-- The ID
|
2020-02-13 18:27:33 +01:00
|
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
2017-10-10 15:28:49 +02:00
|
|
|
-- The localpart of the Matrix user ID associated to this filter
|
|
|
|
localpart TEXT NOT NULL,
|
|
|
|
|
2020-02-13 18:27:33 +01:00
|
|
|
UNIQUE (id, localpart)
|
2017-10-10 15:28:49 +02:00
|
|
|
);
|
|
|
|
|
2020-06-26 16:34:41 +02:00
|
|
|
CREATE INDEX IF NOT EXISTS syncapi_filter_localpart ON syncapi_filter(localpart);
|
2017-10-10 15:28:49 +02:00
|
|
|
`
|
|
|
|
|
|
|
|
const selectFilterSQL = "" +
|
2020-06-26 16:34:41 +02:00
|
|
|
"SELECT filter FROM syncapi_filter WHERE localpart = $1 AND id = $2"
|
2017-10-10 15:28:49 +02:00
|
|
|
|
2017-11-14 10:56:23 +01:00
|
|
|
const selectFilterIDByContentSQL = "" +
|
2020-06-26 16:34:41 +02:00
|
|
|
"SELECT id FROM syncapi_filter WHERE localpart = $1 AND filter = $2"
|
2017-11-14 10:56:23 +01:00
|
|
|
|
2017-10-10 15:28:49 +02:00
|
|
|
const insertFilterSQL = "" +
|
2020-06-26 16:34:41 +02:00
|
|
|
"INSERT INTO syncapi_filter (filter, localpart) VALUES ($1, $2)"
|
2020-02-13 18:27:33 +01:00
|
|
|
|
2017-10-10 15:28:49 +02:00
|
|
|
type filterStatements struct {
|
2020-07-21 16:48:21 +02:00
|
|
|
db *sql.DB
|
2020-02-20 10:28:03 +01:00
|
|
|
selectFilterStmt *sql.Stmt
|
|
|
|
selectFilterIDByContentStmt *sql.Stmt
|
|
|
|
insertFilterStmt *sql.Stmt
|
2017-10-10 15:28:49 +02:00
|
|
|
}
|
|
|
|
|
2020-06-26 16:34:41 +02:00
|
|
|
func NewSqliteFilterTable(db *sql.DB) (tables.Filter, error) {
|
|
|
|
_, err := db.Exec(filterSchema)
|
2017-10-10 15:28:49 +02:00
|
|
|
if err != nil {
|
2020-06-26 16:34:41 +02:00
|
|
|
return nil, err
|
2017-10-10 15:28:49 +02:00
|
|
|
}
|
2020-07-21 16:48:21 +02:00
|
|
|
s := &filterStatements{
|
2020-08-21 11:42:08 +02:00
|
|
|
db: db,
|
2020-07-21 16:48:21 +02:00
|
|
|
}
|
2017-10-10 15:28:49 +02:00
|
|
|
if s.selectFilterStmt, err = db.Prepare(selectFilterSQL); err != nil {
|
2020-06-26 16:34:41 +02:00
|
|
|
return nil, err
|
2017-10-10 15:28:49 +02:00
|
|
|
}
|
2017-11-14 10:56:23 +01:00
|
|
|
if s.selectFilterIDByContentStmt, err = db.Prepare(selectFilterIDByContentSQL); err != nil {
|
2020-06-26 16:34:41 +02:00
|
|
|
return nil, err
|
2017-11-14 10:56:23 +01:00
|
|
|
}
|
2017-10-10 15:28:49 +02:00
|
|
|
if s.insertFilterStmt, err = db.Prepare(insertFilterSQL); err != nil {
|
2020-06-26 16:34:41 +02:00
|
|
|
return nil, err
|
2017-10-10 15:28:49 +02:00
|
|
|
}
|
2020-06-26 16:34:41 +02:00
|
|
|
return s, nil
|
2017-10-10 15:28:49 +02:00
|
|
|
}
|
|
|
|
|
2020-06-26 16:34:41 +02:00
|
|
|
func (s *filterStatements) SelectFilter(
|
2022-09-28 11:18:03 +02:00
|
|
|
ctx context.Context, txn *sql.Tx, target *gomatrixserverlib.Filter, localpart string, filterID string,
|
2022-04-26 17:58:20 +02:00
|
|
|
) error {
|
2019-07-24 18:08:51 +02:00
|
|
|
// Retrieve filter from database (stored as canonical JSON)
|
|
|
|
var filterData []byte
|
2022-09-28 11:18:03 +02:00
|
|
|
err := sqlutil.TxStmt(txn, s.selectFilterStmt).QueryRowContext(ctx, localpart, filterID).Scan(&filterData)
|
2019-07-24 18:08:51 +02:00
|
|
|
if err != nil {
|
2022-04-26 17:58:20 +02:00
|
|
|
return err
|
2019-07-24 18:08:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Unmarshal JSON into Filter struct
|
2022-04-26 17:58:20 +02:00
|
|
|
if err = json.Unmarshal(filterData, &target); err != nil {
|
|
|
|
return err
|
2019-07-24 18:08:51 +02:00
|
|
|
}
|
2022-04-26 17:58:20 +02:00
|
|
|
return nil
|
2017-10-10 15:28:49 +02:00
|
|
|
}
|
|
|
|
|
2020-06-26 16:34:41 +02:00
|
|
|
func (s *filterStatements) InsertFilter(
|
2022-09-28 11:18:03 +02:00
|
|
|
ctx context.Context, txn *sql.Tx, filter *gomatrixserverlib.Filter, localpart string,
|
2017-11-14 10:56:23 +01:00
|
|
|
) (filterID string, err error) {
|
|
|
|
var existingFilterID string
|
|
|
|
|
2019-07-24 18:08:51 +02:00
|
|
|
// Serialise json
|
|
|
|
filterJSON, err := json.Marshal(filter)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
// Remove whitespaces and sort JSON data
|
|
|
|
// needed to prevent from inserting the same filter multiple times
|
|
|
|
filterJSON, err = gomatrixserverlib.CanonicalJSON(filterJSON)
|
2017-11-14 10:56:23 +01:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2020-08-21 11:42:08 +02:00
|
|
|
// Check if filter already exists in the database using its localpart and content
|
|
|
|
//
|
|
|
|
// This can result in a race condition when two clients try to insert the
|
|
|
|
// same filter and localpart at the same time, however this is not a
|
|
|
|
// problem as both calls will result in the same filterID
|
2022-09-28 11:18:03 +02:00
|
|
|
err = sqlutil.TxStmt(txn, s.selectFilterIDByContentStmt).QueryRowContext(
|
|
|
|
ctx, localpart, filterJSON,
|
|
|
|
).Scan(&existingFilterID)
|
2020-08-21 11:42:08 +02:00
|
|
|
if err != nil && err != sql.ErrNoRows {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
// If it does, return the existing ID
|
|
|
|
if existingFilterID != "" {
|
|
|
|
return existingFilterID, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise insert the filter and return the new ID
|
2022-09-28 11:18:03 +02:00
|
|
|
res, err := sqlutil.TxStmt(txn, s.insertFilterStmt).ExecContext(ctx, filterJSON, localpart)
|
2020-08-21 11:42:08 +02:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
rowid, err := res.LastInsertId()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
filterID = fmt.Sprintf("%d", rowid)
|
2017-10-10 15:28:49 +02:00
|
|
|
return
|
|
|
|
}
|