Centralized session URL generation for commands and events.
CI / Formatting (push) Successful in 5s
CI / Linting (push) Successful in 5s
CI / Tests (Python 3.12) (push) Successful in 2m45s
CI / Tests (Python 3.13) (push) Successful in 2m53s
CI / Tests (Python 3.14) (push) Successful in 2m39s
CI / Type Checking (push) Successful in 9s
CI / Spelling (push) Successful in 5s
CI / Formatting (push) Successful in 5s
CI / Linting (push) Successful in 5s
CI / Tests (Python 3.12) (push) Successful in 2m45s
CI / Tests (Python 3.13) (push) Successful in 2m53s
CI / Tests (Python 3.14) (push) Successful in 2m39s
CI / Type Checking (push) Successful in 9s
CI / Spelling (push) Successful in 5s
This commit is contained in:
@@ -22,22 +22,20 @@ from __future__ import annotations
|
||||
import asyncio
|
||||
import logging
|
||||
import math
|
||||
import posixpath
|
||||
import time
|
||||
from collections.abc import Awaitable, Callable
|
||||
from typing import TYPE_CHECKING, cast
|
||||
from urllib.parse import unquote, urlsplit
|
||||
|
||||
from owlbot._version import __version__
|
||||
from owlbot.api.commands import CommandEvent, CommandHandler, CommandInfo, CommandMark
|
||||
from owlbot.api.context import CommandContext, EventContext
|
||||
from owlbot.api.event_types import ChatEvent
|
||||
from owlbot.sessions import make_session_url_for
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from types import ModuleType
|
||||
|
||||
from owlbot.api.context import ModuleContext
|
||||
from owlbot.api.event_types import User
|
||||
from owlbot.api.owncast_client import OwncastClient
|
||||
from owlbot.sessions import SessionManager
|
||||
|
||||
@@ -547,16 +545,18 @@ class CommandDispatcher:
|
||||
event_ctx: EventContext[ChatEvent] = EventContext(
|
||||
event=event,
|
||||
module=module_ctx,
|
||||
_session_url_for=make_session_url_for(
|
||||
session_manager=self._session_manager,
|
||||
public_base_url=self._public_base_url,
|
||||
module_name=command_info.module_name,
|
||||
user=user,
|
||||
),
|
||||
)
|
||||
|
||||
cmd_ctx = CommandContext(
|
||||
command_event=cmd_event,
|
||||
event_context=event_ctx,
|
||||
module=module_ctx,
|
||||
_session_url_for=self._make_session_url_for(
|
||||
command_info.module_name,
|
||||
user,
|
||||
),
|
||||
)
|
||||
|
||||
try:
|
||||
@@ -626,34 +626,6 @@ class CommandDispatcher:
|
||||
unsanitized=True,
|
||||
)
|
||||
|
||||
def _make_session_url_for(
|
||||
self,
|
||||
module_name: str,
|
||||
user: User,
|
||||
) -> Callable[[str], str]:
|
||||
"""Build the command-scoped session URL helper."""
|
||||
session_manager = self._session_manager
|
||||
public_base_url = self._public_base_url
|
||||
|
||||
def session_url_for(path: str) -> str:
|
||||
if not path.startswith("/"):
|
||||
path = "/" + path
|
||||
destination_path = f"/owlbot/{module_name}{path}"
|
||||
|
||||
module_root = f"/owlbot/{module_name}"
|
||||
decoded_path = unquote(urlsplit(destination_path).path)
|
||||
normalized_path = posixpath.normpath(decoded_path)
|
||||
if normalized_path != module_root and not normalized_path.startswith(
|
||||
f"{module_root}/"
|
||||
):
|
||||
msg = "session destination must stay within the module namespace"
|
||||
raise ValueError(msg)
|
||||
|
||||
token = session_manager.issue_connect_token(destination_path, user=user)
|
||||
return f"{public_base_url}/owlbot/connect/{token}"
|
||||
|
||||
return session_url_for
|
||||
|
||||
|
||||
class ModuleCommands:
|
||||
"""Module-scoped wrapper around CommandDispatcher.
|
||||
|
||||
@@ -34,6 +34,7 @@ from owlbot.api.event_types import (
|
||||
log_event,
|
||||
)
|
||||
from owlbot.api.events import EventHandler, EventMark, Priority
|
||||
from owlbot.sessions import make_session_url_for
|
||||
|
||||
|
||||
class HandlerEntry(NamedTuple):
|
||||
@@ -51,6 +52,7 @@ if TYPE_CHECKING:
|
||||
from types import ModuleType
|
||||
|
||||
from owlbot.api.context import ModuleContext
|
||||
from owlbot.api.event_types import User
|
||||
from owlbot.sessions import SessionManager
|
||||
|
||||
logger = logging.getLogger("owlbot.events")
|
||||
@@ -217,6 +219,7 @@ class EventDispatcher:
|
||||
get_module_context: Callable[[str], ModuleContext],
|
||||
handler_timeout: float,
|
||||
session_manager: SessionManager,
|
||||
public_base_url: str,
|
||||
) -> None:
|
||||
"""Initialize the event dispatcher.
|
||||
|
||||
@@ -227,12 +230,15 @@ class EventDispatcher:
|
||||
:param get_module_context: Callable that looks up a
|
||||
ModuleContext by module name.
|
||||
:param handler_timeout: Timeout for individual handlers in seconds.
|
||||
:param session_manager: Session manager used for browser connect links.
|
||||
:param public_base_url: Public base URL for generated connect links.
|
||||
"""
|
||||
self._handler_registry = EventRegistry()
|
||||
self._command_dispatch = command_dispatch
|
||||
self._get_module_context = get_module_context
|
||||
self._handler_timeout = handler_timeout
|
||||
self._session_manager = session_manager
|
||||
self._public_base_url = public_base_url
|
||||
|
||||
def register(
|
||||
self,
|
||||
@@ -356,7 +362,13 @@ class EventDispatcher:
|
||||
)
|
||||
break
|
||||
|
||||
await self._call_handler(handler, event, module_name, propagation)
|
||||
await self._call_handler(
|
||||
handler,
|
||||
event,
|
||||
module_name,
|
||||
propagation,
|
||||
user=user,
|
||||
)
|
||||
else:
|
||||
logger.debug("No handlers registered for event type: %s", event_type)
|
||||
|
||||
@@ -381,6 +393,8 @@ class EventDispatcher:
|
||||
event: Event,
|
||||
module_name: str,
|
||||
propagation: PropagationState,
|
||||
*,
|
||||
user: User | None,
|
||||
) -> None:
|
||||
"""Call a single handler with timeout enforcement.
|
||||
|
||||
@@ -388,15 +402,27 @@ class EventDispatcher:
|
||||
:param event: The event to pass to the handler.
|
||||
:param module_name: The module that owns this handler.
|
||||
:param propagation: Shared propagation state for this dispatch cycle.
|
||||
:param user: User resolved from the event, if the event carries one.
|
||||
"""
|
||||
handler_name = handler.__name__
|
||||
logger.debug("Calling handler: %s from module: %s", handler_name, module_name)
|
||||
|
||||
module_ctx = self._get_module_context(module_name)
|
||||
session_url_for = (
|
||||
make_session_url_for(
|
||||
session_manager=self._session_manager,
|
||||
public_base_url=self._public_base_url,
|
||||
module_name=module_name,
|
||||
user=user,
|
||||
)
|
||||
if user is not None
|
||||
else None
|
||||
)
|
||||
|
||||
ctx: EventContext[Event] = EventContext(
|
||||
event=event,
|
||||
module=module_ctx,
|
||||
_session_url_for=session_url_for,
|
||||
_propagation=propagation,
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user