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

Remove remaining usage of cursor_to_dict. #16564

Merged
merged 12 commits into from
Oct 31, 2023
13 changes: 12 additions & 1 deletion synapse/rest/admin/registration_tokens.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,18 @@ async def on_GET(self, request: SynapseRequest) -> Tuple[int, JsonDict]:
await assert_requester_is_admin(self.auth, request)
valid = parse_boolean(request, "valid")
token_list = await self.store.get_registration_tokens(valid)
return HTTPStatus.OK, {"registration_tokens": token_list}
return HTTPStatus.OK, {
"registration_tokens": [
{
"token": t[0],
"uses_allowed": t[1],
"pending": t[2],
"completed": t[3],
"expiry_time": t[4],
}
for t in token_list
]
}


class NewRegistrationTokenRestServlet(RestServlet):
Expand Down
42 changes: 28 additions & 14 deletions synapse/storage/databases/main/registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -1517,7 +1517,7 @@ def _use_registration_token_txn(txn: LoggingTransaction) -> None:

async def get_registration_tokens(
self, valid: Optional[bool] = None
) -> List[Dict[str, Any]]:
) -> List[Tuple[str, Optional[int], int, int, Optional[int]]]:
"""List all registration tokens. Used by the admin API.

Args:
Expand All @@ -1526,34 +1526,48 @@ async def get_registration_tokens(
Default is None: return all tokens regardless of validity.

Returns:
A list of dicts, each containing details of a token.
A list of tuples containing:
* The token
* The number of users allowed (or None)
* Whether it is pending
* Whether it has been completed
DMRobertson marked this conversation as resolved.
Show resolved Hide resolved
* An expiry time (or None)
clokep marked this conversation as resolved.
Show resolved Hide resolved
"""

def select_registration_tokens_txn(
txn: LoggingTransaction, now: int, valid: Optional[bool]
) -> List[Dict[str, Any]]:
) -> List[Tuple[str, Optional[int], int, int, Optional[int]]]:
if valid is None:
# Return all tokens regardless of validity
txn.execute("SELECT * FROM registration_tokens")
txn.execute(
"""
SELECT token, uses_allowed, pending, completed, expiry_time
FROM registration_tokens
"""
)

elif valid:
# Select valid tokens only
sql = (
"SELECT * FROM registration_tokens WHERE "
"(uses_allowed > pending + completed OR uses_allowed IS NULL) "
"AND (expiry_time > ? OR expiry_time IS NULL)"
)
sql = """
SELECT token, uses_allowed, pending, completed, expiry_time
FROM registration_tokens
WHERE (uses_allowed > pending + completed OR uses_allowed IS NULL)
AND (expiry_time > ? OR expiry_time IS NULL)
"""
txn.execute(sql, [now])

else:
# Select invalid tokens only
sql = (
"SELECT * FROM registration_tokens WHERE "
"uses_allowed <= pending + completed OR expiry_time <= ?"
)
sql = """
SELECT token, uses_allowed, pending, completed, expiry_time
FROM registration_tokens
WHERE uses_allowed <= pending + completed OR expiry_time <= ?
"""
txn.execute(sql, [now])

return self.db_pool.cursor_to_dict(txn)
return cast(
List[Tuple[str, Optional[int], int, int, Optional[int]]], txn.fetchall()
)

return await self.db_pool.runInteraction(
"select_registration_tokens",
Expand Down