Files
zeekay 1b367e2b62 chore: rebase the tree onto upstream master
The tree was a debranded snapshot of upstream taken around 2026-02-20, and
upstream has moved about five months since. This replaces it with upstream
master, mechanically re-derived: bin/debrand does the rename, and the
pipeline and squashed migrations are restored from the shipped tree.

Not a merge. The previous import replaced the tree wholesale in a single
commit, so there is no shared content history to merge against. Reproducing
the rename on the new tree is what actually converges.

What survives: .hanzo/workflows, the Dockerfiles and bin/docker* entrypoints,
compose files, LLM.md, and insights/migrations. The squashed initial
migration is load-bearing — the live database records it by name, so
upstream's ~918 individual migrations cannot come back.

Nine products are dropped as unreferenced. Twenty-one more that we do not run
are kept anyway because the core app still imports them; unpicking those is a
refactor and does not belong in a rebase.

This base does not build yet, and the reasons are not in the rename:
upstream now wants Python 3.13.13 against our 3.12.12 and Django 5.2 against
our 4.2, and the dependency manifests still name upstream packages that we
republish under our own names. Those are decisions, not renames.
2026-08-01 15:44:40 -07:00

53 lines
2.2 KiB
Python

"""Parsing and user resolution for @-mention tokens embedded in thread message content.
Mentions are stored inline as ``@[Display Name](email)`` (the canonical format
shared with Insights Desktop clients), so any layer can answer "who does this
message mention?" from the plain string alone.
"""
import re
from typing import Any
from django.db.models.functions import Lower
MENTION_TOKEN_PATTERN = re.compile(r"@\[[^\][\n]+\]\(([^\s()]+@[^\s()]+)\)")
# Content length is unbounded, so cap how many distinct emails one message may
# resolve to keep the lookup's IN list from growing with attacker-sized input.
MAX_RESOLVED_MENTIONS_PER_MESSAGE = 50
def extract_mention_emails(content: str) -> set[str]:
"""Emails mentioned in the content, lowercased and deduped."""
return {match.group(1).lower() for match in MENTION_TOKEN_PATTERN.finditer(content)}
def format_mention_token(name: str, email: str) -> str:
"""Serialize a user reference into the inline mention token.
Brackets and newlines would break token parsing; the email is the identity,
so the name falls back to its local part when unusable.
"""
safe_name = re.sub(r"[\[\]\n]", " ", name).strip() or email.split("@")[0] or email
return f"@[{safe_name}]({email})"
def resolve_mentioned_user_ids(user_model: Any, content: str, *, team_id: int, author_id: int | None) -> list[int]:
"""Ids of the team's org members mentioned in the content, excluding the author.
Emails resolve case-insensitively and only to members of the team's organization.
``user_model`` is injected so the backfill migration can pass its historical model
and stay filter-identical with the write path.
"""
emails = sorted(extract_mention_emails(content))[:MAX_RESOLVED_MENTIONS_PER_MESSAGE]
if not emails:
return []
member_ids = (
user_model.objects.annotate(_email_lower=Lower("email"))
# Organization.members sets related_query_name="organization" (singular) for User filters.
.filter(organization__team__id=team_id, _email_lower__in=emails)
.values_list("id", flat=True)
.distinct()
)
return [user_id for user_id in member_ids if user_id != author_id]