handoff fixes

This commit is contained in:
debu-2 2026-10-06 03:14:53 +05:30
parent 89ddebdb60
commit 8d5e9712a6
20 changed files with 1056 additions and 141 deletions

View file

@ -5,8 +5,8 @@ from datetime import datetime, timedelta, timezone
from bson import ObjectId
from app.services.message_ids import (
first_message_id,
normalize_message_id,
canonical_message_id,
folded_message_id,
parse_message_id_list,
)
from app.services.subject_normalize import normalize_subject
@ -52,14 +52,17 @@ def resolve_thread_from_known(
known_threads: dict[ObjectId, list[KnownMessage]] | None = None,
now: datetime | None = None,
) -> ThreadResolution:
scoped = {
key: value
for key, value in known_by_internet_id.items()
if value.account_id == account_id
}
in_reply = normalize_message_id(in_reply_to)
if in_reply and in_reply in scoped:
parent = scoped[in_reply]
scoped: dict[str, KnownMessage] = {}
for key, value in known_by_internet_id.items():
if value.account_id != account_id:
continue
fold_key = folded_message_id(key) or folded_message_id(value.internet_message_id)
if fold_key:
scoped[fold_key] = value
in_reply = canonical_message_id(in_reply_to)
in_reply_folded = folded_message_id(in_reply)
if in_reply_folded and in_reply_folded in scoped:
parent = scoped[in_reply_folded]
return ThreadResolution(
thread_id=parent.thread_id,
parent_message_id=parent.id,
@ -68,9 +71,9 @@ def resolve_thread_from_known(
)
reference_ids = [
normalize_message_id(value)
folded_message_id(value)
for value in references
if normalize_message_id(value)
if folded_message_id(value)
]
for reference in reversed(reference_ids):
if reference in scoped:
@ -155,22 +158,31 @@ async def lookup_known_messages(
account_id: str,
internet_ids: list[str],
) -> dict[str, KnownMessage]:
normalized_ids = [
folded_ids = [
value
for value in {normalize_message_id(item) for item in internet_ids}
for value in {folded_message_id(item) for item in internet_ids}
if value
]
if not normalized_ids:
if not folded_ids:
return {}
pipeline = [
{
"$match": {
"message_id": {"$in": normalized_ids},
"$or": [
{"message_scope": "EXTERNAL"},
{"message_scope": {"$exists": False}},
],
"$and": [
{
"$or": [
{"message_id_folded": {"$in": folded_ids}},
{"message_id": {"$in": folded_ids}},
]
},
{
"$or": [
{"message_scope": "EXTERNAL"},
{"message_scope": {"$exists": False}},
]
},
]
}
},
{
@ -198,15 +210,18 @@ async def lookup_known_messages(
known: dict[str, KnownMessage] = {}
cursor = await database.messages.aggregate(pipeline)
async for document in cursor:
internet_id = normalize_message_id(document.get("message_id"))
if not internet_id:
internet_id = canonical_message_id(
document.get("internet_message_id") or document.get("message_id")
)
internet_key = folded_message_id(internet_id)
if not internet_id or not internet_key:
continue
entry = document["entry"][0]
thread_id = entry.get("thread_id") or document.get("thread_id")
if thread_id is None:
continue
participants = participants_from_parsed(document)
known[internet_id] = KnownMessage(
known[internet_key] = KnownMessage(
id=document["_id"],
thread_id=thread_id,
internet_message_id=internet_id,