Fix handling of public rooms filter with a network tuple. (#14053)

Fixes two related bugs:

* The handling of `[null]` for a `room_types` filter was incorrect.
* The ordering of arguments when providing both a network tuple
  and room type field was incorrect.
This commit is contained in:
Patrick Cloke 2022-10-05 08:49:52 -04:00 committed by GitHub
parent dcced5a8d7
commit 0b037d6c91
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 58 additions and 27 deletions

View file

@ -207,21 +207,30 @@ class RoomWorkerStore(CacheInvalidationWorkerStore):
def _construct_room_type_where_clause(
self, room_types: Union[List[Union[str, None]], None]
) -> Tuple[Union[str, None], List[str]]:
) -> Tuple[Union[str, None], list]:
if not room_types:
return None, []
else:
# We use None when we want get rooms without a type
is_null_clause = ""
if None in room_types:
is_null_clause = "OR room_type IS NULL"
room_types = [value for value in room_types if value is not None]
# Since None is used to represent a room without a type, care needs to
# be taken into account when constructing the where clause.
clauses = []
args: list = []
room_types_set = set(room_types)
# We use None to represent a room without a type.
if None in room_types_set:
clauses.append("room_type IS NULL")
room_types_set.remove(None)
# If there are other room types, generate the proper clause.
if room_types:
list_clause, args = make_in_list_sql_clause(
self.database_engine, "room_type", room_types
self.database_engine, "room_type", room_types_set
)
clauses.append(list_clause)
return f"({list_clause} {is_null_clause})", args
return f"({' OR '.join(clauses)})", args
async def count_public_rooms(
self,
@ -241,14 +250,6 @@ class RoomWorkerStore(CacheInvalidationWorkerStore):
def _count_public_rooms_txn(txn: LoggingTransaction) -> int:
query_args = []
room_type_clause, args = self._construct_room_type_where_clause(
search_filter.get(PublicRoomsFilterFields.ROOM_TYPES, None)
if search_filter
else None
)
room_type_clause = f" AND {room_type_clause}" if room_type_clause else ""
query_args += args
if network_tuple:
if network_tuple.appservice_id:
published_sql = """
@ -268,6 +269,14 @@ class RoomWorkerStore(CacheInvalidationWorkerStore):
UNION SELECT room_id from appservice_room_list
"""
room_type_clause, args = self._construct_room_type_where_clause(
search_filter.get(PublicRoomsFilterFields.ROOM_TYPES, None)
if search_filter
else None
)
room_type_clause = f" AND {room_type_clause}" if room_type_clause else ""
query_args += args
sql = f"""
SELECT
COUNT(*)