diff --git a/src/aleph/sdk/models.py b/src/aleph/sdk/models.py index f8cdec9d..3de92c74 100644 --- a/src/aleph/sdk/models.py +++ b/src/aleph/sdk/models.py @@ -37,12 +37,15 @@ class Post(BaseModel): ) address: str = Field(description="The address of the sender of the POST message") ref: Optional[str] = Field(description="Other message referenced by this one") - channel: str = Field(description="The channel where the POST message was published") + channel: Optional[str] = Field(description="The channel where the POST message was published") created: datetime = Field(description="The time when the POST message was created") last_updated: datetime = Field( description="The time when the POST message was last updated" ) + class Config: + allow_extra = False + class PostsResponse(PaginationResponse): """Response from an Aleph node API on the path /api/v0/posts.json""" diff --git a/src/aleph/sdk/node/__init__.py b/src/aleph/sdk/node/__init__.py index 1477ac2c..f95256cc 100644 --- a/src/aleph/sdk/node/__init__.py +++ b/src/aleph/sdk/node/__init__.py @@ -98,6 +98,8 @@ def add(self, messages: Union[AlephMessage, Iterable[AlephMessage]]): if isinstance(messages, typing.get_args(AlephMessage)): messages = [messages] + messages = list(messages) + message_data = (message_to_model(message) for message in messages) MessageModel.insert_many(message_data).on_conflict_replace().execute() @@ -105,16 +107,18 @@ def add(self, messages: Union[AlephMessage, Iterable[AlephMessage]]): post_data = [] amend_messages = [] for message in messages: - if message.item_type != MessageType.post: + if message.type != MessageType.post.value: continue if message.content.type == "amend": amend_messages.append(message) - else: - post = message_to_post(message).dict() - post_data.append(post) - # Check if we can now add any amend messages that had missing refs - if message.item_hash in self.missing_posts: - amend_messages += self.missing_posts.pop(message.item_hash) + continue + post = message_to_post(message).dict() + post["chain"] = message.chain.value + post["tags"] = message.content.content.get("tags", None) + post_data.append(post) + # Check if we can now add any amend messages that had missing refs + if message.item_hash in self.missing_posts: + amend_messages += self.missing_posts.pop(message.item_hash) PostModel.insert_many(post_data).on_conflict_replace().execute() diff --git a/src/aleph/sdk/node/post.py b/src/aleph/sdk/node/post.py index b68a421d..e4af0807 100644 --- a/src/aleph/sdk/node/post.py +++ b/src/aleph/sdk/node/post.py @@ -58,7 +58,7 @@ def message_to_post(message: PostMessage) -> Post: "ref": message.content.ref if hasattr(message.content, "ref") else None, "channel": message.channel, "created": datetime.fromtimestamp(message.time), - "last_updated": datetime.fromtimestamp(message.time), + "last_updated": datetime.fromtimestamp(message.time) } ) diff --git a/tests/unit/test_node_get.py b/tests/unit/test_node_get.py index 48bff3b8..5f80d2c6 100644 --- a/tests/unit/test_node_get.py +++ b/tests/unit/test_node_get.py @@ -13,7 +13,7 @@ from aleph.sdk.chains.ethereum import get_fallback_account from aleph.sdk.exceptions import MessageNotFoundError -from aleph.sdk.node import MessageCache +from aleph.sdk.node import MessageCache, message_to_post @pytest.mark.asyncio @@ -137,7 +137,7 @@ def class_teardown(self): @pytest.mark.asyncio async def test_addresses(self): items = (await self.cache.get_posts(addresses=[self.messages[1].sender])).posts - assert items[0] == self.messages[1] + assert items[0] == message_to_post(self.messages[1]) @pytest.mark.asyncio async def test_tags(self): @@ -153,15 +153,16 @@ async def test_types(self): @pytest.mark.asyncio async def test_channels(self): + print(self.messages[1]) assert (await self.cache.get_posts(channels=[self.messages[1].channel])).posts[ 0 - ] == self.messages[1] + ] == message_to_post(self.messages[1]) @pytest.mark.asyncio async def test_chains(self): assert (await self.cache.get_posts(chains=[self.messages[1].chain])).posts[ 0 - ] == self.messages[1] + ] == message_to_post(self.messages[1]) @pytest.mark.asyncio