Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Matrix
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Container Registry
Model registry
Operate
Environments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
TeDomum
Matrix
Commits
323d3e50
Commit
323d3e50
authored
9 years ago
by
Erik Johnston
Browse files
Options
Downloads
Plain Diff
Merge branch 'develop' of github.com:matrix-org/synapse into erikj/search
parents
88971fd0
ff2b66f4
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
synapse/api/filtering.py
+44
-18
44 additions, 18 deletions
synapse/api/filtering.py
tests/api/test_filtering.py
+6
-6
6 additions, 6 deletions
tests/api/test_filtering.py
with
50 additions
and
24 deletions
synapse/api/filtering.py
+
44
−
18
View file @
323d3e50
...
...
@@ -54,7 +54,7 @@ class Filtering(object):
]
room_level_definitions
=
[
"
state
"
,
"
events
"
,
"
ephemeral
"
"
state
"
,
"
timeline
"
,
"
ephemeral
"
]
for
key
in
top_level_definitions
:
...
...
@@ -135,17 +135,23 @@ class Filter(object):
def
__init__
(
self
,
filter_json
):
self
.
filter_json
=
filter_json
def
filter_public_user_data
(
self
,
events
):
return
self
.
_
filter_
on_key
(
events
,
[
"
public_user_data
"
]
)
def
timeline_limit
(
self
):
return
self
.
filter_
json
.
get
(
"
room
"
,
{}).
get
(
"
timeline
"
,
{}).
get
(
"
limit
"
,
10
)
def
filter_private_user_data
(
self
,
events
):
return
self
.
_filter_on_key
(
events
,
[
"
private_user_data
"
])
def
presence_limit
(
self
):
return
self
.
filter_json
.
get
(
"
presence
"
,
{}).
get
(
"
limit
"
,
10
)
def
ephemeral_limit
(
self
):
return
self
.
filter_json
.
get
(
"
room
"
,
{}).
get
(
"
ephemeral
"
,
{}).
get
(
"
limit
"
,
10
)
def
filter_presence
(
self
,
events
):
return
self
.
_filter_on_key
(
events
,
[
"
presence
"
])
def
filter_room_state
(
self
,
events
):
return
self
.
_filter_on_key
(
events
,
[
"
room
"
,
"
state
"
])
def
filter_room_
events
(
self
,
events
):
return
self
.
_filter_on_key
(
events
,
[
"
room
"
,
"
events
"
])
def
filter_room_
timeline
(
self
,
events
):
return
self
.
_filter_on_key
(
events
,
[
"
room
"
,
"
timeline
"
])
def
filter_room_ephemeral
(
self
,
events
):
return
self
.
_filter_on_key
(
events
,
[
"
room
"
,
"
ephemeral
"
])
...
...
@@ -169,11 +175,34 @@ class Filter(object):
return
[
e
for
e
in
events
if
self
.
_passes_definition
(
definition
,
e
)]
def
_passes_definition
(
self
,
definition
,
event
):
"""
Check if the event passes the filter definition
Args:
definition(dict): The filter definition to check against
event(dict or Event): The event to check
Returns:
True if the event passes the filter in the definition
"""
if
type
(
event
)
is
dict
:
room_id
=
event
.
get
(
"
room_id
"
)
sender
=
event
.
get
(
"
sender
"
)
event_type
=
event
[
"
type
"
]
else
:
room_id
=
getattr
(
event
,
"
room_id
"
,
None
)
sender
=
getattr
(
event
,
"
sender
"
,
None
)
event_type
=
event
.
type
return
self
.
_event_passes_definition
(
definition
,
room_id
,
sender
,
event_type
)
def
_event_passes_definition
(
self
,
definition
,
room_id
,
sender
,
event_type
):
"""
Check if the event passes through the given definition.
Args:
definition(dict): The definition to check against.
event(Event): The event to check.
room_id(str): The id of the room this event is in or None.
sender(str): The sender of the event
event_type(str): The type of the event.
Returns:
True if the event passes through the filter.
"""
...
...
@@ -185,8 +214,7 @@ class Filter(object):
# and 'not_types' then it is treated as only being in 'not_types')
# room checks
if
hasattr
(
event
,
"
room_id
"
):
room_id
=
event
.
room_id
if
room_id
is
not
None
:
allow_rooms
=
definition
.
get
(
"
rooms
"
,
None
)
reject_rooms
=
definition
.
get
(
"
not_rooms
"
,
None
)
if
reject_rooms
and
room_id
in
reject_rooms
:
...
...
@@ -195,9 +223,7 @@ class Filter(object):
return
False
# sender checks
if
hasattr
(
event
,
"
sender
"
):
# Should we be including event.state_key for some event types?
sender
=
event
.
sender
if
sender
is
not
None
:
allow_senders
=
definition
.
get
(
"
senders
"
,
None
)
reject_senders
=
definition
.
get
(
"
not_senders
"
,
None
)
if
reject_senders
and
sender
in
reject_senders
:
...
...
@@ -208,12 +234,12 @@ class Filter(object):
# type checks
if
"
not_types
"
in
definition
:
for
def_type
in
definition
[
"
not_types
"
]:
if
self
.
_event_matches_type
(
event
,
def_type
):
if
self
.
_event_matches_type
(
event
_type
,
def_type
):
return
False
if
"
types
"
in
definition
:
included
=
False
for
def_type
in
definition
[
"
types
"
]:
if
self
.
_event_matches_type
(
event
,
def_type
):
if
self
.
_event_matches_type
(
event
_type
,
def_type
):
included
=
True
break
if
not
included
:
...
...
@@ -221,9 +247,9 @@ class Filter(object):
return
True
def
_event_matches_type
(
self
,
event
,
def_type
):
def
_event_matches_type
(
self
,
event
_type
,
def_type
):
if
def_type
.
endswith
(
"
*
"
):
type_prefix
=
def_type
[:
-
1
]
return
event
.
type
.
startswith
(
type_prefix
)
return
event
_
type
.
startswith
(
type_prefix
)
else
:
return
event
.
type
==
def_type
return
event
_
type
==
def_type
This diff is collapsed.
Click to expand it.
tests/api/test_filtering.py
+
6
−
6
View file @
323d3e50
...
...
@@ -345,9 +345,9 @@ class FilteringTestCase(unittest.TestCase):
)
@defer.inlineCallbacks
def
test_filter_p
ublic_user_data
_match
(
self
):
def
test_filter_p
resence
_match
(
self
):
user_filter_json
=
{
"
p
ublic_user_data
"
:
{
"
p
resence
"
:
{
"
types
"
:
[
"
m.*
"
]
}
}
...
...
@@ -368,13 +368,13 @@ class FilteringTestCase(unittest.TestCase):
filter_id
=
filter_id
,
)
results
=
user_filter
.
filter_p
ublic_user_data
(
events
=
events
)
results
=
user_filter
.
filter_p
resence
(
events
=
events
)
self
.
assertEquals
(
events
,
results
)
@defer.inlineCallbacks
def
test_filter_p
ublic_user_data
_no_match
(
self
):
def
test_filter_p
resence
_no_match
(
self
):
user_filter_json
=
{
"
p
ublic_user_data
"
:
{
"
p
resence
"
:
{
"
types
"
:
[
"
m.*
"
]
}
}
...
...
@@ -395,7 +395,7 @@ class FilteringTestCase(unittest.TestCase):
filter_id
=
filter_id
,
)
results
=
user_filter
.
filter_p
ublic_user_data
(
events
=
events
)
results
=
user_filter
.
filter_p
resence
(
events
=
events
)
self
.
assertEquals
([],
results
)
@defer.inlineCallbacks
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment