Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

1.2 KB
Raw Blame
"""Application service for listing Explore banners.

ExploreBanner is the legacy contract name shared by the API, feature flag, and database model.
"""

from collections.abc import Sequence
from datetime import datetime
from typing import Any, NamedTuple, Protocol

from constants.languages import languages

_DEFAULT_LANGUAGE = "en-US"


class ExploreBannerRecord(NamedTuple):
    id: str
    content: dict[str, Any]
    link: str
    sort: int
    status: str
    created_at: datetime


class ExploreBannerQuery(Protocol):
    def list_enabled(self, language: str) -> Sequence[ExploreBannerRecord]: ...


class ExploreBannerQueryService:
    def __init__(
        self,
        *,
        banners: ExploreBannerQuery,
        enabled: bool,
    ) -> None:
        self._banners = banners
        self._enabled = enabled

    def list_for_language(self, language: str) -> tuple[ExploreBannerRecord, ...]:
        if not self._enabled:
            return ()

        language = language if language in languages else _DEFAULT_LANGUAGE
        banners = tuple(self._banners.list_enabled(language))
        if banners or language == _DEFAULT_LANGUAGE:
            return banners

        return tuple(self._banners.list_enabled(_DEFAULT_LANGUAGE))