diff --git a/config_dist/config.yml b/config_dist/config.yml index c695aed8..0c36d5d2 100644 --- a/config_dist/config.yml +++ b/config_dist/config.yml @@ -51,3 +51,5 @@ file-storage-config: mkdir: true exist-ok: true parents: true +web: + base-url: https://exmple.org diff --git a/shvatka/api/config/models/main.py b/shvatka/api/config/models/main.py index 60290c26..8512651c 100644 --- a/shvatka/api/config/models/main.py +++ b/shvatka/api/config/models/main.py @@ -29,4 +29,5 @@ def from_base( context_path=context_path, app=base.app, enable_logging=enable_logging, + web=base.web, ) diff --git a/shvatka/common/config/models/main.py b/shvatka/common/config/models/main.py index c23193ab..cc77aad3 100644 --- a/shvatka/common/config/models/main.py +++ b/shvatka/common/config/models/main.py @@ -14,6 +14,7 @@ class Config: db: DBConfig redis: RedisConfig file_storage_config: FileStorageConfig + web: WebConfig @property def app_dir(self) -> Path: @@ -39,3 +40,8 @@ class FileStorageConfig: mkdir: bool parents: bool exist_ok: bool + + +@dataclass +class WebConfig: + base_url: str diff --git a/shvatka/common/config/parser/main.py b/shvatka/common/config/parser/main.py index 960dc576..e0218f65 100644 --- a/shvatka/common/config/parser/main.py +++ b/shvatka/common/config/parser/main.py @@ -1,6 +1,6 @@ from dataclass_factory import Factory -from shvatka.common.config.models.main import Config, FileStorageConfig, AppConfig +from shvatka.common.config.models.main import Config, FileStorageConfig, AppConfig, WebConfig from shvatka.common.config.models.paths import Paths from shvatka.infrastructure.db.config.parser.db import load_db_config, load_redis_config @@ -12,6 +12,7 @@ def load_config(config_dct: dict, paths: Paths, dcf: Factory) -> Config: redis=load_redis_config(config_dct["redis"]), file_storage_config=load_file_storage_config(config_dct["file-storage-config"], dcf), app=load_app_config(config_dct["app"], dcf), + web=load_web_config(config_dct["web"], dcf), ) @@ -21,3 +22,7 @@ def load_app_config(config_dct: dict, dcf: Factory) -> AppConfig: def load_file_storage_config(config_dct: dict, dcf: Factory) -> FileStorageConfig: return dcf.load(config_dct, FileStorageConfig) + + +def load_web_config(config_dct: dict, dcf: Factory) -> WebConfig: + return dcf.load(config_dct, WebConfig) diff --git a/shvatka/common/factory.py b/shvatka/common/factory.py index 95bcbffc..73caea12 100644 --- a/shvatka/common/factory.py +++ b/shvatka/common/factory.py @@ -3,6 +3,7 @@ from dishka import Provider, Scope, provide from telegraph.aio import Telegraph +from shvatka.common.url_factory import UrlFactory from shvatka.core.models.schems import schemas from shvatka.tgbot.config.models.bot import BotConfig @@ -26,3 +27,9 @@ def create_dataclass_factory(self) -> dataclass_factory.Factory: default_schema=Schema(name_style=NameStyle.kebab), ) return dcf + + +class UrlProvider(Provider): + scope = Scope.APP + + url_factory = provide(UrlFactory) diff --git a/shvatka/common/url_factory.py b/shvatka/common/url_factory.py new file mode 100644 index 00000000..307d6164 --- /dev/null +++ b/shvatka/common/url_factory.py @@ -0,0 +1,9 @@ +from shvatka.common.config.models.main import WebConfig + + +class UrlFactory: + def __init__(self, config: WebConfig): + self.config = config + + def get_game_id_web_url(self, game_id: int) -> str: + return f"{self.config.base_url}/games/{game_id}" diff --git a/shvatka/infrastructure/di/config.py b/shvatka/infrastructure/di/config.py index 847498ae..53e87b9a 100644 --- a/shvatka/infrastructure/di/config.py +++ b/shvatka/infrastructure/di/config.py @@ -1,6 +1,7 @@ from dishka import Provider, provide, Scope from shvatka.common import FileStorageConfig, Paths, Config +from shvatka.common.config.models.main import WebConfig from shvatka.common.config.parser.paths import common_get_paths from shvatka.infrastructure.db.config.models.db import RedisConfig, DBConfig from shvatka.infrastructure.db.config.models.storage import StorageConfig @@ -44,6 +45,10 @@ def get_bot_storage_config(self, config: TgBotConfig) -> StorageConfig: def get_tg_client_config(self, config: TgBotConfig) -> TgClientConfig: return config.tg_client + @provide + def get_web_app_config(self, config: TgBotConfig) -> WebConfig: + return config.web + class DbConfigProvider(Provider): scope = Scope.APP diff --git a/shvatka/tgbot/config/models/main.py b/shvatka/tgbot/config/models/main.py index bc395d43..ab6100f5 100644 --- a/shvatka/tgbot/config/models/main.py +++ b/shvatka/tgbot/config/models/main.py @@ -30,4 +30,5 @@ def from_base( tg_client=tg_client, file_storage_config=base.file_storage_config, app=base.app, + web=base.web, ) diff --git a/shvatka/tgbot/dialogs/game_manage/getters.py b/shvatka/tgbot/dialogs/game_manage/getters.py index 4c639dd2..26e497a3 100644 --- a/shvatka/tgbot/dialogs/game_manage/getters.py +++ b/shvatka/tgbot/dialogs/game_manage/getters.py @@ -7,6 +7,7 @@ from dishka import AsyncContainer from telegraph import Telegraph +from shvatka.common.url_factory import UrlFactory from shvatka.core.models import dto from shvatka.core.services import game from shvatka.core.services.game import get_authors_games, get_completed_games @@ -30,14 +31,13 @@ async def get_completed_game(dao: HolderDao, dialog_manager: DialogManager, **_) dialog_manager.dialog_data.get("game_id", None) or dialog_manager.start_data["game_id"] ) dishka: AsyncContainer = dialog_manager.middleware_data["dishka_container"] - bot = await dishka.get(Bot) - username = (await bot.get_me()).username + url_factory = await dishka.get(UrlFactory) return { "game": await game.get_game( id_=game_id, dao=dao.game, ), - "webapp_url": f"https://t.me/{username}/games?startapp={game_id}", + "webapp_url": url_factory.get_game_id_web_url(game_id), } diff --git a/shvatka/tgbot/main_factory.py b/shvatka/tgbot/main_factory.py index cf812961..6f4f9022 100644 --- a/shvatka/tgbot/main_factory.py +++ b/shvatka/tgbot/main_factory.py @@ -11,7 +11,7 @@ from dishka.integrations.aiogram import setup_dishka from redis.asyncio import Redis -from shvatka.common.factory import TelegraphProvider, DCFProvider +from shvatka.common.factory import TelegraphProvider, DCFProvider, UrlProvider from shvatka.core.interfaces.clients.file_storage import FileStorage from shvatka.core.utils.key_checker_lock import KeyCheckerFactory from shvatka.core.views.game import GameLogWriter, GameView, GameViewPreparer, OrgNotifier @@ -59,6 +59,7 @@ def get_bot_specific_providers() -> list[Provider]: GameToolsProvider(), UserGetterProvider(), LockProvider(), + UrlProvider(), ] diff --git a/tests/integration/conftest.py b/tests/integration/conftest.py index 5f358c01..de258a4a 100644 --- a/tests/integration/conftest.py +++ b/tests/integration/conftest.py @@ -12,7 +12,7 @@ from shvatka.api.dependencies import AuthProvider, ApiConfigProvider, PlayerProvider, TeamProvider from shvatka.common import Paths -from shvatka.common.factory import DCFProvider, TelegraphProvider +from shvatka.common.factory import DCFProvider, TelegraphProvider, UrlProvider from shvatka.core.interfaces.clients.file_storage import FileStorage, FileGateway from shvatka.core.interfaces.scheduler import Scheduler from shvatka.core.utils.key_checker_lock import KeyCheckerFactory @@ -69,6 +69,7 @@ async def dishka(): MockMessageManagerProvider(), LockProvider(), DCFProvider(), + UrlProvider(), TelegraphProvider(), GamePlayProvider(), GameToolsProvider(),