Cleanups: Normalize pub keys, prefix message claiming, cursor + null timestamp DB cleanups

This commit is contained in:
Jack Kingsman
2026-02-02 16:22:10 -08:00
parent 8674e804c2
commit 71872517e5
19 changed files with 563 additions and 64 deletions
+10 -4
View File
@@ -19,7 +19,7 @@ from app.models import (
from app.packet_processor import start_historical_dm_decryption
from app.radio import radio_manager
from app.radio_sync import pause_polling
from app.repository import ContactRepository
from app.repository import ContactRepository, MessageRepository
logger = logging.getLogger(__name__)
@@ -119,8 +119,9 @@ async def create_contact(
return existing
# Create new contact
lower_key = request.public_key.lower()
contact_data = {
"public_key": request.public_key,
"public_key": lower_key,
"name": request.name,
"type": 0, # Unknown
"flags": 0,
@@ -134,11 +135,16 @@ async def create_contact(
"last_contacted": None,
}
await ContactRepository.upsert(contact_data)
logger.info("Created contact %s", request.public_key[:12])
logger.info("Created contact %s", lower_key[:12])
# Promote any prefix-stored messages to this full key
claimed = await MessageRepository.claim_prefix_messages(lower_key)
if claimed > 0:
logger.info("Claimed %d prefix messages for contact %s", claimed, lower_key[:12])
# Trigger historical decryption if requested
if request.try_historical:
await start_historical_dm_decryption(background_tasks, request.public_key, request.name)
await start_historical_dm_decryption(background_tasks, lower_key, request.name)
return Contact(**contact_data)
+10 -4
View File
@@ -22,6 +22,10 @@ async def list_messages(
conversation_key: str | None = Query(
default=None, description="Filter by conversation key (channel key or contact pubkey)"
),
before: int | None = Query(
default=None, description="Cursor: received_at of last seen message"
),
before_id: int | None = Query(default=None, description="Cursor: id of last seen message"),
) -> list[Message]:
"""List messages from the database."""
return await MessageRepository.get_all(
@@ -29,6 +33,8 @@ async def list_messages(
offset=offset,
msg_type=type,
conversation_key=conversation_key,
before=before,
before_id=before_id,
)
@@ -94,7 +100,7 @@ async def send_direct_message(request: SendDirectMessageRequest) -> Message:
message_id = await MessageRepository.create(
msg_type="PRIV",
text=request.text,
conversation_key=db_contact.public_key,
conversation_key=db_contact.public_key.lower(),
sender_timestamp=now,
received_at=now,
outgoing=True,
@@ -106,7 +112,7 @@ async def send_direct_message(request: SendDirectMessageRequest) -> Message:
)
# Update last_contacted for the contact
await ContactRepository.update_last_contacted(db_contact.public_key, now)
await ContactRepository.update_last_contacted(db_contact.public_key.lower(), now)
# Track the expected ACK for this message
expected_ack = result.payload.get("expected_ack")
@@ -119,7 +125,7 @@ async def send_direct_message(request: SendDirectMessageRequest) -> Message:
message = Message(
id=message_id,
type="PRIV",
conversation_key=db_contact.public_key,
conversation_key=db_contact.public_key.lower(),
text=request.text,
sender_timestamp=now,
received_at=now,
@@ -133,7 +139,7 @@ async def send_direct_message(request: SendDirectMessageRequest) -> Message:
asyncio.create_task(
run_bot_for_message(
sender_name=None,
sender_key=db_contact.public_key,
sender_key=db_contact.public_key.lower(),
message_text=request.text,
is_dm=True,
channel_key=None,