Fix some task quirks

This commit is contained in:
2026-03-08 23:16:15 +00:00
parent acedc01e83
commit a7421b9350
7 changed files with 312 additions and 34 deletions

View File

@@ -7,7 +7,7 @@ from asgiref.sync import sync_to_async
from core.clients import transport
from core.models import ChatSession, Message
STATUS_VISIBLE_SOURCE_SERVICES = {"web", "xmpp"}
STATUS_VISIBLE_SOURCE_SERVICES = {"web", "xmpp", "signal", "whatsapp"}
def chunk_for_transport(text: str, limit: int = 3000) -> list[str]:

View File

@@ -15,7 +15,13 @@ from core.commands.policies import ensure_variant_policies_for_profile
from core.commands.registry import get as get_handler
from core.commands.registry import register
from core.messaging.reply_sync import is_mirrored_origin
from core.models import CommandAction, CommandChannelBinding, CommandProfile, Message
from core.models import (
CommandAction,
CommandChannelBinding,
CommandProfile,
Message,
PersonIdentifier,
)
from core.security.command_policy import CommandSecurityContext, evaluate_command_policy
from core.tasks.chat_defaults import ensure_default_source_for_chat
from core.util import logs
@@ -50,6 +56,65 @@ def _canonical_channel_identifier(service: str, channel_identifier: str) -> str:
return value
def _signal_identifier_rank(identifier_value: str) -> int:
identifier_text = str(identifier_value or "").strip()
if not identifier_text:
return 99
if identifier_text.startswith("group."):
return 0
if identifier_text.startswith("+"):
return 1
return 2
def _expand_service_channel_variants(
user_id: int,
service: str,
identifiers: list[str],
) -> list[str]:
variants: list[str] = []
for identifier in identifiers:
for value in _channel_variants(service, identifier):
if value and value not in variants:
variants.append(value)
if str(service or "").strip().lower() != "signal" or not variants:
return variants
person_ids = list(
PersonIdentifier.objects.filter(
user_id=user_id,
service="signal",
identifier__in=variants,
)
.values_list("person_id", flat=True)
.distinct()
)
if not person_ids:
return variants
alias_rows = list(
PersonIdentifier.objects.filter(
user_id=user_id,
service="signal",
person_id__in=person_ids,
).values_list("identifier", flat=True)
)
for value in alias_rows:
cleaned = str(value or "").strip()
if cleaned and cleaned not in variants:
variants.append(cleaned)
variants.sort(key=lambda value: (_signal_identifier_rank(value), value))
return variants
def _preferred_channel_identifier(service: str, identifiers: list[str]) -> str:
cleaned = [str(value or "").strip() for value in identifiers if str(value or "").strip()]
if not cleaned:
return ""
if str(service or "").strip().lower() == "signal":
cleaned.sort(key=lambda value: (_signal_identifier_rank(value), value))
return cleaned[0]
return cleaned[0]
def _effective_bootstrap_scope(
ctx: CommandContext,
trigger_message: Message,
@@ -192,7 +257,8 @@ def _auto_setup_profile_bindings_for_first_command(
service, identifier = _effective_bootstrap_scope(ctx, trigger_message)
service = str(service or "").strip().lower()
canonical = _canonical_channel_identifier(service, identifier)
variants = _channel_variants(service, canonical)
variants = _expand_service_channel_variants(ctx.user_id, service, [canonical])
canonical = _preferred_channel_identifier(service, variants) or canonical
if not service or not variants:
return
for slug in slugs:
@@ -252,9 +318,17 @@ async def _eligible_profiles(ctx: CommandContext) -> list[CommandProfile]:
.filter(id=ctx.message_id, user_id=ctx.user_id)
.first()
)
direct_variants = _channel_variants(ctx.service, ctx.channel_identifier)
direct_variants = _expand_service_channel_variants(
ctx.user_id,
ctx.service,
[ctx.channel_identifier],
)
source_channel = str(getattr(trigger, "source_chat_id", "") or "").strip()
for expanded in _channel_variants(ctx.service, source_channel):
for expanded in _expand_service_channel_variants(
ctx.user_id,
ctx.service,
[source_channel],
):
if expanded and expanded not in direct_variants:
direct_variants.append(expanded)
if not direct_variants:
@@ -278,8 +352,16 @@ async def _eligible_profiles(ctx: CommandContext) -> list[CommandProfile]:
identifier = getattr(getattr(trigger, "session", None), "identifier", None)
fallback_service = str(getattr(identifier, "service", "") or "").strip().lower()
fallback_identifier = str(getattr(identifier, "identifier", "") or "").strip()
fallback_variants = _channel_variants(fallback_service, fallback_identifier)
for expanded in _channel_variants(fallback_service, source_channel):
fallback_variants = _expand_service_channel_variants(
ctx.user_id,
fallback_service,
[fallback_identifier],
)
for expanded in _expand_service_channel_variants(
ctx.user_id,
fallback_service,
[source_channel],
):
if expanded and expanded not in fallback_variants:
fallback_variants.append(expanded)
if not fallback_service or not fallback_variants: