mirror of
https://mau.dev/maunium/synapse.git
synced 2024-06-14 16:48:18 +02:00
Handle wildcard type filters properly (#14984)
This commit is contained in:
parent
9902b91257
commit
b99f6db039
1
changelog.d/14984.bugfix
Normal file
1
changelog.d/14984.bugfix
Normal file
|
@ -0,0 +1 @@
|
||||||
|
Handle wildcard type filters properly for room messages endpoint. Contributed by Mo Balaa.
|
|
@ -398,14 +398,25 @@ def filter_to_clause(event_filter: Optional[Filter]) -> Tuple[str, List[str]]:
|
||||||
clauses = []
|
clauses = []
|
||||||
args = []
|
args = []
|
||||||
|
|
||||||
|
# Handle event types with potential wildcard characters
|
||||||
if event_filter.types:
|
if event_filter.types:
|
||||||
clauses.append(
|
type_clauses = []
|
||||||
"(%s)" % " OR ".join("event.type = ?" for _ in event_filter.types)
|
for typ in event_filter.types:
|
||||||
)
|
if "*" in typ:
|
||||||
args.extend(event_filter.types)
|
type_clauses.append("event.type LIKE ?")
|
||||||
|
typ = typ.replace("*", "%") # Replace * with % for SQL LIKE pattern
|
||||||
|
else:
|
||||||
|
type_clauses.append("event.type = ?")
|
||||||
|
args.append(typ)
|
||||||
|
clauses.append("(%s)" % " OR ".join(type_clauses))
|
||||||
|
|
||||||
|
# Handle event types to exclude with potential wildcard characters
|
||||||
for typ in event_filter.not_types:
|
for typ in event_filter.not_types:
|
||||||
clauses.append("event.type != ?")
|
if "*" in typ:
|
||||||
|
clauses.append("event.type NOT LIKE ?")
|
||||||
|
typ = typ.replace("*", "%")
|
||||||
|
else:
|
||||||
|
clauses.append("event.type != ?")
|
||||||
args.append(typ)
|
args.append(typ)
|
||||||
|
|
||||||
if event_filter.senders:
|
if event_filter.senders:
|
||||||
|
|
|
@ -2155,6 +2155,33 @@ class RoomMessageListTestCase(RoomBase):
|
||||||
self.assertEqual(len(chunk), 0, [event["content"] for event in chunk])
|
self.assertEqual(len(chunk), 0, [event["content"] for event in chunk])
|
||||||
|
|
||||||
|
|
||||||
|
class RoomMessageFilterTestCase(RoomBase):
|
||||||
|
"""Tests /rooms/$room_id/messages REST events."""
|
||||||
|
|
||||||
|
user_id = "@sid1:red"
|
||||||
|
|
||||||
|
def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
|
||||||
|
self.room_id = self.helper.create_room_as(self.user_id)
|
||||||
|
|
||||||
|
def test_room_message_filter_wildcard(self) -> None:
|
||||||
|
# Send a first message in the room, which will be removed by the purge.
|
||||||
|
self.helper.send(self.room_id, "message 1", type="f.message.1")
|
||||||
|
self.helper.send(self.room_id, "message 1", type="f.message.2")
|
||||||
|
self.helper.send(self.room_id, "not returned in filter")
|
||||||
|
channel = self.make_request(
|
||||||
|
"GET",
|
||||||
|
"/rooms/%s/messages?access_token=x&dir=b&filter=%s"
|
||||||
|
% (
|
||||||
|
self.room_id,
|
||||||
|
json.dumps({"types": ["f.message.*"]}),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
self.assertEqual(channel.code, HTTPStatus.OK, channel.json_body)
|
||||||
|
|
||||||
|
chunk = channel.json_body["chunk"]
|
||||||
|
self.assertEqual(len(chunk), 2, [event["content"] for event in chunk])
|
||||||
|
|
||||||
|
|
||||||
class RoomSearchTestCase(unittest.HomeserverTestCase):
|
class RoomSearchTestCase(unittest.HomeserverTestCase):
|
||||||
servlets = [
|
servlets = [
|
||||||
synapse.rest.admin.register_servlets_for_client_rest_resource,
|
synapse.rest.admin.register_servlets_for_client_rest_resource,
|
||||||
|
|
|
@ -364,6 +364,7 @@ class RestHelper:
|
||||||
tok: Optional[str] = None,
|
tok: Optional[str] = None,
|
||||||
expect_code: int = HTTPStatus.OK,
|
expect_code: int = HTTPStatus.OK,
|
||||||
custom_headers: Optional[Iterable[Tuple[AnyStr, AnyStr]]] = None,
|
custom_headers: Optional[Iterable[Tuple[AnyStr, AnyStr]]] = None,
|
||||||
|
type: str = "m.room.message",
|
||||||
) -> JsonDict:
|
) -> JsonDict:
|
||||||
if body is None:
|
if body is None:
|
||||||
body = "body_text_here"
|
body = "body_text_here"
|
||||||
|
@ -372,7 +373,7 @@ class RestHelper:
|
||||||
|
|
||||||
return self.send_event(
|
return self.send_event(
|
||||||
room_id,
|
room_id,
|
||||||
"m.room.message",
|
type,
|
||||||
content,
|
content,
|
||||||
txn_id,
|
txn_id,
|
||||||
tok,
|
tok,
|
||||||
|
|
Loading…
Reference in a new issue