|
| 1 | +from typing import List, Optional |
| 2 | + |
| 3 | +from aleph_message.models import Chain, ItemHash, MessageType |
| 4 | +from pydantic import BaseModel, ConfigDict, Field, field_validator, model_validator |
| 5 | + |
| 6 | +from aleph.types.message_status import MessageStatus |
| 7 | +from aleph.types.sort_order import SortBy, SortOrder |
| 8 | + |
| 9 | +DEFAULT_WS_HISTORY = 10 |
| 10 | +DEFAULT_MESSAGES_PER_PAGE = 20 |
| 11 | +DEFAULT_PAGE = 1 |
| 12 | +LIST_FIELD_SEPARATOR = "," |
| 13 | + |
| 14 | + |
| 15 | +class BaseMessageQueryParams(BaseModel): |
| 16 | + sort_by: SortBy = Field( |
| 17 | + default=SortBy.TIME, |
| 18 | + alias="sortBy", |
| 19 | + description="Key to use to sort the messages. " |
| 20 | + "'time' uses the message time field. " |
| 21 | + "'tx-time' uses the first on-chain confirmation time.", |
| 22 | + ) |
| 23 | + sort_order: SortOrder = Field( |
| 24 | + default=SortOrder.DESCENDING, |
| 25 | + alias="sortOrder", |
| 26 | + description="Order in which messages should be listed: " |
| 27 | + "-1 means most recent messages first, 1 means older messages first.", |
| 28 | + ) |
| 29 | + message_type: Optional[MessageType] = Field( |
| 30 | + default=None, |
| 31 | + alias="msgType", |
| 32 | + description="Message type. Deprecated: use msgTypes instead", |
| 33 | + ) |
| 34 | + message_types: Optional[List[MessageType]] = Field( |
| 35 | + default=None, alias="msgTypes", description="Accepted message types." |
| 36 | + ) |
| 37 | + message_statuses: Optional[List[MessageStatus]] = Field( |
| 38 | + default=[MessageStatus.PROCESSED, MessageStatus.REMOVING], |
| 39 | + alias="msgStatuses", |
| 40 | + description="Accepted values for the 'status' field.", |
| 41 | + ) |
| 42 | + addresses: Optional[List[str]] = Field( |
| 43 | + default=None, description="Accepted values for the 'sender' field." |
| 44 | + ) |
| 45 | + refs: Optional[List[str]] = Field( |
| 46 | + default=None, description="Accepted values for the 'content.ref' field." |
| 47 | + ) |
| 48 | + content_hashes: Optional[List[ItemHash]] = Field( |
| 49 | + default=None, |
| 50 | + alias="contentHashes", |
| 51 | + description="Accepted values for the 'content.item_hash' field.", |
| 52 | + ) |
| 53 | + content_keys: Optional[List[ItemHash]] = Field( |
| 54 | + default=None, |
| 55 | + alias="contentKeys", |
| 56 | + description="Accepted values for the 'content.keys' field.", |
| 57 | + ) |
| 58 | + content_types: Optional[List[str]] = Field( |
| 59 | + default=None, |
| 60 | + alias="contentTypes", |
| 61 | + description="Accepted values for the 'content.type' field.", |
| 62 | + ) |
| 63 | + chains: Optional[List[Chain]] = Field( |
| 64 | + default=None, description="Accepted values for the 'chain' field." |
| 65 | + ) |
| 66 | + channels: Optional[List[str]] = Field( |
| 67 | + default=None, description="Accepted values for the 'channel' field." |
| 68 | + ) |
| 69 | + tags: Optional[List[str]] = Field( |
| 70 | + default=None, description="Accepted values for the 'content.content.tag' field." |
| 71 | + ) |
| 72 | + hashes: Optional[List[ItemHash]] = Field( |
| 73 | + default=None, description="Accepted values for the 'item_hash' field." |
| 74 | + ) |
| 75 | + |
| 76 | + start_date: float = Field( |
| 77 | + default=0, |
| 78 | + ge=0, |
| 79 | + alias="startDate", |
| 80 | + description="Start date timestamp. If specified, only messages with " |
| 81 | + "a time field greater or equal to this value will be returned.", |
| 82 | + ) |
| 83 | + end_date: float = Field( |
| 84 | + default=0, |
| 85 | + ge=0, |
| 86 | + alias="endDate", |
| 87 | + description="End date timestamp. If specified, only messages with " |
| 88 | + "a time field lower than this value will be returned.", |
| 89 | + ) |
| 90 | + |
| 91 | + start_block: int = Field( |
| 92 | + default=0, |
| 93 | + ge=0, |
| 94 | + alias="startBlock", |
| 95 | + description="Start block number. If specified, only messages with " |
| 96 | + "a block number greater or equal to this value will be returned.", |
| 97 | + ) |
| 98 | + end_block: int = Field( |
| 99 | + default=0, |
| 100 | + ge=0, |
| 101 | + alias="endBlock", |
| 102 | + description="End block number. If specified, only messages with " |
| 103 | + "a block number lower than this value will be returned.", |
| 104 | + ) |
| 105 | + |
| 106 | + @model_validator(mode="after") |
| 107 | + def validate_field_dependencies(self): |
| 108 | + start_date = self.start_date |
| 109 | + end_date = self.end_date |
| 110 | + if start_date and end_date and (end_date < start_date): |
| 111 | + raise ValueError("end date cannot be lower than start date.") |
| 112 | + start_block = self.start_block |
| 113 | + end_block = self.end_block |
| 114 | + if start_block and end_block and (end_block < start_block): |
| 115 | + raise ValueError("end block cannot be lower than start block.") |
| 116 | + |
| 117 | + return self |
| 118 | + |
| 119 | + @field_validator( |
| 120 | + "hashes", |
| 121 | + "addresses", |
| 122 | + "refs", |
| 123 | + "content_hashes", |
| 124 | + "content_keys", |
| 125 | + "content_types", |
| 126 | + "chains", |
| 127 | + "channels", |
| 128 | + "message_types", |
| 129 | + "message_statuses", |
| 130 | + "tags", |
| 131 | + mode="before", |
| 132 | + ) |
| 133 | + def split_str(cls, v): |
| 134 | + if isinstance(v, str): |
| 135 | + return v.split(LIST_FIELD_SEPARATOR) |
| 136 | + return v |
| 137 | + |
| 138 | + model_config = ConfigDict(populate_by_name=True) |
| 139 | + |
| 140 | + |
| 141 | +class MessageQueryParams(BaseMessageQueryParams): |
| 142 | + pagination: int = Field( |
| 143 | + default=DEFAULT_MESSAGES_PER_PAGE, |
| 144 | + ge=0, |
| 145 | + description="Maximum number of messages to return. Specifying 0 removes this limit.", |
| 146 | + ) |
| 147 | + page: int = Field( |
| 148 | + default=DEFAULT_PAGE, ge=1, description="Offset in pages. Starts at 1." |
| 149 | + ) |
| 150 | + |
| 151 | + |
| 152 | +class WsMessageQueryParams(BaseMessageQueryParams): |
| 153 | + history: Optional[int] = Field( |
| 154 | + DEFAULT_WS_HISTORY, |
| 155 | + ge=0, |
| 156 | + lt=200, |
| 157 | + description="Historical elements to send through the websocket.", |
| 158 | + ) |
| 159 | + |
| 160 | + |
| 161 | +class MessageHashesQueryParams(BaseModel): |
| 162 | + status: Optional[MessageStatus] = Field( |
| 163 | + default=None, |
| 164 | + description="Message status.", |
| 165 | + ) |
| 166 | + page: int = Field( |
| 167 | + default=DEFAULT_PAGE, ge=1, description="Offset in pages. Starts at 1." |
| 168 | + ) |
| 169 | + pagination: int = Field( |
| 170 | + default=DEFAULT_MESSAGES_PER_PAGE, |
| 171 | + ge=0, |
| 172 | + description="Maximum number of messages to return. Specifying 0 removes this limit.", |
| 173 | + ) |
| 174 | + start_date: float = Field( |
| 175 | + default=0, |
| 176 | + ge=0, |
| 177 | + alias="startDate", |
| 178 | + description="Start date timestamp. If specified, only messages with " |
| 179 | + "a time field greater or equal to this value will be returned.", |
| 180 | + ) |
| 181 | + end_date: float = Field( |
| 182 | + default=0, |
| 183 | + ge=0, |
| 184 | + alias="endDate", |
| 185 | + description="End date timestamp. If specified, only messages with " |
| 186 | + "a time field lower than this value will be returned.", |
| 187 | + ) |
| 188 | + sort_order: SortOrder = Field( |
| 189 | + default=SortOrder.DESCENDING, |
| 190 | + alias="sortOrder", |
| 191 | + description="Order in which messages should be listed: " |
| 192 | + "-1 means most recent messages first, 1 means older messages first.", |
| 193 | + ) |
| 194 | + hash_only: bool = Field( |
| 195 | + default=True, |
| 196 | + description="By default, only hashes are returned. " |
| 197 | + "Set this to false to include metadata alongside the hashes in the response.", |
| 198 | + ) |
| 199 | + |
| 200 | + @model_validator(mode="after") |
| 201 | + def validate_field_dependencies(self): |
| 202 | + start_date = self.start_date |
| 203 | + end_date = self.end_date |
| 204 | + if start_date and end_date and (end_date < start_date): |
| 205 | + raise ValueError("end date cannot be lower than start date.") |
| 206 | + return self |
| 207 | + |
| 208 | + model_config = ConfigDict(populate_by_name=True) |
0 commit comments