Incorporate review
parent
dcc069a2e2
commit
c6dbca2422
|
@ -1 +1 @@
|
||||||
Implement label-based filtering.
|
Implement label-based filtering on `/sync` and `/messages` ([MSC2326](https://github.com/matrix-org/matrix-doc/pull/2326)).
|
||||||
|
|
|
@ -140,4 +140,7 @@ class LimitBlockingTypes(object):
|
||||||
HS_DISABLED = "hs_disabled"
|
HS_DISABLED = "hs_disabled"
|
||||||
|
|
||||||
|
|
||||||
LabelsField = "org.matrix.labels"
|
class EventContentFields(object):
|
||||||
|
"""Fields found in events' content, regardless of type."""
|
||||||
|
# Labels for the event, cf https://github.com/matrix-org/matrix-doc/pull/2326
|
||||||
|
Labels = "org.matrix.labels"
|
||||||
|
|
|
@ -20,7 +20,7 @@ from jsonschema import FormatChecker
|
||||||
|
|
||||||
from twisted.internet import defer
|
from twisted.internet import defer
|
||||||
|
|
||||||
from synapse.api.constants import LabelsField
|
from synapse.api.constants import EventContentFields
|
||||||
from synapse.api.errors import SynapseError
|
from synapse.api.errors import SynapseError
|
||||||
from synapse.storage.presence import UserPresenceState
|
from synapse.storage.presence import UserPresenceState
|
||||||
from synapse.types import RoomID, UserID
|
from synapse.types import RoomID, UserID
|
||||||
|
@ -67,6 +67,8 @@ ROOM_EVENT_FILTER_SCHEMA = {
|
||||||
"contains_url": {"type": "boolean"},
|
"contains_url": {"type": "boolean"},
|
||||||
"lazy_load_members": {"type": "boolean"},
|
"lazy_load_members": {"type": "boolean"},
|
||||||
"include_redundant_members": {"type": "boolean"},
|
"include_redundant_members": {"type": "boolean"},
|
||||||
|
# Include or exclude events with the provided labels.
|
||||||
|
# cf https://github.com/matrix-org/matrix-doc/pull/2326
|
||||||
"org.matrix.labels": {"type": "array", "items": {"type": "string"}},
|
"org.matrix.labels": {"type": "array", "items": {"type": "string"}},
|
||||||
"org.matrix.not_labels": {"type": "array", "items": {"type": "string"}},
|
"org.matrix.not_labels": {"type": "array", "items": {"type": "string"}},
|
||||||
},
|
},
|
||||||
|
@ -307,7 +309,7 @@ class Filter(object):
|
||||||
content = event.get("content", {})
|
content = event.get("content", {})
|
||||||
# check if there is a string url field in the content for filtering purposes
|
# check if there is a string url field in the content for filtering purposes
|
||||||
contains_url = isinstance(content.get("url"), text_type)
|
contains_url = isinstance(content.get("url"), text_type)
|
||||||
labels = content.get(LabelsField, [])
|
labels = content.get(EventContentFields.Labels, [])
|
||||||
|
|
||||||
return self.check_fields(room_id, sender, ev_type, labels, contains_url)
|
return self.check_fields(room_id, sender, ev_type, labels, contains_url)
|
||||||
|
|
||||||
|
|
|
@ -29,7 +29,7 @@ from prometheus_client import Counter, Histogram
|
||||||
from twisted.internet import defer
|
from twisted.internet import defer
|
||||||
|
|
||||||
import synapse.metrics
|
import synapse.metrics
|
||||||
from synapse.api.constants import EventTypes, LabelsField
|
from synapse.api.constants import EventTypes, EventContentFields
|
||||||
from synapse.api.errors import SynapseError
|
from synapse.api.errors import SynapseError
|
||||||
from synapse.events import EventBase # noqa: F401
|
from synapse.events import EventBase # noqa: F401
|
||||||
from synapse.events.snapshot import EventContext # noqa: F401
|
from synapse.events.snapshot import EventContext # noqa: F401
|
||||||
|
@ -1491,7 +1491,7 @@ class EventsStore(
|
||||||
self._handle_event_relations(txn, event)
|
self._handle_event_relations(txn, event)
|
||||||
|
|
||||||
# Store the labels for this event.
|
# Store the labels for this event.
|
||||||
labels = event.content.get(LabelsField)
|
labels = event.content.get(EventContentFields.Labels)
|
||||||
if labels:
|
if labels:
|
||||||
self.insert_labels_for_event_txn(txn, event.event_id, labels)
|
self.insert_labels_for_event_txn(txn, event.event_id, labels)
|
||||||
|
|
||||||
|
@ -2483,6 +2483,14 @@ class EventsStore(
|
||||||
)
|
)
|
||||||
|
|
||||||
def insert_labels_for_event_txn(self, txn, event_id, labels):
|
def insert_labels_for_event_txn(self, txn, event_id, labels):
|
||||||
|
"""Store the mapping between an event's ID and its labels, with one row per
|
||||||
|
(event_id, label) tuple.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
txn (LoggingTransaction): The transaction to execute.
|
||||||
|
event_id (str): The event's ID.
|
||||||
|
labels (list[str]): A list of text labels.
|
||||||
|
"""
|
||||||
return self._simple_insert_many_txn(
|
return self._simple_insert_many_txn(
|
||||||
txn=txn,
|
txn=txn,
|
||||||
table="event_labels",
|
table="event_labels",
|
||||||
|
|
|
@ -19,7 +19,7 @@ import jsonschema
|
||||||
|
|
||||||
from twisted.internet import defer
|
from twisted.internet import defer
|
||||||
|
|
||||||
from synapse.api.constants import LabelsField
|
from synapse.api.constants import EventContentFields
|
||||||
from synapse.api.errors import SynapseError
|
from synapse.api.errors import SynapseError
|
||||||
from synapse.api.filtering import Filter
|
from synapse.api.filtering import Filter
|
||||||
from synapse.events import FrozenEvent
|
from synapse.events import FrozenEvent
|
||||||
|
@ -329,7 +329,7 @@ class FilteringTestCase(unittest.TestCase):
|
||||||
sender="@foo:bar",
|
sender="@foo:bar",
|
||||||
type="m.room.message",
|
type="m.room.message",
|
||||||
room_id="!secretbase:unknown",
|
room_id="!secretbase:unknown",
|
||||||
content={LabelsField: ["#fun"]},
|
content={EventContentFields.Labels: ["#fun"]},
|
||||||
)
|
)
|
||||||
|
|
||||||
self.assertTrue(Filter(definition).check(event))
|
self.assertTrue(Filter(definition).check(event))
|
||||||
|
@ -338,7 +338,7 @@ class FilteringTestCase(unittest.TestCase):
|
||||||
sender="@foo:bar",
|
sender="@foo:bar",
|
||||||
type="m.room.message",
|
type="m.room.message",
|
||||||
room_id="!secretbase:unknown",
|
room_id="!secretbase:unknown",
|
||||||
content={LabelsField: ["#notfun"]},
|
content={EventContentFields.Labels: ["#notfun"]},
|
||||||
)
|
)
|
||||||
|
|
||||||
self.assertFalse(Filter(definition).check(event))
|
self.assertFalse(Filter(definition).check(event))
|
||||||
|
@ -349,7 +349,7 @@ class FilteringTestCase(unittest.TestCase):
|
||||||
sender="@foo:bar",
|
sender="@foo:bar",
|
||||||
type="m.room.message",
|
type="m.room.message",
|
||||||
room_id="!secretbase:unknown",
|
room_id="!secretbase:unknown",
|
||||||
content={LabelsField: ["#fun"]},
|
content={EventContentFields.Labels: ["#fun"]},
|
||||||
)
|
)
|
||||||
|
|
||||||
self.assertFalse(Filter(definition).check(event))
|
self.assertFalse(Filter(definition).check(event))
|
||||||
|
@ -358,7 +358,7 @@ class FilteringTestCase(unittest.TestCase):
|
||||||
sender="@foo:bar",
|
sender="@foo:bar",
|
||||||
type="m.room.message",
|
type="m.room.message",
|
||||||
room_id="!secretbase:unknown",
|
room_id="!secretbase:unknown",
|
||||||
content={LabelsField: ["#notfun"]},
|
content={EventContentFields.Labels: ["#notfun"]},
|
||||||
)
|
)
|
||||||
|
|
||||||
self.assertTrue(Filter(definition).check(event))
|
self.assertTrue(Filter(definition).check(event))
|
||||||
|
|
|
@ -24,7 +24,7 @@ from six.moves.urllib import parse as urlparse
|
||||||
from twisted.internet import defer
|
from twisted.internet import defer
|
||||||
|
|
||||||
import synapse.rest.admin
|
import synapse.rest.admin
|
||||||
from synapse.api.constants import EventTypes, LabelsField, Membership
|
from synapse.api.constants import EventContentFields, EventTypes, Membership
|
||||||
from synapse.rest.client.v1 import login, profile, room
|
from synapse.rest.client.v1 import login, profile, room
|
||||||
|
|
||||||
from tests import unittest
|
from tests import unittest
|
||||||
|
@ -860,7 +860,7 @@ class RoomMessageListTestCase(RoomBase):
|
||||||
content={
|
content={
|
||||||
"msgtype": "m.text",
|
"msgtype": "m.text",
|
||||||
"body": "with right label",
|
"body": "with right label",
|
||||||
LabelsField: ["#fun"],
|
EventContentFields.Labels: ["#fun"],
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -876,7 +876,7 @@ class RoomMessageListTestCase(RoomBase):
|
||||||
content={
|
content={
|
||||||
"msgtype": "m.text",
|
"msgtype": "m.text",
|
||||||
"body": "with wrong label",
|
"body": "with wrong label",
|
||||||
LabelsField: ["#work"],
|
EventContentFields.Labels: ["#work"],
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -886,7 +886,7 @@ class RoomMessageListTestCase(RoomBase):
|
||||||
content={
|
content={
|
||||||
"msgtype": "m.text",
|
"msgtype": "m.text",
|
||||||
"body": "with two wrong labels",
|
"body": "with two wrong labels",
|
||||||
LabelsField: ["#work", "#notfun"],
|
EventContentFields.Labels: ["#work", "#notfun"],
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -896,7 +896,7 @@ class RoomMessageListTestCase(RoomBase):
|
||||||
content={
|
content={
|
||||||
"msgtype": "m.text",
|
"msgtype": "m.text",
|
||||||
"body": "with right label",
|
"body": "with right label",
|
||||||
LabelsField: ["#fun"],
|
EventContentFields.Labels: ["#fun"],
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
@ -17,7 +17,7 @@ import json
|
||||||
from mock import Mock
|
from mock import Mock
|
||||||
|
|
||||||
import synapse.rest.admin
|
import synapse.rest.admin
|
||||||
from synapse.api.constants import EventTypes, LabelsField
|
from synapse.api.constants import EventContentFields, EventTypes
|
||||||
from synapse.rest.client.v1 import login, room
|
from synapse.rest.client.v1 import login, room
|
||||||
from synapse.rest.client.v2_alpha import sync
|
from synapse.rest.client.v2_alpha import sync
|
||||||
|
|
||||||
|
@ -157,7 +157,7 @@ class SyncFilterTestCase(unittest.HomeserverTestCase):
|
||||||
content={
|
content={
|
||||||
"msgtype": "m.text",
|
"msgtype": "m.text",
|
||||||
"body": "with right label",
|
"body": "with right label",
|
||||||
LabelsField: ["#fun"],
|
EventContentFields.Labels: ["#fun"],
|
||||||
},
|
},
|
||||||
tok=tok,
|
tok=tok,
|
||||||
)
|
)
|
||||||
|
@ -175,7 +175,7 @@ class SyncFilterTestCase(unittest.HomeserverTestCase):
|
||||||
content={
|
content={
|
||||||
"msgtype": "m.text",
|
"msgtype": "m.text",
|
||||||
"body": "with wrong label",
|
"body": "with wrong label",
|
||||||
LabelsField: ["#work"],
|
EventContentFields.Labels: ["#work"],
|
||||||
},
|
},
|
||||||
tok=tok,
|
tok=tok,
|
||||||
)
|
)
|
||||||
|
@ -186,7 +186,7 @@ class SyncFilterTestCase(unittest.HomeserverTestCase):
|
||||||
content={
|
content={
|
||||||
"msgtype": "m.text",
|
"msgtype": "m.text",
|
||||||
"body": "with two wrong labels",
|
"body": "with two wrong labels",
|
||||||
LabelsField: ["#work", "#notfun"],
|
EventContentFields.Labels: ["#work", "#notfun"],
|
||||||
},
|
},
|
||||||
tok=tok,
|
tok=tok,
|
||||||
)
|
)
|
||||||
|
@ -197,7 +197,7 @@ class SyncFilterTestCase(unittest.HomeserverTestCase):
|
||||||
content={
|
content={
|
||||||
"msgtype": "m.text",
|
"msgtype": "m.text",
|
||||||
"body": "with right label",
|
"body": "with right label",
|
||||||
LabelsField: ["#fun"],
|
EventContentFields.Labels: ["#fun"],
|
||||||
},
|
},
|
||||||
tok=tok,
|
tok=tok,
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in New Issue