diff --git a/src/aleph/chains/common.py b/src/aleph/chains/common.py index 1f5885c0e..72cc7e4d2 100644 --- a/src/aleph/chains/common.py +++ b/src/aleph/chains/common.py @@ -5,7 +5,7 @@ from enum import IntEnum from typing import Dict, Optional, Tuple, List -from aleph_message.models import MessageType +from aleph_message.models import MessageType, ItemType from bson import ObjectId from pymongo import UpdateOne @@ -28,6 +28,7 @@ from aleph.storage import get_json, pin_hash, add_json, get_message_content from .tx_context import TxContext from ..schemas.pending_messages import BasePendingMessage +from ..utils import item_type_from_hash LOGGER = logging.getLogger("chains.common") @@ -104,6 +105,23 @@ async def mark_message_for_retry( LOGGER.debug(f"Update result {result}") +def update_message_item_type(message_dict: Dict) -> Dict: + """ + Ensures that the item_type field of a message is present. + Sets it to the default value if the field is not specified. + """ + if "item_type" in message_dict: + return message_dict + + if "item_content" in message_dict: + item_type = ItemType.inline + else: + item_type = item_type_from_hash(message_dict["item_hash"]) + + message_dict["item_type"] = item_type + return message_dict + + async def incoming( message: Dict, chain_name: Optional[str] = None, @@ -120,6 +138,10 @@ async def incoming( if existing in database, created if not. """ + # TODO: this is a temporary fix to set the item_type of the message to the correct + # value. This should be replaced by a full use of Pydantic models. + message = update_message_item_type(message) + item_hash = message["item_hash"] sender = message["sender"] ids_key = (item_hash, sender, chain_name) diff --git a/src/aleph/network.py b/src/aleph/network.py index 148cc6612..7074d5b6f 100644 --- a/src/aleph/network.py +++ b/src/aleph/network.py @@ -69,10 +69,6 @@ async def check_message( message = parse_message(message_dict) - # TODO: this is a temporary fix to set the item_type of the message to the correct - # value. This should be replaced by a full use of Pydantic models. - message_dict["item_type"] = message.item_type.value - if trusted: # only in the case of a message programmatically built here # from legacy native chain signing for example (signing offloaded)