Fix room timeline-specific filtering & linter warns

Signed-off-by: Thibaut CHARLES cromfr@gmail.com
This commit is contained in:
Crom (Thibaut CHARLES) 2018-01-05 15:06:58 +01:00
parent da0658dc67
commit a762aecc62
No known key found for this signature in database
GPG key ID: 45A3D5F880B9E6D0
4 changed files with 55 additions and 6 deletions

View file

@ -0,0 +1,48 @@
// 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.
package storage
import (
"github.com/matrix-org/gomatrix"
)
func isRoomFiltered(roomID string, filter *gomatrix.Filter, filterPart *gomatrix.FilterPart) bool {
if filter != nil {
if filter.Room.Rooms != nil && !hasValue(roomID, filter.Room.Rooms) {
return true
}
if filter.Room.NotRooms != nil && hasValue(roomID, filter.Room.NotRooms) {
return true
}
}
if filterPart != nil {
if filterPart.Rooms != nil && !hasValue(roomID, filterPart.Rooms) {
return true
}
if filterPart.NotRooms != nil && hasValue(roomID, filterPart.NotRooms) {
return true
}
}
return false
}
func hasValue(value string, list []string) bool {
for i := range list {
if list[i] == value {
return true
}
}
return false
}

View file

@ -245,9 +245,11 @@ func (d *SyncServerDatabase) IncrementalSync(
res := types.NewResponse(toPos)
for _, delta := range deltas {
err = d.addRoomDeltaToResponse(ctx, &device, txn, fromPos, toPos, delta, filter, res)
if err != nil {
return nil, err
if !isRoomFiltered(delta.roomID, filter, &filter.Room.Timeline) {
err = d.addRoomDeltaToResponse(ctx, &device, txn, fromPos, toPos, delta, filter, res)
if err != nil {
return nil, err
}
}
}
@ -295,7 +297,7 @@ func (d *SyncServerDatabase) CompleteSync(
jr := types.NewJoinResponse()
//Join response should contain events only if room isn't filtered
if !isRoomFiltered(filter, roomID) {
if !isRoomFiltered(roomID, filter, &filter.Room.Timeline) {
// Timeline events
var recentStreamEvents []streamEvent
var limited bool

View file

@ -31,7 +31,6 @@ import (
)
const defaultSyncTimeout = time.Duration(30) * time.Second
const defaultTimelineLimit = 20
// syncRequest represents a /sync request, with sensible defaults/sanity checks applied.
type syncRequest struct {

View file

@ -74,7 +74,7 @@ func defaultFilterPart() FilterPart {
return FilterPart{
NotRooms: nil,
Rooms: nil,
Limit: 100, //TODO check this on synapse
Limit: 20,
NotSenders: nil,
NotTypes: nil,
Senders: nil,