mirror of
https://github.com/matrix-org/dendrite.git
synced 2026-01-01 03:03:10 -06:00
Fix issues; Use prepareWithFilters
This commit is contained in:
parent
7c39cc3e48
commit
320a8f6f18
|
|
@ -41,10 +41,10 @@ const insertAccountDataSQL = "" +
|
||||||
" ON CONFLICT (user_id, room_id, type) DO UPDATE" +
|
" ON CONFLICT (user_id, room_id, type) DO UPDATE" +
|
||||||
" SET id = $5"
|
" SET id = $5"
|
||||||
|
|
||||||
|
// further parameters are added by prepareWithFilters
|
||||||
const selectAccountDataInRangeSQL = "" +
|
const selectAccountDataInRangeSQL = "" +
|
||||||
"SELECT room_id, type FROM syncapi_account_data_type" +
|
"SELECT room_id, type FROM syncapi_account_data_type" +
|
||||||
" WHERE user_id = $1 AND id > $2 AND id <= $3" +
|
" WHERE user_id = $1 AND id > $2 AND id <= $3"
|
||||||
" ORDER BY id ASC"
|
|
||||||
|
|
||||||
const selectMaxAccountDataIDSQL = "" +
|
const selectMaxAccountDataIDSQL = "" +
|
||||||
"SELECT MAX(id) FROM syncapi_account_data_type"
|
"SELECT MAX(id) FROM syncapi_account_data_type"
|
||||||
|
|
@ -94,18 +94,25 @@ func (s *accountDataStatements) SelectAccountDataInRange(
|
||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
userID string,
|
userID string,
|
||||||
r types.Range,
|
r types.Range,
|
||||||
accountDataFilterPart *gomatrixserverlib.EventFilter,
|
filter *gomatrixserverlib.EventFilter,
|
||||||
) (data map[string][]string, err error) {
|
) (data map[string][]string, err error) {
|
||||||
data = make(map[string][]string)
|
data = make(map[string][]string)
|
||||||
|
stmt, params, err := prepareWithFilters(
|
||||||
|
s.db, nil, selectAccountDataInRangeSQL,
|
||||||
|
[]interface{}{
|
||||||
|
userID, r.Low(), r.High(),
|
||||||
|
},
|
||||||
|
filter.Senders, filter.NotSenders,
|
||||||
|
filter.Types, filter.NotTypes,
|
||||||
|
[]string{}, filter.Limit, FilterOrderAsc,
|
||||||
|
)
|
||||||
|
|
||||||
rows, err := s.selectAccountDataInRangeStmt.QueryContext(ctx, userID, r.Low(), r.High())
|
rows, err := stmt.QueryContext(ctx, params...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer internal.CloseAndLogIfError(ctx, rows, "selectAccountDataInRange: rows.close() failed")
|
defer internal.CloseAndLogIfError(ctx, rows, "selectAccountDataInRange: rows.close() failed")
|
||||||
|
|
||||||
var entries int
|
|
||||||
|
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
var dataType string
|
var dataType string
|
||||||
var roomID string
|
var roomID string
|
||||||
|
|
@ -114,31 +121,11 @@ func (s *accountDataStatements) SelectAccountDataInRange(
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// check if we should add this by looking at the filter.
|
|
||||||
// It would be nice if we could do this in SQL-land, but the mix of variadic
|
|
||||||
// and positional parameters makes the query annoyingly hard to do, it's easier
|
|
||||||
// and clearer to do it in Go-land. If there are no filters for [not]types then
|
|
||||||
// this gets skipped.
|
|
||||||
for _, includeType := range *accountDataFilterPart.Types {
|
|
||||||
if includeType != dataType { // TODO: wildcard support
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for _, excludeType := range *accountDataFilterPart.NotTypes {
|
|
||||||
if excludeType == dataType { // TODO: wildcard support
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(data[roomID]) > 0 {
|
if len(data[roomID]) > 0 {
|
||||||
data[roomID] = append(data[roomID], dataType)
|
data[roomID] = append(data[roomID], dataType)
|
||||||
} else {
|
} else {
|
||||||
data[roomID] = []string{dataType}
|
data[roomID] = []string{dataType}
|
||||||
}
|
}
|
||||||
entries++
|
|
||||||
if entries >= accountDataFilterPart.Limit {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return data, nil
|
return data, nil
|
||||||
|
|
|
||||||
|
|
@ -35,6 +35,8 @@ func prepareWithFilters(
|
||||||
for _, v := range *senders {
|
for _, v := range *senders {
|
||||||
params, offset = append(params, v), offset+1
|
params, offset = append(params, v), offset+1
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
query += ` AND sender = ""`
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if notsenders != nil {
|
if notsenders != nil {
|
||||||
|
|
@ -43,6 +45,8 @@ func prepareWithFilters(
|
||||||
for _, v := range *notsenders {
|
for _, v := range *notsenders {
|
||||||
params, offset = append(params, v), offset+1
|
params, offset = append(params, v), offset+1
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
query += ` AND sender NOT = ""`
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if types != nil {
|
if types != nil {
|
||||||
|
|
@ -51,6 +55,8 @@ func prepareWithFilters(
|
||||||
for _, v := range *types {
|
for _, v := range *types {
|
||||||
params, offset = append(params, v), offset+1
|
params, offset = append(params, v), offset+1
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
query += ` AND type = ""`
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if nottypes != nil {
|
if nottypes != nil {
|
||||||
|
|
@ -59,6 +65,8 @@ func prepareWithFilters(
|
||||||
for _, v := range *nottypes {
|
for _, v := range *nottypes {
|
||||||
params, offset = append(params, v), offset+1
|
params, offset = append(params, v), offset+1
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
query += ` AND type NOT = ""`
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if count := len(excludeEventIDs); count > 0 {
|
if count := len(excludeEventIDs); count > 0 {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue