mirror of
https://github.com/matrix-org/dendrite.git
synced 2025-12-12 09:23:09 -06:00
Canonicalize JSON and mention race condition
Signed-off-by: Andrew (anoa) <anoa@openmailbox.org>
This commit is contained in:
parent
5a09d5123d
commit
8c217e9f6e
|
|
@ -17,6 +17,8 @@ package accounts
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
|
|
||||||
|
"github.com/matrix-org/gomatrixserverlib"
|
||||||
)
|
)
|
||||||
|
|
||||||
const filterSchema = `
|
const filterSchema = `
|
||||||
|
|
@ -70,7 +72,7 @@ func (s *filterStatements) prepare(db *sql.DB) (err error) {
|
||||||
func (s *filterStatements) selectFilter(
|
func (s *filterStatements) selectFilter(
|
||||||
ctx context.Context, localpart string, filterID string,
|
ctx context.Context, localpart string, filterID string,
|
||||||
) (filter string, err error) {
|
) (filter string, err error) {
|
||||||
err = s.selectFilterStmt.QueryRowContext(ctx, localpart, filterID).Scan(&filter)
|
err = s.selectFilterStmt.QueryRowContext(ctx, localpart, filterID).Scan(&filterJSON)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -79,13 +81,21 @@ func (s *filterStatements) insertFilter(
|
||||||
) (pos string, err error) {
|
) (pos string, err error) {
|
||||||
var existingFilter string
|
var existingFilter string
|
||||||
|
|
||||||
|
// 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
|
||||||
|
filterJSON, errN := gomatrixserverlib.CanonicalJSON(filter)
|
||||||
|
if err {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
// Check if filter already exists in the database
|
// Check if filter already exists in the database
|
||||||
err = s.selectFilterByContentStmt.QueryRowContext(ctx,
|
err = s.selectFilterByContentStmt.QueryRowContext(ctx,
|
||||||
localpart, filter).Scan(&existingFilter)
|
localpart, filterJSON).Scan(&existingFilter)
|
||||||
if existingFilter != "" {
|
if existingFilter != "" {
|
||||||
return existingFilter, err
|
return existingFilter, err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = s.insertFilterStmt.QueryRowContext(ctx, filter, localpart).Scan(&pos)
|
err = s.insertFilterStmt.QueryRowContext(ctx, filterJSON, localpart).Scan(&pos)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue