Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
Add PublicRoomsFilterFields
Browse files Browse the repository at this point in the history
Signed-off-by: Šimon Brandner <simon.bra.ag@gmail.com>
  • Loading branch information
SimonBrandner committed Jun 18, 2022
1 parent 085caba commit ec975f1
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 13 deletions.
5 changes: 5 additions & 0 deletions synapse/api/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,3 +259,8 @@ class ReceiptTypes:
READ: Final = "m.read"
READ_PRIVATE: Final = "org.matrix.msc2285.read.private"
FULLY_READ: Final = "m.fully_read"


class PublicRoomsFilterFields:
GENERIC_SEARCH_TERM: Final = "generic_search_term"
ROOM_TYPES: Final = "org.matrix.msc3827.room_types"
9 changes: 7 additions & 2 deletions synapse/handlers/room_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
GuestAccess,
HistoryVisibility,
JoinRules,
PublicRoomsFilterFields,
)
from synapse.api.errors import (
Codes,
Expand Down Expand Up @@ -511,8 +512,12 @@ def copy_and_replace(self, **kwds: Any) -> "RoomListNextBatch":


def _matches_room_entry(room_entry: JsonDict, search_filter: dict) -> bool:
if search_filter and search_filter.get("generic_search_term", None):
generic_search_term = search_filter["generic_search_term"].upper()
if search_filter and search_filter.get(
PublicRoomsFilterFields.GENERIC_SEARCH_TERM, None
):
generic_search_term = search_filter[
PublicRoomsFilterFields.GENERIC_SEARCH_TERM
].upper()
if generic_search_term in room_entry.get("name", "").upper():
return True
elif generic_search_term in room_entry.get("topic", "").upper():
Expand Down
27 changes: 18 additions & 9 deletions synapse/storage/databases/main/room.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@

import attr

from synapse.api.constants import EventContentFields, EventTypes, JoinRules
from synapse.api.constants import (
EventContentFields,
EventTypes,
JoinRules,
PublicRoomsFilterFields,
)
from synapse.api.errors import StoreError
from synapse.api.room_versions import RoomVersion, RoomVersions
from synapse.config.homeserver import HomeServerConfig
Expand Down Expand Up @@ -238,16 +243,16 @@ def _count_public_rooms_txn(txn: LoggingTransaction) -> int:
if (
not self.config.experimental.msc3827_enabled
or not search_filter
or search_filter.get("room_types", None) is None
or search_filter.get(PublicRoomsFilterFields.ROOM_TYPES, None) is None
):
where_clause = "AND room_type IS NULL"
elif (
self.config.experimental.msc3827_enabled
and search_filter
and search_filter.get("room_types", None)
and search_filter.get(PublicRoomsFilterFields.ROOM_TYPES, None)
):
clause, args = self._construct_room_type_where_clause(
search_filter["room_types"]
search_filter[PublicRoomsFilterFields.ROOM_TYPES]
)
where_clause = f" AND {clause}"
query_args += args
Expand Down Expand Up @@ -385,8 +390,12 @@ async def get_largest_public_rooms(
if ignore_non_federatable:
where_clauses.append("is_federatable")

if search_filter and search_filter.get("generic_search_term", None):
search_term = "%" + search_filter["generic_search_term"] + "%"
if search_filter and search_filter.get(
PublicRoomsFilterFields.GENERIC_SEARCH_TERM, None
):
search_term = (
"%" + search_filter[PublicRoomsFilterFields.GENERIC_SEARCH_TERM] + "%"
)

where_clauses.append(
"""
Expand All @@ -406,16 +415,16 @@ async def get_largest_public_rooms(
if (
not self.config.experimental.msc3827_enabled
or not search_filter
or search_filter.get("room_types", None) is None
or search_filter.get(PublicRoomsFilterFields.ROOM_TYPES, None) is None
):
where_clauses.append("room_type IS NULL")
elif (
self.config.experimental.msc3827_enabled
and search_filter
and search_filter.get("room_types", None)
and search_filter.get(PublicRoomsFilterFields.ROOM_TYPES, None)
):
clause, args = self._construct_room_type_where_clause(
search_filter["room_types"]
search_filter[PublicRoomsFilterFields.ROOM_TYPES]
)
where_clauses.append(clause)
query_args += args
Expand Down
5 changes: 3 additions & 2 deletions tests/rest/client/test_rooms.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
EventContentFields,
EventTypes,
Membership,
PublicRoomsFilterFields,
RelationTypes,
)
from synapse.api.errors import Codes, HttpResponseException
Expand Down Expand Up @@ -1882,7 +1883,7 @@ def test_simple(self) -> None:
"Simple test for searching rooms over federation"
self.federation_client.get_public_rooms.return_value = make_awaitable({}) # type: ignore[attr-defined]

search_filter = {"generic_search_term": "foobar"}
search_filter = {PublicRoomsFilterFields.GENERIC_SEARCH_TERM: "foobar"}

channel = self.make_request(
"POST",
Expand Down Expand Up @@ -1911,7 +1912,7 @@ def test_fallback(self) -> None:
make_awaitable({}),
)

search_filter = {"generic_search_term": "foobar"}
search_filter = {PublicRoomsFilterFields.GENERIC_SEARCH_TERM: "foobar"}

channel = self.make_request(
"POST",
Expand Down

0 comments on commit ec975f1

Please sign in to comment.