Skip to content

Commit 8255900

Browse files
committed
fix: avoid null values for post_types
1 parent ad569d7 commit 8255900

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

src/aleph/db/accessors/messages.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,10 +382,14 @@ def get_distinct_post_types_for_address(session: DbSession, address: str) -> lis
382382
select(MessageDb.content["type"].astext)
383383
.where(MessageDb.sender == address)
384384
.where(MessageDb.type == MessageType.post)
385+
.where(MessageDb.content["type"].astext.isnot(None))
385386
.distinct()
386387
.order_by(MessageDb.content["type"].astext)
387388
)
388-
return list(session.execute(select_stmt).scalars())
389+
# Explicitly filter out nulls to keep the return type list[str]
390+
return [
391+
ptype for ptype in session.execute(select_stmt).scalars() if ptype is not None
392+
]
389393

390394

391395
def get_distinct_channels_for_address(session: DbSession, address: str) -> list[str]:

tests/api/test_accounts.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,27 @@ def fixture_post_messages_with_types(
103103
time=now + dt.timedelta(seconds=3),
104104
channel=Channel("TEST"),
105105
),
106+
# POST message with null type should be ignored in distinct list
107+
MessageDb(
108+
item_hash="hash_null_type",
109+
chain=Chain.ETH,
110+
sender=TEST_ADDRESS,
111+
signature="0x" + "0" * 128,
112+
item_type=ItemType.inline,
113+
type=MessageType.post,
114+
item_content='{"address":"'
115+
+ TEST_ADDRESS
116+
+ '","time":1652126650.0,"type":null,"content":{"title":"Missing type"}}',
117+
content={
118+
"address": TEST_ADDRESS,
119+
"time": 1652126650.0,
120+
"type": None,
121+
"content": {"title": "Missing type"},
122+
},
123+
size=100,
124+
time=now + dt.timedelta(seconds=3, milliseconds=500),
125+
channel=Channel("TEST"),
126+
),
106127
# Add a non-POST message to ensure it's filtered out
107128
MessageDb(
108129
item_hash="hash5",

tests/db/test_accounts.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,25 @@ def fixture_post_messages_for_types():
176176
time=timestamp_to_datetime(1652126649.5),
177177
channel=Channel("TEST"),
178178
),
179+
# POST message with null type should be ignored
180+
MessageDb(
181+
item_hash="post_hash_null_type",
182+
chain=Chain.ETH,
183+
sender="0xPostAddress123",
184+
signature="0x" + "0" * 128,
185+
item_type=ItemType.inline,
186+
type=MessageType.post,
187+
item_content='{"address":"0xPostAddress123","time":1652126650.0,"type":null,"content":{}}',
188+
content={
189+
"address": "0xPostAddress123",
190+
"time": 1652126650.0,
191+
"type": None,
192+
"content": {},
193+
},
194+
size=100,
195+
time=timestamp_to_datetime(1652126650.0),
196+
channel=Channel("TEST"),
197+
),
179198
# Non-POST message should be filtered out
180199
MessageDb(
181200
item_hash="agg_hash1",

0 commit comments

Comments
 (0)