2017-10-10 13:28:49 +00: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 17:27:33 +00:00
|
|
|
package sqlite3
|
2017-10-10 13:28:49 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2017-10-10 14:17:29 +00:00
|
|
|
"database/sql"
|
2019-07-24 16:08:51 +00:00
|
|
|
"encoding/json"
|
2020-02-20 09:28:03 +00:00
|
|
|
"fmt"
|
2017-11-14 09:56:23 +00:00
|
|
|
|
2020-06-26 14:34:41 +00:00
|
|
|
"github.com/matrix-org/dendrite/syncapi/storage/tables"
|
2017-11-14 09:56:23 +00:00
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
2017-10-10 13:28:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const filterSchema = `
|
|
|
|
-- Stores data about filters
|
2020-06-26 14:34:41 +00:00
|
|
|
CREATE TABLE IF NOT EXISTS syncapi_filter (
|
2017-10-10 13:28:49 +00:00
|
|
|
-- The filter
|
|
|
|
filter TEXT NOT NULL,
|
|
|
|
-- The ID
|
2020-02-13 17:27:33 +00:00
|
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
2017-10-10 13:28:49 +00:00
|
|
|
-- The localpart of the Matrix user ID associated to this filter
|
|
|
|
localpart TEXT NOT NULL,
|
|
|
|
|
2020-02-13 17:27:33 +00:00
|
|
|
UNIQUE (id, localpart)
|
2017-10-10 13:28:49 +00:00
|
|
|
);
|
|
|
|
|
2020-06-26 14:34:41 +00:00
|
|
|
CREATE INDEX IF NOT EXISTS syncapi_filter_localpart ON syncapi_filter(localpart);
|
2017-10-10 13:28:49 +00:00
|
|
|
`
|
|
|
|
|
|
|
|
const selectFilterSQL = "" +
|
2020-06-26 14:34:41 +00:00
|
|
|
"SELECT filter FROM syncapi_filter WHERE localpart = $1 AND id = $2"
|
2017-10-10 13:28:49 +00:00
|
|
|
|
2017-11-14 09:56:23 +00:00
|
|
|
const selectFilterIDByContentSQL = "" +
|
2020-06-26 14:34:41 +00:00
|
|
|
"SELECT id FROM syncapi_filter WHERE localpart = $1 AND filter = $2"
|
2017-11-14 09:56:23 +00:00
|
|
|
|
2017-10-10 13:28:49 +00:00
|
|
|
const insertFilterSQL = "" +
|
2020-06-26 14:34:41 +00:00
|
|
|
"INSERT INTO syncapi_filter (filter, localpart) VALUES ($1, $2)"
|
2020-02-13 17:27:33 +00:00
|
|
|
|
2017-10-10 13:28:49 +00:00
|
|
|
type filterStatements struct {
|
2020-07-21 14:48:21 +00:00
|
|
|
db *sql.DB
|
2020-02-20 09:28:03 +00:00
|
|
|
selectFilterStmt *sql.Stmt
|
|
|
|
selectFilterIDByContentStmt *sql.Stmt
|
|
|
|
insertFilterStmt *sql.Stmt
|
2017-10-10 13:28:49 +00:00
|
|
|
}
|
|
|
|
|
2020-06-26 14:34:41 +00:00
|
|
|
func NewSqliteFilterTable(db *sql.DB) (tables.Filter, error) {
|
|
|
|
_, err := db.Exec(filterSchema)
|
2017-10-10 13:28:49 +00:00
|
|
|
if err != nil {
|
2020-06-26 14:34:41 +00:00
|
|
|
return nil, err
|
2017-10-10 13:28:49 +00:00
|
|
|
}
|
2020-07-21 14:48:21 +00:00
|
|
|
s := &filterStatements{
|
2020-08-21 09:42:08 +00:00
|
|
|
db: db,
|
2020-07-21 14:48:21 +00:00
|
|
|
}
|
2017-10-10 13:28:49 +00:00
|
|
|
if s.selectFilterStmt, err = db.Prepare(selectFilterSQL); err != nil {
|
2020-06-26 14:34:41 +00:00
|
|
|
return nil, err
|
2017-10-10 13:28:49 +00:00
|
|
|
}
|
2017-11-14 09:56:23 +00:00
|
|
|
if s.selectFilterIDByContentStmt, err = db.Prepare(selectFilterIDByContentSQL); err != nil {
|
2020-06-26 14:34:41 +00:00
|
|
|
return nil, err
|
2017-11-14 09:56:23 +00:00
|
|
|
}
|
2017-10-10 13:28:49 +00:00
|
|
|
if s.insertFilterStmt, err = db.Prepare(insertFilterSQL); err != nil {
|
2020-06-26 14:34:41 +00:00
|
|
|
return nil, err
|
2017-10-10 13:28:49 +00:00
|
|
|
}
|
2020-06-26 14:34:41 +00:00
|
|
|
return s, nil
|
2017-10-10 13:28:49 +00:00
|
|
|
}
|
|
|
|
|
2020-06-26 14:34:41 +00:00
|
|
|
func (s *filterStatements) SelectFilter(
|
2017-10-10 13:28:49 +00:00
|
|
|
ctx context.Context, localpart string, filterID string,
|
2019-07-24 16:08:51 +00:00
|
|
|
) (*gomatrixserverlib.Filter, error) {
|
|
|
|
// Retrieve filter from database (stored as canonical JSON)
|
|
|
|
var filterData []byte
|
|
|
|
err := s.selectFilterStmt.QueryRowContext(ctx, localpart, filterID).Scan(&filterData)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unmarshal JSON into Filter struct
|
2021-01-19 18:00:42 +00:00
|
|
|
filter := gomatrixserverlib.DefaultFilter()
|
2019-07-24 16:08:51 +00:00
|
|
|
if err = json.Unmarshal(filterData, &filter); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &filter, nil
|
2017-10-10 13:28:49 +00:00
|
|
|
}
|
|
|
|
|
2020-06-26 14:34:41 +00:00
|
|
|
func (s *filterStatements) InsertFilter(
|
2019-07-24 16:08:51 +00:00
|
|
|
ctx context.Context, filter *gomatrixserverlib.Filter, localpart string,
|
2017-11-14 09:56:23 +00:00
|
|
|
) (filterID string, err error) {
|
|
|
|
var existingFilterID string
|
|
|
|
|
2019-07-24 16:08:51 +00: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 09:56:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2020-08-21 09:42:08 +00: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
|
|
|
|
err = s.selectFilterIDByContentStmt.QueryRowContext(ctx,
|
|
|
|
localpart, filterJSON).Scan(&existingFilterID)
|
|
|
|
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
|
|
|
|
res, err := s.insertFilterStmt.ExecContext(ctx, filterJSON, localpart)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
rowid, err := res.LastInsertId()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
filterID = fmt.Sprintf("%d", rowid)
|
2017-10-10 13:28:49 +00:00
|
|
|
return
|
|
|
|
}
|