Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix sortBy/sortOrder on Message/Post queries #555

Merged
merged 2 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/aleph/web/controllers/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,21 @@
class BaseMessageQueryParams(BaseModel):
sort_by: SortBy = Field(
default=SortBy.TIME,
alias="sortBy",
description="Key to use to sort the messages. "
"'time' uses the message time field. "
"'tx-time' uses the first on-chain confirmation time.",
)
sort_order: SortOrder = Field(
default=SortOrder.DESCENDING,
alias="sortOrder",
description="Order in which messages should be listed: "
"-1 means most recent messages first, 1 means older messages first.",
)
message_type: Optional[MessageType] = Field(
default=None, alias="msgType", description="Message type. Deprecated: use msgTypes instead"
default=None,
alias="msgType",
description="Message type. Deprecated: use msgTypes instead",
)
message_types: Optional[List[MessageType]] = Field(
default=None, alias="msgTypes", description="Accepted message types."
Expand Down
2 changes: 2 additions & 0 deletions src/aleph/web/controllers/posts.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,14 @@ class PostQueryParams(BaseModel):
)
sort_by: SortBy = Field(
default=SortBy.TIME,
alias="sortBy",
description="Key to use to sort the posts. "
"'time' uses the post creation time field. "
"'tx-time' uses the first on-chain confirmation time.",
)
sort_order: SortOrder = Field(
default=SortOrder.DESCENDING,
alias="sortOrder",
description="Order in which messages should be listed: "
"-1 means most recent messages first, 1 means older messages first.",
)
Expand Down
15 changes: 6 additions & 9 deletions tests/api/test_list_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,8 +272,8 @@ async def fetch_messages_filter_time(
) -> aiohttp.ClientResponse:

params: Dict[str, Union[float, int, str]] = {
"sort_by": sort_by,
"sort_order": sort_order,
"sortBy": sort_by,
"sortOrder": sort_order,
}
if start:
params["startDate"] = start
Expand Down Expand Up @@ -373,7 +373,7 @@ async def fetch_messages_with_pagination(
):
return await api_client.get(
MESSAGES_URI,
params={"page": page, "pagination": pagination, "sort_order": sort_order},
params={"page": page, "pagination": pagination, "sortOrder": sort_order},
philogicae marked this conversation as resolved.
Show resolved Hide resolved
)


Expand Down Expand Up @@ -465,18 +465,15 @@ def get_confirmed_time(msg: Dict) -> Tuple[float, float]:
# Return a high timestamp to make unconfirmed messages last
return dt.datetime(3000, 1, 1).timestamp(), msg["time"]

sorted_messages_by_time = sorted(fixture_messages, key=get_confirmed_time)
sorted_messages_by_time = list(
sorted(fixture_messages, key=get_confirmed_time, reverse=sort_order == -1)
)

# Descending order
messages = await fetch_messages_filter_time_expect_success(
ccn_api_client, sort_by="tx-time", sort_order=sort_order
)
assert_messages_equal(messages=messages, expected_messages=sorted_messages_by_time)

if sort_order == -1:
# Note: list() is used for mypy
sorted_messages_by_time = list(reversed(sorted_messages_by_time))

assert [msg["item_hash"] for msg in messages] == [
msg["item_hash"] for msg in sorted_messages_by_time
]
Expand Down
Loading