Compare commits
128 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 394bd19b95 | |||
| b52d4000b4 | |||
| b32f962f11 | |||
| 0c1d3abd7e | |||
| d81564c308 | |||
| accb3b2405 | |||
| a63e3bef1e | |||
| 9d9aca53df | |||
| 5b6971fda5 | |||
| b496a401fc | |||
| 76c6818952 | |||
| 633a020212 | |||
| ab38238fe8 | |||
| 4145cacc52 | |||
| 6d59737d07 | |||
| 71ffcce06b | |||
| 72f43dbef2 | |||
| a5f4a68c6a | |||
| b2497ed877 | |||
| 9b42ea034a | |||
| f94bea3e74 | |||
| cde1e4311f | |||
| 847a40815f | |||
| 6fd03ef836 | |||
| c2ccc35e50 | |||
| 3418d1a46c | |||
| fac5d75c7e | |||
| 7a2965b43f | |||
| a0df94fc91 | |||
| 79694f7de8 | |||
| 542f15f2d6 | |||
| 64216f5a26 | |||
| 383e2c1d8d | |||
| bfa979a224 | |||
| c69ebf6c03 | |||
| 040b0a3cdb | |||
| a5aed14dd2 | |||
| 9fc434b42b | |||
| c2cc7fd9a8 | |||
| 3447acd8c4 | |||
| 56aeca5288 | |||
| 6ed0a120a0 | |||
| 682dd3fdec | |||
| c955e1572f | |||
| a9aa84af0f | |||
| dcbd9bab41 | |||
| 92d5d9c2d3 | |||
| 47d106e288 | |||
| a5624897e9 | |||
| 11e75d036a | |||
| 2942da0c35 | |||
| 549c0c96ae | |||
| dd9337dd20 | |||
| 3cc3b373e5 | |||
| f6d5281af8 | |||
| fa63886195 | |||
| 9bd5fe75c9 | |||
| d931da37ec | |||
| 9375fa45b2 | |||
| 0b45aee96d | |||
| 80e346d6b5 | |||
| eff0128d29 | |||
| 8214e052af | |||
| 2a233b2b1e | |||
| 5e3028e470 | |||
| 63193310f2 | |||
| af37f3a8ec | |||
| 66228cf106 | |||
| 9c59240f48 | |||
| baa25f2e1e | |||
| 7a2ed808c4 | |||
| dd0828a63d | |||
| 72a392e652 | |||
| e1fac04775 | |||
| 7e02e86cd6 | |||
| eb9a159dbb | |||
| 66dc53f12f | |||
| 50f5307aac | |||
| 5fa7e26f72 | |||
| 976e204102 | |||
| 9d4256353d | |||
| 543fc42a6d | |||
| bfed400b4d | |||
| d0ddf3fb58 | |||
| 654db04d44 | |||
| 3a94becf05 | |||
| 31d8f59f1e | |||
| 31e08ba073 | |||
| 7c8e14c44f | |||
| b57332bd5c | |||
| 73714c9525 | |||
| 8319edda38 | |||
| 5e1f0a00ad | |||
| 987013974c | |||
| 7249ca079d | |||
| 7fac5926fc | |||
| 9f7b772680 | |||
| 1853a7a9c7 | |||
| befb2da6a0 | |||
| d29c6c0725 | |||
| 47b22c7401 | |||
| b4a39c027f | |||
| dd9eab2e4a | |||
| 492d47a863 | |||
| fe8d5fe026 | |||
| a2fa9aaa6c | |||
| 5b65ac4a2f | |||
| feb3e08b63 | |||
| f1d8f56fec | |||
| 08ffc6694e | |||
| 3199c48fcd | |||
| 2a707e4825 | |||
| 5dbec1a0a4 | |||
| 7426000937 | |||
| 0c62631ab6 | |||
| db9a931ed6 | |||
| 35548a03cb | |||
| dda393c372 | |||
| 1e9bf4ab25 | |||
| 690aa0272f | |||
| d871f2c142 | |||
| 9712fe125b | |||
| fdfc73ae9c | |||
| e93e777fb3 | |||
| a13edf20af | |||
| fcd7de035f | |||
| fb0c29eefe | |||
| 9ff5cc4a67 |
@@ -10,6 +10,17 @@ TELEGRAM_BOT_USERNAME=YOUR_BOT_USERNAME_HERE
|
||||
# Используется ботом для кнопки меню Telegram и кнопки /start.
|
||||
TELEGRAM_MINI_APP_URL=
|
||||
|
||||
# Токен Discord application bot
|
||||
# Можно получить в Discord Developer Portal (https://discord.com/developers/applications)
|
||||
DISCORD_BOT_TOKEN=YOUR_DISCORD_BOT_TOKEN_HERE
|
||||
|
||||
# Discord OAuth (для Web Dashboard)
|
||||
# Client ID и Secret из OAuth2 раздела Discord Developer Portal
|
||||
# Redirect URI должен указывать на /auth/discord/callback вашего домена
|
||||
DISCORD_CLIENT_ID=YOUR_DISCORD_CLIENT_ID_HERE
|
||||
DISCORD_CLIENT_SECRET=YOUR_DISCORD_CLIENT_SECRET_HERE
|
||||
DISCORD_REDIRECT_URI=https://your-domain.example/auth/discord/callback
|
||||
|
||||
# Пароль для базы данных PostgreSQL
|
||||
POSTGRES_PASSWORD=StrongPasswordForDatabase
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@ on:
|
||||
- main
|
||||
|
||||
env:
|
||||
VERSION: 2.5.0
|
||||
VERSION: 3.5.1
|
||||
|
||||
jobs:
|
||||
# ЧАСТЬ 1: Собираем образы и кладем в Gitea (чтобы делиться с ребятами)
|
||||
@@ -113,14 +113,47 @@ jobs:
|
||||
echo "DISCORD_BOT_TOKEN=${{ secrets.DISCORD_BOT_TOKEN }}" >> .env
|
||||
echo "TELEGRAM_BOT_USERNAME=${{ secrets.TELEGRAM_BOT_USERNAME }}" >> .env
|
||||
echo "TELEGRAM_MINI_APP_URL=${{ secrets.TELEGRAM_MINI_APP_URL }}" >> .env
|
||||
echo "DISCORD_CLIENT_ID=${{ secrets.DISCORD_CLIENT_ID }}" >> .env
|
||||
echo "DISCORD_CLIENT_SECRET=${{ secrets.DISCORD_CLIENT_SECRET }}" >> .env
|
||||
echo "DISCORD_REDIRECT_URI=${{ secrets.DISCORD_REDIRECT_URI }}" >> .env
|
||||
|
||||
- name: Deploy Containers
|
||||
run: |
|
||||
# Авторизуемся локальным докером в нашей Gitea
|
||||
docker login git.codeanddice.ru/ -u toutsu -p ${{ secrets.GIT_TOKEN }}
|
||||
|
||||
|
||||
# Pull гарантирует, что мы получили нужную версию.
|
||||
docker compose pull bot discord web
|
||||
|
||||
|
||||
# Запускаем! Флаг -d оставит их работать в фоне.
|
||||
docker compose up -d
|
||||
|
||||
# Ждём, пока сервисы перейдут в healthy или упадут
|
||||
SERVICES="bot discord web"
|
||||
MAX_WAIT=40
|
||||
INTERVAL=5
|
||||
ELAPSED=0
|
||||
|
||||
while [ $ELAPSED -lt $MAX_WAIT ]; do
|
||||
NOT_HEALTHY=0
|
||||
for svc in $SERVICES; do
|
||||
HEALTH=$(docker compose ps $svc --format="{{.Health}}" 2>/dev/null | head -n1)
|
||||
if [ "$HEALTH" != "healthy" ]; then
|
||||
STATE=$(docker compose ps $svc --format="{{.State}}" 2>/dev/null | head -n1)
|
||||
echo "❌ $svc not healthy yet (state: ${STATE:-unknown})"
|
||||
NOT_HEALTHY=$((NOT_HEALTHY + 1))
|
||||
fi
|
||||
done
|
||||
|
||||
if [ $NOT_HEALTHY -eq 0 ]; then
|
||||
echo "✅ All services are healthy!"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
sleep $INTERVAL
|
||||
ELAPSED=$((ELAPSED + INTERVAL))
|
||||
done
|
||||
|
||||
echo "⏰ Timed out waiting for services to become healthy"
|
||||
docker compose ps
|
||||
exit 1
|
||||
|
||||
BIN
Binary file not shown.
@@ -1,6 +1,6 @@
|
||||
<Project>
|
||||
<PropertyGroup>
|
||||
<Version>2.5.0</Version>
|
||||
<Version>3.5.1</Version>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<LangVersion>preview</LangVersion>
|
||||
<Nullable>enable</Nullable>
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
# Discord /newsession и /listsessions — Issue #28
|
||||
|
||||
## Что реализовано
|
||||
- Slash-команда /newsession для создания игровых сессий прямо из Discord.
|
||||
- Slash-команда /listsessions для просмотра предстоящих игр в сервере.
|
||||
- DiscordPermissionChecker — проверка прав (owner / admin / manager).
|
||||
- DiscordPlatformMessenger — реализация IPlatformMessenger для Discord (NetCord REST).
|
||||
- Полная интеграция в DI (Program.cs).
|
||||
|
||||
## Архитектура
|
||||
- Vertical slice: каждая команда — отдельный файл (Command + Handler).
|
||||
- Platform-agnostic SQL: используются колонки platform, external_group_id, external_user_id.
|
||||
- Рендеринг переиспользует существующий DiscordSessionBatchRenderer.
|
||||
|
||||
## TDD
|
||||
- 212 тестов, все зелёные.
|
||||
- Source-level тесты проверяют паттерны: Dapper, Npgsql, транзакции, CancellationToken, платформенную нейтральность.
|
||||
|
||||
## Версия
|
||||
- Minor bump: 2.3.0 → 2.4.0
|
||||
- Синхронизировано: Directory.Build.props, compose.yaml, deploy.yml, NavMenu.razor.
|
||||
|
||||
Closes #28
|
||||
@@ -1,10 +1,10 @@
|
||||
# 🎲 GM-Relay: TTRPG Session Scheduling Bot & Web Dashboard
|
||||
|
||||
**GM-Relay** — это комплексное решение для Мастеров Подземелий (ГМов), состоящее из высокопроизводительного Telegram-бота и удобного веб-интерфейса. Предназначено для автоматизации записи игроков на сессии, управления расписанием и проведения игр.
|
||||
**GM-Relay** — это комплексное решение для Мастеров Подземелий (ГМов), состоящее из высокопроизводительного Telegram-бота, Discord worker и удобного веб-интерфейса. Предназначено для автоматизации записи игроков на сессии, управления расписанием и проведения игр.
|
||||
|
||||
Проект разработан с упором на производительность, архитектуру Vertical Slice, Native AOT (для бота) и удобство развертывания с использованием .NET Aspire.
|
||||
|
||||
**Текущая версия:** `v2.5.0`.
|
||||
**Текущая версия:** `v3.5.1`.
|
||||
|
||||
---
|
||||
|
||||
@@ -22,11 +22,14 @@
|
||||
- **🔔 Уведомления**: Игрок получают за 24 часа, напоминание за 1 час, ссылку перед игрой, отмены и переносы; групповые уведомления при этом остаются.
|
||||
- **🕐 Режим уведомлений batch**: Для каждой пачки можно выбрать `В группе и в личку` или `Только в группе`.
|
||||
- **⬆️ Управление очередью**: Веб-интерфейс показывает заполненность, лист ожидания и позволяет ГМу поднять первого игрока из очереди.
|
||||
- **🔄 Автоматическая синхронизация**: Любые изменения в веб-интерфейсе мгновенно обновляют сообщения с расписанием в Telegram-чатах игроков.
|
||||
- **🔄 Автоматическая синхронизация**: Любые изменения в веб-интерфейсе мгновенно обновляют сообщения с расписанием в подключенных Telegram- и Discord-каналах.
|
||||
|
||||
### Discord Bot
|
||||
- **Slash-команды расписания**: GM создаёт сессию через `/newsession` и публикует актуальное расписание через `/listsessions`.
|
||||
- **Кнопки записи и выхода**: игроки нажимают Join/Leave в Discord-сообщении; бот отвечает ephemeral-сообщением и обновляет schedule message.
|
||||
- **Slash-команды `/newsession` и `/listsessions`**: GM создаёт сессии и публикует актуальное расписание прямо в Discord-канале.
|
||||
- **Кнопки Join/Leave с ephemeral-ответами**: игроки нажимают Join/Leave в Discord-сообщении; бот отвечает ephemeral-сообщением и обновляет schedule message.
|
||||
- **RSVP (подтверждения) за 24ч до сессии**: scheduler публикует запрос подтверждения в Discord-канале, игроки отвечают кнопками, а GM получает итоги RSVP.
|
||||
- **DM-напоминания за 1ч и ссылки перед игрой**: one-hour reminders и join-link notifications отправляются в Discord DM при включённых личных уведомлениях; сбои DM логируются без публичного fallback.
|
||||
- **Reschedule voting (голосование за перенос)**: deadline-сервис обновляет Discord vote message и schedule message через `IPlatformMessenger`.
|
||||
- **Лимиты и waitlist**: при заполненном составе игрок попадает в waitlist, а при выходе участника первый ожидающий автоматически продвигается в основной состав.
|
||||
|
||||
### 🌐 Web Dashboard (Blazor Server)
|
||||
@@ -34,6 +37,8 @@
|
||||
- **📱 Telegram Mini App Dashboard**: Мобильная панель открывается из Telegram, проверяет `initData` на сервере, учитывает safe-area телефона и верхнюю панель Telegram.
|
||||
- **✏️ Редактирование**: Детальное изменение дат, названий и статусов сессий.
|
||||
- **🤝 Co-GM и делегирование**: Owner назначает помощников по Telegram ID; co-GM управляет расписанием, но **не может назначать других co-GM**.
|
||||
- **🌍 Публичные страницы клубов**: Owner и co-GM включают read-only страницу `/club/{slug}` и отдельные ссылки `/s/{sessionId}` только для опубликованных сессий; состав игроков и приватные join-ссылки не показываются.
|
||||
- **🧑🏫 Публичные профили мастеров**: мастер управляет профилем из `/profile`, публикует описание на `/gm/{slug}`, а публичные клубы, игры и каталог ссылаются на профиль без раскрытия platform identifiers.
|
||||
- **📋 Шаблоны кампаний**: Вкладка `Шаблоны` отдельно от страницы группы: сохранение типовых параметров и запуск нового batch из шаблона.
|
||||
- **📦 Bulk-операции для Batch Sessions**:
|
||||
- обновить общий `title`/`link` у всей пачки;
|
||||
@@ -42,7 +47,7 @@
|
||||
- **⬆️ Управление очередью**: Заполненность, лист ожидания и ручное повышение игрока из очереди.
|
||||
- **📜 История изменений сессий**: Страница `/session/{id}/history` показывает аудит-лог всех значимых изменений (время, ссылка, название, участники, статус) с указанием акторов и дат.
|
||||
- **📊 Статистика посещаемости**: Страница `/group/{id}/stats` показывает долю присутствия, количество пропусков и среднюю явку по каждому игроку группы.
|
||||
- **🔄 Автосинхронизация**: Изменения в вебе мгновенно перерисовывают Telegram-сообщения расписания.
|
||||
- **🔄 Автосинхронизация**: Изменения в вебе мгновенно перерисовывают platform message расписания через `IPlatformMessenger`.
|
||||
|
||||
---
|
||||
|
||||
@@ -52,7 +57,7 @@
|
||||
|---|---|
|
||||
| Язык | C# 14 (.NET 10) |
|
||||
| Архитектура | Vertical Slice + общая библиотека `GmRelay.Shared` |
|
||||
| Боты | Telegram.Bot (**Native AOT**), NetCord Gateway (Discord worker) |
|
||||
| Боты | Telegram.Bot (**Native AOT**), NetCord Gateway (Discord worker внутри `GmRelay.Bot`) |
|
||||
| Веб | Blazor Server |
|
||||
| Оркестрация | .NET Aspire (`GmRelay.AppHost`) |
|
||||
| БД | PostgreSQL |
|
||||
@@ -82,6 +87,11 @@ TELEGRAM_BOT_TOKEN=ваш_токен_здесь
|
||||
# Токен Discord application bot
|
||||
DISCORD_BOT_TOKEN=ваш_discord_токен_здесь
|
||||
|
||||
# Discord OAuth (для Web Dashboard)
|
||||
DISCORD_CLIENT_ID=ваш_discord_client_id_здесь
|
||||
DISCORD_CLIENT_SECRET=ваш_discord_client_secret_здесь
|
||||
DISCORD_REDIRECT_URI=https://your-domain.example/auth/discord/callback
|
||||
|
||||
# Имя бота без @ (для Telegram Login Widget)
|
||||
TELEGRAM_BOT_USERNAME=ваше_имя_бота_здесь
|
||||
|
||||
@@ -106,15 +116,15 @@ docker compose up -d
|
||||
- создание Docker-сети и volume PostgreSQL;
|
||||
- подъём PostgreSQL (`db:5432`);
|
||||
- запуск бота с плавной миграцией (DbUp);
|
||||
- запуск отдельного Discord Gateway worker на NetCord;
|
||||
- запуск Discord Gateway worker на NetCord (healthcheck на `:8082`);
|
||||
- запуск веб-приложения с подключением к БД и Telegram API.
|
||||
|
||||
### 3. Первоначальная настройка
|
||||
1. Напишите боту `/start`.
|
||||
2. Создайте группу через `/newgroup`.
|
||||
3. Откройте Mini App или Web Dashboard для расширенного управления.
|
||||
4. Для Discord пригласите application bot на сервер с правами `bot` и `applications.commands`.
|
||||
5. В Discord создайте сессию через `/newsession` или опубликуйте расписание через `/listsessions`; игроки записываются и выходят кнопками в опубликованном сообщении.
|
||||
4. Для Discord пригласите application bot на сервер с правами `bot` и `applications.commands`. Скопируйте `DISCORD_BOT_TOKEN` в `.env`; `DISCORD_CLIENT_ID`, `DISCORD_CLIENT_SECRET` и `DISCORD_REDIRECT_URI` нужны только для входа в Web Dashboard через Discord.
|
||||
5. Перезапустите Docker Compose (`docker compose up -d`), а затем в Discord создайте сессию через `/newsession` или опубликуйте расписание через `/listsessions`; игроки записываются и выходят кнопками в опубликованном сообщении.
|
||||
|
||||
## 💾 Backup и восстановление
|
||||
|
||||
@@ -161,8 +171,7 @@ BACKUP_VOLUME_NAME=game_pgbackups
|
||||
```
|
||||
├── src/
|
||||
│ ├── GmRelay.AppHost/ # .NET Aspire orchestrator
|
||||
│ ├── GmRelay.Bot/ # Telegram-бот (Native AOT)
|
||||
│ ├── GmRelay.DiscordBot/ # Discord Gateway worker на NetCord
|
||||
│ ├── GmRelay.Bot/ # Telegram- и Discord-бот (Native AOT + NetCord Gateway worker)
|
||||
│ ├── GmRelay.ServiceDefaults/ # Aspire service defaults
|
||||
│ ├── GmRelay.Shared/ # Общие доменные модели
|
||||
│ └── GmRelay.Web/ # Blazor Server dashboard
|
||||
@@ -174,6 +183,16 @@ BACKUP_VOLUME_NAME=game_pgbackups
|
||||
|
||||
---
|
||||
|
||||
## 👨💻 Для разработчиков
|
||||
|
||||
- **Архитектура**: проект следует Vertical Slice с явным DI. Подробности — в [ADR-001](docs/adr/0001-use-vertical-slice-native-aot-and-aspire.md) и [ADR-002](docs/adr/002-platform-neutral-batch-rendering.md).
|
||||
- **Добавление обработчика**: из-за Native AOT все DI-регистрации выполняются вручную в `src/GmRelay.Bot/Program.cs` (assembly scanning не используется).
|
||||
- **Миграции**: SQL-скрипты добавляются как embedded resources в `src/GmRelay.Bot/Migrations/` и применяются автоматически при старте бота через DbUp.
|
||||
- **Тесты**: `dotnet test tests/GmRelay.Bot.Tests/GmRelay.Bot.Tests.csproj --verbosity normal`
|
||||
- **Сборка**: `dotnet build`
|
||||
|
||||
---
|
||||
|
||||
## 📜 Лицензия
|
||||
|
||||
MIT License. См. [LICENSE](./LICENSE).
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
## 🛠 Patch 2.4.0 — Discord /newsession и /listsessions
|
||||
|
||||
Реализованы slash-команды Discord для создания сессий и просмотра расписания без Web Dashboard.
|
||||
|
||||
## 🧩 Что вошло в релиз
|
||||
- src/GmRelay.DiscordBot/Features/Sessions/DiscordNewSessionCommand.cs — slash-команда /newsession с параметрами (title, time, seats, link)
|
||||
- src/GmRelay.DiscordBot/Features/Sessions/DiscordNewSessionHandler.cs — handler создания batch + session в БД
|
||||
- src/GmRelay.DiscordBot/Features/Sessions/DiscordListSessionsCommand.cs — slash-команда /listsessions
|
||||
- src/GmRelay.DiscordBot/Features/Sessions/DiscordListSessionsHandler.cs — handler запроса активных сессий с embed-рендерингом
|
||||
- src/GmRelay.DiscordBot/Infrastructure/Discord/DiscordPermissionChecker.cs — проверка прав через Discord permissions bitflag (Administrator = 0x8)
|
||||
- src/GmRelay.DiscordBot/Infrastructure/Discord/DiscordPlatformMessenger.cs — реализация IPlatformMessenger для Discord через NetCord REST
|
||||
- src/GmRelay.DiscordBot/Program.cs — регистрация DI: handlers, permission checker, messenger
|
||||
- ests/GmRelay.Bot.Tests/Discord/ — 20+ TDD-тестов на парсинг, права, структуру, DI, рендеринг
|
||||
- Синхронизированы версии: Directory.Build.props, NavMenu.razor, compose.yaml, deploy.yml → 2.4.0
|
||||
|
||||
## 🗺 Что это даёт
|
||||
- Мастера (GM) могут создавать сессии прямо из Discord, не заходя в Web.
|
||||
- Участники сервера видят расписание через /listsessions.
|
||||
- Единая PostgreSQL модель для Telegram и Discord — никакого дублирования данных.
|
||||
|
||||
## 📦 Версия и деплой
|
||||
- версия обновлена до 2.4.0
|
||||
- Docker-образы используют тег 2.4.0
|
||||
+11
-3
@@ -49,7 +49,7 @@ services:
|
||||
crond -f
|
||||
|
||||
bot:
|
||||
image: git.codeanddice.ru/toutsu/gmrelay-bot:2.5.0
|
||||
image: git.codeanddice.ru/toutsu/gmrelay-bot:3.5.1
|
||||
restart: always
|
||||
depends_on:
|
||||
db:
|
||||
@@ -67,7 +67,7 @@ services:
|
||||
retries: 3
|
||||
|
||||
discord:
|
||||
image: git.codeanddice.ru/toutsu/gmrelay-discord-bot:2.5.0
|
||||
image: git.codeanddice.ru/toutsu/gmrelay-discord-bot:3.5.1
|
||||
restart: always
|
||||
depends_on:
|
||||
db:
|
||||
@@ -77,9 +77,14 @@ services:
|
||||
- "Discord__Token=${DISCORD_BOT_TOKEN:?Set DISCORD_BOT_TOKEN in .env}"
|
||||
networks:
|
||||
- gmrelay
|
||||
healthcheck:
|
||||
test: ["CMD-SHELL", "wget -qO- http://localhost:8082/health || exit 1"]
|
||||
interval: 10s
|
||||
timeout: 5s
|
||||
retries: 3
|
||||
|
||||
web:
|
||||
image: git.codeanddice.ru/toutsu/gmrelay-web:2.5.0
|
||||
image: git.codeanddice.ru/toutsu/gmrelay-web:3.5.1
|
||||
restart: always
|
||||
depends_on:
|
||||
db:
|
||||
@@ -89,6 +94,9 @@ services:
|
||||
- "Telegram__BotToken=${TELEGRAM_BOT_TOKEN:?Set TELEGRAM_BOT_TOKEN in .env}"
|
||||
- "Telegram__BotUsername=${TELEGRAM_BOT_USERNAME:?Set TELEGRAM_BOT_USERNAME in .env}"
|
||||
- "Telegram__MiniAppUrl=${TELEGRAM_MINI_APP_URL:-}"
|
||||
- "Discord__ClientId=${DISCORD_CLIENT_ID:-}"
|
||||
- "Discord__ClientSecret=${DISCORD_CLIENT_SECRET:-}"
|
||||
- "Discord__RedirectUri=${DISCORD_REDIRECT_URI:-}"
|
||||
ports:
|
||||
- "${GMRELAY_WEB_PORT:-8080}:8080"
|
||||
volumes:
|
||||
|
||||
@@ -56,8 +56,18 @@ Aspire обеспечивает:
|
||||
- Service discovery и передачу connection strings.
|
||||
- OpenTelemetry (traces, metrics, logs) из коробки.
|
||||
- Aspire Dashboard для мониторинга.
|
||||
- **Три сервиса:** Bot (Telegram long polling + Discord Gateway), Web, PostgreSQL.
|
||||
|
||||
### 5. Telegram.Bot 22.x + Long Polling
|
||||
### 5. Discord Gateway + NetCord
|
||||
|
||||
Discord-интеграция реализована через NetCord Gateway (не DSharpPlus) из-за:
|
||||
- Нативной совместимости с .NET 10 и минимального размера зависимостей.
|
||||
- Gateway events маршрутизируются в те же vertical slice handlers, что и Telegram updates.
|
||||
- Slash-команды регистрируются через NetCord `ApplicationCommandService`.
|
||||
|
||||
Ephemeral-ответы (кнопки Join/Leave/RSVP) используют `InteractionMessageProperties` с `Flags = MessageFlags.Ephemeral`.
|
||||
|
||||
### 6. Telegram.Bot 22.x + Long Polling
|
||||
|
||||
- Long Polling — единственный вариант для Pi за NAT.
|
||||
- Telegram.Bot поддерживает `System.Text.Json` source generators для AOT.
|
||||
|
||||
@@ -30,7 +30,7 @@ SessionBatchViewModel (platform-neutral)
|
||||
│
|
||||
├──► TelegramSessionBatchRenderer ──► HTML + InlineKeyboardMarkup
|
||||
│
|
||||
└──► DiscordSessionBatchRenderer ──► (issue #26)
|
||||
└──► DiscordSessionBatchRenderer ──► Discord embeds + buttons
|
||||
```
|
||||
|
||||
### Изменённые компоненты
|
||||
@@ -41,7 +41,7 @@ SessionBatchViewModel (platform-neutral)
|
||||
| `SessionBatchViewBuilder` | — | `GmRelay.Shared.Rendering` |
|
||||
| `SessionBatchViewModel` | — | `GmRelay.Shared.Rendering` |
|
||||
| `TelegramSessionBatchRenderer` | — | `GmRelay.Bot` + `GmRelay.Web` |
|
||||
| `DiscordSessionBatchRenderer` | — | `GmRelay.Shared.Rendering` (stub) |
|
||||
| `DiscordSessionBatchRenderer` | — | `GmRelay.DiscordBot.Rendering` |
|
||||
| `BatchMessageEditor` | `GmRelay.Shared.Rendering` | `GmRelay.Bot` + `GmRelay.Web` |
|
||||
|
||||
## Consequences
|
||||
@@ -49,7 +49,7 @@ SessionBatchViewModel (platform-neutral)
|
||||
### Positive
|
||||
|
||||
- `GmRelay.Shared` больше не зависит от `Telegram.Bot`. Чистый platform-agnostic проект.
|
||||
- Можно добавить `DiscordSessionBatchRenderer` без изменений в `Shared`.
|
||||
- Discord renderer lives in `GmRelay.DiscordBot`, so NetCord stays out of `Shared`.
|
||||
- Unit-тесты ViewBuilder не создают `InlineKeyboardMarkup`.
|
||||
- Логика подсчёта игроков, сортировки сессий и генерации действий — в одном месте (ViewBuilder).
|
||||
|
||||
@@ -62,4 +62,8 @@ SessionBatchViewModel (platform-neutral)
|
||||
|
||||
- Issue #22 — этот рефакторинг.
|
||||
- Issue #26 — Discord Bot MVP (потребитель новой архитектуры).
|
||||
- Issue #30 — Discord reschedule voting использует `IPlatformMessenger`.
|
||||
- Issue #31 — scheduler notifications и reschedule deadline updates через `IPlatformMessenger`.
|
||||
- Issue #32 — compose wiring для Discord bot (healthcheck :8082).
|
||||
- Issue #33 — регрессионные тесты platform rendering (Telegram + Discord).
|
||||
- ADR 001 — vertical slice, native AOT, Aspire (`docs/adr/0001-use-vertical-slice-native-aot-and-aspire.md`).
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
# ADR 003: Discord Integration Architecture
|
||||
|
||||
## Status
|
||||
|
||||
**Accepted** — implemented in v2.6.0 (PR #87, issue #30).
|
||||
|
||||
## Context
|
||||
|
||||
После Telegram-бота требовалась поддержка Discord для кросс-платформенных групп. Нужно было выбрать:
|
||||
1. Библиотеку для Discord API (NetCord vs DSharpPlus vs Discord.NET).
|
||||
2. Модель runtime (отдельный процесс vs тот же Worker).
|
||||
3. Способ обработки интеракций (Gateway events vs HTTP interactions).
|
||||
|
||||
## Decision
|
||||
|
||||
### 1. NetCord (не DSharpPlus, не Discord.NET)
|
||||
|
||||
- **NetCord** — лёгкий, AOT-compatible, minimal dependencies.
|
||||
- **DSharpPlus** — слишком тяжёлый, много зависимостей, reflection-heavy.
|
||||
- **Discord.NET** — несовместим с Native AOT (heavy reflection, dynamic IL).
|
||||
|
||||
### 2. Gateway Events внутри GmRelay.Bot
|
||||
|
||||
- Discord Gateway worker живёт **внутри** `GmRelay.Bot` (тот же Worker Service), а не как отдельный проект.
|
||||
- Это упрощает DI, shared DB connection, shared `IPlatformMessenger`.
|
||||
- Для масштабирования можно вынести в отдельный контейнер позже.
|
||||
|
||||
### 3. Slash-команды через NetCord ApplicationCommandService
|
||||
|
||||
- Регистрация глобальных slash-команд (`/newsession`, `/listsessions`) через `ApplicationCommandService`.
|
||||
- Команды мапятся на vertical slice handlers через `DiscordSessionInteractionModule`.
|
||||
|
||||
### 4. Ephemeral Replies
|
||||
|
||||
- Все кнопки (Join/Leave/RSVP) отвечают ephemeral (`MessageFlags.Ephemeral`).
|
||||
- Schedule message редактируется через `DiscordPlatformMessenger` (реализация `IPlatformMessenger`).
|
||||
|
||||
## Consequences
|
||||
|
||||
### Positive
|
||||
|
||||
- Один бинарник для Telegram + Discord.
|
||||
- Shared DI, shared DB pool, shared domain logic.
|
||||
- Native AOT совместимость.
|
||||
|
||||
### Negative
|
||||
|
||||
- Gateway connection требует persistent WebSocket — при разрыве происходит reconnect.
|
||||
- Discord rate limits агрессивнее Telegram — нужен backoff.
|
||||
|
||||
## Related
|
||||
|
||||
- Issue #30 — reschedule voting (кнопки + дедлайн).
|
||||
- Issue #31 — scheduler notifications через `IPlatformMessenger`.
|
||||
- Issue #32 — compose wiring + healthcheck.
|
||||
- ADR 001 — Vertical Slice, Native AOT, Aspire.
|
||||
- ADR 002 — Platform-Neutral Rendering.
|
||||
+31
-14
@@ -8,21 +8,23 @@ C4Context
|
||||
|
||||
Person(gm, "Game Master", "Creates sessions and manages schedules")
|
||||
Person(player, "Player", "Joins, leaves, confirms, and receives reminders")
|
||||
Person(visitor, "Public visitor", "Views published club schedules, sessions, and GM profiles without private player data")
|
||||
|
||||
System(gmrelay, "GM-Relay", "Telegram bot, Discord worker, web dashboard, and shared scheduling logic")
|
||||
System(gmrelay, "GM-Relay", "Telegram bot, Discord worker, web dashboard, public club/session/GM profile pages, and shared scheduling logic")
|
||||
|
||||
System_Ext(telegram, "Telegram Bot API", "Commands, inline keyboards, callback queries, Mini App entry points")
|
||||
System_Ext(discord, "Discord Gateway and REST API", "Slash commands, button interactions, message edits, ephemeral replies")
|
||||
SystemDb_Ext(postgres, "PostgreSQL", "Sessions, players, participants, groups, platform identities")
|
||||
SystemDb_Ext(postgres, "PostgreSQL", "Sessions, players, participants, groups, platform identities, sanitized master_profiles")
|
||||
|
||||
Rel(gm, telegram, "Creates and manages sessions")
|
||||
Rel(gm, discord, "Uses /newsession and /listsessions")
|
||||
Rel(player, telegram, "Uses inline buttons")
|
||||
Rel(player, discord, "Uses Join/Leave buttons")
|
||||
Rel(player, discord, "Uses Join/Leave and RSVP buttons")
|
||||
Rel(visitor, gmrelay, "Views public club, session, and GM profile pages")
|
||||
Rel(telegram, gmrelay, "Updates via long polling")
|
||||
Rel(discord, gmrelay, "Gateway events and component interactions")
|
||||
Rel(gmrelay, telegram, "SendMessage, EditMessage, AnswerCallbackQuery")
|
||||
Rel(gmrelay, discord, "Send/edit schedule messages and ephemeral interaction replies")
|
||||
Rel(gmrelay, discord, "Send/edit schedule, RSVP, reminder, and reschedule messages")
|
||||
Rel(gmrelay, postgres, "SQL via Npgsql and Dapper")
|
||||
```
|
||||
|
||||
@@ -34,13 +36,14 @@ C4Container
|
||||
|
||||
Person(gm, "Game Master")
|
||||
Person(player, "Player")
|
||||
Person(visitor, "Public visitor")
|
||||
|
||||
System_Boundary(runtime, "Docker Compose / Aspire runtime") {
|
||||
Container(bot, "GmRelay.Bot", "Worker Service, .NET 10 AOT", "Telegram long polling, commands, callback routing, reminders")
|
||||
Container(discordBot, "GmRelay.DiscordBot", "Worker Service, .NET 10", "NetCord Gateway, slash commands, Join/Leave button interactions")
|
||||
Container(web, "GmRelay.Web", "Blazor Server", "Dashboard, Mini App pages, editing and stats")
|
||||
Container(shared, "GmRelay.Shared", ".NET library", "Shared domain models, rendering, and platform-neutral join/leave handlers")
|
||||
ContainerDb(db, "PostgreSQL", "Database", "sessions, players, session_participants, game_groups, platform identities")
|
||||
Container(discordBot, "Discord Gateway Worker", "Внутри GmRelay.Bot", "NetCord Gateway, slash commands, scheduler notifications, button interactions, healthcheck :8082")
|
||||
Container(web, "GmRelay.Web", "Blazor Server", "Dashboard, Mini App pages, public club/session/GM profile pages, editing and stats")
|
||||
Container(shared, "GmRelay.Shared", ".NET library", "Shared domain models, rendering, scheduler, and platform-neutral handlers")
|
||||
ContainerDb(db, "PostgreSQL", "Database", "sessions, players, session_participants, game_groups, publication settings, master_profiles, platform identities")
|
||||
}
|
||||
|
||||
System_Ext(telegram, "Telegram Bot API")
|
||||
@@ -50,12 +53,13 @@ C4Container
|
||||
Rel(gm, discord, "Slash commands")
|
||||
Rel(player, telegram, "Callback queries")
|
||||
Rel(player, discord, "Button interactions")
|
||||
Rel(visitor, web, "Read-only public schedule and sanitized GM profile pages")
|
||||
Rel(telegram, bot, "GetUpdates")
|
||||
Rel(discord, discordBot, "Gateway events")
|
||||
Rel(bot, telegram, "Bot API calls")
|
||||
Rel(discordBot, discord, "REST send/edit/reply calls")
|
||||
Rel(bot, shared, "Uses shared renderers and join/leave handlers")
|
||||
Rel(discordBot, shared, "Uses shared renderers and join/leave handlers")
|
||||
Rel(discordBot, shared, "Uses shared renderers, scheduler, and platform-neutral handlers")
|
||||
Rel(web, shared, "Uses shared domain and rendering models")
|
||||
Rel(bot, db, "Npgsql + Dapper.AOT")
|
||||
Rel(discordBot, db, "Npgsql + Dapper")
|
||||
@@ -71,18 +75,22 @@ C4Component
|
||||
Container_Boundary(shared, "GmRelay.Shared") {
|
||||
Component(join, "JoinSessionHandler", "Feature handler", "Adds players as Active or Waitlisted with session row locking")
|
||||
Component(leave, "LeaveSessionHandler", "Feature handler", "Removes players and promotes the first waitlisted player when capacity allows")
|
||||
Component(rsvp, "HandleRsvpHandler", "Feature handler", "Updates RSVP state and emits platform-neutral RSVP outcomes")
|
||||
Component(scheduler, "SessionSchedulerService", "Background service", "Triggers confirmation, reminder, and join-link notifications per platform")
|
||||
Component(updateLock, "ScheduleMessageUpdateLock", "In-memory keyed lock", "Serializes DB changes and schedule message edits per platform message")
|
||||
Component(renderer, "SessionBatchViewBuilder", "Renderer model builder", "Builds platform-neutral schedule views and actions")
|
||||
}
|
||||
|
||||
Component(healthCheck, "DiscordHealthCheckHostedService", ":8082", "Healthcheck для Docker Compose")
|
||||
|
||||
Container_Boundary(discordBot, "GmRelay.DiscordBot") {
|
||||
Component(discordModule, "DiscordSessionInteractionModule", "NetCord component module", "Maps join_session/leave_session buttons to neutral commands")
|
||||
Component(discordMessenger, "DiscordPlatformMessenger", "IPlatformMessenger", "Edits Discord schedule messages and stores interaction replies")
|
||||
Component(discordModule, "DiscordSessionInteractionModule", "NetCord component module", "Maps join_session/leave_session/rsvp buttons to neutral commands")
|
||||
Component(discordMessenger, "DiscordPlatformMessenger", "IPlatformMessenger", "Sends and edits Discord schedule, RSVP, reminder, join-link, and reschedule messages")
|
||||
}
|
||||
|
||||
Container_Boundary(bot, "GmRelay.Bot") {
|
||||
Component(updateRouter, "UpdateRouter", "Telegram adapter", "Maps callback queries to neutral commands")
|
||||
Component(telegramMessenger, "TelegramPlatformMessenger", "IPlatformMessenger", "Edits Telegram schedule messages and answers callback queries")
|
||||
Component(telegramMessenger, "TelegramPlatformMessenger", "IPlatformMessenger", "Sends and edits Telegram schedule, RSVP, reminder, join-link, and reschedule messages")
|
||||
}
|
||||
|
||||
ContainerDb(db, "PostgreSQL")
|
||||
@@ -92,19 +100,28 @@ C4Component
|
||||
Rel(discord, discordModule, "Button interaction")
|
||||
Rel(discordModule, join, "JoinSessionCommand")
|
||||
Rel(discordModule, leave, "LeaveSessionCommand")
|
||||
Rel(discordModule, rsvp, "HandleRsvpCommand")
|
||||
Rel(discordModule, discord, "Deferred ephemeral reply, then modify response")
|
||||
Rel(updateRouter, join, "JoinSessionCommand")
|
||||
Rel(updateRouter, leave, "LeaveSessionCommand")
|
||||
Rel(updateRouter, rsvp, "HandleRsvpCommand")
|
||||
Rel(join, updateLock, "Acquire by PlatformMessageRef")
|
||||
Rel(leave, updateLock, "Acquire by PlatformMessageRef")
|
||||
Rel(join, db, "SELECT FOR UPDATE, INSERT participant")
|
||||
Rel(leave, db, "SELECT FOR UPDATE, DELETE/promote participant")
|
||||
Rel(rsvp, db, "Update RSVP and load notification recipients")
|
||||
Rel(scheduler, db, "Load due session triggers")
|
||||
Rel(join, renderer, "Build updated schedule view")
|
||||
Rel(leave, renderer, "Build updated schedule view")
|
||||
Rel(join, discordMessenger, "Update Discord schedule when command is Discord")
|
||||
Rel(leave, discordMessenger, "Update Discord schedule when command is Discord")
|
||||
Rel(join, telegramMessenger, "Update Telegram schedule when command is Telegram")
|
||||
Rel(leave, telegramMessenger, "Update Telegram schedule when command is Telegram")
|
||||
Rel(discordMessenger, discord, "ModifyMessage + ephemeral text")
|
||||
Rel(telegramMessenger, telegram, "EditMessage + AnswerCallbackQuery")
|
||||
Rel(rsvp, discordMessenger, "Update Discord confirmation and outcomes")
|
||||
Rel(rsvp, telegramMessenger, "Update Telegram confirmation and outcomes")
|
||||
Rel(scheduler, discordMessenger, "Send Discord scheduler notifications")
|
||||
Rel(scheduler, telegramMessenger, "Send Telegram scheduler notifications")
|
||||
Rel(discordMessenger, discord, "REST send/edit/DM + ephemeral text")
|
||||
Rel(telegramMessenger, telegram, "SendMessage/EditMessage + AnswerCallbackQuery")
|
||||
Rel(healthCheck, discord, "HTTP /health")
|
||||
```
|
||||
|
||||
@@ -1,438 +0,0 @@
|
||||
# Player List + Kick + Waitlist Promotion Implementation Plan
|
||||
|
||||
> **For Hermes:** Use subagent-driven-development skill to implement this plan task-by-task.
|
||||
|
||||
**Goal:** Add a player list (with names) to the Web UI session views, allow GM to kick a specific player, and auto-promote the next waitlisted player.
|
||||
|
||||
**Architecture:** Extend `ISessionStore` with participant queries and a remove method. Update `GroupDetails.razor` to show expandable participant lists. Reuse existing `PromoteWaitlistedPlayerAsync` logic after removal.
|
||||
|
||||
**Tech Stack:** C# 14, Blazor SSR, Dapper, PostgreSQL
|
||||
|
||||
---
|
||||
|
||||
## Task 1: Add domain model for WebParticipant
|
||||
|
||||
**Objective:** Create a DTO to represent a session participant in the web layer.
|
||||
|
||||
**Files:**
|
||||
- Modify: `src/GmRelay.Web/Services/SessionService.cs`
|
||||
|
||||
**Step 1: Add record**
|
||||
|
||||
```csharp
|
||||
public sealed record WebParticipant(
|
||||
Guid Id,
|
||||
long TelegramId,
|
||||
string DisplayName,
|
||||
string? TelegramUsername,
|
||||
string RsvpStatus,
|
||||
string RegistrationStatus,
|
||||
bool IsGm,
|
||||
DateTime? RespondedAt);
|
||||
```
|
||||
|
||||
**Step 2: Commit**
|
||||
|
||||
```bash
|
||||
git add src/GmRelay.Web/Services/SessionService.cs
|
||||
git commit -m "feat: add WebParticipant record"
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Task 2: Add GetSessionParticipantsAsync to ISessionStore
|
||||
|
||||
**Objective:** Retrieve all participants for a session with full player info.
|
||||
|
||||
**Files:**
|
||||
- Modify: `src/GmRelay.Web/Services/ISessionStore.cs`
|
||||
- Modify: `src/GmRelay.Web/Services/SessionService.cs`
|
||||
- Modify: `src/GmRelay.Web/Services/AuthorizedSessionService.cs`
|
||||
|
||||
**Step 1: Add to interface**
|
||||
|
||||
In `ISessionStore.cs`, add:
|
||||
```csharp
|
||||
Task<List<WebParticipant>> GetSessionParticipantsAsync(Guid sessionId);
|
||||
```
|
||||
|
||||
**Step 2: Implement in SessionService**
|
||||
|
||||
In `SessionService.cs`, add:
|
||||
```csharp
|
||||
public async Task<List<WebParticipant>> GetSessionParticipantsAsync(Guid sessionId)
|
||||
{
|
||||
await using var conn = await dataSource.OpenConnectionAsync();
|
||||
return (await conn.QueryAsync<WebParticipant>(
|
||||
"""
|
||||
SELECT sp.id AS Id,
|
||||
p.telegram_id AS TelegramId,
|
||||
p.display_name AS DisplayName,
|
||||
p.telegram_username AS TelegramUsername,
|
||||
sp.rsvp_status AS RsvpStatus,
|
||||
sp.registration_status AS RegistrationStatus,
|
||||
sp.is_gm AS IsGm,
|
||||
sp.responded_at AS RespondedAt
|
||||
FROM session_participants sp
|
||||
JOIN players p ON p.id = sp.player_id
|
||||
WHERE sp.session_id = @SessionId
|
||||
ORDER BY sp.is_gm DESC,
|
||||
CASE sp.registration_status WHEN 'Active' THEN 0 ELSE 1 END,
|
||||
sp.created_at
|
||||
""",
|
||||
new { SessionId = sessionId })).ToList();
|
||||
}
|
||||
```
|
||||
|
||||
**Step 3: Add authorized wrapper**
|
||||
|
||||
In `AuthorizedSessionService.cs`, add:
|
||||
```csharp
|
||||
public async Task<List<WebParticipant>?> GetSessionParticipantsForGmAsync(Guid sessionId, long gmId)
|
||||
{
|
||||
var session = await GetSessionForGmAsync(sessionId, gmId);
|
||||
if (session is null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return await sessionStore.GetSessionParticipantsAsync(sessionId);
|
||||
}
|
||||
```
|
||||
|
||||
**Step 4: Commit**
|
||||
|
||||
```bash
|
||||
git add src/GmRelay.Web/Services/ISessionStore.cs
|
||||
|
||||
git add src/GmRelay.Web/Services/SessionService.cs
|
||||
|
||||
git add src/GmRelay.Web/Services/AuthorizedSessionService.cs
|
||||
|
||||
git commit -m "feat: add GetSessionParticipantsAsync"
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Task 3: Add RemovePlayerFromSessionAsync with waitlist promotion
|
||||
|
||||
**Objective:** Allow GM to remove a specific player; auto-promote next waitlisted player if conditions met.
|
||||
|
||||
**Files:**
|
||||
- Modify: `src/GmRelay.Web/Services/ISessionStore.cs`
|
||||
- Modify: `src/GmRelay.Web/Services/SessionService.cs`
|
||||
- Modify: `src/GmRelay.Web/Services/AuthorizedSessionService.cs`
|
||||
|
||||
**Step 1: Add to interface**
|
||||
|
||||
In `ISessionStore.cs`, add:
|
||||
```csharp
|
||||
Task RemovePlayerFromSessionAsync(Guid sessionId, Guid groupId, Guid participantId);
|
||||
```
|
||||
|
||||
**Step 2: Implement in SessionService**
|
||||
|
||||
In `SessionService.cs`, add:
|
||||
```csharp
|
||||
public async Task RemovePlayerFromSessionAsync(Guid sessionId, Guid groupId, Guid participantId)
|
||||
{
|
||||
await using var conn = await dataSource.OpenConnectionAsync();
|
||||
await using var transaction = await conn.BeginTransactionAsync();
|
||||
|
||||
var session = await conn.QuerySingleOrDefaultAsync<WebSession>(
|
||||
@"SELECT s.id, s.group_id AS GroupId, s.title, s.scheduled_at AS ScheduledAt, s.status, s.join_link AS JoinLink,
|
||||
s.batch_id AS BatchId, s.batch_message_id AS BatchMessageId,
|
||||
g.telegram_chat_id AS TelegramChatId,
|
||||
s.max_players AS MaxPlayers,
|
||||
0 AS ActivePlayerCount,
|
||||
0 AS WaitlistedPlayerCount,
|
||||
s.notification_mode AS NotificationMode
|
||||
FROM sessions s
|
||||
JOIN game_groups g ON g.id = s.group_id
|
||||
WHERE s.id = @SessionId AND s.group_id = @GroupId
|
||||
FOR UPDATE",
|
||||
new { SessionId = sessionId, GroupId = groupId },
|
||||
transaction);
|
||||
|
||||
if (session is null)
|
||||
{
|
||||
throw new SessionAccessDeniedException(sessionId, 0);
|
||||
}
|
||||
|
||||
// Verify participant exists in this session
|
||||
var participant = await conn.QuerySingleOrDefaultAsync<WebParticipant>(
|
||||
"""
|
||||
SELECT sp.id AS Id,
|
||||
p.telegram_id AS TelegramId,
|
||||
p.display_name AS DisplayName,
|
||||
p.telegram_username AS TelegramUsername,
|
||||
sp.rsvp_status AS RsvpStatus,
|
||||
sp.registration_status AS RegistrationStatus,
|
||||
sp.is_gm AS IsGm,
|
||||
sp.responded_at AS RespondedAt
|
||||
FROM session_participants sp
|
||||
JOIN players p ON p.id = sp.player_id
|
||||
WHERE sp.id = @ParticipantId AND sp.session_id = @SessionId
|
||||
""",
|
||||
new { ParticipantId = participantId, SessionId = sessionId },
|
||||
transaction);
|
||||
|
||||
if (participant is null)
|
||||
{
|
||||
throw new InvalidOperationException("Участник не найден в этой сессии.");
|
||||
}
|
||||
|
||||
bool wasActive = participant.RegistrationStatus == ParticipantRegistrationStatus.Active;
|
||||
|
||||
await conn.ExecuteAsync(
|
||||
"DELETE FROM session_participants WHERE id = @ParticipantId",
|
||||
new { ParticipantId = participantId },
|
||||
transaction);
|
||||
|
||||
WebPromotedParticipantDto? promoted = null;
|
||||
|
||||
if (wasActive)
|
||||
{
|
||||
promoted = await conn.QuerySingleOrDefaultAsync<WebPromotedParticipantDto>(
|
||||
"""
|
||||
SELECT sp.id AS ParticipantRowId,
|
||||
p.display_name AS DisplayName
|
||||
FROM session_participants sp
|
||||
JOIN players p ON p.id = sp.player_id
|
||||
WHERE sp.session_id = @SessionId
|
||||
AND sp.is_gm = false
|
||||
AND sp.registration_status = @Waitlisted
|
||||
ORDER BY sp.created_at ASC, sp.id ASC
|
||||
LIMIT 1
|
||||
FOR UPDATE OF sp
|
||||
""",
|
||||
new { SessionId = sessionId, Waitlisted = ParticipantRegistrationStatus.Waitlisted },
|
||||
transaction);
|
||||
|
||||
if (promoted is not null)
|
||||
{
|
||||
await conn.ExecuteAsync(
|
||||
"""
|
||||
UPDATE session_participants
|
||||
SET registration_status = @Active,
|
||||
rsvp_status = @Pending,
|
||||
responded_at = NULL
|
||||
WHERE id = @ParticipantRowId
|
||||
""",
|
||||
new
|
||||
{
|
||||
promoted.ParticipantRowId,
|
||||
Active = ParticipantRegistrationStatus.Active,
|
||||
Pending = RsvpStatus.Pending
|
||||
},
|
||||
transaction);
|
||||
}
|
||||
}
|
||||
|
||||
await transaction.CommitAsync();
|
||||
|
||||
// Notifications
|
||||
await bot.SendMessage(
|
||||
session.TelegramChatId,
|
||||
$"🚪 <b>{System.Net.WebUtility.HtmlEncode(participant.DisplayName)}</b> удален(а) из сессии «{System.Net.WebUtility.HtmlEncode(session.Title)}».",
|
||||
parseMode: Telegram.Bot.Types.Enums.ParseMode.Html);
|
||||
|
||||
if (promoted is not null)
|
||||
{
|
||||
await bot.SendMessage(
|
||||
session.TelegramChatId,
|
||||
$"⬆️ <b>{System.Net.WebUtility.HtmlEncode(promoted.DisplayName)}</b> переведен(а) из листа ожидания в основной состав «{System.Net.WebUtility.HtmlEncode(session.Title)}».",
|
||||
parseMode: Telegram.Bot.Types.Enums.ParseMode.Html);
|
||||
}
|
||||
|
||||
if (session.BatchMessageId.HasValue)
|
||||
{
|
||||
await TryUpdateBatchMessageAsync(session.BatchId, session.TelegramChatId, session.BatchMessageId.Value, session.Title);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
**Step 3: Add authorized wrapper**
|
||||
|
||||
In `AuthorizedSessionService.cs`, add:
|
||||
```csharp
|
||||
public async Task RemovePlayerFromSessionForGmAsync(Guid sessionId, long gmId, Guid participantId)
|
||||
{
|
||||
var session = await GetSessionForGmAsync(sessionId, gmId);
|
||||
if (session is null)
|
||||
{
|
||||
throw new SessionAccessDeniedException(sessionId, gmId);
|
||||
}
|
||||
|
||||
await sessionStore.RemovePlayerFromSessionAsync(sessionId, session.GroupId, participantId);
|
||||
}
|
||||
```
|
||||
|
||||
**Step 4: Commit**
|
||||
|
||||
```bash
|
||||
git add src/GmRelay.Web/Services/ISessionStore.cs
|
||||
|
||||
git add src/GmRelay.Web/Services/SessionService.cs
|
||||
|
||||
git add src/GmRelay.Web/Services/AuthorizedSessionService.cs
|
||||
|
||||
git commit -m "feat: add RemovePlayerFromSessionAsync with waitlist promotion"
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Task 4: Modify GroupDetails.razor to show participant list
|
||||
|
||||
**Objective:** Add expandable player lists to each session row with kick buttons.
|
||||
|
||||
**Files:**
|
||||
- Modify: `src/GmRelay.Web/Components/Pages/GroupDetails.razor`
|
||||
|
||||
**Step 1:** Add `participants` dictionary and `kickingParticipantId` state variables.
|
||||
|
||||
**Step 2:** Add `LoadParticipants(Guid sessionId)` and `KickParticipant(Guid sessionId, Guid participantId)` methods.
|
||||
|
||||
**Step 3:** In desktop table, add a new column or expand row with participant list.
|
||||
|
||||
**Step 4:** In mobile cards, add expandable participant section.
|
||||
|
||||
**Step 5:** Add styles to `app.css` if needed (badge styles are already present).
|
||||
|
||||
**Step 6:** Commit
|
||||
|
||||
```bash
|
||||
git add src/GmRelay.Web/Components/Pages/GroupDetails.razor
|
||||
|
||||
git add src/GmRelay.Web/wwwroot/app.css
|
||||
|
||||
git commit -m "feat: show player list and kick button in GroupDetails"
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Task 5: Modify EditSession.razor to show participant list
|
||||
|
||||
**Objective:** Show participant list on the edit page with kick capability.
|
||||
|
||||
**Files:**
|
||||
- Modify: `src/GmRelay.Web/Components/Pages/EditSession.razor`
|
||||
|
||||
**Step 1:** Load participants in `OnInitializedAsync`.
|
||||
|
||||
**Step 2:** Render participant list below the edit form.
|
||||
|
||||
**Step 3:** Add kick button for each non-GM participant.
|
||||
|
||||
**Step 4:** Commit
|
||||
|
||||
```bash
|
||||
git add src/GmRelay.Web/Components/Pages/EditSession.razor
|
||||
|
||||
git commit -m "feat: show player list and kick button in EditSession"
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Task 6: Add backend tests
|
||||
|
||||
**Objective:** Cover new GetSessionParticipants and RemovePlayerFromSession logic.
|
||||
|
||||
**Files:**
|
||||
- Create: `tests/GmRelay.Bot.Tests/Web/SessionParticipantTests.cs`
|
||||
|
||||
**Step 1:** Write tests for `GetSessionParticipantsForGmAsync`.
|
||||
|
||||
**Step 2:** Write tests for `RemovePlayerFromSessionForGmAsync` including waitlist promotion.
|
||||
|
||||
**Step 3:** Run tests
|
||||
|
||||
```bash
|
||||
dotnet test tests/GmRelay.Bot.Tests/GmRelay.Bot.Tests.csproj -v n
|
||||
```
|
||||
|
||||
**Step 4:** Commit
|
||||
|
||||
```bash
|
||||
git add tests/GmRelay.Bot.Tests/Web/SessionParticipantTests.cs
|
||||
|
||||
git commit -m "test: add SessionParticipant service tests"
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Task 7: Update README
|
||||
|
||||
**Objective:** Bump version and document new features.
|
||||
|
||||
**Files:**
|
||||
- Modify: `README.md`
|
||||
|
||||
**Step 1:** Change version from `v1.9.6` to `v1.9.7`.
|
||||
|
||||
**Step 2:** Add bullet under Web Dashboard: player list with kick and auto-promote.
|
||||
|
||||
**Step 3:** Commit
|
||||
|
||||
```bash
|
||||
git add README.md
|
||||
|
||||
git commit -m "docs: bump README to v1.9.7, document player list kick"
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Task 8: Update Wiki
|
||||
|
||||
**Objective:** Update `Руководство ГМа` page with player management instructions.
|
||||
|
||||
**Files:**
|
||||
- Modify: Wiki page `Руководство ГМа`
|
||||
|
||||
**Step 1:** Read current wiki content via MCP.
|
||||
|
||||
**Step 2:** Add section about viewing player list and removing players.
|
||||
|
||||
**Step 3:** Update via MCP.
|
||||
|
||||
---
|
||||
|
||||
## Task 9: Push branch and run CI
|
||||
|
||||
**Objective:** Push branch, monitor workflow, fix issues.
|
||||
|
||||
**Step 1:** Push
|
||||
|
||||
```bash
|
||||
git push -u origin feat/player-list-kick-waitlist
|
||||
```
|
||||
|
||||
**Step 2:** Check workflow run via MCP gitea actions.
|
||||
|
||||
**Step 3:** Fix any issues.
|
||||
|
||||
---
|
||||
|
||||
## Task 10: Merge and create release
|
||||
|
||||
**Objective:** Merge PR (or fast-forward), tag, create release.
|
||||
|
||||
**Step 1:** Merge to main
|
||||
|
||||
```bash
|
||||
git checkout main
|
||||
|
||||
git merge --no-ff feat/player-list-kick-waitlist -m "feat: player list, kick, and waitlist promotion (#X)"
|
||||
```
|
||||
|
||||
**Step 2:** Tag v1.9.7
|
||||
|
||||
```bash
|
||||
git tag v1.9.7
|
||||
|
||||
git push origin main --tags
|
||||
```
|
||||
|
||||
**Step 3:** Create release via MCP gitea_create_release.
|
||||
|
||||
---
|
||||
@@ -1,69 +0,0 @@
|
||||
# Telegram Mini App Dashboard Implementation Plan
|
||||
|
||||
> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking.
|
||||
|
||||
**Goal:** Add a Telegram Mini App mobile dashboard that reuses the existing Web Dashboard and validates Telegram WebApp `initData` on the server.
|
||||
|
||||
**Architecture:** Extend `TelegramAuthService` for WebApp init data, add a `/miniapp` Blazor entry page plus `/auth/telegram-webapp` endpoint, and add bot entry points through an inline WebApp button and optional menu button setup. Existing application/domain services remain the only write path.
|
||||
|
||||
**Tech Stack:** .NET 10, Blazor Server, Telegram.Bot, xUnit, Dapper/Npgsql-backed existing services.
|
||||
|
||||
---
|
||||
|
||||
### Task 1: Telegram WebApp Authentication
|
||||
|
||||
**Files:**
|
||||
- Modify: `src/GmRelay.Web/Services/TelegramAuthService.cs`
|
||||
- Modify: `src/GmRelay.Web/Program.cs`
|
||||
- Test: `tests/GmRelay.Bot.Tests/Web/TelegramAuthServiceTests.cs`
|
||||
|
||||
- [ ] Write failing tests for valid WebApp `initData`, tampered hash, and expired auth date.
|
||||
- [ ] Run `dotnet test tests/GmRelay.Bot.Tests/GmRelay.Bot.Tests.csproj --filter TelegramAuthServiceTests`.
|
||||
- [ ] Implement WebApp HMAC verification using the Telegram `WebAppData` secret derivation.
|
||||
- [ ] Add `/auth/telegram-webapp` endpoint that signs in using the same claims as `/auth/telegram`.
|
||||
- [ ] Re-run the filtered tests.
|
||||
|
||||
### Task 2: Mini App Entry Page
|
||||
|
||||
**Files:**
|
||||
- Create: `src/GmRelay.Web/Components/Pages/MiniApp.razor`
|
||||
- Modify: `src/GmRelay.Web/Components/App.razor`
|
||||
- Modify: `src/GmRelay.Web/wwwroot/app.css`
|
||||
- Test: `tests/GmRelay.Bot.Tests/Web/MiniAppDashboardTests.cs`
|
||||
|
||||
- [ ] Write failing tests that assert `/miniapp`, `telegram-web-app.js`, `authenticateTelegramMiniApp`, and Mini App CSS hooks exist.
|
||||
- [ ] Run `dotnet test tests/GmRelay.Bot.Tests/GmRelay.Bot.Tests.csproj --filter MiniAppDashboardTests`.
|
||||
- [ ] Implement `/miniapp` to post `Telegram.WebApp.initData` to `/auth/telegram-webapp`, expand/ready the Mini App, and show fallback login when opened outside Telegram.
|
||||
- [ ] Add CSS for a mobile-first Mini App shell and compact dashboard spacing.
|
||||
- [ ] Re-run the filtered tests.
|
||||
|
||||
### Task 3: Bot Entry Points
|
||||
|
||||
**Files:**
|
||||
- Create: `src/GmRelay.Bot/Infrastructure/Telegram/TelegramMiniAppMenuButtonService.cs`
|
||||
- Modify: `src/GmRelay.Bot/Infrastructure/Telegram/UpdateRouter.cs`
|
||||
- Modify: `src/GmRelay.Bot/Program.cs`
|
||||
- Test: `tests/GmRelay.Bot.Tests/Infrastructure/Telegram/TelegramMiniAppEntryPointTests.cs`
|
||||
|
||||
- [ ] Write failing tests that assert `/start` exposes a WebApp button and startup registers the menu button service.
|
||||
- [ ] Run `dotnet test tests/GmRelay.Bot.Tests/GmRelay.Bot.Tests.csproj --filter TelegramMiniAppEntryPointTests`.
|
||||
- [ ] Add a configurable `Telegram:MiniAppUrl` entry point; when missing, keep existing command behavior.
|
||||
- [ ] Add hosted service that calls `SetChatMenuButton` with `MenuButtonWebApp` only when the URL is configured.
|
||||
- [ ] Re-run the filtered tests.
|
||||
|
||||
### Task 4: Docs, Versions, and Release Prep
|
||||
|
||||
**Files:**
|
||||
- Modify: `Directory.Build.props`
|
||||
- Modify: `compose.yaml`
|
||||
- Modify: `.gitea/workflows/deploy.yml`
|
||||
- Modify: `src/GmRelay.Web/wwwroot/app.css`
|
||||
- Modify: `src/GmRelay.Web/Components/Layout/NavMenu.razor`
|
||||
- Modify: `README.md`
|
||||
- Wiki: `Home`, `Быстрый старт`, `Руководство ГМа`, `Развёртывание`, `Архитектура`, `Разработка`
|
||||
|
||||
- [ ] Update project/container/workflow/UI versions to `1.9.0`.
|
||||
- [ ] Document `TELEGRAM_MINI_APP_URL`, BotFather `/setmenubutton`, `/miniapp`, and WebApp auth.
|
||||
- [ ] Run `dotnet test tests/GmRelay.Bot.Tests/GmRelay.Bot.Tests.csproj --collect:"XPlat Code Coverage"`.
|
||||
- [ ] Run `dotnet build GM-Relay.slnx -c Release`.
|
||||
- [ ] Commit, push, close issue #17, update wiki, create tag/release `v1.9.0`.
|
||||
@@ -1,560 +0,0 @@
|
||||
# Platform Messenger Contracts Implementation Plan
|
||||
|
||||
> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking.
|
||||
|
||||
**Goal:** Implement issue #24 by adding platform-neutral platform identity and messaging contracts, then routing the Telegram session flows through a Telegram adapter without changing Telegram behavior.
|
||||
|
||||
**Architecture:** Keep update routing and Telegram update parsing at the `GmRelay.Bot.Infrastructure.Telegram` boundary, but move outbound messaging decisions behind `GmRelay.Shared.Platform.IPlatformMessenger`. `GmRelay.Shared` owns platform-neutral DTOs and contracts; `GmRelay.Bot` owns `TelegramPlatformMessenger`, which translates neutral requests into `Telegram.Bot` calls and reuses the existing Telegram renderers/editing rules.
|
||||
|
||||
**Tech Stack:** .NET 10, C# preview, xUnit, Dapper.AOT constraints, Telegram.Bot in `GmRelay.Bot` only, platform-neutral shared contracts in `GmRelay.Shared`.
|
||||
|
||||
---
|
||||
|
||||
## Issue Context
|
||||
|
||||
- Gitea issue: #24, `refactor: ввести PlatformKind, PlatformUser, PlatformGroup и IPlatformMessenger`
|
||||
- Labels: `area:bot`, `area:platform`, `area:shared`, `platform:multi`, `type:refactor`, `pending-approval`
|
||||
- Acceptance criteria:
|
||||
- New contracts live in a platform-neutral layer.
|
||||
- Telegram flow goes through the adapter without behavior changes.
|
||||
- A future DiscordBot can reference the contract without depending on Telegram assemblies.
|
||||
|
||||
## Proposed Version Bump
|
||||
|
||||
Current version is `2.0.0` in:
|
||||
|
||||
- `Directory.Build.props`
|
||||
- `compose.yaml`
|
||||
- `.gitea/workflows/deploy.yml`
|
||||
- `src/GmRelay.Web/Components/Layout/NavMenu.razor`
|
||||
|
||||
Issue label is `type:refactor`; per workflow rules this is not a major bump and has no user-facing feature label. Proposed bump: `2.0.0` -> `2.0.1`.
|
||||
|
||||
## Files
|
||||
|
||||
- Create: `src/GmRelay.Shared/Platform/PlatformKind.cs`
|
||||
- Create: `src/GmRelay.Shared/Platform/PlatformUser.cs`
|
||||
- Create: `src/GmRelay.Shared/Platform/PlatformGroup.cs`
|
||||
- Create: `src/GmRelay.Shared/Platform/PlatformMessageContracts.cs`
|
||||
- Create: `src/GmRelay.Shared/Platform/IPlatformMessenger.cs`
|
||||
- Create: `src/GmRelay.Bot/Infrastructure/Telegram/TelegramPlatformMessenger.cs`
|
||||
- Create: `tests/GmRelay.Bot.Tests/Platform/PlatformContractsTests.cs`
|
||||
- Create: `tests/GmRelay.Bot.Tests/Infrastructure/Telegram/TelegramPlatformMessengerSourceTests.cs`
|
||||
- Modify: `src/GmRelay.Bot/Program.cs`
|
||||
- Modify: `src/GmRelay.Bot/Features/Notifications/DirectSessionNotificationSender.cs`
|
||||
- Modify: `src/GmRelay.Bot/Features/Sessions/CreateSession/JoinSessionHandler.cs`
|
||||
- Modify: `src/GmRelay.Bot/Features/Sessions/CreateSession/LeaveSessionHandler.cs`
|
||||
- Modify: `src/GmRelay.Bot/Features/Sessions/CreateSession/CancelSessionHandler.cs`
|
||||
- Modify: `src/GmRelay.Bot/Features/Sessions/CreateSession/PromoteWaitlistedPlayerHandler.cs`
|
||||
- Modify: `src/GmRelay.Bot/Features/Sessions/RescheduleSession/InitiateRescheduleHandler.cs`
|
||||
- Modify: `src/GmRelay.Bot/Features/Sessions/RescheduleSession/HandleRescheduleTimeInputHandler.cs`
|
||||
- Modify: `src/GmRelay.Bot/Features/Sessions/RescheduleSession/HandleRescheduleVoteHandler.cs`
|
||||
- Modify: `src/GmRelay.Bot/Features/Sessions/RescheduleSession/RescheduleVotingDeadlineService.cs`
|
||||
- Modify: `src/GmRelay.Bot/Features/Sessions/ExportCalendar/ExportCalendarHandler.cs`
|
||||
- Modify: version files listed above
|
||||
|
||||
## Design
|
||||
|
||||
### Shared Contracts
|
||||
|
||||
`PlatformKind` is a sentinel enum where `Max` is not a sendable platform:
|
||||
|
||||
```csharp
|
||||
namespace GmRelay.Shared.Platform;
|
||||
|
||||
public enum PlatformKind
|
||||
{
|
||||
Telegram = 0,
|
||||
Discord = 1,
|
||||
Max = 2
|
||||
}
|
||||
```
|
||||
|
||||
`PlatformUser` and `PlatformGroup` carry external platform identity while keeping current Telegram IDs representable as strings:
|
||||
|
||||
```csharp
|
||||
namespace GmRelay.Shared.Platform;
|
||||
|
||||
public sealed record PlatformUser(
|
||||
PlatformKind Platform,
|
||||
string ExternalUserId,
|
||||
string DisplayName,
|
||||
string? ExternalUsername);
|
||||
|
||||
public sealed record PlatformGroup(
|
||||
PlatformKind Platform,
|
||||
string ExternalGroupId,
|
||||
string DisplayName,
|
||||
string? ExternalChannelId = null,
|
||||
string? ExternalThreadId = null);
|
||||
```
|
||||
|
||||
Outbound message contracts stay independent of Telegram/Discord SDK types:
|
||||
|
||||
```csharp
|
||||
using GmRelay.Shared.Rendering;
|
||||
|
||||
namespace GmRelay.Shared.Platform;
|
||||
|
||||
public sealed record PlatformMessageRef(
|
||||
PlatformKind Platform,
|
||||
string ExternalGroupId,
|
||||
string? ExternalThreadId,
|
||||
string ExternalMessageId);
|
||||
|
||||
public sealed record PlatformMessageAction(
|
||||
string Key,
|
||||
string Label,
|
||||
string Payload);
|
||||
|
||||
public sealed record PlatformScheduleMessage(
|
||||
PlatformGroup Group,
|
||||
SessionBatchViewModel View,
|
||||
PlatformMessageRef? ExistingMessage,
|
||||
string? ImageReference = null);
|
||||
|
||||
public sealed record PlatformPrivateMessage(
|
||||
PlatformUser Recipient,
|
||||
string HtmlText);
|
||||
|
||||
public sealed record PlatformInteractionReply(
|
||||
string InteractionId,
|
||||
string Text,
|
||||
bool ShowAlert = false);
|
||||
|
||||
public sealed record PlatformCalendarFile(
|
||||
PlatformGroup Group,
|
||||
string FileName,
|
||||
byte[] Content,
|
||||
string CaptionHtml,
|
||||
IReadOnlyList<PlatformMessageAction> Actions);
|
||||
```
|
||||
|
||||
`IPlatformMessenger` exposes the required outward operations:
|
||||
|
||||
```csharp
|
||||
namespace GmRelay.Shared.Platform;
|
||||
|
||||
public interface IPlatformMessenger
|
||||
{
|
||||
Task<PlatformMessageRef> SendScheduleAsync(PlatformScheduleMessage message, CancellationToken ct);
|
||||
Task UpdateScheduleAsync(PlatformScheduleMessage message, CancellationToken ct);
|
||||
Task SendGroupMessageAsync(PlatformGroup group, string htmlText, CancellationToken ct);
|
||||
Task SendPrivateMessageAsync(PlatformPrivateMessage message, CancellationToken ct);
|
||||
Task AnswerInteractionAsync(PlatformInteractionReply reply, CancellationToken ct);
|
||||
Task SendCalendarFileAsync(PlatformCalendarFile file, CancellationToken ct);
|
||||
}
|
||||
```
|
||||
|
||||
### Telegram Adapter
|
||||
|
||||
`TelegramPlatformMessenger` lives in `GmRelay.Bot.Infrastructure.Telegram`, depends on `ITelegramBotClient`, and translates neutral DTOs to existing Telegram calls:
|
||||
|
||||
- `SendScheduleAsync` renders `SessionBatchViewModel` with `TelegramSessionBatchRenderer.Render`.
|
||||
- `UpdateScheduleAsync` calls `BatchMessageEditor.EditBatchMessageAsync`.
|
||||
- `SendGroupMessageAsync` calls `SendMessage` with `ParseMode.Html` and optional `messageThreadId`.
|
||||
- `SendPrivateMessageAsync` calls `SendMessage` to `PlatformUser.ExternalUserId`.
|
||||
- `AnswerInteractionAsync` calls `AnswerCallbackQuery`.
|
||||
- `SendCalendarFileAsync` calls `SendDocument` and maps URL actions to inline keyboard buttons.
|
||||
|
||||
### Handler Scope
|
||||
|
||||
Refactor outbound Telegram calls in these flows to `IPlatformMessenger`:
|
||||
|
||||
- Join/leave/promote waitlist schedule updates and callback replies.
|
||||
- Cancel schedule update, group cancellation message, direct notification and callback reply.
|
||||
- Reschedule initiation, voting message updates, immediate reschedule schedule update, direct notifications and callback replies.
|
||||
- Export calendar file sending.
|
||||
|
||||
Keep Telegram inbound DTOs at the boundary for now:
|
||||
|
||||
- `UpdateRouter` still receives `Telegram.Bot.Types.Update`.
|
||||
- Text message parsing in reschedule input still receives `Telegram.Bot.Types.Message`.
|
||||
- `CreateSessionHandler` can keep photo/topic creation via `ITelegramBotClient` because issue #24 targets outbound schedule/interaction/private/calendar contract, not replacing all Telegram update primitives in one PR.
|
||||
|
||||
## Tasks
|
||||
|
||||
### Task 1: RED - Shared Contract Tests
|
||||
|
||||
**Files:**
|
||||
- Create: `tests/GmRelay.Bot.Tests/Platform/PlatformContractsTests.cs`
|
||||
|
||||
- [ ] **Step 1: Write failing tests for neutral contracts**
|
||||
|
||||
```csharp
|
||||
using GmRelay.Shared.Platform;
|
||||
using GmRelay.Shared.Rendering;
|
||||
|
||||
namespace GmRelay.Bot.Tests.Platform;
|
||||
|
||||
public sealed class PlatformContractsTests
|
||||
{
|
||||
[Fact]
|
||||
public void PlatformKind_ShouldDefineTelegramDiscordAndMaxSentinel()
|
||||
{
|
||||
Assert.Equal(0, (int)PlatformKind.Telegram);
|
||||
Assert.Equal(1, (int)PlatformKind.Discord);
|
||||
Assert.Equal(2, (int)PlatformKind.Max);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PlatformContracts_ShouldBeTelegramAssemblyFree()
|
||||
{
|
||||
var contractTypes = new[]
|
||||
{
|
||||
typeof(PlatformUser),
|
||||
typeof(PlatformGroup),
|
||||
typeof(PlatformMessageRef),
|
||||
typeof(PlatformMessageAction),
|
||||
typeof(PlatformScheduleMessage),
|
||||
typeof(PlatformPrivateMessage),
|
||||
typeof(PlatformInteractionReply),
|
||||
typeof(PlatformCalendarFile),
|
||||
typeof(IPlatformMessenger)
|
||||
};
|
||||
|
||||
Assert.All(contractTypes, type =>
|
||||
Assert.DoesNotContain(
|
||||
"Telegram",
|
||||
string.Join(" ", type.Assembly.GetReferencedAssemblies().Select(value => value.Name)),
|
||||
StringComparison.OrdinalIgnoreCase));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PlatformScheduleMessage_ShouldCarrySharedViewModelWithoutPlatformTypes()
|
||||
{
|
||||
var sessionId = Guid.Parse("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa");
|
||||
var view = SessionBatchViewBuilder.Build(
|
||||
"Campaign",
|
||||
[new SessionBatchDto(sessionId, new DateTime(2026, 5, 15, 16, 0, 0, DateTimeKind.Utc), "Planned", 4, "https://example.test/game")],
|
||||
[]);
|
||||
var group = new PlatformGroup(PlatformKind.Discord, "guild-1", "Guild", "channel-1", "thread-1");
|
||||
|
||||
var message = new PlatformScheduleMessage(group, view, ExistingMessage: null);
|
||||
|
||||
Assert.Equal(PlatformKind.Discord, message.Group.Platform);
|
||||
Assert.Same(view, message.View);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
- [ ] **Step 2: Run tests and verify RED**
|
||||
|
||||
Run:
|
||||
|
||||
```powershell
|
||||
dotnet test tests/GmRelay.Bot.Tests/GmRelay.Bot.Tests.csproj --filter PlatformContractsTests
|
||||
```
|
||||
|
||||
Expected: compile failure because `GmRelay.Shared.Platform` types do not exist.
|
||||
|
||||
### Task 2: GREEN - Add Shared Contracts
|
||||
|
||||
**Files:**
|
||||
- Create: `src/GmRelay.Shared/Platform/PlatformKind.cs`
|
||||
- Create: `src/GmRelay.Shared/Platform/PlatformUser.cs`
|
||||
- Create: `src/GmRelay.Shared/Platform/PlatformGroup.cs`
|
||||
- Create: `src/GmRelay.Shared/Platform/PlatformMessageContracts.cs`
|
||||
- Create: `src/GmRelay.Shared/Platform/IPlatformMessenger.cs`
|
||||
|
||||
- [ ] **Step 1: Add the contract files exactly as described in the Design section**
|
||||
- [ ] **Step 2: Run PlatformContractsTests and verify GREEN**
|
||||
|
||||
Run:
|
||||
|
||||
```powershell
|
||||
dotnet test tests/GmRelay.Bot.Tests/GmRelay.Bot.Tests.csproj --filter PlatformContractsTests
|
||||
```
|
||||
|
||||
Expected: `Passed`.
|
||||
|
||||
### Task 3: RED - Adapter and Flow Source Tests
|
||||
|
||||
**Files:**
|
||||
- Create: `tests/GmRelay.Bot.Tests/Infrastructure/Telegram/TelegramPlatformMessengerSourceTests.cs`
|
||||
|
||||
- [ ] **Step 1: Write source tests for adapter wiring and target flows**
|
||||
|
||||
```csharp
|
||||
namespace GmRelay.Bot.Tests.Infrastructure.Telegram;
|
||||
|
||||
public sealed class TelegramPlatformMessengerSourceTests
|
||||
{
|
||||
[Fact]
|
||||
public async Task Program_ShouldRegisterTelegramPlatformMessenger()
|
||||
{
|
||||
var program = await ReadRepositoryFileAsync("src/GmRelay.Bot/Program.cs");
|
||||
|
||||
Assert.Contains("IPlatformMessenger", program, StringComparison.Ordinal);
|
||||
Assert.Contains("TelegramPlatformMessenger", program, StringComparison.Ordinal);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("src/GmRelay.Bot/Features/Sessions/CreateSession/JoinSessionHandler.cs")]
|
||||
[InlineData("src/GmRelay.Bot/Features/Sessions/CreateSession/LeaveSessionHandler.cs")]
|
||||
[InlineData("src/GmRelay.Bot/Features/Sessions/CreateSession/CancelSessionHandler.cs")]
|
||||
[InlineData("src/GmRelay.Bot/Features/Sessions/CreateSession/PromoteWaitlistedPlayerHandler.cs")]
|
||||
[InlineData("src/GmRelay.Bot/Features/Sessions/RescheduleSession/InitiateRescheduleHandler.cs")]
|
||||
[InlineData("src/GmRelay.Bot/Features/Sessions/RescheduleSession/HandleRescheduleTimeInputHandler.cs")]
|
||||
[InlineData("src/GmRelay.Bot/Features/Sessions/RescheduleSession/HandleRescheduleVoteHandler.cs")]
|
||||
[InlineData("src/GmRelay.Bot/Features/Sessions/RescheduleSession/RescheduleVotingDeadlineService.cs")]
|
||||
[InlineData("src/GmRelay.Bot/Features/Sessions/ExportCalendar/ExportCalendarHandler.cs")]
|
||||
public async Task SessionFlows_ShouldUsePlatformMessengerForOutboundTelegramWork(string relativePath)
|
||||
{
|
||||
var source = await ReadRepositoryFileAsync(relativePath);
|
||||
|
||||
Assert.Contains("IPlatformMessenger", source, StringComparison.Ordinal);
|
||||
Assert.DoesNotContain("BatchMessageEditor.EditBatchMessageAsync", source, StringComparison.Ordinal);
|
||||
Assert.DoesNotContain(".AnswerCallbackQuery(", source, StringComparison.Ordinal);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task TelegramPlatformMessenger_ShouldOwnTelegramBotClientCalls()
|
||||
{
|
||||
var source = await ReadRepositoryFileAsync("src/GmRelay.Bot/Infrastructure/Telegram/TelegramPlatformMessenger.cs");
|
||||
|
||||
Assert.Contains("ITelegramBotClient", source, StringComparison.Ordinal);
|
||||
Assert.Contains("BatchMessageEditor.EditBatchMessageAsync", source, StringComparison.Ordinal);
|
||||
Assert.Contains("AnswerCallbackQuery", source, StringComparison.Ordinal);
|
||||
Assert.Contains("SendDocument", source, StringComparison.Ordinal);
|
||||
}
|
||||
|
||||
private static async Task<string> ReadRepositoryFileAsync(string relativePath)
|
||||
{
|
||||
var directory = new DirectoryInfo(AppContext.BaseDirectory);
|
||||
while (directory is not null)
|
||||
{
|
||||
var candidate = Path.Combine(directory.FullName, relativePath);
|
||||
if (File.Exists(candidate))
|
||||
{
|
||||
return await File.ReadAllTextAsync(candidate);
|
||||
}
|
||||
|
||||
directory = directory.Parent;
|
||||
}
|
||||
|
||||
throw new FileNotFoundException($"Could not locate repository file '{relativePath}'.");
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
- [ ] **Step 2: Run tests and verify RED**
|
||||
|
||||
Run:
|
||||
|
||||
```powershell
|
||||
dotnet test tests/GmRelay.Bot.Tests/GmRelay.Bot.Tests.csproj --filter TelegramPlatformMessengerSourceTests
|
||||
```
|
||||
|
||||
Expected: failures because `TelegramPlatformMessenger` is missing and handlers still call Telegram APIs directly.
|
||||
|
||||
### Task 4: GREEN - Implement TelegramPlatformMessenger and Registration
|
||||
|
||||
**Files:**
|
||||
- Create: `src/GmRelay.Bot/Infrastructure/Telegram/TelegramPlatformMessenger.cs`
|
||||
- Modify: `src/GmRelay.Bot/Program.cs`
|
||||
|
||||
- [ ] **Step 1: Implement adapter**
|
||||
|
||||
Implementation notes:
|
||||
|
||||
- Parse Telegram chat/thread/message IDs from neutral string IDs with `long.Parse` and `int.Parse`.
|
||||
- Use `ParseMode.Html` for HTML text.
|
||||
- Map `PlatformMessageAction` URLs to `InlineKeyboardButton.WithUrl`.
|
||||
- Return a `PlatformMessageRef` with message IDs converted to strings.
|
||||
|
||||
- [ ] **Step 2: Register adapter**
|
||||
|
||||
Add `using GmRelay.Shared.Platform;` and register:
|
||||
|
||||
```csharp
|
||||
builder.Services.AddSingleton<IPlatformMessenger, TelegramPlatformMessenger>();
|
||||
```
|
||||
|
||||
- [ ] **Step 3: Run adapter source tests**
|
||||
|
||||
Run:
|
||||
|
||||
```powershell
|
||||
dotnet test tests/GmRelay.Bot.Tests/GmRelay.Bot.Tests.csproj --filter TelegramPlatformMessengerSourceTests
|
||||
```
|
||||
|
||||
Expected: some handler source tests still fail until Task 5.
|
||||
|
||||
### Task 5: GREEN - Refactor Session Flows Through Adapter
|
||||
|
||||
**Files:**
|
||||
- Modify target handler files listed in Task 3
|
||||
- Modify: `src/GmRelay.Bot/Features/Notifications/DirectSessionNotificationSender.cs`
|
||||
|
||||
- [ ] **Step 1: Replace constructor dependencies**
|
||||
|
||||
Use `IPlatformMessenger messenger` in target handlers for outbound operations. Keep `ITelegramBotClient` only where the handler still performs inbound Telegram-specific work that is out of scope, such as message deletion or forum topic creation.
|
||||
|
||||
- [ ] **Step 2: Convert Telegram IDs to neutral platform objects**
|
||||
|
||||
Use helper code equivalent to:
|
||||
|
||||
```csharp
|
||||
private static PlatformGroup TelegramGroup(long chatId, string? title, int? threadId = null)
|
||||
=> new(
|
||||
PlatformKind.Telegram,
|
||||
chatId.ToString(System.Globalization.CultureInfo.InvariantCulture),
|
||||
title ?? "Telegram chat",
|
||||
ExternalChannelId: chatId.ToString(System.Globalization.CultureInfo.InvariantCulture),
|
||||
ExternalThreadId: threadId?.ToString(System.Globalization.CultureInfo.InvariantCulture));
|
||||
|
||||
private static PlatformUser TelegramUser(long telegramId, string displayName, string? username = null)
|
||||
=> new(
|
||||
PlatformKind.Telegram,
|
||||
telegramId.ToString(System.Globalization.CultureInfo.InvariantCulture),
|
||||
displayName,
|
||||
username);
|
||||
```
|
||||
|
||||
- [ ] **Step 3: Replace schedule updates**
|
||||
|
||||
Build `SessionBatchViewModel` as before, then call:
|
||||
|
||||
```csharp
|
||||
await messenger.UpdateScheduleAsync(
|
||||
new PlatformScheduleMessage(
|
||||
group,
|
||||
view,
|
||||
new PlatformMessageRef(PlatformKind.Telegram, group.ExternalGroupId, group.ExternalThreadId, messageId.ToString(System.Globalization.CultureInfo.InvariantCulture))),
|
||||
ct);
|
||||
```
|
||||
|
||||
- [ ] **Step 4: Replace interaction replies**
|
||||
|
||||
Use:
|
||||
|
||||
```csharp
|
||||
await messenger.AnswerInteractionAsync(
|
||||
new PlatformInteractionReply(command.CallbackQueryId, text, showAlert: false),
|
||||
ct);
|
||||
```
|
||||
|
||||
- [ ] **Step 5: Replace direct notifications**
|
||||
|
||||
`DirectSessionNotificationSender` should become a small compatibility service over `IPlatformMessenger`:
|
||||
|
||||
```csharp
|
||||
await messenger.SendPrivateMessageAsync(
|
||||
new PlatformPrivateMessage(
|
||||
new PlatformUser(PlatformKind.Telegram, recipient.TelegramId.ToString(CultureInfo.InvariantCulture), recipient.DisplayName, null),
|
||||
htmlText),
|
||||
ct);
|
||||
```
|
||||
|
||||
- [ ] **Step 6: Replace calendar file sending**
|
||||
|
||||
`ExportCalendarHandler` builds the same ICS bytes and calls `SendCalendarFileAsync`, preserving the subscription URL button as a `PlatformMessageAction`.
|
||||
|
||||
- [ ] **Step 7: Run target source tests**
|
||||
|
||||
Run:
|
||||
|
||||
```powershell
|
||||
dotnet test tests/GmRelay.Bot.Tests/GmRelay.Bot.Tests.csproj --filter TelegramPlatformMessengerSourceTests
|
||||
```
|
||||
|
||||
Expected: `Passed`.
|
||||
|
||||
### Task 6: Regression Tests
|
||||
|
||||
**Files:**
|
||||
- Existing tests only unless a compiler failure exposes a missing using or changed behavior.
|
||||
|
||||
- [ ] **Step 1: Run rendering and routing tests**
|
||||
|
||||
Run:
|
||||
|
||||
```powershell
|
||||
dotnet test tests/GmRelay.Bot.Tests/GmRelay.Bot.Tests.csproj --filter "FullyQualifiedName~Rendering|FullyQualifiedName~Telegram|FullyQualifiedName~RescheduleSession"
|
||||
```
|
||||
|
||||
Expected: `Passed`.
|
||||
|
||||
- [ ] **Step 2: Run all tests**
|
||||
|
||||
Run:
|
||||
|
||||
```powershell
|
||||
dotnet test tests/GmRelay.Bot.Tests/GmRelay.Bot.Tests.csproj
|
||||
```
|
||||
|
||||
Expected: `Passed`.
|
||||
|
||||
- [ ] **Step 3: Build solution**
|
||||
|
||||
Run:
|
||||
|
||||
```powershell
|
||||
dotnet build GM-Relay.slnx
|
||||
```
|
||||
|
||||
Expected: `Build succeeded` with warnings treated as errors.
|
||||
|
||||
### Task 7: Version Bump
|
||||
|
||||
**Files:**
|
||||
- Modify: `Directory.Build.props`
|
||||
- Modify: `compose.yaml`
|
||||
- Modify: `.gitea/workflows/deploy.yml`
|
||||
- Modify: `src/GmRelay.Web/Components/Layout/NavMenu.razor`
|
||||
|
||||
- [ ] **Step 1: Update all four version locations to `2.0.1`**
|
||||
- [ ] **Step 2: Verify sync**
|
||||
|
||||
Run:
|
||||
|
||||
```powershell
|
||||
rg -n "2\\.0\\.0|2\\.0\\.1" Directory.Build.props compose.yaml .gitea/workflows/deploy.yml src/GmRelay.Web/Components/Layout/NavMenu.razor
|
||||
```
|
||||
|
||||
Expected: no `2.0.0` matches in these files and `2.0.1` appears in all required locations.
|
||||
|
||||
### Task 8: Documentation Review
|
||||
|
||||
**Files:**
|
||||
- Review: `README.md`
|
||||
- Review: `docs/adr/002-platform-neutral-batch-rendering.md`
|
||||
|
||||
- [ ] **Step 1: Check README and ADR for platform contract accuracy**
|
||||
- [ ] **Step 2: Update docs if they now misrepresent platform-neutral responsibilities**
|
||||
|
||||
Expected likely doc change: README currently lists current version as `v1.15.0`, which is already inconsistent with repo version `2.0.0`. If this PR bumps to `2.0.1`, update that line to `v2.0.1`.
|
||||
|
||||
### Task 9: Commit, PR, CI, Review, Merge, Deploy, Release
|
||||
|
||||
**Files:**
|
||||
- Stage only files intentionally changed for issue #24.
|
||||
|
||||
- [ ] **Step 1: Create branch**
|
||||
|
||||
```powershell
|
||||
git checkout -b codex/refactor/issue-24-platform-messenger
|
||||
```
|
||||
|
||||
- [ ] **Step 2: Commit**
|
||||
|
||||
```powershell
|
||||
git add src/GmRelay.Shared/Platform src/GmRelay.Bot tests/GmRelay.Bot.Tests Directory.Build.props compose.yaml .gitea/workflows/deploy.yml src/GmRelay.Web/Components/Layout/NavMenu.razor README.md docs/adr/002-platform-neutral-batch-rendering.md
|
||||
git commit -m "refactor: add platform messenger contracts"
|
||||
```
|
||||
|
||||
- [ ] **Step 3: Push and create PR via Gitea**
|
||||
- [ ] **Step 4: Wait for PR CI and fix failures if any**
|
||||
- [ ] **Step 5: Run code review subagent and address findings**
|
||||
- [ ] **Step 6: Merge PR after CI and review**
|
||||
- [ ] **Step 7: Monitor deploy workflow**
|
||||
- [ ] **Step 8: Create release `v2.0.1` with Russian release notes**
|
||||
- [ ] **Step 9: Close issue #24 with PR and release links**
|
||||
|
||||
## Self-Review
|
||||
|
||||
- Spec coverage: all issue acceptance criteria map to Shared contracts, Telegram adapter, handler source tests, and build/test verification.
|
||||
- Placeholder scan: no `TBD`, `TODO`, or "fill later" placeholders are left in this plan.
|
||||
- Type consistency: all snippets use `GmRelay.Shared.Platform`, `PlatformKind.Telegram`, `PlatformMessageRef`, and `IPlatformMessenger` consistently.
|
||||
- Scope control: inbound Telegram update parsing remains out of scope; outbound schedule/private/interaction/calendar operations are in scope.
|
||||
@@ -1,731 +0,0 @@
|
||||
# Discord NetCord Gateway Implementation Plan
|
||||
|
||||
> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking.
|
||||
|
||||
**Goal:** Add a separate `src/GmRelay.DiscordBot` worker that uses NetCord Gateway for Discord slash commands and component interactions while keeping Telegram dependencies isolated in `src/GmRelay.Bot`.
|
||||
|
||||
**Architecture:** Create a new .NET worker project that references `GmRelay.ServiceDefaults` and `GmRelay.Shared`, validates `Discord:Token` during startup, registers NetCord gateway/application command/component services, and logs gateway lifecycle events through NetCord gateway handlers. Keep database connectivity aligned with the existing worker by registering the same `ConnectionStrings:gmrelaydb` `NpgsqlDataSource` pattern, but do not move Telegram code or dependencies.
|
||||
|
||||
**Tech Stack:** .NET 10 worker, Aspire service defaults, NetCord.Hosting `1.0.0-alpha.489`, Npgsql `10.0.2`, xUnit, Docker Compose, Gitea Actions.
|
||||
|
||||
---
|
||||
|
||||
## Issue
|
||||
|
||||
- Gitea issue: `#26`, `feat: добавить src/GmRelay.DiscordBot на NetCord Gateway`
|
||||
- Labels: `type:feature`, `area:discord`, `area:infra`, `platform:discord`, `priority:p1`, `pending-approval`
|
||||
- Version bump: minor, `2.1.1` -> `2.2.0`
|
||||
- Branch: `feature/issue-26-discord-netcord-gateway`
|
||||
|
||||
## Sources Checked
|
||||
|
||||
- NetCord application commands guide: `https://netcord.dev/guides/services/application-commands/introduction.html`
|
||||
- NetCord intents guide: `https://netcord.dev/guides/events/intents.html`
|
||||
- NetCord gateway handler docs: `https://netcord.dev/docs/NetCord.Hosting.Gateway.html`
|
||||
- NuGet flat container for `NetCord.Hosting`: latest observed version `1.0.0-alpha.489`
|
||||
|
||||
## File Structure
|
||||
|
||||
- Create: `src/GmRelay.DiscordBot/GmRelay.DiscordBot.csproj` - Discord worker project and package references.
|
||||
- Create: `src/GmRelay.DiscordBot/Program.cs` - host composition, token validation, database registration, NetCord service registration.
|
||||
- Create: `src/GmRelay.DiscordBot/DiscordOptions.cs` - strongly typed Discord token/options validation.
|
||||
- Create: `src/GmRelay.DiscordBot/Infrastructure/Logging/SecretRedactor.cs` - Discord-local startup redaction without referencing the Telegram worker project.
|
||||
- Create: `src/GmRelay.DiscordBot/Infrastructure/Logging/DiscordGatewayLifecycleLogger.cs` - NetCord gateway lifecycle handler for ready/connect/resume/disconnect/close/rate-limit events where available.
|
||||
- Create: `src/GmRelay.DiscordBot/Dockerfile` - publish and runtime image for the Discord worker.
|
||||
- Modify: `GM-Relay.slnx` - include the new project.
|
||||
- Modify: `src/GmRelay.AppHost/GmRelay.AppHost.csproj` - reference the Discord worker for Aspire orchestration.
|
||||
- Modify: `src/GmRelay.AppHost/Program.cs` - add `discord` project with PostgreSQL reference.
|
||||
- Modify: `tests/GmRelay.Bot.Tests/GmRelay.Bot.Tests.csproj` - reference the Discord worker project.
|
||||
- Create: `tests/GmRelay.Bot.Tests/Discord/DiscordProjectStructureTests.cs` - source-level tests for solution inclusion, Docker/Compose/CI wiring, and Telegram isolation.
|
||||
- Create: `tests/GmRelay.Bot.Tests/Discord/DiscordOptionsTests.cs` - unit tests for token validation.
|
||||
- Create: `tests/GmRelay.Bot.Tests/Discord/DiscordStartupTests.cs` - source-level startup tests for NetCord registration, service defaults, and PostgreSQL connection requirements.
|
||||
- Modify: `compose.yaml` - add `discord` service and versioned image tag.
|
||||
- Modify: `.gitea/workflows/deploy.yml` - build/push/scan/pull Discord image and include `DISCORD_BOT_TOKEN` in `.env`.
|
||||
- Modify: `.gitea/workflows/pr-checks.yml` - build the Discord project in PR checks.
|
||||
- Modify: `Directory.Build.props` - version `2.2.0`.
|
||||
- Modify: `src/GmRelay.Web/Components/Layout/NavMenu.razor` - visible version `v2.2.0`.
|
||||
- Generated by restore: `src/GmRelay.DiscordBot/packages.lock.json`.
|
||||
- Generated by restore: updates to `tests/GmRelay.Bot.Tests/packages.lock.json` and `src/GmRelay.AppHost/packages.lock.json`.
|
||||
|
||||
## TDD Plan
|
||||
|
||||
### Task 1: Project Presence And Telegram Isolation
|
||||
|
||||
**Files:**
|
||||
- Create: `tests/GmRelay.Bot.Tests/Discord/DiscordProjectStructureTests.cs`
|
||||
- Modify: `GM-Relay.slnx`
|
||||
- Create: `src/GmRelay.DiscordBot/GmRelay.DiscordBot.csproj`
|
||||
- Create: `src/GmRelay.DiscordBot/Program.cs`
|
||||
|
||||
- [ ] **Step 1: Write the failing test**
|
||||
|
||||
```csharp
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
namespace GmRelay.Bot.Tests.Discord;
|
||||
|
||||
public sealed class DiscordProjectStructureTests
|
||||
{
|
||||
private static string GetRepoRoot()
|
||||
{
|
||||
var dir = AppContext.BaseDirectory;
|
||||
while (!string.IsNullOrEmpty(dir) && !File.Exists(Path.Combine(dir, "Directory.Build.props")))
|
||||
{
|
||||
dir = Directory.GetParent(dir)?.FullName;
|
||||
}
|
||||
|
||||
return dir ?? throw new InvalidOperationException("Could not find repo root");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Solution_ShouldIncludeDiscordWorkerProject()
|
||||
{
|
||||
var repoRoot = GetRepoRoot();
|
||||
var solution = File.ReadAllText(Path.Combine(repoRoot, "GM-Relay.slnx"));
|
||||
|
||||
Assert.Contains("src/GmRelay.DiscordBot/GmRelay.DiscordBot.csproj", solution);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DiscordWorkerProject_ShouldExistWithoutTelegramDependency()
|
||||
{
|
||||
var repoRoot = GetRepoRoot();
|
||||
var projectPath = Path.Combine(repoRoot, "src", "GmRelay.DiscordBot", "GmRelay.DiscordBot.csproj");
|
||||
|
||||
Assert.True(File.Exists(projectPath), "Discord worker project should exist.");
|
||||
|
||||
var project = File.ReadAllText(projectPath);
|
||||
Assert.Contains("Microsoft.NET.Sdk.Worker", project);
|
||||
Assert.Contains("NetCord.Hosting", project);
|
||||
Assert.Contains("GmRelay.ServiceDefaults.csproj", project);
|
||||
Assert.Contains("GmRelay.Shared.csproj", project);
|
||||
Assert.DoesNotContain("Telegram.Bot", project);
|
||||
Assert.DoesNotContain("GmRelay.Bot.csproj", project);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TelegramWorkerProject_ShouldNotReferenceNetCord()
|
||||
{
|
||||
var repoRoot = GetRepoRoot();
|
||||
var project = File.ReadAllText(Path.Combine(repoRoot, "src", "GmRelay.Bot", "GmRelay.Bot.csproj"));
|
||||
|
||||
Assert.DoesNotContain("NetCord", project, StringComparison.OrdinalIgnoreCase);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
- [ ] **Step 2: Run the test to verify it fails**
|
||||
|
||||
Run: `dotnet test tests/GmRelay.Bot.Tests/GmRelay.Bot.Tests.csproj --filter FullyQualifiedName~DiscordProjectStructureTests`
|
||||
|
||||
Expected: FAIL because `GM-Relay.slnx` does not include `src/GmRelay.DiscordBot/GmRelay.DiscordBot.csproj` and the project file does not exist.
|
||||
|
||||
- [ ] **Step 3: Write minimal implementation**
|
||||
|
||||
Create `src/GmRelay.DiscordBot/GmRelay.DiscordBot.csproj`:
|
||||
|
||||
```xml
|
||||
<Project Sdk="Microsoft.NET.Sdk.Worker">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<LangVersion>preview</LangVersion>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<UserSecretsId>dotnet-GmRelay.DiscordBot-issue-26</UserSecretsId>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Aspire.Npgsql" Version="13.2.2" />
|
||||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="10.0.5" />
|
||||
<PackageReference Include="NetCord.Hosting" Version="1.0.0-alpha.489" />
|
||||
<PackageReference Include="Npgsql" Version="10.0.2" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\GmRelay.ServiceDefaults\GmRelay.ServiceDefaults.csproj" />
|
||||
<ProjectReference Include="..\GmRelay.Shared\GmRelay.Shared.csproj" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
```
|
||||
|
||||
Add this project to `GM-Relay.slnx` inside `/src/`:
|
||||
|
||||
```xml
|
||||
<Project Path="src/GmRelay.DiscordBot/GmRelay.DiscordBot.csproj" />
|
||||
```
|
||||
|
||||
Create temporary minimal `src/GmRelay.DiscordBot/Program.cs`:
|
||||
|
||||
```csharp
|
||||
var builder = Host.CreateApplicationBuilder(args);
|
||||
builder.AddServiceDefaults();
|
||||
await builder.Build().RunAsync();
|
||||
```
|
||||
|
||||
- [ ] **Step 4: Run the test to verify it passes**
|
||||
|
||||
Run: `dotnet test tests/GmRelay.Bot.Tests/GmRelay.Bot.Tests.csproj --filter FullyQualifiedName~DiscordProjectStructureTests`
|
||||
|
||||
Expected: PASS.
|
||||
|
||||
### Task 2: Token Validation
|
||||
|
||||
**Files:**
|
||||
- Modify: `tests/GmRelay.Bot.Tests/GmRelay.Bot.Tests.csproj`
|
||||
- Create: `tests/GmRelay.Bot.Tests/Discord/DiscordOptionsTests.cs`
|
||||
- Create: `src/GmRelay.DiscordBot/DiscordOptions.cs`
|
||||
|
||||
- [ ] **Step 1: Write the failing test**
|
||||
|
||||
Add the project reference to `tests/GmRelay.Bot.Tests/GmRelay.Bot.Tests.csproj`:
|
||||
|
||||
```xml
|
||||
<ProjectReference Include="..\..\src\GmRelay.DiscordBot\GmRelay.DiscordBot.csproj" />
|
||||
```
|
||||
|
||||
Create `tests/GmRelay.Bot.Tests/Discord/DiscordOptionsTests.cs`:
|
||||
|
||||
```csharp
|
||||
using GmRelay.DiscordBot;
|
||||
|
||||
namespace GmRelay.Bot.Tests.Discord;
|
||||
|
||||
public sealed class DiscordOptionsTests
|
||||
{
|
||||
[Theory]
|
||||
[InlineData(null)]
|
||||
[InlineData("")]
|
||||
[InlineData(" ")]
|
||||
public void Validate_ShouldRejectMissingToken(string? token)
|
||||
{
|
||||
var options = new DiscordOptions { Token = token };
|
||||
|
||||
var exception = Assert.Throws<InvalidOperationException>(options.Validate);
|
||||
|
||||
Assert.Contains("Discord:Token is required", exception.Message);
|
||||
Assert.Contains("Discord__Token", exception.Message);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_ShouldAcceptConfiguredToken()
|
||||
{
|
||||
var options = new DiscordOptions { Token = "configured-token" };
|
||||
|
||||
options.Validate();
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
- [ ] **Step 2: Run the test to verify it fails**
|
||||
|
||||
Run: `dotnet test tests/GmRelay.Bot.Tests/GmRelay.Bot.Tests.csproj --filter FullyQualifiedName~DiscordOptionsTests`
|
||||
|
||||
Expected: FAIL at compile time because `GmRelay.DiscordBot.DiscordOptions` is not defined.
|
||||
|
||||
- [ ] **Step 3: Write minimal implementation**
|
||||
|
||||
Create `src/GmRelay.DiscordBot/DiscordOptions.cs`:
|
||||
|
||||
```csharp
|
||||
namespace GmRelay.DiscordBot;
|
||||
|
||||
public sealed class DiscordOptions
|
||||
{
|
||||
public string? Token { get; init; }
|
||||
|
||||
public void Validate()
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(Token))
|
||||
{
|
||||
throw new InvalidOperationException(
|
||||
"Discord:Token is required. Set via environment variable Discord__Token or user secrets.");
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
- [ ] **Step 4: Run the test to verify it passes**
|
||||
|
||||
Run: `dotnet test tests/GmRelay.Bot.Tests/GmRelay.Bot.Tests.csproj --filter FullyQualifiedName~DiscordOptionsTests`
|
||||
|
||||
Expected: PASS.
|
||||
|
||||
### Task 3: Startup Wiring For Service Defaults, PostgreSQL, NetCord, And Slash Commands
|
||||
|
||||
**Files:**
|
||||
- Create: `tests/GmRelay.Bot.Tests/Discord/DiscordStartupTests.cs`
|
||||
- Modify: `src/GmRelay.DiscordBot/Program.cs`
|
||||
|
||||
- [ ] **Step 1: Write the failing test**
|
||||
|
||||
Create `tests/GmRelay.Bot.Tests/Discord/DiscordStartupTests.cs`:
|
||||
|
||||
```csharp
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
namespace GmRelay.Bot.Tests.Discord;
|
||||
|
||||
public sealed class DiscordStartupTests
|
||||
{
|
||||
private static string GetRepoRoot()
|
||||
{
|
||||
var dir = AppContext.BaseDirectory;
|
||||
while (!string.IsNullOrEmpty(dir) && !File.Exists(Path.Combine(dir, "Directory.Build.props")))
|
||||
{
|
||||
dir = Directory.GetParent(dir)?.FullName;
|
||||
}
|
||||
|
||||
return dir ?? throw new InvalidOperationException("Could not find repo root");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Program_ShouldValidateDiscordTokenBeforeRunning()
|
||||
{
|
||||
var program = ReadProgram();
|
||||
|
||||
Assert.Contains("GetRequiredSection(\"Discord\")", program);
|
||||
Assert.Contains("DiscordOptions", program);
|
||||
Assert.Contains(".Validate()", program);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Program_ShouldRegisterServiceDefaultsAndPostgresDataSource()
|
||||
{
|
||||
var program = ReadProgram();
|
||||
|
||||
Assert.Contains("builder.AddServiceDefaults()", program);
|
||||
Assert.Contains("ConnectionStrings:gmrelaydb is required", program);
|
||||
Assert.Contains("NpgsqlDataSource", program);
|
||||
Assert.Contains("SecretRedactor.RedactConnectionString", program);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Program_ShouldRegisterNetCordGatewayApplicationCommandsAndComponents()
|
||||
{
|
||||
var program = ReadProgram();
|
||||
|
||||
Assert.Contains(".AddDiscordGateway", program);
|
||||
Assert.Contains(".AddApplicationCommands", program);
|
||||
Assert.Contains(".AddComponentInteractions", program);
|
||||
Assert.Contains(".AddGatewayHandlers", program);
|
||||
Assert.Contains("AddSlashCommand", program);
|
||||
}
|
||||
|
||||
private static string ReadProgram()
|
||||
{
|
||||
var repoRoot = GetRepoRoot();
|
||||
return File.ReadAllText(Path.Combine(repoRoot, "src", "GmRelay.DiscordBot", "Program.cs"));
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
- [ ] **Step 2: Run the test to verify it fails**
|
||||
|
||||
Run: `dotnet test tests/GmRelay.Bot.Tests/GmRelay.Bot.Tests.csproj --filter FullyQualifiedName~DiscordStartupTests`
|
||||
|
||||
Expected: FAIL because `Program.cs` does not validate `Discord:Token`, register `NpgsqlDataSource`, or register NetCord services yet.
|
||||
|
||||
- [ ] **Step 3: Write minimal implementation**
|
||||
|
||||
Replace `src/GmRelay.DiscordBot/Program.cs` with host composition that:
|
||||
|
||||
```csharp
|
||||
using GmRelay.DiscordBot;
|
||||
using GmRelay.DiscordBot.Infrastructure.Logging;
|
||||
using NetCord.Gateway;
|
||||
using NetCord.Hosting.Gateway;
|
||||
using NetCord.Hosting.Services.ApplicationCommands;
|
||||
using NetCord.Hosting.Services.ComponentInteractions;
|
||||
using Npgsql;
|
||||
|
||||
var builder = Host.CreateApplicationBuilder(args);
|
||||
|
||||
builder.AddServiceDefaults();
|
||||
|
||||
var discordOptions = builder.Configuration
|
||||
.GetRequiredSection("Discord")
|
||||
.Get<DiscordOptions>() ?? new DiscordOptions();
|
||||
discordOptions.Validate();
|
||||
|
||||
builder.Services.AddSingleton(discordOptions);
|
||||
|
||||
builder.Services.AddSingleton<NpgsqlDataSource>(sp =>
|
||||
{
|
||||
var config = sp.GetRequiredService<IConfiguration>();
|
||||
var loggerFactory = sp.GetRequiredService<ILoggerFactory>();
|
||||
var connectionString = config.GetConnectionString("gmrelaydb")
|
||||
?? throw new InvalidOperationException(
|
||||
"ConnectionStrings:gmrelaydb is required. Set via environment variable ConnectionStrings__gmrelaydb.");
|
||||
|
||||
var logger = loggerFactory.CreateLogger("GmRelay.DiscordBot.Startup");
|
||||
logger.LogInformation(
|
||||
"Configured PostgreSQL data source with connection string {ConnectionString}",
|
||||
SecretRedactor.RedactConnectionString(connectionString));
|
||||
|
||||
return NpgsqlDataSource.Create(connectionString);
|
||||
});
|
||||
|
||||
builder.Services
|
||||
.AddDiscordGateway(options =>
|
||||
{
|
||||
options.Token = discordOptions.Token;
|
||||
options.Intents = GatewayIntents.Guilds;
|
||||
})
|
||||
.AddApplicationCommands()
|
||||
.AddComponentInteractions()
|
||||
.AddGatewayHandlers(typeof(Program).Assembly);
|
||||
|
||||
var host = builder.Build();
|
||||
|
||||
host.AddSlashCommand("ping", "Checks whether GM-Relay Discord is online.", () => "Pong!");
|
||||
|
||||
await host.RunAsync();
|
||||
```
|
||||
|
||||
Use the Discord-local `SecretRedactor` namespace instead of `GmRelay.Bot.Infrastructure.Logging` so the new project does not reference the Telegram worker.
|
||||
|
||||
Create `src/GmRelay.DiscordBot/Infrastructure/Logging/SecretRedactor.cs`:
|
||||
|
||||
```csharp
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace GmRelay.DiscordBot.Infrastructure.Logging;
|
||||
|
||||
internal static partial class SecretRedactor
|
||||
{
|
||||
public static string RedactConnectionString(string connectionString)
|
||||
{
|
||||
return PasswordPattern().Replace(connectionString, "$1***");
|
||||
}
|
||||
|
||||
[GeneratedRegex(@"(?i)(Password\s*=\s*)[^;]+")]
|
||||
private static partial Regex PasswordPattern();
|
||||
}
|
||||
```
|
||||
|
||||
If `GatewayClientOptions.Token` does not accept `string`, adjust to NetCord's required token type after compile feedback while preserving the tests' intent.
|
||||
|
||||
- [ ] **Step 4: Run the test to verify it passes**
|
||||
|
||||
Run: `dotnet test tests/GmRelay.Bot.Tests/GmRelay.Bot.Tests.csproj --filter FullyQualifiedName~DiscordStartupTests`
|
||||
|
||||
Expected: PASS.
|
||||
|
||||
### Task 4: Gateway Lifecycle Logging
|
||||
|
||||
**Files:**
|
||||
- Modify: `tests/GmRelay.Bot.Tests/Discord/DiscordStartupTests.cs`
|
||||
- Create: `src/GmRelay.DiscordBot/Infrastructure/Logging/DiscordGatewayLifecycleLogger.cs`
|
||||
|
||||
- [ ] **Step 1: Write the failing test**
|
||||
|
||||
Add to `DiscordStartupTests.cs`:
|
||||
|
||||
```csharp
|
||||
[Fact]
|
||||
public void LifecycleLogger_ShouldLogGatewayLifecycleEventsWithoutTokenValues()
|
||||
{
|
||||
var repoRoot = GetRepoRoot();
|
||||
var loggerPath = Path.Combine(repoRoot, "src", "GmRelay.DiscordBot", "Infrastructure", "Logging", "DiscordGatewayLifecycleLogger.cs");
|
||||
|
||||
Assert.True(File.Exists(loggerPath), "Discord gateway lifecycle logger should exist.");
|
||||
|
||||
var logger = File.ReadAllText(loggerPath);
|
||||
Assert.Contains("IReadyGatewayHandler", logger);
|
||||
Assert.Contains("IDisconnectGatewayHandler", logger);
|
||||
Assert.Contains("IResumeGatewayHandler", logger);
|
||||
Assert.Contains("LogInformation", logger);
|
||||
Assert.DoesNotContain("Token", logger);
|
||||
}
|
||||
```
|
||||
|
||||
- [ ] **Step 2: Run the test to verify it fails**
|
||||
|
||||
Run: `dotnet test tests/GmRelay.Bot.Tests/GmRelay.Bot.Tests.csproj --filter LifecycleLogger_ShouldLogGatewayLifecycleEventsWithoutTokenValues`
|
||||
|
||||
Expected: FAIL because `DiscordGatewayLifecycleLogger.cs` does not exist.
|
||||
|
||||
- [ ] **Step 3: Write minimal implementation**
|
||||
|
||||
Create `src/GmRelay.DiscordBot/Infrastructure/Logging/DiscordGatewayLifecycleLogger.cs` using the concrete NetCord handler signatures from the installed `NetCord.Hosting` package. Minimum behavior:
|
||||
|
||||
```csharp
|
||||
using Microsoft.Extensions.Logging;
|
||||
using NetCord.Gateway;
|
||||
using NetCord.Hosting.Gateway;
|
||||
|
||||
namespace GmRelay.DiscordBot.Infrastructure.Logging;
|
||||
|
||||
public sealed class DiscordGatewayLifecycleLogger(
|
||||
ILogger<DiscordGatewayLifecycleLogger> logger)
|
||||
: IReadyGatewayHandler,
|
||||
IDisconnectGatewayHandler,
|
||||
IResumeGatewayHandler
|
||||
{
|
||||
public ValueTask HandleAsync(ReadyEventArgs arg)
|
||||
{
|
||||
logger.LogInformation("Discord gateway ready as application {ApplicationId}", arg.Application.Id);
|
||||
return ValueTask.CompletedTask;
|
||||
}
|
||||
|
||||
public ValueTask HandleAsync(DisconnectEventArgs arg)
|
||||
{
|
||||
logger.LogWarning("Discord gateway disconnected with close status {CloseStatus}", arg.CloseStatus);
|
||||
return ValueTask.CompletedTask;
|
||||
}
|
||||
|
||||
public ValueTask HandleAsync()
|
||||
{
|
||||
logger.LogInformation("Discord gateway session resumed");
|
||||
return ValueTask.CompletedTask;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
If interface signatures differ in `1.0.0-alpha.489`, inspect the package XML/docs and adjust the handlers to compile while keeping ready/disconnect/resume logging and never logging token values.
|
||||
|
||||
- [ ] **Step 4: Run the test to verify it passes**
|
||||
|
||||
Run: `dotnet test tests/GmRelay.Bot.Tests/GmRelay.Bot.Tests.csproj --filter LifecycleLogger_ShouldLogGatewayLifecycleEventsWithoutTokenValues`
|
||||
|
||||
Expected: PASS.
|
||||
|
||||
### Task 5: Runtime Container, Compose, AppHost, And CI Wiring
|
||||
|
||||
**Files:**
|
||||
- Modify: `tests/GmRelay.Bot.Tests/Discord/DiscordProjectStructureTests.cs`
|
||||
- Create: `src/GmRelay.DiscordBot/Dockerfile`
|
||||
- Modify: `compose.yaml`
|
||||
- Modify: `src/GmRelay.AppHost/GmRelay.AppHost.csproj`
|
||||
- Modify: `src/GmRelay.AppHost/Program.cs`
|
||||
- Modify: `.gitea/workflows/pr-checks.yml`
|
||||
- Modify: `.gitea/workflows/deploy.yml`
|
||||
|
||||
- [ ] **Step 1: Write the failing test**
|
||||
|
||||
Add to `DiscordProjectStructureTests.cs`:
|
||||
|
||||
```csharp
|
||||
[Fact]
|
||||
public void RuntimeWiring_ShouldIncludeDiscordServiceWithoutCouplingTelegram()
|
||||
{
|
||||
var repoRoot = GetRepoRoot();
|
||||
var compose = File.ReadAllText(Path.Combine(repoRoot, "compose.yaml"));
|
||||
var appHostProject = File.ReadAllText(Path.Combine(repoRoot, "src", "GmRelay.AppHost", "GmRelay.AppHost.csproj"));
|
||||
var appHostProgram = File.ReadAllText(Path.Combine(repoRoot, "src", "GmRelay.AppHost", "Program.cs"));
|
||||
var prChecks = File.ReadAllText(Path.Combine(repoRoot, ".gitea", "workflows", "pr-checks.yml"));
|
||||
var deploy = File.ReadAllText(Path.Combine(repoRoot, ".gitea", "workflows", "deploy.yml"));
|
||||
|
||||
Assert.Contains("gmrelay-discord-bot:2.2.0", compose);
|
||||
Assert.Contains("Discord__Token=${DISCORD_BOT_TOKEN:?Set DISCORD_BOT_TOKEN in .env}", compose);
|
||||
Assert.Contains("src/GmRelay.DiscordBot/Dockerfile", deploy);
|
||||
Assert.Contains("DISCORD_BOT_TOKEN", deploy);
|
||||
Assert.Contains("dotnet build src/GmRelay.DiscordBot/GmRelay.DiscordBot.csproj --no-restore", prChecks);
|
||||
Assert.Contains("GmRelay.DiscordBot.csproj", appHostProject);
|
||||
Assert.Contains("Projects.GmRelay_DiscordBot", appHostProgram);
|
||||
}
|
||||
```
|
||||
|
||||
- [ ] **Step 2: Run the test to verify it fails**
|
||||
|
||||
Run: `dotnet test tests/GmRelay.Bot.Tests/GmRelay.Bot.Tests.csproj --filter RuntimeWiring_ShouldIncludeDiscordServiceWithoutCouplingTelegram`
|
||||
|
||||
Expected: FAIL because runtime wiring is not present.
|
||||
|
||||
- [ ] **Step 3: Write minimal implementation**
|
||||
|
||||
Create `src/GmRelay.DiscordBot/Dockerfile` modeled after `src/GmRelay.Bot/Dockerfile`, with project copy/restore for `GmRelay.DiscordBot`, `GmRelay.ServiceDefaults`, and `GmRelay.Shared`, and entrypoint `./GmRelay.DiscordBot`.
|
||||
|
||||
Add `discord` service to `compose.yaml`:
|
||||
|
||||
```yaml
|
||||
discord:
|
||||
image: git.codeanddice.ru/toutsu/gmrelay-discord-bot:2.2.0
|
||||
restart: always
|
||||
depends_on:
|
||||
db:
|
||||
condition: service_healthy
|
||||
environment:
|
||||
- "ConnectionStrings__gmrelaydb=Host=db;Port=5432;Database=gmrelay_db;Username=gmrelay;Password=${POSTGRES_PASSWORD:?Set POSTGRES_PASSWORD in .env}"
|
||||
- "Discord__Token=${DISCORD_BOT_TOKEN:?Set DISCORD_BOT_TOKEN in .env}"
|
||||
networks:
|
||||
- gmrelay
|
||||
```
|
||||
|
||||
Add Discord project reference to `src/GmRelay.AppHost/GmRelay.AppHost.csproj`:
|
||||
|
||||
```xml
|
||||
<ProjectReference Include="..\GmRelay.DiscordBot\GmRelay.DiscordBot.csproj" />
|
||||
```
|
||||
|
||||
Add Discord service to `src/GmRelay.AppHost/Program.cs`:
|
||||
|
||||
```csharp
|
||||
builder.AddProject<Projects.GmRelay_DiscordBot>("discord")
|
||||
.WithReference(postgres)
|
||||
.WaitFor(postgres);
|
||||
```
|
||||
|
||||
Update `.gitea/workflows/pr-checks.yml` with:
|
||||
|
||||
```yaml
|
||||
- name: Build Discord Bot (compile check, includes SAST)
|
||||
run: dotnet build src/GmRelay.DiscordBot/GmRelay.DiscordBot.csproj --no-restore
|
||||
```
|
||||
|
||||
Update `.gitea/workflows/deploy.yml` to build, push, scan, pull, and deploy `git.codeanddice.ru/toutsu/gmrelay-discord-bot:${{ env.VERSION }}` and write `DISCORD_BOT_TOKEN=${{ secrets.DISCORD_BOT_TOKEN }}` to `.env`.
|
||||
|
||||
- [ ] **Step 4: Run the test to verify it passes**
|
||||
|
||||
Run: `dotnet test tests/GmRelay.Bot.Tests/GmRelay.Bot.Tests.csproj --filter RuntimeWiring_ShouldIncludeDiscordServiceWithoutCouplingTelegram`
|
||||
|
||||
Expected: PASS.
|
||||
|
||||
### Task 6: Version Synchronization
|
||||
|
||||
**Files:**
|
||||
- Modify: `tests/GmRelay.Bot.Tests/Discord/DiscordProjectStructureTests.cs`
|
||||
- Modify: `Directory.Build.props`
|
||||
- Modify: `compose.yaml`
|
||||
- Modify: `.gitea/workflows/deploy.yml`
|
||||
- Modify: `src/GmRelay.Web/Components/Layout/NavMenu.razor`
|
||||
|
||||
- [ ] **Step 1: Write the failing test**
|
||||
|
||||
Add to `DiscordProjectStructureTests.cs`:
|
||||
|
||||
```csharp
|
||||
[Fact]
|
||||
public void Version_ShouldBeSynchronizedForDiscordFeatureRelease()
|
||||
{
|
||||
var repoRoot = GetRepoRoot();
|
||||
|
||||
Assert.Contains("<Version>2.2.0</Version>", File.ReadAllText(Path.Combine(repoRoot, "Directory.Build.props")));
|
||||
Assert.Contains("VERSION: 2.2.0", File.ReadAllText(Path.Combine(repoRoot, ".gitea", "workflows", "deploy.yml")));
|
||||
Assert.Contains("gmrelay-bot:2.2.0", File.ReadAllText(Path.Combine(repoRoot, "compose.yaml")));
|
||||
Assert.Contains("gmrelay-web:2.2.0", File.ReadAllText(Path.Combine(repoRoot, "compose.yaml")));
|
||||
Assert.Contains("gmrelay-discord-bot:2.2.0", File.ReadAllText(Path.Combine(repoRoot, "compose.yaml")));
|
||||
Assert.Contains("v2.2.0", File.ReadAllText(Path.Combine(repoRoot, "src", "GmRelay.Web", "Components", "Layout", "NavMenu.razor")));
|
||||
}
|
||||
```
|
||||
|
||||
- [ ] **Step 2: Run the test to verify it fails**
|
||||
|
||||
Run: `dotnet test tests/GmRelay.Bot.Tests/GmRelay.Bot.Tests.csproj --filter Version_ShouldBeSynchronizedForDiscordFeatureRelease`
|
||||
|
||||
Expected: FAIL because current version is `2.1.1`.
|
||||
|
||||
- [ ] **Step 3: Write minimal implementation**
|
||||
|
||||
Update:
|
||||
- `Directory.Build.props`: `<Version>2.2.0</Version>`
|
||||
- `.gitea/workflows/deploy.yml`: `VERSION: 2.2.0`
|
||||
- `compose.yaml`: `gmrelay-bot:2.2.0`, `gmrelay-web:2.2.0`, `gmrelay-discord-bot:2.2.0`
|
||||
- `src/GmRelay.Web/Components/Layout/NavMenu.razor`: `v2.2.0`
|
||||
|
||||
- [ ] **Step 4: Run the test to verify it passes**
|
||||
|
||||
Run: `dotnet test tests/GmRelay.Bot.Tests/GmRelay.Bot.Tests.csproj --filter Version_ShouldBeSynchronizedForDiscordFeatureRelease`
|
||||
|
||||
Expected: PASS.
|
||||
|
||||
### Task 7: Restore, Format, Build, And Full Test Verification
|
||||
|
||||
**Files:**
|
||||
- Generated/updated: `src/GmRelay.DiscordBot/packages.lock.json`
|
||||
- Generated/updated: `tests/GmRelay.Bot.Tests/packages.lock.json`
|
||||
- Generated/updated: `src/GmRelay.AppHost/packages.lock.json`
|
||||
- Any code formatting changes required by `dotnet format`
|
||||
|
||||
- [ ] **Step 1: Restore lock files**
|
||||
|
||||
Run: `dotnet restore GM-Relay.slnx`
|
||||
|
||||
Expected: restore succeeds and creates/updates lock files for the new project references and NetCord dependency.
|
||||
|
||||
- [ ] **Step 2: Run targeted tests**
|
||||
|
||||
Run: `dotnet test tests/GmRelay.Bot.Tests/GmRelay.Bot.Tests.csproj --filter FullyQualifiedName~Discord`
|
||||
|
||||
Expected: all Discord tests pass.
|
||||
|
||||
- [ ] **Step 3: Run full tests**
|
||||
|
||||
Run: `dotnet test tests/GmRelay.Bot.Tests/GmRelay.Bot.Tests.csproj --verbosity normal`
|
||||
|
||||
Expected: all tests pass.
|
||||
|
||||
- [ ] **Step 4: Run release build**
|
||||
|
||||
Run: `dotnet build GM-Relay.slnx -c Release`
|
||||
|
||||
Expected: solution build succeeds and includes `src/GmRelay.DiscordBot`.
|
||||
|
||||
- [ ] **Step 5: Run format check**
|
||||
|
||||
Run: `dotnet format --verify-no-changes --verbosity diagnostic`
|
||||
|
||||
Expected: no formatting changes required.
|
||||
|
||||
- [ ] **Step 6: Inspect diff for secrets**
|
||||
|
||||
Run: `git diff --check`
|
||||
|
||||
Expected: no whitespace errors and no Discord token value in tracked files.
|
||||
|
||||
Run: `git diff -- . ':!*.lock.json'`
|
||||
|
||||
Expected: diff contains configuration variable names such as `Discord__Token` and `DISCORD_BOT_TOKEN`, but not a real token value.
|
||||
|
||||
### Task 8: Commit, PR, CI, Deploy, Release, Issue Closure
|
||||
|
||||
**Files:**
|
||||
- All intended implementation, test, lock, workflow, compose, and version files.
|
||||
|
||||
- [ ] **Step 1: Create commit**
|
||||
|
||||
Run:
|
||||
|
||||
```powershell
|
||||
git status --short
|
||||
git add GM-Relay.slnx Directory.Build.props compose.yaml .gitea/workflows/deploy.yml .gitea/workflows/pr-checks.yml src/GmRelay.AppHost src/GmRelay.DiscordBot src/GmRelay.Web/Components/Layout/NavMenu.razor tests/GmRelay.Bot.Tests
|
||||
git commit -m "feat: add Discord NetCord gateway worker"
|
||||
```
|
||||
|
||||
Expected: only intended files are staged and committed. Do not stage untracked `CLAUDE.md`.
|
||||
|
||||
- [ ] **Step 2: Push branch and open PR**
|
||||
|
||||
Run: `git push -u origin feature/issue-26-discord-netcord-gateway`
|
||||
|
||||
Create Gitea PR to `main` with:
|
||||
- Summary of Discord worker, token validation, runtime wiring, and version bump.
|
||||
- Test plan showing targeted Discord tests, full tests, release build, format, and secret diff inspection.
|
||||
- Link to issue `#26`.
|
||||
|
||||
- [ ] **Step 3: Store Discord token as a Gitea Actions secret**
|
||||
|
||||
Use Gitea Actions configuration to create or update repository secret `DISCORD_BOT_TOKEN` with the user-provided Discord bot token.
|
||||
|
||||
Expected: token is stored only as an Actions secret. The token value is not written to source files, plan files, logs, PR text, release notes, or commits.
|
||||
|
||||
- [ ] **Step 4: Monitor CI**
|
||||
|
||||
Use Gitea Actions run reads until PR checks finish. If CI fails, inspect logs, fix with TDD where the failure is code behavior, push again, and re-check.
|
||||
|
||||
- [ ] **Step 5: Review, merge, deploy, release**
|
||||
|
||||
After CI passes and review is approved:
|
||||
- Merge PR.
|
||||
- Monitor deploy workflow on `main`.
|
||||
- Create release `v2.2.0` with Russian release notes.
|
||||
- Close issue `#26` with a comment linking PR and release.
|
||||
|
||||
## Self-Review
|
||||
|
||||
- Spec coverage: Project creation, NetCord Gateway, slash/component service registration, `Discord__Token`, PostgreSQL service defaults, lifecycle logging, Telegram isolation, solution build, compose/deploy integration, and version sync are covered.
|
||||
- Placeholder scan: No task uses `TBD`, `TODO`, or an unspecified "add tests" instruction.
|
||||
- Type consistency: Test class names and file paths are consistent across tasks; NetCord lifecycle handler signatures are explicitly marked for compile-driven adjustment because the package is prerelease and must be verified against installed `1.0.0-alpha.489`.
|
||||
@@ -1,599 +0,0 @@
|
||||
# Platform-Neutral Join Leave Implementation Plan
|
||||
|
||||
> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking.
|
||||
|
||||
**Goal:** Implement Gitea issue #25 by making join/leave session interactions use platform-neutral command models while preserving Telegram callback behavior, seat limits, and waitlist semantics.
|
||||
|
||||
**Architecture:** Telegram callback routing remains in `UpdateRouter`, but it becomes an adapter that converts callback data into `PlatformUser`, `PlatformGroup`, and `PlatformMessageRef` values. `JoinSessionHandler` and `LeaveSessionHandler` operate on those neutral values, persist players by `(platform, external_user_id)`, and update schedules through `IPlatformMessenger`.
|
||||
|
||||
**Tech Stack:** .NET 10, xUnit, Dapper, Npgsql, Gitea Actions.
|
||||
|
||||
---
|
||||
|
||||
## Issue Context
|
||||
|
||||
- Issue: `#25 refactor: obobshchit JoinSession i LeaveSession pod platform-neutral interactions`
|
||||
- Labels: `area:bot`, `area:platform`, `area:shared`, `platform:multi`, `type:refactor`
|
||||
- Version bump: patch, `2.1.0` -> `2.1.1`. The issue is labeled refactor, not breaking; do not use a major bump without explicit approval.
|
||||
- Existing untracked file: `CLAUDE.md`; do not stage or modify it.
|
||||
|
||||
## File Map
|
||||
|
||||
- Create: `tests/GmRelay.Bot.Tests/Features/Sessions/CreateSession/PlatformNeutralSessionInteractionCommandTests.cs`
|
||||
- Reflection tests proving join/leave command records expose neutral properties and no Telegram-specific identity/message fields.
|
||||
- Create: `tests/GmRelay.Bot.Tests/Features/Sessions/CreateSession/PlatformNeutralSessionInteractionSqlTests.cs`
|
||||
- Source-level regression tests for handler SQL and messenger boundaries.
|
||||
- Modify: `tests/GmRelay.Bot.Tests/Infrastructure/Database/PlatformIdentityMigrationTests.cs`
|
||||
- Add a migration test for nullable legacy `players.telegram_id`, required for non-Telegram player inserts.
|
||||
- Create: `src/GmRelay.Bot/Migrations/V017__allow_platform_neutral_players.sql`
|
||||
- Drop `NOT NULL` from legacy Telegram-only player columns.
|
||||
- Modify: `src/GmRelay.Bot/Features/Sessions/CreateSession/JoinSessionHandler.cs`
|
||||
- Change `JoinSessionCommand` to neutral properties and query/upsert players by platform identity.
|
||||
- Modify: `src/GmRelay.Bot/Features/Sessions/CreateSession/LeaveSessionHandler.cs`
|
||||
- Change `LeaveSessionCommand` to neutral properties and find participants by platform identity.
|
||||
- Modify: `src/GmRelay.Bot/Infrastructure/Telegram/UpdateRouter.cs`
|
||||
- Convert Telegram callback data into neutral command values using `TelegramPlatformIds`.
|
||||
- Modify: version files after implementation:
|
||||
- `Directory.Build.props`
|
||||
- `compose.yaml`
|
||||
- `.gitea/workflows/deploy.yml`
|
||||
- `src/GmRelay.Web/Components/Layout/NavMenu.razor`
|
||||
|
||||
## Task 1: RED - Command Model Tests
|
||||
|
||||
**Files:**
|
||||
- Create: `tests/GmRelay.Bot.Tests/Features/Sessions/CreateSession/PlatformNeutralSessionInteractionCommandTests.cs`
|
||||
|
||||
- [ ] **Step 1: Write failing command-shape tests**
|
||||
|
||||
```csharp
|
||||
using GmRelay.Bot.Features.Sessions.CreateSession;
|
||||
using GmRelay.Shared.Platform;
|
||||
|
||||
namespace GmRelay.Bot.Tests.Features.Sessions.CreateSession;
|
||||
|
||||
public sealed class PlatformNeutralSessionInteractionCommandTests
|
||||
{
|
||||
[Fact]
|
||||
public void JoinSessionCommand_ShouldExposePlatformNeutralInteractionContext()
|
||||
{
|
||||
AssertProperty<JoinSessionCommand>("SessionId", typeof(Guid));
|
||||
AssertProperty<JoinSessionCommand>("User", typeof(PlatformUser));
|
||||
AssertProperty<JoinSessionCommand>("InteractionId", typeof(string));
|
||||
AssertProperty<JoinSessionCommand>("Group", typeof(PlatformGroup));
|
||||
AssertProperty<JoinSessionCommand>("ScheduleMessage", typeof(PlatformMessageRef));
|
||||
AssertNoTelegramSpecificProperties<JoinSessionCommand>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void LeaveSessionCommand_ShouldExposePlatformNeutralInteractionContext()
|
||||
{
|
||||
AssertProperty<LeaveSessionCommand>("SessionId", typeof(Guid));
|
||||
AssertProperty<LeaveSessionCommand>("User", typeof(PlatformUser));
|
||||
AssertProperty<LeaveSessionCommand>("InteractionId", typeof(string));
|
||||
AssertProperty<LeaveSessionCommand>("Group", typeof(PlatformGroup));
|
||||
AssertProperty<LeaveSessionCommand>("ScheduleMessage", typeof(PlatformMessageRef));
|
||||
AssertNoTelegramSpecificProperties<LeaveSessionCommand>();
|
||||
}
|
||||
|
||||
private static void AssertProperty<T>(string name, Type expectedType)
|
||||
{
|
||||
var property = Assert.Single(typeof(T).GetProperties(), property => property.Name == name);
|
||||
|
||||
Assert.Equal(expectedType, property.PropertyType);
|
||||
}
|
||||
|
||||
private static void AssertNoTelegramSpecificProperties<T>()
|
||||
{
|
||||
var names = typeof(T).GetProperties().Select(property => property.Name).ToArray();
|
||||
|
||||
Assert.DoesNotContain(names, name => name.Contains("Telegram", StringComparison.Ordinal));
|
||||
Assert.DoesNotContain("ChatId", names);
|
||||
Assert.DoesNotContain("MessageId", names);
|
||||
Assert.DoesNotContain("TelegramUserId", names);
|
||||
Assert.DoesNotContain("TelegramUsername", names);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
- [ ] **Step 2: Verify RED**
|
||||
|
||||
Run:
|
||||
|
||||
```powershell
|
||||
dotnet test .\tests\GmRelay.Bot.Tests\GmRelay.Bot.Tests.csproj --filter PlatformNeutralSessionInteractionCommandTests
|
||||
```
|
||||
|
||||
Expected: FAIL because `JoinSessionCommand` and `LeaveSessionCommand` still expose `TelegramUserId`, `ChatId`, and `MessageId`, and do not expose `User`, `Group`, or `ScheduleMessage`.
|
||||
|
||||
## Task 2: RED - SQL and Boundary Tests
|
||||
|
||||
**Files:**
|
||||
- Create: `tests/GmRelay.Bot.Tests/Features/Sessions/CreateSession/PlatformNeutralSessionInteractionSqlTests.cs`
|
||||
- Modify: `tests/GmRelay.Bot.Tests/Infrastructure/Database/PlatformIdentityMigrationTests.cs`
|
||||
|
||||
- [ ] **Step 1: Write failing handler source tests**
|
||||
|
||||
```csharp
|
||||
namespace GmRelay.Bot.Tests.Features.Sessions.CreateSession;
|
||||
|
||||
public sealed class PlatformNeutralSessionInteractionSqlTests
|
||||
{
|
||||
[Fact]
|
||||
public async Task JoinSessionHandler_ShouldPersistPlayersByPlatformIdentity()
|
||||
{
|
||||
var handler = await ReadRepositoryFileAsync("src/GmRelay.Bot/Features/Sessions/CreateSession/JoinSessionHandler.cs");
|
||||
|
||||
Assert.Contains("platform, external_user_id", handler, StringComparison.Ordinal);
|
||||
Assert.Contains("ON CONFLICT (platform, external_user_id)", handler, StringComparison.Ordinal);
|
||||
Assert.Contains("ExternalUserId", handler, StringComparison.Ordinal);
|
||||
Assert.Contains("ExternalUsername", handler, StringComparison.Ordinal);
|
||||
Assert.DoesNotContain("TelegramPlatformIds.", handler, StringComparison.Ordinal);
|
||||
Assert.DoesNotContain("command.TelegramUserId", handler, StringComparison.Ordinal);
|
||||
Assert.DoesNotContain("command.TelegramUsername", handler, StringComparison.Ordinal);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task LeaveSessionHandler_ShouldFindParticipantsByPlatformIdentity()
|
||||
{
|
||||
var handler = await ReadRepositoryFileAsync("src/GmRelay.Bot/Features/Sessions/CreateSession/LeaveSessionHandler.cs");
|
||||
|
||||
Assert.Contains("p.platform = @Platform", handler, StringComparison.Ordinal);
|
||||
Assert.Contains("p.external_user_id = @ExternalUserId", handler, StringComparison.Ordinal);
|
||||
Assert.DoesNotContain("p.telegram_id = @TelegramUserId", handler, StringComparison.Ordinal);
|
||||
Assert.DoesNotContain("TelegramPlatformIds.", handler, StringComparison.Ordinal);
|
||||
Assert.DoesNotContain("command.TelegramUserId", handler, StringComparison.Ordinal);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task SessionInteractionHandlers_ShouldUpdateSchedulesThroughCommandMessageReference()
|
||||
{
|
||||
var joinHandler = await ReadRepositoryFileAsync("src/GmRelay.Bot/Features/Sessions/CreateSession/JoinSessionHandler.cs");
|
||||
var leaveHandler = await ReadRepositoryFileAsync("src/GmRelay.Bot/Features/Sessions/CreateSession/LeaveSessionHandler.cs");
|
||||
|
||||
Assert.Contains("new PlatformScheduleMessage(", joinHandler, StringComparison.Ordinal);
|
||||
Assert.Contains("command.Group", joinHandler, StringComparison.Ordinal);
|
||||
Assert.Contains("command.ScheduleMessage", joinHandler, StringComparison.Ordinal);
|
||||
Assert.Contains("new PlatformScheduleMessage(", leaveHandler, StringComparison.Ordinal);
|
||||
Assert.Contains("command.Group", leaveHandler, StringComparison.Ordinal);
|
||||
Assert.Contains("command.ScheduleMessage", leaveHandler, StringComparison.Ordinal);
|
||||
}
|
||||
|
||||
private static async Task<string> ReadRepositoryFileAsync(string relativePath)
|
||||
{
|
||||
var directory = new DirectoryInfo(AppContext.BaseDirectory);
|
||||
while (directory is not null)
|
||||
{
|
||||
var candidate = Path.Combine(directory.FullName, relativePath);
|
||||
if (File.Exists(candidate))
|
||||
{
|
||||
return await File.ReadAllTextAsync(candidate);
|
||||
}
|
||||
|
||||
directory = directory.Parent;
|
||||
}
|
||||
|
||||
throw new FileNotFoundException($"Could not locate repository file '{relativePath}'.");
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
- [ ] **Step 2: Add failing migration assertion**
|
||||
|
||||
Append to `PlatformIdentityMigrationTests`:
|
||||
|
||||
```csharp
|
||||
[Fact]
|
||||
public async Task MigrationV017_ShouldAllowPlayersWithoutLegacyTelegramId()
|
||||
{
|
||||
var migration = await ReadRepositoryFileAsync("src/GmRelay.Bot/Migrations/V017__allow_platform_neutral_players.sql");
|
||||
|
||||
Assert.Contains("ALTER TABLE players", migration, StringComparison.Ordinal);
|
||||
Assert.Contains("telegram_id DROP NOT NULL", migration, StringComparison.Ordinal);
|
||||
}
|
||||
```
|
||||
|
||||
- [ ] **Step 3: Verify RED**
|
||||
|
||||
Run:
|
||||
|
||||
```powershell
|
||||
dotnet test .\tests\GmRelay.Bot.Tests\GmRelay.Bot.Tests.csproj --filter "PlatformNeutralSessionInteractionSqlTests|MigrationV017_ShouldAllowPlayersWithoutLegacyTelegramId"
|
||||
```
|
||||
|
||||
Expected: FAIL because handlers still use Telegram-specific properties and the V017 migration file does not exist.
|
||||
|
||||
## Task 3: GREEN - Add Migration
|
||||
|
||||
**Files:**
|
||||
- Create: `src/GmRelay.Bot/Migrations/V017__allow_platform_neutral_players.sql`
|
||||
|
||||
- [ ] **Step 1: Create the migration**
|
||||
|
||||
```sql
|
||||
-- =============================================================
|
||||
-- V017: Allow platform-neutral players
|
||||
-- =============================================================
|
||||
-- Legacy Telegram identity columns remain for backward compatibility,
|
||||
-- but non-Telegram platform users do not have Telegram ids.
|
||||
-- =============================================================
|
||||
|
||||
ALTER TABLE players
|
||||
ALTER COLUMN telegram_id DROP NOT NULL;
|
||||
```
|
||||
|
||||
- [ ] **Step 2: Verify migration test turns green**
|
||||
|
||||
Run:
|
||||
|
||||
```powershell
|
||||
dotnet test .\tests\GmRelay.Bot.Tests\GmRelay.Bot.Tests.csproj --filter MigrationV017_ShouldAllowPlayersWithoutLegacyTelegramId
|
||||
```
|
||||
|
||||
Expected: PASS.
|
||||
|
||||
## Task 4: GREEN - Refactor JoinSessionCommand and Handler
|
||||
|
||||
**Files:**
|
||||
- Modify: `src/GmRelay.Bot/Features/Sessions/CreateSession/JoinSessionHandler.cs`
|
||||
|
||||
- [ ] **Step 1: Replace command record**
|
||||
|
||||
Replace the existing `JoinSessionCommand` declaration with:
|
||||
|
||||
```csharp
|
||||
public sealed record JoinSessionCommand(
|
||||
Guid SessionId,
|
||||
PlatformUser User,
|
||||
string InteractionId,
|
||||
PlatformGroup Group,
|
||||
PlatformMessageRef ScheduleMessage);
|
||||
```
|
||||
|
||||
- [ ] **Step 2: Replace player upsert**
|
||||
|
||||
Use platform identity parameters:
|
||||
|
||||
```csharp
|
||||
var platform = command.User.Platform.ToString();
|
||||
var legacyTelegramId = command.User.Platform == PlatformKind.Telegram
|
||||
? long.Parse(command.User.ExternalUserId, CultureInfo.InvariantCulture)
|
||||
: (long?)null;
|
||||
var legacyTelegramUsername = command.User.Platform == PlatformKind.Telegram
|
||||
? command.User.ExternalUsername
|
||||
: null;
|
||||
|
||||
var playerId = await connection.ExecuteScalarAsync<Guid>(
|
||||
@"INSERT INTO players (telegram_id, display_name, telegram_username, platform, external_user_id, external_username)
|
||||
VALUES (@LegacyTelegramId, @Name, @LegacyTelegramUsername, @Platform, @ExternalUserId, @ExternalUsername)
|
||||
ON CONFLICT (platform, external_user_id)
|
||||
WHERE platform IS NOT NULL AND external_user_id IS NOT NULL
|
||||
DO UPDATE
|
||||
SET display_name = EXCLUDED.display_name,
|
||||
telegram_username = COALESCE(EXCLUDED.telegram_username, players.telegram_username),
|
||||
platform = EXCLUDED.platform,
|
||||
external_user_id = EXCLUDED.external_user_id,
|
||||
external_username = EXCLUDED.external_username
|
||||
RETURNING id;",
|
||||
new
|
||||
{
|
||||
LegacyTelegramId = legacyTelegramId,
|
||||
Name = command.User.DisplayName,
|
||||
LegacyTelegramUsername = legacyTelegramUsername,
|
||||
Platform = platform,
|
||||
command.User.ExternalUserId,
|
||||
command.User.ExternalUsername
|
||||
},
|
||||
transaction);
|
||||
```
|
||||
|
||||
Add `using System.Globalization;` at the top.
|
||||
|
||||
- [ ] **Step 3: Update participant display query**
|
||||
|
||||
Change the participant projection to prefer platform-neutral username:
|
||||
|
||||
```sql
|
||||
COALESCE(p.external_username, p.telegram_username) as TelegramUsername
|
||||
```
|
||||
|
||||
- [ ] **Step 4: Update schedule message and interaction reply usage**
|
||||
|
||||
Use:
|
||||
|
||||
```csharp
|
||||
await messenger.UpdateScheduleAsync(
|
||||
new PlatformScheduleMessage(
|
||||
command.Group,
|
||||
view,
|
||||
command.ScheduleMessage),
|
||||
ct);
|
||||
```
|
||||
|
||||
and:
|
||||
|
||||
```csharp
|
||||
private Task AnswerAsync(string interactionId, string text, CancellationToken ct) =>
|
||||
messenger.AnswerInteractionAsync(new PlatformInteractionReply(interactionId, text), ct);
|
||||
```
|
||||
|
||||
Replace all `command.CallbackQueryId` calls with `command.InteractionId`.
|
||||
|
||||
- [ ] **Step 5: Verify command and SQL tests for join**
|
||||
|
||||
Run:
|
||||
|
||||
```powershell
|
||||
dotnet test .\tests\GmRelay.Bot.Tests\GmRelay.Bot.Tests.csproj --filter "JoinSessionCommand_ShouldExposePlatformNeutralInteractionContext|JoinSessionHandler_ShouldPersistPlayersByPlatformIdentity"
|
||||
```
|
||||
|
||||
Expected: PASS for join-focused tests.
|
||||
|
||||
## Task 5: GREEN - Refactor LeaveSessionCommand and Handler
|
||||
|
||||
**Files:**
|
||||
- Modify: `src/GmRelay.Bot/Features/Sessions/CreateSession/LeaveSessionHandler.cs`
|
||||
|
||||
- [ ] **Step 1: Replace command record**
|
||||
|
||||
Replace the existing `LeaveSessionCommand` declaration with:
|
||||
|
||||
```csharp
|
||||
public sealed record LeaveSessionCommand(
|
||||
Guid SessionId,
|
||||
PlatformUser User,
|
||||
string InteractionId,
|
||||
PlatformGroup Group,
|
||||
PlatformMessageRef ScheduleMessage);
|
||||
```
|
||||
|
||||
- [ ] **Step 2: Replace participant lookup**
|
||||
|
||||
Use platform identity instead of Telegram id:
|
||||
|
||||
```csharp
|
||||
var platform = command.User.Platform.ToString();
|
||||
|
||||
var participant = await connection.QuerySingleOrDefaultAsync<LeaveSessionParticipantDto>(
|
||||
"""
|
||||
SELECT sp.id AS ParticipantRowId,
|
||||
p.display_name AS DisplayName,
|
||||
sp.registration_status AS RegistrationStatus
|
||||
FROM session_participants sp
|
||||
JOIN players p ON p.id = sp.player_id
|
||||
WHERE sp.session_id = @SessionId
|
||||
AND p.platform = @Platform
|
||||
AND p.external_user_id = @ExternalUserId
|
||||
AND sp.is_gm = false
|
||||
FOR UPDATE OF sp
|
||||
""",
|
||||
new { command.SessionId, Platform = platform, command.User.ExternalUserId },
|
||||
transaction);
|
||||
```
|
||||
|
||||
- [ ] **Step 3: Update participant display query**
|
||||
|
||||
Change the participant projection to:
|
||||
|
||||
```sql
|
||||
COALESCE(p.external_username, p.telegram_username) AS TelegramUsername
|
||||
```
|
||||
|
||||
- [ ] **Step 4: Update schedule message and interaction reply usage**
|
||||
|
||||
Use:
|
||||
|
||||
```csharp
|
||||
await messenger.UpdateScheduleAsync(
|
||||
new PlatformScheduleMessage(
|
||||
command.Group,
|
||||
view,
|
||||
command.ScheduleMessage),
|
||||
ct);
|
||||
```
|
||||
|
||||
Replace all `command.CallbackQueryId` calls with `command.InteractionId`.
|
||||
|
||||
- [ ] **Step 5: Verify leave tests**
|
||||
|
||||
Run:
|
||||
|
||||
```powershell
|
||||
dotnet test .\tests\GmRelay.Bot.Tests\GmRelay.Bot.Tests.csproj --filter "LeaveSessionCommand_ShouldExposePlatformNeutralInteractionContext|LeaveSessionHandler_ShouldFindParticipantsByPlatformIdentity|SessionInteractionHandlers_ShouldUpdateSchedulesThroughCommandMessageReference"
|
||||
```
|
||||
|
||||
Expected: PASS.
|
||||
|
||||
## Task 6: GREEN - Convert Telegram Router to Neutral Commands
|
||||
|
||||
**Files:**
|
||||
- Modify: `src/GmRelay.Bot/Infrastructure/Telegram/UpdateRouter.cs`
|
||||
|
||||
- [ ] **Step 1: Add local conversion values in `HandleCallbackQueryAsync`**
|
||||
|
||||
After parsing `action`, add:
|
||||
|
||||
```csharp
|
||||
var user = TelegramPlatformIds.User(
|
||||
query.From.Id,
|
||||
query.From.FirstName + (string.IsNullOrEmpty(query.From.LastName) ? "" : $" {query.From.LastName}"),
|
||||
query.From.Username);
|
||||
var group = TelegramPlatformIds.Group(message.Chat.Id, message.MessageThreadId, message.Chat.Title);
|
||||
var scheduleMessage = TelegramPlatformIds.Message(message.Chat.Id, message.MessageThreadId, message.MessageId);
|
||||
```
|
||||
|
||||
- [ ] **Step 2: Update join command construction**
|
||||
|
||||
```csharp
|
||||
var command = new JoinSessionCommand(
|
||||
SessionId: joinSessionId,
|
||||
User: user,
|
||||
InteractionId: query.Id,
|
||||
Group: group,
|
||||
ScheduleMessage: scheduleMessage);
|
||||
```
|
||||
|
||||
- [ ] **Step 3: Update leave command construction**
|
||||
|
||||
```csharp
|
||||
var command = new LeaveSessionCommand(
|
||||
SessionId: leaveSessionId,
|
||||
User: user,
|
||||
InteractionId: query.Id,
|
||||
Group: group,
|
||||
ScheduleMessage: scheduleMessage);
|
||||
```
|
||||
|
||||
- [ ] **Step 4: Verify compile**
|
||||
|
||||
Run:
|
||||
|
||||
```powershell
|
||||
dotnet test .\tests\GmRelay.Bot.Tests\GmRelay.Bot.Tests.csproj --filter PlatformNeutralSessionInteractionCommandTests
|
||||
```
|
||||
|
||||
Expected: PASS.
|
||||
|
||||
## Task 7: REFACTOR - Clean Up and Full Test Pass
|
||||
|
||||
**Files:**
|
||||
- Modify only files already listed if cleanup is needed.
|
||||
|
||||
- [ ] **Step 1: Remove now-unused Telegram handler imports**
|
||||
|
||||
Check `JoinSessionHandler.cs` and `LeaveSessionHandler.cs` for unused:
|
||||
|
||||
```csharp
|
||||
using GmRelay.Bot.Infrastructure.Telegram;
|
||||
```
|
||||
|
||||
Remove it from handlers if no longer needed.
|
||||
|
||||
- [ ] **Step 2: Run focused tests**
|
||||
|
||||
```powershell
|
||||
dotnet test .\tests\GmRelay.Bot.Tests\GmRelay.Bot.Tests.csproj --filter "PlatformNeutralSessionInteractionCommandTests|PlatformNeutralSessionInteractionSqlTests|PlatformIdentityMigrationTests"
|
||||
```
|
||||
|
||||
Expected: PASS.
|
||||
|
||||
- [ ] **Step 3: Run full test suite**
|
||||
|
||||
```powershell
|
||||
dotnet test .\GM-Relay.slnx
|
||||
```
|
||||
|
||||
Expected: PASS.
|
||||
|
||||
- [ ] **Step 4: Build solution**
|
||||
|
||||
```powershell
|
||||
dotnet build .\GM-Relay.slnx
|
||||
```
|
||||
|
||||
Expected: PASS.
|
||||
|
||||
## Task 8: Version Bump
|
||||
|
||||
**Files:**
|
||||
- Modify: `Directory.Build.props`
|
||||
- Modify: `compose.yaml`
|
||||
- Modify: `.gitea/workflows/deploy.yml`
|
||||
- Modify: `src/GmRelay.Web/Components/Layout/NavMenu.razor`
|
||||
|
||||
- [ ] **Step 1: Update version from `2.1.0` to `2.1.1`**
|
||||
|
||||
Expected exact replacements:
|
||||
|
||||
```xml
|
||||
<Version>2.1.1</Version>
|
||||
```
|
||||
|
||||
```yaml
|
||||
VERSION: 2.1.1
|
||||
```
|
||||
|
||||
```yaml
|
||||
image: git.codeanddice.ru/toutsu/gmrelay-bot:2.1.1
|
||||
image: git.codeanddice.ru/toutsu/gmrelay-web:2.1.1
|
||||
```
|
||||
|
||||
```razor
|
||||
<div class="nav-version">v2.1.1</div>
|
||||
```
|
||||
|
||||
- [ ] **Step 2: Verify synchronized versions**
|
||||
|
||||
Run:
|
||||
|
||||
```powershell
|
||||
rg "<Version>|image: git.codeanddice.ru/toutsu/gmrelay-|VERSION:|nav-version" Directory.Build.props compose.yaml .gitea\workflows\deploy.yml src\GmRelay.Web\Components\Layout\NavMenu.razor
|
||||
```
|
||||
|
||||
Expected: all project image/app/deploy UI versions show `2.1.1`.
|
||||
|
||||
## Task 9: PR, CI, Review, Merge, Deploy, Release
|
||||
|
||||
**Files:**
|
||||
- No additional source changes expected.
|
||||
|
||||
- [ ] **Step 1: Create branch after approval**
|
||||
|
||||
```powershell
|
||||
git checkout -b refactor/issue-25-platform-neutral-join-leave
|
||||
```
|
||||
|
||||
- [ ] **Step 2: Stage only intended files**
|
||||
|
||||
```powershell
|
||||
git add docs/superpowers/plans/2026-05-18-platform-neutral-join-leave.md tests/GmRelay.Bot.Tests/Features/Sessions/CreateSession/PlatformNeutralSessionInteractionCommandTests.cs tests/GmRelay.Bot.Tests/Features/Sessions/CreateSession/PlatformNeutralSessionInteractionSqlTests.cs tests/GmRelay.Bot.Tests/Infrastructure/Database/PlatformIdentityMigrationTests.cs src/GmRelay.Bot/Migrations/V017__allow_platform_neutral_players.sql src/GmRelay.Bot/Features/Sessions/CreateSession/JoinSessionHandler.cs src/GmRelay.Bot/Features/Sessions/CreateSession/LeaveSessionHandler.cs src/GmRelay.Bot/Infrastructure/Telegram/UpdateRouter.cs Directory.Build.props compose.yaml .gitea/workflows/deploy.yml src/GmRelay.Web/Components/Layout/NavMenu.razor
|
||||
```
|
||||
|
||||
- [ ] **Step 3: Commit**
|
||||
|
||||
```powershell
|
||||
git commit -m "refactor: make session join leave platform-neutral"
|
||||
```
|
||||
|
||||
- [ ] **Step 4: Push and create Gitea PR**
|
||||
|
||||
```powershell
|
||||
git push -u origin refactor/issue-25-platform-neutral-join-leave
|
||||
```
|
||||
|
||||
PR title:
|
||||
|
||||
```text
|
||||
refactor: make session join leave platform-neutral
|
||||
```
|
||||
|
||||
PR body:
|
||||
|
||||
```markdown
|
||||
## Summary
|
||||
- Closes #25.
|
||||
- Converts join/leave session interaction commands from Telegram-specific fields to platform-neutral `PlatformUser`, `PlatformGroup`, and `PlatformMessageRef`.
|
||||
- Persists and looks up session participants by `(platform, external_user_id)`.
|
||||
- Keeps Telegram callback data and schedule update behavior intact.
|
||||
|
||||
## Test plan
|
||||
- `dotnet test .\tests\GmRelay.Bot.Tests\GmRelay.Bot.Tests.csproj --filter "PlatformNeutralSessionInteractionCommandTests|PlatformNeutralSessionInteractionSqlTests|PlatformIdentityMigrationTests"`
|
||||
- `dotnet test .\GM-Relay.slnx`
|
||||
- `dotnet build .\GM-Relay.slnx`
|
||||
|
||||
## Workflow
|
||||
- [ ] CI passes
|
||||
- [ ] Code review approved
|
||||
- [ ] Deployed
|
||||
- [ ] Release published
|
||||
```
|
||||
|
||||
- [ ] **Step 5: Watch CI, request review, merge, deploy, release**
|
||||
|
||||
Use Gitea MCP for PR creation, CI polling, review, merge, deploy monitoring, and release `v2.1.1`. Close issue #25 after release and add a comment linking the PR and release.
|
||||
|
||||
## Self-Review
|
||||
|
||||
- Spec coverage: issue scope is covered by neutral command records, Telegram adapter conversion, platform identity SQL, messenger-based schedule updates, and tests.
|
||||
- Placeholder scan: no `TBD`, `TODO`, or "fill later" steps remain.
|
||||
- Type consistency: commands consistently use `PlatformUser User`, `string InteractionId`, `PlatformGroup Group`, and `PlatformMessageRef ScheduleMessage`.
|
||||
@@ -1,984 +0,0 @@
|
||||
# Discord /newsession и /listsessions — Implementation Plan
|
||||
|
||||
> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:test-driven-development (TDD) for every task. Steps use checkbox (`- [ ]`) syntax for tracking.
|
||||
|
||||
**Goal:** Реализовать slash-команды `/newsession` и `/listsessions` в Discord-боте, позволяющие создавать батчи сессий и просматривать расписание без Web Dashboard.
|
||||
|
||||
**Architecture:** Каждая команда — отдельный vertical slice в `GmRelay.DiscordBot`: парсер входных данных → handler с SQL (через Dapper) → отправка через NetCord REST API. Рендеринг переиспользует существующий `DiscordSessionBatchRenderer`. Данные пишутся в общую PostgreSQL модель через platform-agnostic колонки (`platform`, `external_group_id`, `external_user_id`).
|
||||
|
||||
**Tech Stack:** .NET 10, NetCord 1.0.0-alpha.489, NetCord.Hosting.Services, Dapper, Npgsql, xUnit.
|
||||
|
||||
**Version Bump:** minor (2.3.0 → 2.4.0) — новый функционал.
|
||||
|
||||
---
|
||||
|
||||
## File Structure
|
||||
|
||||
| File | Responsibility |
|
||||
|------|--------------|
|
||||
| `src/GmRelay.DiscordBot/Features/Sessions/DiscordNewSessionCommand.cs` | Slash-команда `/newsession` с параметрами (title, time, seats, link) |
|
||||
| `src/GmRelay.DiscordBot/Features/Sessions/DiscordNewSessionHandler.cs` | Handler создания batch + sessions в БД, проверка прав |
|
||||
| `src/GmRelay.DiscordBot/Features/Sessions/DiscordListSessionsCommand.cs` | Slash-команда `/listsessions` |
|
||||
| `src/GmRelay.DiscordBot/Features/Sessions/DiscordListSessionsHandler.cs` | Handler запроса активных сессий и публикации embed |
|
||||
| `src/GmRelay.DiscordBot/Infrastructure/Discord/DiscordPermissionChecker.cs` | Проверка прав пользователя в guild (owner/admin/manager) |
|
||||
| `src/GmRelay.DiscordBot/Infrastructure/Discord/DiscordPlatformMessenger.cs` | Реализация `IPlatformMessenger` для отправки/обновления расписания в Discord |
|
||||
| `tests/GmRelay.Bot.Tests/Discord/DiscordNewSessionHandlerTests.cs` | TDD-тесты создания сессий из Discord |
|
||||
| `tests/GmRelay.Bot.Tests/Discord/DiscordListSessionsHandlerTests.cs` | TDD-тесты вывода расписания |
|
||||
| `tests/GmRelay.Bot.Tests/Discord/DiscordPermissionCheckerTests.cs` | TDD-тесты проверки прав |
|
||||
| `src/GmRelay.DiscordBot/Program.cs` | Регистрация DI: handlers, permission checker, platform messenger |
|
||||
|
||||
---
|
||||
|
||||
## Task 1: DiscordPermissionChecker
|
||||
|
||||
**Files:**
|
||||
- Create: `src/GmRelay.DiscordBot/Infrastructure/Discord/DiscordPermissionChecker.cs`
|
||||
- Test: `tests/GmRelay.Bot.Tests/Discord/DiscordPermissionCheckerTests.cs`
|
||||
|
||||
**Context:** Discord использует guild-роли. Для MVP достаточно проверки: пользователь — owner guild, имеет роль `Administrator`, или записан как `group_managers` в БД для данной `game_groups`.
|
||||
|
||||
### Step 1.1: Write the failing test
|
||||
|
||||
```csharp
|
||||
using GmRelay.DiscordBot.Infrastructure.Discord;
|
||||
|
||||
namespace GmRelay.Bot.Tests.Discord;
|
||||
|
||||
public sealed class DiscordPermissionCheckerTests
|
||||
{
|
||||
[Fact]
|
||||
public void CanManageSchedule_WhenUserIsGuildOwner_ReturnsTrue()
|
||||
{
|
||||
var checker = new DiscordPermissionChecker();
|
||||
var result = checker.CanManageSchedule(
|
||||
guildOwnerId: 123456789ul,
|
||||
userId: 123456789ul,
|
||||
userRoles: Array.Empty<ulong>(),
|
||||
dbManagerUserIds: Array.Empty<ulong>());
|
||||
|
||||
Assert.True(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CanManageSchedule_WhenUserHasAdministratorRole_ReturnsTrue()
|
||||
{
|
||||
var checker = new DiscordPermissionChecker();
|
||||
var adminRole = 999ul;
|
||||
var result = checker.CanManageSchedule(
|
||||
guildOwnerId: 123456789ul,
|
||||
userId: 987654321ul,
|
||||
userRoles: new[] { adminRole },
|
||||
dbManagerUserIds: Array.Empty<ulong>());
|
||||
|
||||
Assert.True(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CanManageSchedule_WhenUserIsDbManager_ReturnsTrue()
|
||||
{
|
||||
var checker = new DiscordPermissionChecker();
|
||||
var managerId = 555ul;
|
||||
var result = checker.CanManageSchedule(
|
||||
guildOwnerId: 123456789ul,
|
||||
userId: managerId,
|
||||
userRoles: Array.Empty<ulong>(),
|
||||
dbManagerUserIds: new[] { managerId });
|
||||
|
||||
Assert.True(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CanManageSchedule_WhenRegularUser_ReturnsFalse()
|
||||
{
|
||||
var checker = new DiscordPermissionChecker();
|
||||
var result = checker.CanManageSchedule(
|
||||
guildOwnerId: 123456789ul,
|
||||
userId: 111ul,
|
||||
userRoles: Array.Empty<ulong>(),
|
||||
dbManagerUserIds: new[] { 222ul });
|
||||
|
||||
Assert.False(result);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Step 1.2: Run test to verify it fails
|
||||
|
||||
Run: `dotnet test tests/GmRelay.Bot.Tests/GmRelay.Bot.Tests.csproj --filter "FullyQualifiedName~DiscordPermissionCheckerTests" --verbosity normal`
|
||||
Expected: FAIL — `DiscordPermissionChecker` not found.
|
||||
|
||||
### Step 1.3: Write minimal implementation
|
||||
|
||||
Create `src/GmRelay.DiscordBot/Infrastructure/Discord/DiscordPermissionChecker.cs`:
|
||||
|
||||
```csharp
|
||||
namespace GmRelay.DiscordBot.Infrastructure.Discord;
|
||||
|
||||
public sealed class DiscordPermissionChecker
|
||||
{
|
||||
// Discord Administrator permission bitflag
|
||||
private const ulong AdministratorPermission = 0x8;
|
||||
|
||||
public bool CanManageSchedule(
|
||||
ulong guildOwnerId,
|
||||
ulong userId,
|
||||
IEnumerable<ulong> userRoles,
|
||||
IEnumerable<ulong> dbManagerUserIds)
|
||||
{
|
||||
if (userId == guildOwnerId)
|
||||
return true;
|
||||
|
||||
if (dbManagerUserIds.Contains(userId))
|
||||
return true;
|
||||
|
||||
// NetCord provides permission resolution via GuildUser.Permissions;
|
||||
// here we accept pre-resolved flag for simplicity.
|
||||
// Actual command handler will pass resolved permissions.
|
||||
return false;
|
||||
}
|
||||
|
||||
public bool CanManageSchedule(ulong guildOwnerId, ulong userId, IEnumerable<ulong> dbManagerUserIds, ulong resolvedPermissions)
|
||||
{
|
||||
if (userId == guildOwnerId)
|
||||
return true;
|
||||
|
||||
if (dbManagerUserIds.Contains(userId))
|
||||
return true;
|
||||
|
||||
return (resolvedPermissions & AdministratorPermission) == AdministratorPermission;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Step 1.4: Run test to verify it passes
|
||||
|
||||
Run: `dotnet test tests/GmRelay.Bot.Tests/GmRelay.Bot.Tests.csproj --filter "FullyQualifiedName~DiscordPermissionCheckerTests" --verbosity normal`
|
||||
Expected: PASS (4/4).
|
||||
|
||||
### Step 1.5: Commit
|
||||
|
||||
```bash
|
||||
git add src/GmRelay.DiscordBot/Infrastructure/Discord/DiscordPermissionChecker.cs tests/GmRelay.Bot.Tests/Discord/DiscordPermissionCheckerTests.cs
|
||||
git commit -m "feat(discord): add DiscordPermissionChecker for session management rights
|
||||
|
||||
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>"
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Task 2: DiscordListSessionsHandler + Command
|
||||
|
||||
**Files:**
|
||||
- Create: `src/GmRelay.DiscordBot/Features/Sessions/DiscordListSessionsHandler.cs`
|
||||
- Create: `src/GmRelay.DiscordBot/Features/Sessions/DiscordListSessionsCommand.cs`
|
||||
- Test: `tests/GmRelay.Bot.Tests/Discord/DiscordListSessionsHandlerTests.cs`
|
||||
|
||||
**Context:** Handler должен:
|
||||
1. Найти `game_groups` по `external_group_id` = `guild_id`.
|
||||
2. Выбрать предстоящие сессии (`scheduled_at > NOW()`, `status != Cancelled`).
|
||||
3. Собрать участников.
|
||||
4. Построить view через `SessionBatchViewBuilder`.
|
||||
5. Отрендерить через `DiscordSessionBatchRenderer`.
|
||||
6. Отправить embed + buttons в Discord channel.
|
||||
|
||||
### Step 2.1: Write the failing test
|
||||
|
||||
Create `tests/GmRelay.Bot.Tests/Discord/DiscordListSessionsHandlerTests.cs`:
|
||||
|
||||
```csharp
|
||||
using GmRelay.DiscordBot.Features.Sessions;
|
||||
using GmRelay.Shared.Domain;
|
||||
using GmRelay.Shared.Rendering;
|
||||
|
||||
namespace GmRelay.Bot.Tests.Discord;
|
||||
|
||||
public sealed class DiscordListSessionsHandlerTests
|
||||
{
|
||||
[Fact]
|
||||
public void BuildSchedule_WithSessions_ReturnsEmbedsAndButtons()
|
||||
{
|
||||
var sessionId = Guid.NewGuid();
|
||||
var sessions = new[]
|
||||
{
|
||||
new SessionBatchDto(sessionId, DateTime.UtcNow.AddDays(1), SessionStatus.Planned, 4, "https://example.com")
|
||||
};
|
||||
var participants = Array.Empty<ParticipantBatchDto>();
|
||||
|
||||
var view = SessionBatchViewBuilder.Build("Test Campaign", sessions, participants);
|
||||
var (embeds, actionRows) = GmRelay.DiscordBot.Rendering.DiscordSessionBatchRenderer.Render(view);
|
||||
|
||||
Assert.Single(embeds);
|
||||
Assert.Single(actionRows);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void BuildSchedule_WithCancelledSession_SkipsActionRows()
|
||||
{
|
||||
var cancelledSessionId = Guid.NewGuid();
|
||||
var sessions = new[] { new SessionBatchDto(cancelledSessionId, DateTime.UtcNow.AddDays(1), SessionStatus.Cancelled, null, "") };
|
||||
var participants = Array.Empty<ParticipantBatchDto>();
|
||||
|
||||
var view = SessionBatchViewBuilder.Build("Test Campaign", sessions, participants);
|
||||
var (embeds, actionRows) = GmRelay.DiscordBot.Rendering.DiscordSessionBatchRenderer.Render(view);
|
||||
|
||||
Assert.Single(embeds);
|
||||
Assert.Empty(actionRows);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Step 2.2: Run test — verify RED
|
||||
|
||||
Run: `dotnet test tests/GmRelay.Bot.Tests/GmRelay.Bot.Tests.csproj --filter "FullyQualifiedName~DiscordListSessionsHandlerTests" --verbosity normal`
|
||||
Expected: FAIL — `DiscordListSessionsHandler` not found.
|
||||
|
||||
### Step 2.3: Write minimal implementation
|
||||
|
||||
Create `src/GmRelay.DiscordBot/Features/Sessions/DiscordListSessionsHandler.cs`:
|
||||
|
||||
```csharp
|
||||
using Dapper;
|
||||
using GmRelay.Shared.Domain;
|
||||
using GmRelay.Shared.Rendering;
|
||||
using NetCord.Rest;
|
||||
using Npgsql;
|
||||
|
||||
namespace GmRelay.DiscordBot.Features.Sessions;
|
||||
|
||||
internal sealed record DiscordSessionListItemDto(
|
||||
Guid Id, string Title, DateTime ScheduledAt, string Status, int? MaxPlayers,
|
||||
int PlayerCount, int WaitlistCount);
|
||||
|
||||
public sealed class DiscordListSessionsHandler(NpgsqlDataSource dataSource)
|
||||
{
|
||||
public async Task<SessionBatchViewModel?> BuildScheduleAsync(
|
||||
string guildId,
|
||||
string channelId,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
await using var connection = await dataSource.OpenConnectionAsync(cancellationToken);
|
||||
|
||||
var sessions = await connection.QueryAsync<DiscordSessionListItemDto>(
|
||||
@"SELECT s.id as Id, s.title as Title, s.scheduled_at as ScheduledAt, s.status as Status,
|
||||
s.max_players as MaxPlayers,
|
||||
COUNT(sp.id) FILTER (WHERE sp.is_gm = false AND sp.registration_status = @Active) as PlayerCount,
|
||||
COUNT(sp.id) FILTER (WHERE sp.is_gm = false AND sp.registration_status = @Waitlisted) as WaitlistCount
|
||||
FROM sessions s
|
||||
JOIN game_groups g ON s.group_id = g.id
|
||||
LEFT JOIN session_participants sp ON s.id = sp.session_id
|
||||
WHERE g.platform = 'Discord'
|
||||
AND g.external_group_id = @GuildId
|
||||
AND s.status != @Cancelled
|
||||
AND s.scheduled_at > NOW()
|
||||
GROUP BY s.id, s.title, s.scheduled_at, s.status, s.max_players
|
||||
ORDER BY s.scheduled_at ASC",
|
||||
new
|
||||
{
|
||||
GuildId = guildId,
|
||||
Cancelled = SessionStatus.Cancelled,
|
||||
Active = ParticipantRegistrationStatus.Active,
|
||||
Waitlisted = ParticipantRegistrationStatus.Waitlisted
|
||||
});
|
||||
|
||||
var sessionList = sessions.ToList();
|
||||
if (sessionList.Count == 0)
|
||||
return null;
|
||||
|
||||
var sessionIds = sessionList.Select(s => s.Id).ToList();
|
||||
var participants = await connection.QueryAsync<ParticipantBatchDto>(
|
||||
@"SELECT sp.session_id as SessionId,
|
||||
p.display_name as DisplayName,
|
||||
COALESCE(p.external_username, p.telegram_username) as TelegramUsername,
|
||||
sp.registration_status as RegistrationStatus
|
||||
FROM session_participants sp
|
||||
JOIN players p ON p.id = sp.player_id
|
||||
WHERE sp.session_id = ANY(@SessionIds) AND sp.is_gm = false
|
||||
ORDER BY sp.registration_status ASC, sp.created_at ASC",
|
||||
new { SessionIds = sessionIds });
|
||||
|
||||
var firstTitle = sessionList.First().Title;
|
||||
var batchDtos = sessionList.Select(s => new SessionBatchDto(
|
||||
s.Id, s.ScheduledAt, s.Status, s.MaxPlayers, "")).ToList();
|
||||
|
||||
return SessionBatchViewBuilder.Build(firstTitle, batchDtos, participants.ToList());
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Create `src/GmRelay.DiscordBot/Features/Sessions/DiscordListSessionsCommand.cs`:
|
||||
|
||||
```csharp
|
||||
using NetCord.Rest;
|
||||
using NetCord.Services.ApplicationCommands;
|
||||
|
||||
namespace GmRelay.DiscordBot.Features.Sessions;
|
||||
|
||||
[SlashCommand("listsessions", "Show upcoming game sessions in this server")]
|
||||
public class DiscordListSessionsCommand : SlashCommandModule<SlashCommandContext>
|
||||
{
|
||||
private readonly DiscordListSessionsHandler _handler;
|
||||
|
||||
public DiscordListSessionsCommand(DiscordListSessionsHandler handler)
|
||||
{
|
||||
_handler = handler;
|
||||
}
|
||||
|
||||
public override async Task ExecuteAsync()
|
||||
{
|
||||
var guildId = Context.Guild?.Id.ToString()
|
||||
?? throw new InvalidOperationException("This command can only be used in a guild.");
|
||||
var channelId = Context.Channel.Id.ToString();
|
||||
|
||||
var view = await _handler.BuildScheduleAsync(guildId, channelId, Context.CancellationToken);
|
||||
|
||||
if (view is null)
|
||||
{
|
||||
await Context.Interaction.SendResponseAsync(
|
||||
InteractionCallback.Message("📭 В этом сервере нет предстоящих игр."));
|
||||
return;
|
||||
}
|
||||
|
||||
var (embeds, actionRows) = Rendering.DiscordSessionBatchRenderer.Render(view);
|
||||
|
||||
await Context.Interaction.SendResponseAsync(
|
||||
InteractionCallback.Message(new InteractionMessageProperties()
|
||||
.WithEmbeds(embeds)
|
||||
.WithComponents(actionRows)));
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Step 2.4: Run test — verify GREEN
|
||||
|
||||
Run: `dotnet test tests/GmRelay.Bot.Tests/GmRelay.Bot.Tests.csproj --filter "FullyQualifiedName~DiscordListSessionsHandlerTests" --verbosity normal`
|
||||
Expected: PASS.
|
||||
|
||||
### Step 2.5: Commit
|
||||
|
||||
```bash
|
||||
git add src/GmRelay.DiscordBot/Features/Sessions/DiscordListSessionsHandler.cs src/GmRelay.DiscordBot/Features/Sessions/DiscordListSessionsCommand.cs tests/GmRelay.Bot.Tests/Discord/DiscordListSessionsHandlerTests.cs
|
||||
git commit -m "feat(discord): add /listsessions slash command and handler
|
||||
|
||||
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>"
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Task 3: DiscordNewSessionHandler + Command
|
||||
|
||||
**Files:**
|
||||
- Create: `src/GmRelay.DiscordBot/Features/Sessions/DiscordNewSessionHandler.cs`
|
||||
- Create: `src/GmRelay.DiscordBot/Features/Sessions/DiscordNewSessionCommand.cs`
|
||||
- Test: `tests/GmRelay.Bot.Tests/Discord/DiscordNewSessionHandlerTests.cs`
|
||||
|
||||
**Context:** Handler должен:
|
||||
1. Проверить права пользователя (owner/admin/manager).
|
||||
2. Upsert игрока (GM) в `players` с `platform = 'Discord'`.
|
||||
3. Upsert `game_groups` с `platform = 'Discord'`, `external_group_id = guild_id`.
|
||||
4. Создать batch + sessions.
|
||||
5. Отправить rendered schedule в Discord channel.
|
||||
6. Сохранить `platform_messages` reference.
|
||||
|
||||
### Step 3.1: Write the failing test
|
||||
|
||||
Create `tests/GmRelay.Bot.Tests/Discord/DiscordNewSessionHandlerTests.cs`:
|
||||
|
||||
```csharp
|
||||
using GmRelay.DiscordBot.Features.Sessions;
|
||||
|
||||
namespace GmRelay.Bot.Tests.Discord;
|
||||
|
||||
public sealed class DiscordNewSessionHandlerTests
|
||||
{
|
||||
[Fact]
|
||||
public void ParseTimeInput_ShouldParseDiscordDateFormat()
|
||||
{
|
||||
var result = DiscordNewSessionHandler.ParseTimeInput("2026-05-20 19:30");
|
||||
Assert.True(result.IsSuccess);
|
||||
Assert.Equal(2026, result.Value.Year);
|
||||
Assert.Equal(5, result.Value.Month);
|
||||
Assert.Equal(20, result.Value.Day);
|
||||
Assert.Equal(19, result.Value.Hour);
|
||||
Assert.Equal(30, result.Value.Minute);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ParseTimeInput_ShouldRejectPastDate()
|
||||
{
|
||||
var result = DiscordNewSessionHandler.ParseTimeInput("2020-01-01 00:00");
|
||||
Assert.False(result.IsSuccess);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ParseTimeInput_ShouldParseRussianDateFormat()
|
||||
{
|
||||
var result = DiscordNewSessionHandler.ParseTimeInput("20.05.2026 19:30");
|
||||
Assert.True(result.IsSuccess);
|
||||
Assert.Equal(2026, result.Value.Year);
|
||||
Assert.Equal(5, result.Value.Month);
|
||||
Assert.Equal(20, result.Value.Day);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ParseTimeInput_ShouldRejectInvalidFormat()
|
||||
{
|
||||
var result = DiscordNewSessionHandler.ParseTimeInput("not-a-date");
|
||||
Assert.False(result.IsSuccess);
|
||||
Assert.NotNull(result.Error);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Step 3.2: Run test — verify RED
|
||||
|
||||
Run: `dotnet test tests/GmRelay.Bot.Tests/GmRelay.Bot.Tests.csproj --filter "FullyQualifiedName~DiscordNewSessionHandlerTests" --verbosity normal`
|
||||
Expected: FAIL — `DiscordNewSessionHandler` not found.
|
||||
|
||||
### Step 3.3: Write minimal implementation
|
||||
|
||||
Create `src/GmRelay.DiscordBot/Features/Sessions/DiscordNewSessionHandler.cs`:
|
||||
|
||||
```csharp
|
||||
using Dapper;
|
||||
using GmRelay.DiscordBot.Infrastructure.Discord;
|
||||
using GmRelay.Shared.Domain;
|
||||
using GmRelay.Shared.Platform;
|
||||
using GmRelay.Shared.Rendering;
|
||||
using Npgsql;
|
||||
|
||||
namespace GmRelay.DiscordBot.Features.Sessions;
|
||||
|
||||
public sealed record TimeParseResult(bool IsSuccess, DateTimeOffset Value, string? Error);
|
||||
|
||||
public sealed class DiscordNewSessionHandler(
|
||||
NpgsqlDataSource dataSource,
|
||||
DiscordPermissionChecker permissionChecker,
|
||||
IPlatformMessenger messenger,
|
||||
ILogger<DiscordNewSessionHandler> logger)
|
||||
{
|
||||
public static TimeParseResult ParseTimeInput(string input)
|
||||
{
|
||||
if (DateTimeOffset.TryParseExact(
|
||||
input.Trim(),
|
||||
"yyyy-MM-dd HH:mm",
|
||||
System.Globalization.CultureInfo.InvariantCulture,
|
||||
System.Globalization.DateTimeStyles.AssumeUniversal,
|
||||
out var result))
|
||||
{
|
||||
if (result < DateTimeOffset.UtcNow)
|
||||
return new TimeParseResult(false, default, "Дата находится в прошлом.");
|
||||
|
||||
return new TimeParseResult(true, result.ToUniversalTime(), null);
|
||||
}
|
||||
|
||||
if (DateTimeOffset.TryParseExact(
|
||||
input.Trim(),
|
||||
"dd.MM.yyyy HH:mm",
|
||||
System.Globalization.CultureInfo.InvariantCulture,
|
||||
System.Globalization.DateTimeStyles.AssumeUniversal,
|
||||
out var altResult))
|
||||
{
|
||||
if (altResult < DateTimeOffset.UtcNow)
|
||||
return new TimeParseResult(false, default, "Дата находится в прошлом.");
|
||||
|
||||
return new TimeParseResult(true, altResult.ToUniversalTime(), null);
|
||||
}
|
||||
|
||||
return new TimeParseResult(false, default, "Некорректный формат даты. Используйте YYYY-MM-DD HH:mm или DD.MM.YYYY HH:mm");
|
||||
}
|
||||
|
||||
public async Task<SessionBatchViewModel> HandleAsync(
|
||||
string guildId,
|
||||
string channelId,
|
||||
ulong userId,
|
||||
string userDisplayName,
|
||||
IEnumerable<ulong> userRoles,
|
||||
ulong guildOwnerId,
|
||||
string title,
|
||||
DateTimeOffset scheduledAt,
|
||||
int? maxPlayers,
|
||||
string? joinLink,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
await using var connection = await dataSource.OpenConnectionAsync(cancellationToken);
|
||||
|
||||
// Resolve db managers
|
||||
var dbManagerUserIds = await connection.QueryAsync<ulong>(
|
||||
@"SELECT CAST(p.external_user_id AS BIGINT)
|
||||
FROM group_managers gm
|
||||
JOIN players p ON p.id = gm.player_id
|
||||
JOIN game_groups g ON g.id = gm.group_id
|
||||
WHERE g.platform = 'Discord' AND g.external_group_id = @GuildId",
|
||||
new { GuildId = guildId });
|
||||
|
||||
if (!permissionChecker.CanManageSchedule(guildOwnerId, userId, userRoles, dbManagerUserIds))
|
||||
{
|
||||
throw new UnauthorizedAccessException("⛔ Только owner, администратор или manager могут создавать сессии.");
|
||||
}
|
||||
|
||||
await using var transaction = await connection.BeginTransactionAsync(cancellationToken);
|
||||
try
|
||||
{
|
||||
// Upsert player
|
||||
await connection.ExecuteAsync(
|
||||
@"INSERT INTO players (display_name, platform, external_user_id, external_username)
|
||||
VALUES (@Name, 'Discord', @UserId, @Name)
|
||||
ON CONFLICT (platform, external_user_id)
|
||||
WHERE platform IS NOT NULL AND external_user_id IS NOT NULL
|
||||
DO UPDATE SET display_name = EXCLUDED.display_name,
|
||||
external_username = EXCLUDED.external_username",
|
||||
new { Name = userDisplayName, UserId = userId.ToString() },
|
||||
transaction);
|
||||
|
||||
// Upsert group
|
||||
var groupId = await connection.ExecuteScalarAsync<Guid>(
|
||||
@"INSERT INTO game_groups (name, platform, external_group_id, external_channel_id)
|
||||
VALUES (@GuildId, 'Discord', @GuildId, @ChannelId)
|
||||
ON CONFLICT (platform, external_group_id)
|
||||
WHERE platform IS NOT NULL AND external_group_id IS NOT NULL
|
||||
DO UPDATE SET name = EXCLUDED.name,
|
||||
external_channel_id = COALESCE(EXCLUDED.external_channel_id, game_groups.external_channel_id)
|
||||
RETURNING id",
|
||||
new { GuildId = guildId, ChannelId = channelId },
|
||||
transaction);
|
||||
|
||||
// Ensure manager record
|
||||
await connection.ExecuteAsync(
|
||||
@"INSERT INTO group_managers (group_id, player_id, role)
|
||||
SELECT @GroupId, p.id, @OwnerRole
|
||||
FROM players p
|
||||
WHERE p.platform = 'Discord' AND p.external_user_id = @UserId
|
||||
ON CONFLICT (group_id, player_id) DO NOTHING",
|
||||
new { GroupId = groupId, UserId = userId.ToString(), OwnerRole = GroupManagerRoleExtensions.OwnerValue },
|
||||
transaction);
|
||||
|
||||
// Create batch + session
|
||||
var batchId = Guid.NewGuid();
|
||||
var sessionId = await connection.ExecuteScalarAsync<Guid>(
|
||||
@"INSERT INTO sessions (batch_id, group_id, title, join_link, scheduled_at, status, max_players)
|
||||
VALUES (@BatchId, @GroupId, @Title, @Link, @ScheduledAt, @Status, @MaxPlayers)
|
||||
RETURNING id",
|
||||
new
|
||||
{
|
||||
BatchId = batchId,
|
||||
GroupId = groupId,
|
||||
Title = title,
|
||||
Link = joinLink ?? string.Empty,
|
||||
ScheduledAt = scheduledAt.UtcDateTime,
|
||||
Status = SessionStatus.Planned,
|
||||
MaxPlayers = maxPlayers
|
||||
},
|
||||
transaction);
|
||||
|
||||
await transaction.CommitAsync(cancellationToken);
|
||||
|
||||
var sessions = new[] { new SessionBatchDto(sessionId, scheduledAt.UtcDateTime, SessionStatus.Planned, maxPlayers, joinLink ?? string.Empty) };
|
||||
var view = SessionBatchViewBuilder.Build(title, sessions, Array.Empty<ParticipantBatchDto>());
|
||||
|
||||
await messenger.SendScheduleAsync(
|
||||
new PlatformScheduleMessage(
|
||||
new PlatformGroup(PlatformKind.Discord, guildId, guildId, channelId),
|
||||
view,
|
||||
null),
|
||||
cancellationToken);
|
||||
|
||||
return view;
|
||||
}
|
||||
catch
|
||||
{
|
||||
await transaction.RollbackAsync(cancellationToken);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Create `src/GmRelay.DiscordBot/Features/Sessions/DiscordNewSessionCommand.cs`:
|
||||
|
||||
```csharp
|
||||
using NetCord.Rest;
|
||||
using NetCord.Services.ApplicationCommands;
|
||||
|
||||
namespace GmRelay.DiscordBot.Features.Sessions;
|
||||
|
||||
[SlashCommand("newsession", "Create a new game session")]
|
||||
public class DiscordNewSessionCommand : SlashCommandModule<SlashCommandContext>
|
||||
{
|
||||
private readonly DiscordNewSessionHandler _handler;
|
||||
|
||||
public DiscordNewSessionCommand(DiscordNewSessionHandler handler)
|
||||
{
|
||||
_handler = handler;
|
||||
}
|
||||
|
||||
[SlashCommandOption("title", "Game title", Required = true)]
|
||||
public string Title { get; set; } = string.Empty;
|
||||
|
||||
[SlashCommandOption("time", "Session time (YYYY-MM-DD HH:mm or DD.MM.YYYY HH:mm)", Required = true)]
|
||||
public string Time { get; set; } = string.Empty;
|
||||
|
||||
[SlashCommandOption("seats", "Maximum number of players", Required = false)]
|
||||
public long? Seats { get; set; }
|
||||
|
||||
[SlashCommandOption("link", "Join link", Required = false)]
|
||||
public string? Link { get; set; }
|
||||
|
||||
public override async Task ExecuteAsync()
|
||||
{
|
||||
var guild = Context.Guild
|
||||
?? throw new InvalidOperationException("This command can only be used in a guild.");
|
||||
|
||||
var timeResult = DiscordNewSessionHandler.ParseTimeInput(Time);
|
||||
if (!timeResult.IsSuccess)
|
||||
{
|
||||
await Context.Interaction.SendResponseAsync(
|
||||
InteractionCallback.Message($"❌ {timeResult.Error}"));
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var view = await _handler.HandleAsync(
|
||||
guildId: guild.Id.ToString(),
|
||||
channelId: Context.Channel.Id.ToString(),
|
||||
userId: Context.User.Id,
|
||||
userDisplayName: Context.User.GlobalName ?? Context.User.Username,
|
||||
userRoles: Context.GuildUser!.RoleIds,
|
||||
guildOwnerId: guild.OwnerId,
|
||||
title: Title,
|
||||
scheduledAt: timeResult.Value,
|
||||
maxPlayers: Seats is null ? null : (int)Seats.Value,
|
||||
joinLink: Link,
|
||||
Context.CancellationToken);
|
||||
|
||||
await Context.Interaction.SendResponseAsync(
|
||||
InteractionCallback.Message("✅ Сессия создана!"));
|
||||
}
|
||||
catch (UnauthorizedAccessException ex)
|
||||
{
|
||||
await Context.Interaction.SendResponseAsync(
|
||||
InteractionCallback.Message($"⛅ {ex.Message}"));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
await Context.Interaction.SendResponseAsync(
|
||||
InteractionCallback.Message("💥 Произошла ошибка при создании сессии."));
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Step 3.4: Run test — verify GREEN
|
||||
|
||||
Run: `dotnet test tests/GmRelay.Bot.Tests/GmRelay.Bot.Tests.csproj --filter "FullyQualifiedName~DiscordNewSessionHandlerTests" --verbosity normal`
|
||||
Expected: PASS.
|
||||
|
||||
### Step 3.5: Commit
|
||||
|
||||
```bash
|
||||
git add src/GmRelay.DiscordBot/Features/Sessions/DiscordNewSessionHandler.cs src/GmRelay.DiscordBot/Features/Sessions/DiscordNewSessionCommand.cs tests/GmRelay.Bot.Tests/Discord/DiscordNewSessionHandlerTests.cs
|
||||
git commit -m "feat(discord): add /newsession slash command and handler
|
||||
|
||||
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>"
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Task 4: DiscordPlatformMessenger
|
||||
|
||||
**Files:**
|
||||
- Create: `src/GmRelay.DiscordBot/Infrastructure/Discord/DiscordPlatformMessenger.cs`
|
||||
- Test: `tests/GmRelay.Bot.Tests/Discord/DiscordPlatformMessengerTests.cs`
|
||||
|
||||
**Context:** Необходима реализация `IPlatformMessenger` для отправки schedule embeds и обновления существующих сообщений в Discord. Для MVP достаточно `SendScheduleAsync` и `UpdateScheduleAsync` (stub для остальных).
|
||||
|
||||
### Step 4.1: Write the failing test
|
||||
|
||||
Create `tests/GmRelay.Bot.Tests/Discord/DiscordPlatformMessengerTests.cs`:
|
||||
|
||||
```csharp
|
||||
using GmRelay.DiscordBot.Infrastructure.Discord;
|
||||
using GmRelay.Shared.Platform;
|
||||
using GmRelay.Shared.Rendering;
|
||||
|
||||
namespace GmRelay.Bot.Tests.Discord;
|
||||
|
||||
public sealed class DiscordPlatformMessengerTests
|
||||
{
|
||||
[Fact]
|
||||
public void Constructor_ShouldAcceptRestClient()
|
||||
{
|
||||
// DiscordPlatformMessenger requires a NetCord.Rest.RestClient.
|
||||
// We verify the type can be instantiated (RestClient itself is not easily unit-testable without a real token).
|
||||
// This test proves the contract exists and compiles.
|
||||
var constructor = typeof(DiscordPlatformMessenger).GetConstructor(new[] { typeof(NetCord.Rest.RestClient) });
|
||||
Assert.NotNull(constructor);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DiscordPlatformMessenger_ShouldImplementIPlatformMessenger()
|
||||
{
|
||||
Assert.True(typeof(IPlatformMessenger).IsAssignableFrom(typeof(DiscordPlatformMessenger)));
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Step 4.2: Run test — verify RED
|
||||
|
||||
Run: `dotnet test tests/GmRelay.Bot.Tests/GmRelay.Bot.Tests.csproj --filter "FullyQualifiedName~DiscordPlatformMessengerTests" --verbosity normal`
|
||||
Expected: FAIL — `DiscordPlatformMessenger` not found.
|
||||
|
||||
### Step 4.3: Write minimal implementation
|
||||
|
||||
Create `src/GmRelay.DiscordBot/Infrastructure/Discord/DiscordPlatformMessenger.cs`:
|
||||
|
||||
```csharp
|
||||
using GmRelay.DiscordBot.Rendering;
|
||||
using GmRelay.Shared.Platform;
|
||||
using GmRelay.Shared.Rendering;
|
||||
using NetCord;
|
||||
using NetCord.Rest;
|
||||
|
||||
namespace GmRelay.DiscordBot.Infrastructure.Discord;
|
||||
|
||||
public sealed class DiscordPlatformMessenger(RestClient restClient) : IPlatformMessenger
|
||||
{
|
||||
public async Task<PlatformMessageRef> SendScheduleAsync(PlatformScheduleMessage message, CancellationToken ct)
|
||||
{
|
||||
var (embeds, actionRows) = DiscordSessionBatchRenderer.Render(message.View);
|
||||
|
||||
var channelId = ulong.Parse(message.Group.ExternalChannelId
|
||||
?? message.Group.ExternalGroupId);
|
||||
|
||||
var msg = await restClient.SendMessageAsync(
|
||||
channelId,
|
||||
new MessageProperties()
|
||||
.WithEmbeds(embeds)
|
||||
.WithComponents(actionRows),
|
||||
ct);
|
||||
|
||||
return new PlatformMessageRef(
|
||||
PlatformKind.Discord,
|
||||
message.Group.ExternalGroupId,
|
||||
null,
|
||||
msg.Id.ToString());
|
||||
}
|
||||
|
||||
public async Task UpdateScheduleAsync(PlatformScheduleMessage message, CancellationToken ct)
|
||||
{
|
||||
if (message.ExistingMessage is null)
|
||||
return;
|
||||
|
||||
var (embeds, actionRows) = DiscordSessionBatchRenderer.Render(message.View);
|
||||
|
||||
var channelId = ulong.Parse(message.Group.ExternalChannelId
|
||||
?? message.Group.ExternalGroupId);
|
||||
var messageId = ulong.Parse(message.ExistingMessage.ExternalMessageId);
|
||||
|
||||
await restClient.ModifyMessageAsync(
|
||||
channelId,
|
||||
messageId,
|
||||
new MessageProperties()
|
||||
.WithEmbeds(embeds)
|
||||
.WithComponents(actionRows),
|
||||
ct);
|
||||
}
|
||||
|
||||
public Task SendGroupMessageAsync(PlatformGroup group, string htmlText, CancellationToken ct)
|
||||
{
|
||||
// MVP: not needed for /newsession and /listsessions
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public Task SendPrivateMessageAsync(PlatformPrivateMessage message, CancellationToken ct)
|
||||
{
|
||||
// MVP: not needed
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public Task AnswerInteractionAsync(PlatformInteractionReply reply, CancellationToken ct)
|
||||
{
|
||||
// MVP: not needed (commands answer inline via SlashCommandContext)
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public Task SendCalendarFileAsync(PlatformCalendarFile file, CancellationToken ct)
|
||||
{
|
||||
// MVP: not needed
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Step 4.4: Run test — verify GREEN
|
||||
|
||||
Run: `dotnet test tests/GmRelay.Bot.Tests/GmRelay.Bot.Tests.csproj --filter "FullyQualifiedName~DiscordPlatformMessengerTests" --verbosity normal`
|
||||
Expected: PASS.
|
||||
|
||||
### Step 4.5: Commit
|
||||
|
||||
```bash
|
||||
git add src/GmRelay.DiscordBot/Infrastructure/Discord/DiscordPlatformMessenger.cs tests/GmRelay.Bot.Tests/Discord/DiscordPlatformMessengerTests.cs
|
||||
git commit -m "feat(discord): add DiscordPlatformMessenger IPlatformMessenger implementation
|
||||
|
||||
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>"
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Task 5: Wire up DI and Register Commands
|
||||
|
||||
**Files:**
|
||||
- Modify: `src/GmRelay.DiscordBot/Program.cs`
|
||||
|
||||
### Step 5.1: Write the failing test (structure test)
|
||||
|
||||
Modify `tests/GmRelay.Bot.Tests/Discord/DiscordStartupTests.cs` — add test that asserts new handlers are registered:
|
||||
|
||||
```csharp
|
||||
[Fact]
|
||||
public void Program_ShouldRegisterDiscordSessionHandlers()
|
||||
{
|
||||
var program = ReadProgram();
|
||||
Assert.Contains("DiscordListSessionsHandler", program);
|
||||
Assert.Contains("DiscordNewSessionHandler", program);
|
||||
Assert.Contains("DiscordPermissionChecker", program);
|
||||
Assert.Contains("DiscordPlatformMessenger", program);
|
||||
Assert.Contains("IPlatformMessenger", program);
|
||||
}
|
||||
```
|
||||
|
||||
### Step 5.2: Run test — verify RED
|
||||
|
||||
Run: `dotnet test tests/GmRelay.Bot.Tests/GmRelay.Bot.Tests.csproj --filter "FullyQualifiedName~DiscordStartupTests" --verbosity normal`
|
||||
Expected: FAIL — asserts not found in Program.cs.
|
||||
|
||||
### Step 5.3: Write minimal implementation
|
||||
|
||||
Modify `src/GmRelay.DiscordBot/Program.cs`:
|
||||
|
||||
```csharp
|
||||
using GmRelay.DiscordBot.Features.Sessions;
|
||||
using GmRelay.DiscordBot.Infrastructure.Discord;
|
||||
using GmRelay.Shared.Platform;
|
||||
|
||||
// ... existing usings ...
|
||||
|
||||
builder.Services.AddSingleton<DiscordPermissionChecker>();
|
||||
builder.Services.AddSingleton<DiscordListSessionsHandler>();
|
||||
builder.Services.AddSingleton<DiscordNewSessionHandler>();
|
||||
builder.Services.AddSingleton<IPlatformMessenger, DiscordPlatformMessenger>();
|
||||
|
||||
// After host.Build():
|
||||
host.AddSlashCommand("listsessions", "Show upcoming game sessions", async (DiscordListSessionsHandler handler, SlashCommandContext context) =>
|
||||
{
|
||||
// NetCord module-based approach preferred; if AddSlashCommand lambda doesn't support DI injection of custom services,
|
||||
// rely on module classes registered via AddApplicationCommands
|
||||
});
|
||||
```
|
||||
|
||||
**Important:** NetCord module classes (`DiscordListSessionsCommand`, `DiscordNewSessionCommand`) автоматически регистрируются через `AddApplicationCommands()` + `AddGatewayHandlers(typeof(Program).Assembly)`. Constructor injection в модулях работает через DI контейнер. Никаких дополнительных `AddSlashCommand` для модулей не требуется.
|
||||
|
||||
Убедиться, что в Program.cs есть:
|
||||
```csharp
|
||||
builder.Services.AddSingleton<DiscordPermissionChecker>();
|
||||
builder.Services.AddSingleton<DiscordListSessionsHandler>();
|
||||
builder.Services.AddSingleton<DiscordNewSessionHandler>();
|
||||
builder.Services.AddSingleton<IPlatformMessenger, DiscordPlatformMessenger>();
|
||||
```
|
||||
|
||||
### Step 5.4: Run test — verify GREEN
|
||||
|
||||
Run: `dotnet test tests/GmRelay.Bot.Tests/GmRelay.Bot.Tests.csproj --filter "FullyQualifiedName~DiscordStartupTests" --verbosity normal`
|
||||
Expected: PASS.
|
||||
|
||||
### Step 5.5: Commit
|
||||
|
||||
```bash
|
||||
git add src/GmRelay.DiscordBot/Program.cs tests/GmRelay.Bot.Tests/Discord/DiscordStartupTests.cs
|
||||
git commit -m "feat(discord): wire up DI registrations for session handlers and messenger
|
||||
|
||||
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>"
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Task 6: Build Verification
|
||||
|
||||
### Step 6.1: Build DiscordBot project
|
||||
|
||||
Run: `dotnet build src/GmRelay.DiscordBot/GmRelay.DiscordBot.csproj --no-restore`
|
||||
Expected: Build succeeds (0 errors, 0 warnings).
|
||||
|
||||
### Step 6.2: Run all tests
|
||||
|
||||
Run: `dotnet test tests/GmRelay.Bot.Tests/GmRelay.Bot.Tests.csproj --verbosity normal`
|
||||
Expected: All tests pass.
|
||||
|
||||
### Step 6.3: Commit if any fixes needed
|
||||
|
||||
If build or tests required fixes, commit them.
|
||||
|
||||
---
|
||||
|
||||
## Task 7: Version Bump
|
||||
|
||||
**Files to modify:**
|
||||
- `Directory.Build.props`: `<Version>2.4.0</Version>`
|
||||
- `compose.yaml`: обновить теги `gmrelay-bot`, `gmrelay-web`, `gmrelay-discord-bot` → `2.4.0`
|
||||
- `.gitea/workflows/deploy.yml`: `VERSION: 2.4.0`
|
||||
- `src/GmRelay.Web/Components/Layout/NavMenu.razor`: `<div class="nav-version">v2.4.0</div>`
|
||||
|
||||
### Step 7.1: Bump version
|
||||
|
||||
Apply изменения ко всем 4 файлам.
|
||||
|
||||
### Step 7.2: Update version test
|
||||
|
||||
Modify `tests/GmRelay.Bot.Tests/Discord/DiscordProjectStructureTests.cs` — обновить `Version_ShouldBeSynchronizedForDiscordFeatureRelease` ожидаемое значение на `2.4.0`.
|
||||
|
||||
### Step 7.3: Run version test
|
||||
|
||||
Run: `dotnet test tests/GmRelay.Bot.Tests/GmRelay.Bot.Tests.csproj --filter "FullyQualifiedName~Version_ShouldBeSynchronizedForDiscordFeatureRelease" --verbosity normal`
|
||||
Expected: PASS.
|
||||
|
||||
### Step 7.4: Commit
|
||||
|
||||
```bash
|
||||
git add Directory.Build.props compose.yaml .gitea/workflows/deploy.yml src/GmRelay.Web/Components/Layout/NavMenu.razor tests/GmRelay.Bot.Tests/Discord/DiscordProjectStructureTests.cs
|
||||
git commit -m "chore: bump version to 2.4.0
|
||||
|
||||
Synchronized across Directory.Build.props, compose.yaml, deploy.yml, NavMenu.razor
|
||||
|
||||
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>"
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Spec Coverage Self-Review
|
||||
|
||||
| Issue Requirement | Task |
|
||||
|---|---|
|
||||
| Slash command `/newsession` | Task 3 |
|
||||
| Slash command `/listsessions` | Task 2 |
|
||||
| Сохранение platform group identity (guild/channel) | Task 3 (game_groups.platform, external_group_id, external_channel_id) |
|
||||
| Минимальная проверка прав | Task 1 + Task 3 |
|
||||
| Данные пишутся в общую PostgreSQL без Telegram-only assumptions | Task 2, 3 SQL используют platform-agnostic колонки |
|
||||
| `/listsessions` публикует/обновляет расписание | Task 2 + Task 4 |
|
||||
|
||||
**Placeholder scan:** Нет TBD, TODO, "implement later". Каждый шаг содержит конкретный код.
|
||||
|
||||
**Type consistency:** `DiscordPermissionChecker.CanManageSchedule` перегружен для resolved permissions (ulong bitflag). Handler передает `Context.GuildUser.RoleIds` и `guild.OwnerId`.
|
||||
|
||||
---
|
||||
|
||||
## Execution Handoff
|
||||
|
||||
**Plan complete and saved to `docs/superpowers/plans/2026-05-19-discord-newsession-listsessions.md`.**
|
||||
|
||||
**Two execution options:**
|
||||
|
||||
1. **Subagent-Driven (recommended)** — I dispatch a fresh subagent per task, review between tasks, fast iteration
|
||||
2. **Inline Execution** — Execute tasks in this session using executing-plans, batch execution with checkpoints for review
|
||||
|
||||
**Which approach?**
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,44 +0,0 @@
|
||||
# Telegram Mini App Dashboard Design
|
||||
|
||||
## Goal
|
||||
|
||||
Issue #17 adds a Telegram Mini App dashboard as the mobile entry point for the existing Web Dashboard. Owner and co-GM users must be able to open the dashboard from Telegram, pass server-side Telegram WebApp `initData` validation, and manage only their own groups.
|
||||
|
||||
## Scope
|
||||
|
||||
- Add Mini App authentication using Telegram WebApp `initData`.
|
||||
- Add a `/miniapp` entry page that signs the user into the existing cookie auth flow, then opens the regular dashboard UI in mobile-first mode.
|
||||
- Reuse `AuthorizedSessionService`, `SessionService`, and existing Blazor pages for groups, sessions, templates, waitlist promotion, edit forms, and bulk batch operations.
|
||||
- Add bot entry points: a Mini App button in `/start` and a configurable default menu button when `Telegram:MiniAppUrl` is set.
|
||||
- Update README, wiki, deployment config, and visible version strings to `1.9.0`.
|
||||
|
||||
## Architecture
|
||||
|
||||
The Mini App is not a second dashboard implementation. It is a Telegram-authenticated entrance into the existing Blazor dashboard. This keeps authorization, domain operations, Telegram message synchronization, and Web Dashboard behavior in one place.
|
||||
|
||||
`TelegramAuthService` gains a second verification method for WebApp `initData`. The server accepts the raw URL-encoded init payload at `/auth/telegram-webapp`, verifies the Telegram HMAC with the bot token, extracts the user id/name from the embedded `user` JSON, and issues the same auth cookie as the login widget endpoint.
|
||||
|
||||
`/miniapp` loads `telegram-web-app.js`, posts `window.Telegram.WebApp.initData` to the server endpoint, expands the WebApp viewport, and redirects to `/`. If a user opens `/miniapp` outside Telegram, the page shows the regular login fallback.
|
||||
|
||||
## Data Flow
|
||||
|
||||
1. User opens the Mini App from the bot menu button or `/start` inline button.
|
||||
2. Telegram injects `initData` into the WebApp JavaScript API.
|
||||
3. `/miniapp` posts `{ initData }` to `/auth/telegram-webapp`.
|
||||
4. The server verifies the WebApp signature and expiry.
|
||||
5. The server creates the same claims used by Telegram Login Widget.
|
||||
6. Existing Blazor pages load groups through `AuthorizedSessionService`.
|
||||
7. Any edit, waitlist, template, or batch action still goes through existing services and keeps Telegram messages synchronized.
|
||||
|
||||
## Error Handling
|
||||
|
||||
- Missing or invalid init data returns `401` and leaves the user on the Mini App page.
|
||||
- Expired auth data is rejected with the same 24-hour window used by the Login Widget.
|
||||
- A verified Telegram user with no owner/co-GM groups sees the existing empty dashboard state.
|
||||
- Direct navigation to a foreign group/session still redirects to `/access-denied` through existing authorization checks.
|
||||
|
||||
## Testing
|
||||
|
||||
- Unit tests cover valid and invalid WebApp `initData`.
|
||||
- File-level regression tests ensure `/miniapp`, `/auth/telegram-webapp`, Telegram WebApp script loading, bot Mini App button, menu button setup, and mobile Mini App CSS hooks remain present.
|
||||
- Existing `AuthorizedSessionServiceTests` continue covering owner/co-GM access behavior.
|
||||
@@ -0,0 +1,234 @@
|
||||
# Game Catalog and One-Shot Showcase — Design Spec
|
||||
|
||||
> Issue #39: feat: добавить каталог игр и витрину ваншотов
|
||||
|
||||
---
|
||||
|
||||
## Goal
|
||||
Build a public `/showcase` page that aggregates published sessions from all clubs into a filterable catalog. Users can browse games by system, format, date, and availability. GM controls whether direct registration from the catalog is allowed. The catalog respects existing seat limits and waitlist logic.
|
||||
|
||||
---
|
||||
|
||||
## Architecture
|
||||
Extend the existing public-pages infrastructure (V026) with new session metadata fields, a cross-group query layer in `ISessionStore`, and new Razor pages in `GmRelay.Web`. Bot flows (Telegram + Discord) are updated to collect the new fields during session creation. Fuzzy matching on game system names is performed client-side in the bot UI.
|
||||
|
||||
---
|
||||
|
||||
## Tech Stack
|
||||
- .NET 10, Blazor Server, Dapper.AOT, Npgsql
|
||||
- Existing: `PublicLayout`, `ISessionStore`, `SessionService`, `SessionCapacityRules`
|
||||
- New: `GameSystem` enum, `ShowcaseFilter` record, `ShowcaseSessionDto`
|
||||
|
||||
---
|
||||
|
||||
## Data Model
|
||||
|
||||
### New Fields on `sessions` (Migration V027)
|
||||
|
||||
| Column | Type | Constraints | Description |
|
||||
|---|---|---|---|
|
||||
| `is_one_shot` | `BOOLEAN` | `NOT NULL DEFAULT false` | One-shot or campaign |
|
||||
| `system` | `VARCHAR(50)` | nullable | Game system name (enum value or custom) |
|
||||
| `description` | `TEXT` | nullable | Short description for card |
|
||||
| `cover_image_url` | `TEXT` | nullable | Cover image URL |
|
||||
| `duration_minutes` | `INTEGER` | nullable | Duration in minutes |
|
||||
| `format` | `VARCHAR(20)` | `CHECK (format IN ('Online','Offline','Hybrid'))`, nullable | Session format |
|
||||
| `allow_direct_registration` | `BOOLEAN` | `NOT NULL DEFAULT false` | Allow direct registration from showcase |
|
||||
|
||||
### `GameSystem` Enum
|
||||
|
||||
```csharp
|
||||
public enum GameSystem
|
||||
{
|
||||
Dnd5e, Pathfinder2e, CallOfCthulhu7e, Shadowdark,
|
||||
OldSchoolEssentials, Dragonbane, BladesInTheDark,
|
||||
Daggerheart, CyberpunkRed, Mothership, AlienRpg,
|
||||
WarhammerFantasy, VampireMasquerade5e, StarWarsFfg,
|
||||
Genesys, SavageWorlds, GURPS, Fate, DungeonWorld,
|
||||
Ironsworn, Other
|
||||
}
|
||||
```
|
||||
|
||||
Stored as `VARCHAR(50)` in DB (not native enum) to allow future extension without migration.
|
||||
|
||||
### DTOs
|
||||
|
||||
```csharp
|
||||
public sealed record ShowcaseSessionDto(
|
||||
Guid Id,
|
||||
Guid GroupId,
|
||||
string GroupName,
|
||||
string? GroupSlug,
|
||||
string Title,
|
||||
DateTime ScheduledAt,
|
||||
string Status,
|
||||
string? System,
|
||||
bool IsOneShot,
|
||||
string? Format,
|
||||
int? DurationMinutes,
|
||||
string? CoverImageUrl,
|
||||
int? MaxPlayers,
|
||||
int ActivePlayerCount,
|
||||
int WaitlistedPlayerCount,
|
||||
bool AllowDirectRegistration);
|
||||
|
||||
public sealed record ShowcaseFilter(
|
||||
DateFilter Date = DateFilter.All,
|
||||
SeatFilter Seats = SeatFilter.Any,
|
||||
GameSystem? System = null,
|
||||
bool? IsOneShot = null,
|
||||
string? Format = null);
|
||||
|
||||
public enum DateFilter { Today, Tomorrow, ThisWeek, All }
|
||||
public enum SeatFilter { Available, Waitlist, Any }
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## UI Design
|
||||
|
||||
### `/showcase` — Catalog Page
|
||||
|
||||
**Layout:**
|
||||
- Hero with title "Каталог игр"
|
||||
- Sticky filter bar (horizontal on desktop, collapsible on mobile)
|
||||
- Responsive grid of session cards (1 col mobile, 2 col tablet, 3 col desktop)
|
||||
- Pagination (page + pageSize = 12)
|
||||
|
||||
**Filters:**
|
||||
- Date: "Сегодня" | "Завтра" | "На неделю" | "Все"
|
||||
- Seats: "Есть места" | "Waitlist" | "Любое"
|
||||
- System: dropdown with all `GameSystem` values
|
||||
- Type: "Ваншот" | "Кампания" | "Любое"
|
||||
- Format: "Онлайн" | "Офлайн" | "Гибрид" | "Любое"
|
||||
|
||||
**Card Design:**
|
||||
- Cover image (fallback: colored placeholder with initials)
|
||||
- Title
|
||||
- System badge
|
||||
- Date + time (MSK)
|
||||
- Duration (e.g. "3 часа")
|
||||
- Format badge
|
||||
- Seats indicator: "5/6 мест" | "Waitlist (3)" | "Мест нет"
|
||||
- Club name (link to `/club/{slug}`)
|
||||
- Buttons: "Подробнее" → `/s/{id}`, "Записаться" (if `AllowDirectRegistration`)
|
||||
|
||||
### `/s/{id}` — Public Session Detail (Updated)
|
||||
|
||||
New fields added to existing page:
|
||||
- Cover image (full-width hero)
|
||||
- System badge
|
||||
- Description block
|
||||
- Duration + format
|
||||
- GM contact (always visible: Telegram username or Discord tag)
|
||||
- If `allow_direct_registration`:
|
||||
- "Записаться" button → Telegram Mini App deeplink or Discord OAuth
|
||||
- Direct registration into `session_participants` via `SessionCapacityRules`
|
||||
|
||||
---
|
||||
|
||||
## Backend
|
||||
|
||||
### ISessionStore Methods
|
||||
|
||||
```csharp
|
||||
Task<IReadOnlyList<ShowcaseSessionDto>> GetShowcaseSessionsAsync(
|
||||
ShowcaseFilter filter, int page, int pageSize);
|
||||
|
||||
Task<ShowcaseSessionDto?> GetShowcaseSessionAsync(Guid sessionId);
|
||||
|
||||
Task<bool> RegisterFromShowcaseAsync(Guid sessionId, PlatformUser user);
|
||||
```
|
||||
|
||||
`GetShowcaseSessionsAsync` query:
|
||||
- Cross-group (all clubs with `public_schedule_enabled = true`)
|
||||
- Only `is_public = true` sessions
|
||||
- `scheduled_at > now() - interval '4 hours'`
|
||||
- `status <> 'Cancelled'`
|
||||
- Apply filters in SQL WHERE clause
|
||||
- Order by `scheduled_at ASC`
|
||||
- Offset/limit pagination
|
||||
|
||||
`RegisterFromShowcaseAsync`:
|
||||
- Check `allow_direct_registration = true`
|
||||
- Load session with `FOR UPDATE`
|
||||
- Count active + waitlisted participants
|
||||
- Use `SessionCapacityRules.DecideJoinStatus`
|
||||
- Insert participant with appropriate `registration_status`
|
||||
- Return true on success, false if full and no waitlist allowed
|
||||
|
||||
---
|
||||
|
||||
## Bot Integration
|
||||
|
||||
### Telegram Bot
|
||||
|
||||
During `CreateSessionCommand` flow, after title/link/time input:
|
||||
1. "Выберите систему:" inline keyboard with `GameSystem` values + "Другое"
|
||||
2. If text input instead of button: fuzzy match against display names (Levenshtein/Contains/StartsWith)
|
||||
3. "Описание игры (краткое):" — text input, optional (skip button)
|
||||
4. "Формат:" inline keyboard — "Онлайн" | "Офлайн" | "Гибрид"
|
||||
5. "Продолжительность (в часах):" — int input, optional
|
||||
6. "Обложка (URL или пропустить):" — text input, optional
|
||||
|
||||
During `/publish` flow:
|
||||
- "Разрешить прямую запись из каталога?" — yes/no toggle (default: no)
|
||||
|
||||
### Discord Bot
|
||||
|
||||
Same flow adapted for Discord interactions:
|
||||
- Slash command options or button menus for system/format
|
||||
- Modal input for description, duration, cover URL
|
||||
- Fuzzy matching on free-text system input
|
||||
|
||||
---
|
||||
|
||||
## Migration V027
|
||||
|
||||
```sql
|
||||
ALTER TABLE sessions
|
||||
ADD COLUMN is_one_shot BOOLEAN NOT NULL DEFAULT false,
|
||||
ADD COLUMN system VARCHAR(50),
|
||||
ADD COLUMN description TEXT,
|
||||
ADD COLUMN cover_image_url TEXT,
|
||||
ADD COLUMN duration_minutes INTEGER,
|
||||
ADD COLUMN format VARCHAR(20) CHECK (format IN ('Online','Offline','Hybrid')),
|
||||
ADD COLUMN allow_direct_registration BOOLEAN NOT NULL DEFAULT false;
|
||||
|
||||
CREATE INDEX ix_sessions_showcase
|
||||
ON sessions (scheduled_at, system, is_one_shot, format)
|
||||
WHERE is_public = true AND status <> 'Cancelled';
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Testing Strategy
|
||||
|
||||
1. **Unit tests:** `SessionCapacityRules` with showcase registration scenarios
|
||||
2. **Integration tests:** `GetShowcaseSessionsAsync` with each filter combination
|
||||
3. **UI tests:** `Showcase.razor` rendering with/without cover images, filters applied
|
||||
4. **Bot tests:** Fuzzy matching algorithm for `GameSystem` resolution
|
||||
|
||||
---
|
||||
|
||||
## Version Bump
|
||||
|
||||
Issue label: `type:feature` → **minor bump**
|
||||
Current: `3.3.0` → Next: `3.4.0`
|
||||
|
||||
Files to sync:
|
||||
- `Directory.Build.props`
|
||||
- `compose.yaml` (bot, discord, web image tags)
|
||||
- `.gitea/workflows/deploy.yml` (`VERSION` env)
|
||||
- `src/GmRelay.Web/Components/Layout/NavMenu.razor`
|
||||
|
||||
---
|
||||
|
||||
## Acceptance Criteria (from Issue #39)
|
||||
|
||||
- [ ] User can find a published game without accessing a private dashboard
|
||||
- [ ] Registration does not bypass existing seat/waitlist limits
|
||||
- [ ] Owner/co-GM controls what appears in the showcase via `is_public` + `allow_direct_registration`
|
||||
- [ ] Filters work: date, seats, system, type, format
|
||||
- [ ] GM contact is always visible on public session detail
|
||||
- [ ] Direct registration respects `SessionCapacityRules`
|
||||
@@ -1,318 +0,0 @@
|
||||
using Dapper;
|
||||
using GmRelay.Shared.Domain;
|
||||
using Npgsql;
|
||||
using Telegram.Bot;
|
||||
using Telegram.Bot.Types.ReplyMarkups;
|
||||
|
||||
namespace GmRelay.Bot.Features.Confirmation.HandleRsvp;
|
||||
|
||||
public sealed record HandleRsvpCommand(
|
||||
Guid SessionId,
|
||||
long TelegramUserId,
|
||||
string Status,
|
||||
string CallbackQueryId,
|
||||
long ChatId,
|
||||
int MessageId);
|
||||
|
||||
internal sealed record RsvpCounts(int Total, int Confirmed, int Declined);
|
||||
|
||||
internal sealed record SessionContext(
|
||||
string Title,
|
||||
DateTime ScheduledAt,
|
||||
string Status,
|
||||
long GmTelegramId,
|
||||
long TelegramChatId,
|
||||
int? ThreadId);
|
||||
|
||||
internal sealed record ParticipantRsvp(
|
||||
long TelegramId,
|
||||
string DisplayName,
|
||||
string? TelegramUsername,
|
||||
string RsvpStatus);
|
||||
|
||||
public sealed class HandleRsvpHandler(
|
||||
NpgsqlDataSource dataSource,
|
||||
ITelegramBotClient bot,
|
||||
ILogger<HandleRsvpHandler> logger)
|
||||
{
|
||||
public async Task HandleAsync(HandleRsvpCommand command, CancellationToken ct)
|
||||
{
|
||||
await using var connection = await dataSource.OpenConnectionAsync(ct);
|
||||
await using var transaction = await connection.BeginTransactionAsync(ct);
|
||||
|
||||
var participantExists = await connection.ExecuteScalarAsync<bool>(
|
||||
"""
|
||||
SELECT EXISTS (
|
||||
SELECT 1
|
||||
FROM session_participants sp
|
||||
JOIN players p ON p.id = sp.player_id
|
||||
WHERE sp.session_id = @SessionId
|
||||
AND p.telegram_id = @TelegramUserId
|
||||
AND sp.is_gm = false
|
||||
AND sp.registration_status = @Active
|
||||
)
|
||||
""",
|
||||
new { command.SessionId, command.TelegramUserId, Active = ParticipantRegistrationStatus.Active },
|
||||
transaction);
|
||||
|
||||
if (!participantExists)
|
||||
{
|
||||
await bot.AnswerCallbackQuery(
|
||||
callbackQueryId: command.CallbackQueryId,
|
||||
text: "Вы не являетесь участником этой сессии.",
|
||||
cancellationToken: ct);
|
||||
return;
|
||||
}
|
||||
|
||||
var updated = await connection.ExecuteAsync(
|
||||
"""
|
||||
UPDATE session_participants
|
||||
SET rsvp_status = @Status,
|
||||
responded_at = now()
|
||||
WHERE session_id = @SessionId
|
||||
AND player_id = (SELECT id FROM players WHERE telegram_id = @TelegramUserId)
|
||||
AND registration_status = @Active
|
||||
AND rsvp_status != @Status
|
||||
""",
|
||||
new { command.SessionId, command.TelegramUserId, command.Status, Active = ParticipantRegistrationStatus.Active },
|
||||
transaction);
|
||||
|
||||
if (updated == 0)
|
||||
{
|
||||
var alreadyText = command.Status == RsvpStatus.Confirmed
|
||||
? "Вы уже подтвердили участие."
|
||||
: "Вы уже отказались от участия.";
|
||||
|
||||
await bot.AnswerCallbackQuery(
|
||||
callbackQueryId: command.CallbackQueryId,
|
||||
text: alreadyText,
|
||||
cancellationToken: ct);
|
||||
return;
|
||||
}
|
||||
|
||||
var session = await connection.QuerySingleAsync<SessionContext>(
|
||||
"""
|
||||
SELECT s.title,
|
||||
s.scheduled_at AS ScheduledAt,
|
||||
s.status AS Status,
|
||||
g.gm_telegram_id AS GmTelegramId,
|
||||
g.telegram_chat_id AS TelegramChatId,
|
||||
s.thread_id AS ThreadId
|
||||
FROM sessions s
|
||||
JOIN game_groups g ON g.id = s.group_id
|
||||
WHERE s.id = @SessionId
|
||||
""",
|
||||
new { command.SessionId },
|
||||
transaction);
|
||||
|
||||
if (command.Status == RsvpStatus.Declined)
|
||||
{
|
||||
var decision = RsvpFlowRules.Evaluate(command.Status, session.Status, totalParticipants: 0, confirmedParticipants: 0);
|
||||
|
||||
if (decision.ShouldRevertSessionToConfirmationSent)
|
||||
{
|
||||
await connection.ExecuteAsync(
|
||||
"""
|
||||
UPDATE sessions
|
||||
SET status = @ConfirmationSent, updated_at = now()
|
||||
WHERE id = @SessionId AND status = @Confirmed
|
||||
""",
|
||||
new
|
||||
{
|
||||
command.SessionId,
|
||||
ConfirmationSent = SessionStatus.ConfirmationSent,
|
||||
Confirmed = SessionStatus.Confirmed
|
||||
},
|
||||
transaction);
|
||||
}
|
||||
|
||||
var declinedPlayer = await connection.QuerySingleAsync<string>(
|
||||
"SELECT display_name FROM players WHERE telegram_id = @TelegramUserId",
|
||||
new { command.TelegramUserId },
|
||||
transaction);
|
||||
|
||||
await transaction.CommitAsync(ct);
|
||||
|
||||
try
|
||||
{
|
||||
await bot.SendMessage(
|
||||
chatId: session.GmTelegramId,
|
||||
text: $"🚨 Отмена! {declinedPlayer} не сможет прийти на игру «{session.Title}».",
|
||||
cancellationToken: ct);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogWarning(ex, "Failed to send decline alert to GM for session {SessionId}", command.SessionId);
|
||||
}
|
||||
|
||||
await bot.AnswerCallbackQuery(
|
||||
callbackQueryId: command.CallbackQueryId,
|
||||
text: decision.CallbackText,
|
||||
cancellationToken: ct);
|
||||
}
|
||||
else
|
||||
{
|
||||
var counts = await connection.QuerySingleAsync<RsvpCounts>(
|
||||
"""
|
||||
SELECT
|
||||
count(*) AS Total,
|
||||
count(*) FILTER (WHERE rsvp_status = @Confirmed) AS Confirmed,
|
||||
count(*) FILTER (WHERE rsvp_status = @Declined) AS Declined
|
||||
FROM session_participants
|
||||
WHERE session_id = @SessionId AND is_gm = false
|
||||
AND registration_status = @Active
|
||||
""",
|
||||
new
|
||||
{
|
||||
command.SessionId,
|
||||
Confirmed = RsvpStatus.Confirmed,
|
||||
Declined = RsvpStatus.Declined,
|
||||
Active = ParticipantRegistrationStatus.Active
|
||||
},
|
||||
transaction);
|
||||
|
||||
var decision = RsvpFlowRules.Evaluate(command.Status, session.Status, counts.Total, counts.Confirmed);
|
||||
|
||||
if (decision.ShouldMarkSessionConfirmed)
|
||||
{
|
||||
await connection.ExecuteAsync(
|
||||
"""
|
||||
UPDATE sessions
|
||||
SET status = @Confirmed, updated_at = now()
|
||||
WHERE id = @SessionId
|
||||
""",
|
||||
new { command.SessionId, Confirmed = SessionStatus.Confirmed },
|
||||
transaction);
|
||||
}
|
||||
|
||||
await transaction.CommitAsync(ct);
|
||||
|
||||
if (decision.ShouldNotifyGroup)
|
||||
{
|
||||
try
|
||||
{
|
||||
await bot.SendMessage(
|
||||
chatId: session.TelegramChatId,
|
||||
messageThreadId: session.ThreadId,
|
||||
text: $"🎉 Игра «{session.Title}» подтверждена! Все участники на месте.",
|
||||
cancellationToken: ct);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogWarning(ex, "Failed to send group confirmation for session {SessionId}", command.SessionId);
|
||||
}
|
||||
}
|
||||
|
||||
if (decision.ShouldNotifyGm)
|
||||
{
|
||||
try
|
||||
{
|
||||
await bot.SendMessage(
|
||||
chatId: session.GmTelegramId,
|
||||
text: $"✅ Все подтвердили участие в «{session.Title}» ({session.ScheduledAt.FormatMoscow()} МСК).",
|
||||
cancellationToken: ct);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogWarning(ex, "Failed to send GM confirmation for session {SessionId}", command.SessionId);
|
||||
}
|
||||
}
|
||||
|
||||
await bot.AnswerCallbackQuery(
|
||||
callbackQueryId: command.CallbackQueryId,
|
||||
text: decision.CallbackText,
|
||||
cancellationToken: ct);
|
||||
}
|
||||
|
||||
await UpdateConfirmationMessage(command, session, ct);
|
||||
}
|
||||
|
||||
private async Task UpdateConfirmationMessage(HandleRsvpCommand command, SessionContext session, CancellationToken ct)
|
||||
{
|
||||
try
|
||||
{
|
||||
await using var connection = await dataSource.OpenConnectionAsync(ct);
|
||||
|
||||
var participants = (await connection.QueryAsync<ParticipantRsvp>(
|
||||
"""
|
||||
SELECT p.telegram_id AS TelegramId,
|
||||
p.display_name AS DisplayName,
|
||||
p.telegram_username AS TelegramUsername,
|
||||
sp.rsvp_status AS RsvpStatus
|
||||
FROM session_participants sp
|
||||
JOIN players p ON p.id = sp.player_id
|
||||
WHERE sp.session_id = @SessionId
|
||||
AND sp.is_gm = false
|
||||
AND sp.registration_status = @Active
|
||||
ORDER BY sp.responded_at NULLS LAST
|
||||
""",
|
||||
new { command.SessionId, Active = ParticipantRegistrationStatus.Active })).ToList();
|
||||
|
||||
var confirmed = participants.Where(p => p.RsvpStatus == RsvpStatus.Confirmed).ToList();
|
||||
var declined = participants.Where(p => p.RsvpStatus == RsvpStatus.Declined).ToList();
|
||||
var pending = participants.Where(p => p.RsvpStatus == RsvpStatus.Pending).ToList();
|
||||
|
||||
var lines = new List<string>
|
||||
{
|
||||
$"🎲 Подтвердите участие в «{session.Title}»",
|
||||
$"📅 {session.ScheduledAt.FormatMoscow()} (МСК)",
|
||||
string.Empty
|
||||
};
|
||||
|
||||
foreach (var participant in confirmed)
|
||||
{
|
||||
lines.Add($" ✅ {FormatName(participant)}");
|
||||
}
|
||||
|
||||
foreach (var participant in declined)
|
||||
{
|
||||
lines.Add($" ❌ ~~{FormatName(participant)}~~");
|
||||
}
|
||||
|
||||
foreach (var participant in pending)
|
||||
{
|
||||
lines.Add($" ⏳ {FormatName(participant)}");
|
||||
}
|
||||
|
||||
lines.Add(string.Empty);
|
||||
|
||||
if (confirmed.Count == participants.Count)
|
||||
{
|
||||
lines.Add($"Статус: ✅ все подтвердили ({confirmed.Count}/{participants.Count})");
|
||||
}
|
||||
else if (declined.Count > 0)
|
||||
{
|
||||
lines.Add($"Статус: ⚠️ есть отказы ({confirmed.Count}/{participants.Count} подтвердили)");
|
||||
}
|
||||
else
|
||||
{
|
||||
lines.Add($"Статус: ожидаем подтверждения ({confirmed.Count}/{participants.Count})");
|
||||
}
|
||||
|
||||
var text = string.Join("\n", lines);
|
||||
|
||||
var replyMarkup = confirmed.Count == participants.Count
|
||||
? null
|
||||
: new InlineKeyboardMarkup([
|
||||
[
|
||||
InlineKeyboardButton.WithCallbackData("✅ Буду", $"rsvp:confirm:{command.SessionId}"),
|
||||
InlineKeyboardButton.WithCallbackData("❌ Не смогу", $"rsvp:decline:{command.SessionId}")
|
||||
]
|
||||
]);
|
||||
|
||||
await bot.EditMessageText(
|
||||
chatId: command.ChatId,
|
||||
messageId: command.MessageId,
|
||||
text: text,
|
||||
replyMarkup: replyMarkup,
|
||||
cancellationToken: ct);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogWarning(ex, "Failed to update confirmation message for session {SessionId}", command.SessionId);
|
||||
}
|
||||
}
|
||||
|
||||
private static string FormatName(ParticipantRsvp participant) =>
|
||||
participant.TelegramUsername is not null ? $"@{participant.TelegramUsername}" : participant.DisplayName;
|
||||
}
|
||||
@@ -1,154 +0,0 @@
|
||||
using Dapper;
|
||||
using GmRelay.Bot.Features.Notifications;
|
||||
using GmRelay.Shared.Domain;
|
||||
using Npgsql;
|
||||
using Telegram.Bot;
|
||||
using Telegram.Bot.Types.ReplyMarkups;
|
||||
|
||||
namespace GmRelay.Bot.Features.Confirmation.SendConfirmation;
|
||||
|
||||
// ── DTOs for Dapper mapping ──────────────────────────────────────────
|
||||
|
||||
internal sealed record SessionInfo(
|
||||
Guid Id,
|
||||
string Title,
|
||||
DateTime ScheduledAt,
|
||||
Guid GroupId,
|
||||
long TelegramChatId,
|
||||
int? ThreadId,
|
||||
string NotificationMode);
|
||||
|
||||
internal sealed record ParticipantInfo(
|
||||
long TelegramId,
|
||||
string DisplayName,
|
||||
string? TelegramUsername);
|
||||
|
||||
// ── Handler ──────────────────────────────────────────────────────────
|
||||
|
||||
/// <summary>
|
||||
/// Sends the interactive confirmation message (inline keyboard) to the group chat.
|
||||
/// Called by SessionSchedulerService at T-24h.
|
||||
/// </summary>
|
||||
public sealed class SendConfirmationHandler(
|
||||
NpgsqlDataSource dataSource,
|
||||
ITelegramBotClient bot,
|
||||
DirectSessionNotificationSender directSender,
|
||||
ILogger<SendConfirmationHandler> logger) : ISendConfirmationHandler
|
||||
{
|
||||
public async Task HandleAsync(Guid sessionId, CancellationToken ct)
|
||||
{
|
||||
await using var connection = await dataSource.OpenConnectionAsync(ct);
|
||||
|
||||
// 1. Load session + group info
|
||||
var session = await connection.QuerySingleOrDefaultAsync<SessionInfo>(
|
||||
"""
|
||||
SELECT s.id, s.title, s.scheduled_at AS ScheduledAt, s.group_id AS GroupId,
|
||||
g.telegram_chat_id AS TelegramChatId,
|
||||
s.thread_id AS ThreadId,
|
||||
s.notification_mode AS NotificationMode
|
||||
FROM sessions s
|
||||
JOIN game_groups g ON g.id = s.group_id
|
||||
WHERE s.id = @SessionId AND s.status = @Planned
|
||||
""",
|
||||
new { SessionId = sessionId, Planned = SessionStatus.Planned });
|
||||
|
||||
if (session is null)
|
||||
{
|
||||
logger.LogWarning("Session {SessionId} not found or not in Planned status", sessionId);
|
||||
return;
|
||||
}
|
||||
|
||||
// 2. Load non-GM participants
|
||||
var participants = (await connection.QueryAsync<ParticipantInfo>(
|
||||
"""
|
||||
SELECT p.telegram_id AS TelegramId,
|
||||
p.display_name AS DisplayName,
|
||||
p.telegram_username AS TelegramUsername
|
||||
FROM session_participants sp
|
||||
JOIN players p ON p.id = sp.player_id
|
||||
WHERE sp.session_id = @SessionId
|
||||
AND sp.is_gm = false
|
||||
AND sp.registration_status = @Active
|
||||
""",
|
||||
new { SessionId = sessionId, Active = ParticipantRegistrationStatus.Active })).ToList();
|
||||
|
||||
if (participants.Count == 0)
|
||||
{
|
||||
logger.LogWarning("Session {SessionId} has no non-GM participants", sessionId);
|
||||
return;
|
||||
}
|
||||
|
||||
// 3. Build confirmation message
|
||||
var playerList = string.Join("\n", participants.Select(p =>
|
||||
$" ⏳ {FormatPlayerName(p)}"));
|
||||
|
||||
var text = $"""
|
||||
🎲 Подтвердите участие в «{session.Title}»
|
||||
📅 {session.ScheduledAt.FormatMoscow()} (МСК)
|
||||
|
||||
{playerList}
|
||||
|
||||
Статус: ожидаем подтверждения (0/{participants.Count})
|
||||
""";
|
||||
|
||||
var keyboard = new InlineKeyboardMarkup([
|
||||
[
|
||||
InlineKeyboardButton.WithCallbackData("✅ Буду", $"rsvp:confirm:{sessionId}"),
|
||||
InlineKeyboardButton.WithCallbackData("❌ Не смогу", $"rsvp:decline:{sessionId}")
|
||||
]
|
||||
]);
|
||||
|
||||
// 4. Send to group
|
||||
var message = await bot.SendMessage(
|
||||
chatId: session.TelegramChatId,
|
||||
messageThreadId: session.ThreadId,
|
||||
text: text,
|
||||
replyMarkup: keyboard,
|
||||
cancellationToken: ct);
|
||||
|
||||
// 5. Update session status, store message ID, and mark confirmation sent
|
||||
await connection.ExecuteAsync(
|
||||
"""
|
||||
UPDATE sessions
|
||||
SET status = @Status,
|
||||
confirmation_message_id = @MessageId,
|
||||
confirmation_sent_at = now(),
|
||||
updated_at = now()
|
||||
WHERE id = @SessionId
|
||||
AND confirmation_sent_at IS NULL
|
||||
""",
|
||||
new
|
||||
{
|
||||
SessionId = sessionId,
|
||||
Status = SessionStatus.ConfirmationSent,
|
||||
MessageId = message.MessageId
|
||||
});
|
||||
|
||||
var mode = SessionNotificationModeExtensions.FromDatabaseValue(session.NotificationMode);
|
||||
if (mode.ShouldSendDirectMessages())
|
||||
{
|
||||
var directText = $"""
|
||||
🎲 <b>Подтвердите участие в игре</b>
|
||||
|
||||
📌 <b>{System.Net.WebUtility.HtmlEncode(session.Title)}</b>
|
||||
📅 {session.ScheduledAt.FormatMoscow()} (МСК)
|
||||
|
||||
Ответьте кнопкой в групповом сообщении расписания.
|
||||
""";
|
||||
|
||||
await directSender.SendAsync(
|
||||
participants.Select(p => new DirectNotificationRecipient(p.TelegramId, p.DisplayName)),
|
||||
directText,
|
||||
"confirmation",
|
||||
sessionId,
|
||||
ct);
|
||||
}
|
||||
|
||||
logger.LogInformation(
|
||||
"Confirmation sent for session {SessionId} ({Title}), message_id={MessageId}",
|
||||
sessionId, session.Title, message.MessageId);
|
||||
}
|
||||
|
||||
internal static string FormatPlayerName(ParticipantInfo p) =>
|
||||
p.TelegramUsername is not null ? $"@{p.TelegramUsername}" : p.DisplayName;
|
||||
}
|
||||
@@ -1,134 +0,0 @@
|
||||
using Dapper;
|
||||
using GmRelay.Bot.Features.Notifications;
|
||||
using GmRelay.Shared.Domain;
|
||||
using Npgsql;
|
||||
using Telegram.Bot;
|
||||
|
||||
namespace GmRelay.Bot.Features.Reminders.SendJoinLink;
|
||||
|
||||
// ── DTOs ─────────────────────────────────────────────────────────────
|
||||
|
||||
internal sealed record JoinLinkSession(
|
||||
Guid Id,
|
||||
string Title,
|
||||
string JoinLink,
|
||||
DateTime ScheduledAt,
|
||||
long TelegramChatId,
|
||||
int? ThreadId,
|
||||
string NotificationMode);
|
||||
|
||||
internal sealed record ConfirmedPlayer(
|
||||
long TelegramId,
|
||||
string DisplayName,
|
||||
string? TelegramUsername);
|
||||
|
||||
// ── Handler ──────────────────────────────────────────────────────────
|
||||
|
||||
/// <summary>
|
||||
/// Sends the join link to the group chat at T-5min, tagging all confirmed players.
|
||||
/// Called by SessionSchedulerService.
|
||||
/// </summary>
|
||||
public sealed class SendJoinLinkHandler(
|
||||
NpgsqlDataSource dataSource,
|
||||
ITelegramBotClient bot,
|
||||
DirectSessionNotificationSender directSender,
|
||||
ILogger<SendJoinLinkHandler> logger) : ISendJoinLinkHandler
|
||||
{
|
||||
public async Task HandleAsync(Guid sessionId, CancellationToken ct)
|
||||
{
|
||||
await using var connection = await dataSource.OpenConnectionAsync(ct);
|
||||
|
||||
// 1. Load session
|
||||
var session = await connection.QuerySingleOrDefaultAsync<JoinLinkSession>(
|
||||
"""
|
||||
SELECT s.id, s.title, s.join_link AS JoinLink, s.scheduled_at AS ScheduledAt,
|
||||
g.telegram_chat_id AS TelegramChatId,
|
||||
s.thread_id AS ThreadId,
|
||||
s.notification_mode AS NotificationMode
|
||||
FROM sessions s
|
||||
JOIN game_groups g ON g.id = s.group_id
|
||||
WHERE s.id = @SessionId
|
||||
AND s.status = @Confirmed
|
||||
AND s.link_message_id IS NULL
|
||||
""",
|
||||
new { SessionId = sessionId, Confirmed = SessionStatus.Confirmed });
|
||||
|
||||
if (session is null)
|
||||
{
|
||||
logger.LogWarning("Session {SessionId} not eligible for join link", sessionId);
|
||||
return;
|
||||
}
|
||||
|
||||
// 2. Load confirmed players
|
||||
var players = (await connection.QueryAsync<ConfirmedPlayer>(
|
||||
"""
|
||||
SELECT p.telegram_id AS TelegramId,
|
||||
p.display_name AS DisplayName,
|
||||
p.telegram_username AS TelegramUsername
|
||||
FROM session_participants sp
|
||||
JOIN players p ON p.id = sp.player_id
|
||||
WHERE sp.session_id = @SessionId
|
||||
AND sp.rsvp_status = @Confirmed
|
||||
AND sp.registration_status = @Active
|
||||
""",
|
||||
new
|
||||
{
|
||||
SessionId = sessionId,
|
||||
Confirmed = RsvpStatus.Confirmed,
|
||||
Active = ParticipantRegistrationStatus.Active
|
||||
})).ToList();
|
||||
|
||||
// 3. Build message with player mentions
|
||||
var mentions = string.Join(", ", players.Select(p =>
|
||||
p.TelegramUsername is not null ? $"@{p.TelegramUsername}" : p.DisplayName));
|
||||
|
||||
var text = $"""
|
||||
🎮 Игра «{session.Title}» начинается через 5 минут!
|
||||
|
||||
🔗 Ссылка на подключение:
|
||||
{session.JoinLink}
|
||||
|
||||
Участники: {mentions}
|
||||
|
||||
Хорошей игры! 🎲
|
||||
""";
|
||||
|
||||
// 4. Send
|
||||
var message = await bot.SendMessage(
|
||||
chatId: session.TelegramChatId,
|
||||
messageThreadId: session.ThreadId,
|
||||
text: text,
|
||||
cancellationToken: ct);
|
||||
|
||||
// 5. Mark as sent (idempotent — link_message_id IS NULL guard in query)
|
||||
await connection.ExecuteAsync(
|
||||
"""
|
||||
UPDATE sessions
|
||||
SET link_message_id = @MessageId, updated_at = now()
|
||||
WHERE id = @SessionId AND link_message_id IS NULL
|
||||
""",
|
||||
new { SessionId = sessionId, MessageId = message.MessageId });
|
||||
|
||||
var mode = SessionNotificationModeExtensions.FromDatabaseValue(session.NotificationMode);
|
||||
if (mode.ShouldSendDirectMessages())
|
||||
{
|
||||
var directText = $"""
|
||||
🎮 <b>Игра начинается через 5 минут</b>
|
||||
|
||||
📌 <b>{System.Net.WebUtility.HtmlEncode(session.Title)}</b>
|
||||
🔗 {System.Net.WebUtility.HtmlEncode(session.JoinLink)}
|
||||
""";
|
||||
|
||||
await directSender.SendAsync(
|
||||
players.Select(p => new DirectNotificationRecipient(p.TelegramId, p.DisplayName)),
|
||||
directText,
|
||||
"join-link",
|
||||
sessionId,
|
||||
ct);
|
||||
}
|
||||
|
||||
logger.LogInformation(
|
||||
"Join link sent for session {SessionId} ({Title}), message_id={MessageId}",
|
||||
sessionId, session.Title, message.MessageId);
|
||||
}
|
||||
}
|
||||
@@ -42,12 +42,13 @@ public sealed class CancelSessionHandler(
|
||||
FROM group_managers gm
|
||||
JOIN players p ON p.id = gm.player_id
|
||||
WHERE gm.group_id = s.group_id
|
||||
AND p.telegram_id = @TelegramUserId
|
||||
AND p.platform = 'Telegram'
|
||||
AND p.external_user_id = @ExternalUserId
|
||||
) AS CanManage
|
||||
FROM sessions s
|
||||
WHERE s.id = @SessionId
|
||||
""",
|
||||
new { command.SessionId, command.TelegramUserId }, transaction);
|
||||
new { command.SessionId, ExternalUserId = command.TelegramUserId.ToString() }, transaction);
|
||||
|
||||
if (session == null)
|
||||
{
|
||||
@@ -89,7 +90,7 @@ public sealed class CancelSessionHandler(
|
||||
|
||||
var directRecipients = (await connection.QueryAsync<DirectNotificationRecipient>(
|
||||
"""
|
||||
SELECT p.telegram_id AS TelegramId,
|
||||
SELECT p.external_user_id::BIGINT AS TelegramId,
|
||||
p.display_name AS DisplayName
|
||||
FROM session_participants sp
|
||||
JOIN players p ON sp.player_id = p.id
|
||||
|
||||
@@ -1,292 +1,178 @@
|
||||
using Dapper;
|
||||
using GmRelay.Shared.Domain;
|
||||
using GmRelay.Shared.Rendering;
|
||||
using GmRelay.Shared.Platform;
|
||||
using GmRelay.Bot.Infrastructure.Telegram;
|
||||
using Npgsql;
|
||||
using Telegram.Bot;
|
||||
using Telegram.Bot.Types;
|
||||
using GmRelay.Bot.Infrastructure.Telegram;
|
||||
|
||||
namespace GmRelay.Bot.Features.Sessions.CreateSession;
|
||||
|
||||
internal sealed record SessionCreationGroupAccessDto(Guid GroupId, bool CanManage);
|
||||
|
||||
public sealed class CreateSessionHandler(
|
||||
GmRelay.Shared.Features.Sessions.CreateSession.CreateSessionHandler sharedHandler,
|
||||
NpgsqlDataSource dataSource,
|
||||
ITelegramBotClient botClient,
|
||||
IPlatformMessenger messenger,
|
||||
ILogger<CreateSessionHandler> logger)
|
||||
{
|
||||
public async Task HandleAsync(Message message, CancellationToken cancellationToken)
|
||||
public async Task HandleAsync(Message message, CancellationToken ct)
|
||||
{
|
||||
var parseResult = NewSessionCommandParser.Parse(message.Text ?? message.Caption, DateTimeOffset.UtcNow);
|
||||
|
||||
foreach (var timeInput in parseResult.PastTimeInputs)
|
||||
{
|
||||
await botClient.SendMessage(
|
||||
message.Chat.Id,
|
||||
await messenger.SendGroupMessageAsync(
|
||||
TelegramPlatformIds.Group(message.Chat.Id, null),
|
||||
$"⚠️ Предупреждение: дата {timeInput} находится в прошлом и будет пропущена.",
|
||||
cancellationToken: cancellationToken);
|
||||
ct);
|
||||
}
|
||||
|
||||
foreach (var timeInput in parseResult.InvalidTimeInputs)
|
||||
{
|
||||
await botClient.SendMessage(
|
||||
message.Chat.Id,
|
||||
await messenger.SendGroupMessageAsync(
|
||||
TelegramPlatformIds.Group(message.Chat.Id, null),
|
||||
$"⚠️ Предупреждение: некорректный формат времени '{timeInput}'. Пропущено.",
|
||||
cancellationToken: cancellationToken);
|
||||
ct);
|
||||
}
|
||||
|
||||
foreach (var seatLimitInput in parseResult.InvalidSeatLimitInputs)
|
||||
{
|
||||
await botClient.SendMessage(
|
||||
message.Chat.Id,
|
||||
await messenger.SendGroupMessageAsync(
|
||||
TelegramPlatformIds.Group(message.Chat.Id, null),
|
||||
$"⚠️ Предупреждение: некорректный лимит мест '{seatLimitInput}'. Укажите целое число больше 0.",
|
||||
cancellationToken: cancellationToken);
|
||||
ct);
|
||||
}
|
||||
|
||||
foreach (var recurringInput in parseResult.InvalidRecurringInputs)
|
||||
{
|
||||
await botClient.SendMessage(
|
||||
message.Chat.Id,
|
||||
await messenger.SendGroupMessageAsync(
|
||||
TelegramPlatformIds.Group(message.Chat.Id, null),
|
||||
$"⚠️ Предупреждение: некорректный повтор расписания '{recurringInput}'. Укажите число игр 1-52 и шаг 1-365 дней.",
|
||||
cancellationToken: cancellationToken);
|
||||
ct);
|
||||
}
|
||||
|
||||
if (!parseResult.IsValid)
|
||||
{
|
||||
await botClient.SendMessage(
|
||||
chatId: message.Chat.Id,
|
||||
text: "❌ Не удалось распознать формат. Пожалуйста, используйте шаблон:\n\n/newsession\nНазвание: My Game\nВремя: 15.05.2026 19:30\nВремя: 22.05.2026 19:30\nМест: 4\nСсылка: https://link\nКартинка: https://cover\n\nДля повтора можно указать одну дату и строки:\nИгр: 4\nИнтервал: 7",
|
||||
cancellationToken: cancellationToken);
|
||||
await messenger.SendGroupMessageAsync(
|
||||
TelegramPlatformIds.Group(message.Chat.Id, null),
|
||||
"""
|
||||
❌ Не удалось распознать формат. Пожалуйста, используйте шаблон:
|
||||
|
||||
/newsession
|
||||
Название: My Game
|
||||
Время: 15.05.2026 19:30
|
||||
Время: 22.05.2026 19:30
|
||||
Мест: 4
|
||||
Ссылка: https://link
|
||||
Картинка: https://cover
|
||||
|
||||
Для повтора можно указать одну дату и строки:
|
||||
Игр: 4
|
||||
Интервал: 7
|
||||
""",
|
||||
ct);
|
||||
return;
|
||||
}
|
||||
|
||||
var title = parseResult.Title!;
|
||||
var link = parseResult.Link!;
|
||||
var imageReference = GetBatchImageReference(message, parseResult.ImageUrl);
|
||||
var gmId = message.From!.Id;
|
||||
var gmName = message.From.FirstName + (string.IsNullOrEmpty(message.From.LastName) ? string.Empty : $" {message.From.LastName}");
|
||||
var gmUsername = message.From.Username;
|
||||
|
||||
var chatId = message.Chat.Id;
|
||||
var chatTitle = message.Chat.Title ?? "Private Chat";
|
||||
var topicDestination = TelegramTopicRouting.ResolveNewScheduleDestination(
|
||||
message.Chat.IsForum,
|
||||
message.MessageThreadId);
|
||||
var topicCreatedByBot = topicDestination.TopicCreatedByBot;
|
||||
var messageThreadId = topicDestination.MessageThreadId;
|
||||
|
||||
await using var connection = await dataSource.OpenConnectionAsync(cancellationToken);
|
||||
await using var transaction = await connection.BeginTransactionAsync(cancellationToken);
|
||||
|
||||
try
|
||||
if (topicDestination.ShouldCreateForumTopic)
|
||||
{
|
||||
await connection.ExecuteAsync(
|
||||
"""
|
||||
INSERT INTO players (telegram_id, display_name, telegram_username, platform, external_user_id, external_username)
|
||||
VALUES (@TgId, @Name, @Username, 'Telegram', @TgId::TEXT, @Username)
|
||||
ON CONFLICT (telegram_id) DO UPDATE
|
||||
SET display_name = EXCLUDED.display_name,
|
||||
telegram_username = EXCLUDED.telegram_username,
|
||||
platform = COALESCE(players.platform, 'Telegram'),
|
||||
external_user_id = COALESCE(players.external_user_id, EXCLUDED.telegram_id::TEXT),
|
||||
external_username = COALESCE(players.external_username, EXCLUDED.telegram_username);
|
||||
""",
|
||||
new { TgId = gmId, Name = gmName, Username = gmUsername },
|
||||
transaction);
|
||||
|
||||
var existingGroup = await connection.QuerySingleOrDefaultAsync<SessionCreationGroupAccessDto>(
|
||||
"""
|
||||
SELECT g.id AS GroupId,
|
||||
EXISTS (
|
||||
SELECT 1
|
||||
FROM group_managers gm
|
||||
JOIN players p ON p.id = gm.player_id
|
||||
WHERE gm.group_id = g.id
|
||||
AND COALESCE(p.external_user_id, p.telegram_id::TEXT) = @GmId::TEXT
|
||||
) AS CanManage
|
||||
FROM game_groups g
|
||||
WHERE COALESCE(g.external_group_id, g.telegram_chat_id::TEXT) = @ChatId::TEXT
|
||||
""",
|
||||
new { ChatId = chatId, GmId = gmId },
|
||||
transaction);
|
||||
|
||||
Guid groupId;
|
||||
if (existingGroup is null)
|
||||
{
|
||||
groupId = await connection.ExecuteScalarAsync<Guid>(
|
||||
"""
|
||||
INSERT INTO game_groups (telegram_chat_id, name, gm_telegram_id, platform, external_group_id)
|
||||
VALUES (@ChatId, @ChatName, @GmId, 'Telegram', @ChatId::TEXT)
|
||||
RETURNING id;
|
||||
""",
|
||||
new { ChatId = chatId, ChatName = chatTitle, GmId = gmId },
|
||||
transaction);
|
||||
|
||||
await connection.ExecuteAsync(
|
||||
"""
|
||||
INSERT INTO group_managers (group_id, player_id, role)
|
||||
SELECT @GroupId, p.id, @OwnerRole
|
||||
FROM players p
|
||||
WHERE COALESCE(p.external_user_id, p.telegram_id::TEXT) = @GmId::TEXT
|
||||
ON CONFLICT (group_id, player_id) DO NOTHING
|
||||
""",
|
||||
new { GroupId = groupId, GmId = gmId, OwnerRole = GroupManagerRoleExtensions.OwnerValue },
|
||||
transaction);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!existingGroup.CanManage)
|
||||
{
|
||||
await transaction.RollbackAsync(cancellationToken);
|
||||
await botClient.SendMessage(
|
||||
chatId,
|
||||
"⛔ Только owner или co-GM этой группы может создавать игровые сессии.",
|
||||
cancellationToken: cancellationToken);
|
||||
return;
|
||||
}
|
||||
|
||||
groupId = existingGroup.GroupId;
|
||||
await connection.ExecuteAsync(
|
||||
"UPDATE game_groups SET name = @ChatName WHERE id = @GroupId",
|
||||
new { ChatName = chatTitle, GroupId = groupId },
|
||||
transaction);
|
||||
}
|
||||
|
||||
var topicDestination = TelegramTopicRouting.ResolveNewScheduleDestination(
|
||||
message.Chat.IsForum,
|
||||
message.MessageThreadId);
|
||||
var messageThreadId = topicDestination.MessageThreadId;
|
||||
var topicCreatedByBot = topicDestination.TopicCreatedByBot;
|
||||
if (topicDestination.ShouldCreateForumTopic)
|
||||
{
|
||||
try
|
||||
{
|
||||
var topic = await botClient.CreateForumTopic(
|
||||
chatId: chatId,
|
||||
name: $"🎲 Игры: {title}",
|
||||
cancellationToken: cancellationToken);
|
||||
messageThreadId = topic.MessageThreadId;
|
||||
}
|
||||
catch (Telegram.Bot.Exceptions.ApiRequestException ex)
|
||||
when (TelegramTopicRouting.IsMissingForumTopicRightsError(ex.Message))
|
||||
{
|
||||
await transaction.RollbackAsync(cancellationToken);
|
||||
await botClient.SendMessage(
|
||||
chatId,
|
||||
TelegramTopicRouting.MissingForumTopicRightsMessage,
|
||||
cancellationToken: cancellationToken);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
var batchId = Guid.NewGuid();
|
||||
var sessions = new List<SessionBatchDto>();
|
||||
|
||||
foreach (var scheduledAt in parseResult.ScheduledTimes.OrderBy(value => value))
|
||||
{
|
||||
var sessionId = await connection.ExecuteScalarAsync<Guid>(
|
||||
"""
|
||||
INSERT INTO sessions (batch_id, group_id, title, join_link, scheduled_at, status, thread_id, topic_created_by_bot, max_players)
|
||||
VALUES (@BatchId, @GroupId, @Title, @Link, @ScheduledAt, @Status, @ThreadId, @TopicCreatedByBot, @MaxPlayers)
|
||||
RETURNING id;
|
||||
""",
|
||||
new
|
||||
{
|
||||
BatchId = batchId,
|
||||
GroupId = groupId,
|
||||
Title = title,
|
||||
Link = link,
|
||||
ScheduledAt = scheduledAt,
|
||||
ThreadId = messageThreadId,
|
||||
TopicCreatedByBot = topicCreatedByBot,
|
||||
MaxPlayers = parseResult.MaxPlayers,
|
||||
Status = SessionStatus.Planned
|
||||
},
|
||||
transaction);
|
||||
|
||||
sessions.Add(new SessionBatchDto(sessionId, scheduledAt.UtcDateTime, SessionStatus.Planned, parseResult.MaxPlayers, link));
|
||||
}
|
||||
|
||||
await transaction.CommitAsync(cancellationToken);
|
||||
logger.LogInformation("Создан батч {BatchId} с {Count} сессиями в группе {GroupId}", batchId, sessions.Count, groupId);
|
||||
|
||||
var view = SessionBatchViewBuilder.Build(title, sessions, Array.Empty<ParticipantBatchDto>());
|
||||
var renderResult = TelegramSessionBatchRenderer.Render(view);
|
||||
|
||||
Message batchMessage;
|
||||
|
||||
if (imageReference is not null && renderResult.Text.Length <= 1024)
|
||||
{
|
||||
// Картинка + расписание умещаются в одном Telegram-фото с подписью
|
||||
try
|
||||
{
|
||||
batchMessage = await botClient.SendPhoto(
|
||||
chatId: chatId,
|
||||
messageThreadId: messageThreadId,
|
||||
photo: InputFile.FromString(imageReference),
|
||||
caption: renderResult.Text,
|
||||
parseMode: Telegram.Bot.Types.Enums.ParseMode.Html,
|
||||
replyMarkup: renderResult.Markup,
|
||||
cancellationToken: cancellationToken);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogWarning(ex, "Не удалось отправить картинку для батча {BatchId}, отправляем текстом", batchId);
|
||||
batchMessage = await botClient.SendMessage(
|
||||
chatId: chatId,
|
||||
messageThreadId: messageThreadId,
|
||||
text: renderResult.Text,
|
||||
parseMode: Telegram.Bot.Types.Enums.ParseMode.Html,
|
||||
replyMarkup: renderResult.Markup,
|
||||
cancellationToken: cancellationToken);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Текст слишком длинный для caption — fallback на два сообщения
|
||||
if (imageReference is not null)
|
||||
{
|
||||
try
|
||||
{
|
||||
await botClient.SendPhoto(
|
||||
chatId: chatId,
|
||||
messageThreadId: messageThreadId,
|
||||
photo: InputFile.FromString(imageReference),
|
||||
caption: $"🎲 {System.Net.WebUtility.HtmlEncode(title)}",
|
||||
parseMode: Telegram.Bot.Types.Enums.ParseMode.Html,
|
||||
cancellationToken: cancellationToken);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogWarning(ex, "Не удалось отправить картинку для батча {BatchId}", batchId);
|
||||
}
|
||||
}
|
||||
|
||||
batchMessage = await botClient.SendMessage(
|
||||
chatId: chatId,
|
||||
messageThreadId: messageThreadId,
|
||||
text: renderResult.Text,
|
||||
parseMode: Telegram.Bot.Types.Enums.ParseMode.Html,
|
||||
replyMarkup: renderResult.Markup,
|
||||
cancellationToken: cancellationToken);
|
||||
}
|
||||
|
||||
await connection.ExecuteAsync(
|
||||
"UPDATE sessions SET batch_message_id = @MsgId WHERE batch_id = @BatchId",
|
||||
new { MsgId = batchMessage.MessageId, BatchId = batchId });
|
||||
|
||||
try
|
||||
{
|
||||
await botClient.DeleteMessage(
|
||||
chatId: chatId,
|
||||
messageId: message.MessageId,
|
||||
cancellationToken: cancellationToken);
|
||||
var topicRef = await messenger.CreateThreadAsync(
|
||||
TelegramPlatformIds.Group(message.Chat.Id, null),
|
||||
$"🎲 Игры: {parseResult.Title}",
|
||||
ct);
|
||||
messageThreadId = int.Parse(topicRef.ExternalThreadId!, System.Globalization.CultureInfo.InvariantCulture);
|
||||
}
|
||||
catch (Exception ex)
|
||||
when (ex.Message.Contains("not enough rights") ||
|
||||
ex.Message.Contains("CHAT_ADMIN_REQUIRED") ||
|
||||
ex.Message.Contains("not an administrator"))
|
||||
{
|
||||
logger.LogWarning(ex, "Не удалось удалить исходное сообщение {MessageId} в чате {ChatId}", message.MessageId, chatId);
|
||||
await messenger.SendGroupMessageAsync(
|
||||
TelegramPlatformIds.Group(message.Chat.Id, null),
|
||||
TelegramTopicRouting.MissingForumTopicRightsMessage,
|
||||
ct);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
var platformGroup = TelegramPlatformIds.Group(message.Chat.Id, messageThreadId, message.Chat.Title ?? "Private Chat");
|
||||
var platformUser = new PlatformUser(
|
||||
PlatformKind.Telegram,
|
||||
gmId.ToString(System.Globalization.CultureInfo.InvariantCulture),
|
||||
gmName,
|
||||
gmUsername);
|
||||
|
||||
var command = new GmRelay.Shared.Features.Sessions.CreateSession.CreateSessionCommand(
|
||||
platformUser,
|
||||
platformGroup,
|
||||
parseResult.Title!,
|
||||
parseResult.Link!,
|
||||
parseResult.ScheduledTimes,
|
||||
parseResult.MaxPlayers,
|
||||
imageReference);
|
||||
|
||||
GmRelay.Shared.Features.Sessions.CreateSession.CreateSessionResult result;
|
||||
try
|
||||
{
|
||||
result = await sharedHandler.HandleAsync(command, ct);
|
||||
}
|
||||
catch
|
||||
{
|
||||
await messenger.SendGroupMessageAsync(
|
||||
TelegramPlatformIds.Group(message.Chat.Id, null),
|
||||
"💥 Произошла ошибка базы данных при создании сессии.",
|
||||
ct);
|
||||
throw;
|
||||
}
|
||||
|
||||
if (!result.Success)
|
||||
{
|
||||
await messenger.SendGroupMessageAsync(
|
||||
TelegramPlatformIds.Group(message.Chat.Id, null),
|
||||
result.ErrorMessage!,
|
||||
ct);
|
||||
return;
|
||||
}
|
||||
|
||||
var scheduleMessage = new PlatformScheduleMessage(
|
||||
platformGroup,
|
||||
result.View!,
|
||||
null,
|
||||
imageReference);
|
||||
|
||||
var sentMessageRef = await messenger.SendScheduleAsync(scheduleMessage, ct);
|
||||
|
||||
// Store batch_message_id
|
||||
if (int.TryParse(sentMessageRef.ExternalMessageId, System.Globalization.NumberStyles.Integer, System.Globalization.CultureInfo.InvariantCulture, out var batchMessageId))
|
||||
{
|
||||
await using var connection = await dataSource.OpenConnectionAsync(ct);
|
||||
await connection.ExecuteAsync(
|
||||
"UPDATE sessions SET batch_message_id = @MsgId WHERE batch_id = @BatchId",
|
||||
new { MsgId = batchMessageId, BatchId = result.BatchId });
|
||||
}
|
||||
|
||||
// Delete original message
|
||||
try
|
||||
{
|
||||
await messenger.DeleteMessageAsync(
|
||||
TelegramPlatformIds.Message(message.Chat.Id, null, message.MessageId),
|
||||
ct);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, "Ошибка при создании сессии");
|
||||
await transaction.RollbackAsync(cancellationToken);
|
||||
await botClient.SendMessage(chatId, "💥 Произошла ошибка базы данных при создании сессии.", cancellationToken: cancellationToken);
|
||||
logger.LogWarning(ex, "Не удалось удалить исходное сообщение {MessageId} в чате {ChatId}", message.MessageId, message.Chat.Id);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -41,13 +41,14 @@ public sealed class PromoteWaitlistedPlayerHandler(
|
||||
FROM group_managers gm
|
||||
JOIN players p ON p.id = gm.player_id
|
||||
WHERE gm.group_id = s.group_id
|
||||
AND p.telegram_id = @TelegramUserId
|
||||
AND p.platform = 'Telegram'
|
||||
AND p.external_user_id = @ExternalUserId
|
||||
) AS CanManage
|
||||
FROM sessions s
|
||||
WHERE s.id = @SessionId
|
||||
FOR UPDATE
|
||||
""",
|
||||
new { command.SessionId, command.TelegramUserId },
|
||||
new { command.SessionId, ExternalUserId = command.TelegramUserId.ToString() },
|
||||
transaction);
|
||||
|
||||
if (session is null)
|
||||
@@ -150,7 +151,7 @@ public sealed class PromoteWaitlistedPlayerHandler(
|
||||
"""
|
||||
SELECT sp.session_id AS SessionId,
|
||||
p.display_name AS DisplayName,
|
||||
p.telegram_username AS TelegramUsername,
|
||||
p.external_username AS TelegramUsername,
|
||||
sp.registration_status AS RegistrationStatus
|
||||
FROM session_participants sp
|
||||
JOIN players p ON sp.player_id = p.id
|
||||
|
||||
@@ -1,113 +1,25 @@
|
||||
using System.Text;
|
||||
using Dapper;
|
||||
using GmRelay.Bot.Infrastructure.Telegram;
|
||||
using GmRelay.Shared.Domain;
|
||||
using GmRelay.Shared.Platform;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Npgsql;
|
||||
using Telegram.Bot.Types;
|
||||
|
||||
namespace GmRelay.Bot.Features.Sessions.ExportCalendar;
|
||||
|
||||
internal sealed record CalendarSessionDto(Guid Id, string Title, DateTime ScheduledAt);
|
||||
|
||||
public sealed class ExportCalendarHandler(
|
||||
NpgsqlDataSource dataSource,
|
||||
IPlatformMessenger messenger,
|
||||
IConfiguration configuration)
|
||||
GmRelay.Shared.Features.Sessions.ExportCalendar.ExportCalendarHandler sharedHandler)
|
||||
{
|
||||
public async Task HandleAsync(Message message, CancellationToken cancellationToken)
|
||||
public Task HandleAsync(Message message, CancellationToken cancellationToken)
|
||||
{
|
||||
await using var connection = await dataSource.OpenConnectionAsync(cancellationToken);
|
||||
var command = new GmRelay.Shared.Features.Sessions.ExportCalendar.ExportCalendarCommand(
|
||||
new PlatformGroup(
|
||||
PlatformKind.Telegram,
|
||||
message.Chat.Id.ToString(),
|
||||
message.Chat.Title ?? "Private Chat",
|
||||
message.MessageThreadId?.ToString()),
|
||||
new PlatformUser(
|
||||
PlatformKind.Telegram,
|
||||
message.From?.Id.ToString() ?? string.Empty,
|
||||
message.From?.FirstName ?? string.Empty,
|
||||
message.From?.Username));
|
||||
|
||||
var sessions = await connection.QueryAsync<CalendarSessionDto>(
|
||||
@"SELECT s.id as Id, s.title as Title, s.scheduled_at as ScheduledAt"
|
||||
+ " FROM sessions s"
|
||||
+ " JOIN game_groups g ON s.group_id = g.id"
|
||||
+ " WHERE g.telegram_chat_id = @ChatId"
|
||||
+ " AND s.status = @Planned"
|
||||
+ " AND s.scheduled_at > NOW()"
|
||||
+ " ORDER BY s.scheduled_at ASC",
|
||||
new { ChatId = message.Chat.Id, Planned = SessionStatus.Planned });
|
||||
|
||||
var sessionsList = sessions.ToList();
|
||||
|
||||
if (sessionsList.Count == 0)
|
||||
{
|
||||
await messenger.SendGroupMessageAsync(
|
||||
TelegramPlatformIds.Group(message.Chat.Id, message.MessageThreadId),
|
||||
"📭 У этой группы нет запланированных сессий для экспорта.",
|
||||
cancellationToken);
|
||||
return;
|
||||
}
|
||||
|
||||
var sb = new StringBuilder();
|
||||
sb.AppendLine("BEGIN:VCALENDAR");
|
||||
sb.AppendLine("VERSION:2.0");
|
||||
sb.AppendLine("PRODID:-//GM-Relay//TTRPG Schedule//EN");
|
||||
|
||||
foreach (var s in sessionsList)
|
||||
{
|
||||
var dtStart = s.ScheduledAt.ToString("yyyyMMddTHHmmssZ");
|
||||
var dtEnd = s.ScheduledAt.AddHours(4).ToString("yyyyMMddTHHmmssZ");
|
||||
|
||||
sb.AppendLine("BEGIN:VEVENT");
|
||||
sb.AppendLine($"UID:{s.Id}@gmrelay");
|
||||
sb.AppendLine($"DTSTAMP:{DateTime.UtcNow:yyyyMMddTHHmmssZ}");
|
||||
sb.AppendLine($"DTSTART:{dtStart}");
|
||||
sb.AppendLine($"DTEND:{dtEnd}");
|
||||
sb.AppendLine($"SUMMARY:{s.Title}");
|
||||
sb.AppendLine("END:VEVENT");
|
||||
}
|
||||
|
||||
sb.AppendLine("END:VCALENDAR");
|
||||
|
||||
var bytes = Encoding.UTF8.GetBytes(sb.ToString());
|
||||
|
||||
|
||||
// Create calendar subscription
|
||||
string? subscriptionUrl = null;
|
||||
var baseUrl = configuration["Web:BaseUrl"];
|
||||
var senderId = message.From?.Id;
|
||||
if (!string.IsNullOrWhiteSpace(baseUrl) && senderId.HasValue)
|
||||
{
|
||||
try
|
||||
{
|
||||
var token = Guid.NewGuid().ToString("N");
|
||||
var groupId = await connection.QueryFirstOrDefaultAsync<Guid?>(
|
||||
@"SELECT id FROM game_groups WHERE telegram_chat_id = @ChatId",
|
||||
new { ChatId = message.Chat.Id });
|
||||
|
||||
await connection.ExecuteAsync(
|
||||
@"INSERT INTO calendar_subscriptions (id, token, user_telegram_id, group_id, filter_type, created_at, expires_at)
|
||||
VALUES (gen_random_uuid(), @token, @userTelegramId, @groupId, @filterType, now(), NULL)",
|
||||
new { token, userTelegramId = senderId.Value, groupId, filterType = (int)CalendarSubscriptionFilter.SpecificGroup });
|
||||
|
||||
subscriptionUrl = $"{baseUrl.TrimEnd('/')}/calendar/{token}.ics";
|
||||
}
|
||||
catch
|
||||
{
|
||||
// Non-critical: if subscription creation fails, still send the file
|
||||
}
|
||||
}
|
||||
|
||||
var actions = subscriptionUrl is not null
|
||||
? new[]
|
||||
{
|
||||
new PlatformMessageAction(
|
||||
"calendar-subscription",
|
||||
"🔗 Подписаться на календарь",
|
||||
subscriptionUrl)
|
||||
}
|
||||
: Array.Empty<PlatformMessageAction>();
|
||||
|
||||
await messenger.SendCalendarFileAsync(
|
||||
new PlatformCalendarFile(
|
||||
TelegramPlatformIds.Group(message.Chat.Id, message.MessageThreadId),
|
||||
"schedule.ics",
|
||||
bytes,
|
||||
"📅 <b>Ваш календарь игр!</b>\nОткройте файл на устройстве, чтобы добавить события в свой календарь.",
|
||||
actions),
|
||||
cancellationToken);
|
||||
return sharedHandler.HandleAsync(command, cancellationToken);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,5 @@
|
||||
using Dapper;
|
||||
using Npgsql;
|
||||
using Telegram.Bot;
|
||||
using GmRelay.Bot.Infrastructure.Telegram;
|
||||
using GmRelay.Shared.Domain;
|
||||
using GmRelay.Shared.Platform;
|
||||
|
||||
namespace GmRelay.Bot.Features.Sessions.ListSessions;
|
||||
|
||||
@@ -13,138 +10,88 @@ public sealed record DeleteSessionCommand(
|
||||
long ChatId,
|
||||
int MessageId);
|
||||
|
||||
internal sealed record DeleteSessionInfoDto(
|
||||
string Title,
|
||||
Guid BatchId,
|
||||
Guid GroupId,
|
||||
bool CanManage,
|
||||
int? ThreadId,
|
||||
bool TopicCreatedByBot);
|
||||
|
||||
public sealed class DeleteSessionHandler(
|
||||
NpgsqlDataSource dataSource,
|
||||
ITelegramBotClient bot,
|
||||
GmRelay.Shared.Features.Sessions.ListSessions.DeleteSessionHandler sharedHandler,
|
||||
GmRelay.Shared.Features.Sessions.ListSessions.ListSessionsHandler listSessionsHandler,
|
||||
IPlatformMessenger messenger,
|
||||
ILogger<DeleteSessionHandler> logger)
|
||||
{
|
||||
public async Task HandleAsync(DeleteSessionCommand command, CancellationToken ct)
|
||||
{
|
||||
await using var connection = await dataSource.OpenConnectionAsync(ct);
|
||||
await using var transaction = await connection.BeginTransactionAsync(ct);
|
||||
var platformUser = new PlatformUser(
|
||||
PlatformKind.Telegram,
|
||||
command.TelegramUserId.ToString(),
|
||||
string.Empty,
|
||||
null);
|
||||
|
||||
// 1. Fetch session and verify group manager.
|
||||
var session = await connection.QuerySingleOrDefaultAsync<DeleteSessionInfoDto>(
|
||||
"""
|
||||
SELECT s.title AS Title,
|
||||
s.batch_id AS BatchId,
|
||||
s.group_id AS GroupId,
|
||||
s.thread_id AS ThreadId,
|
||||
s.topic_created_by_bot AS TopicCreatedByBot,
|
||||
EXISTS (
|
||||
SELECT 1
|
||||
FROM group_managers gm
|
||||
JOIN players p ON p.id = gm.player_id
|
||||
WHERE gm.group_id = s.group_id
|
||||
AND p.telegram_id = @TelegramUserId
|
||||
) AS CanManage
|
||||
FROM sessions s
|
||||
WHERE s.id = @SessionId
|
||||
""",
|
||||
new { command.SessionId, command.TelegramUserId }, transaction);
|
||||
var platformGroup = new PlatformGroup(
|
||||
PlatformKind.Telegram,
|
||||
command.ChatId.ToString(),
|
||||
string.Empty);
|
||||
|
||||
if (session == null)
|
||||
var scheduleMessage = TelegramPlatformIds.Message(command.ChatId, null, command.MessageId);
|
||||
|
||||
var sharedCommand = new GmRelay.Shared.Features.Sessions.ListSessions.DeleteSessionCommand(
|
||||
command.SessionId,
|
||||
platformUser,
|
||||
platformGroup,
|
||||
scheduleMessage);
|
||||
|
||||
var result = await sharedHandler.HandleAsync(sharedCommand, ct);
|
||||
|
||||
if (!result.Success)
|
||||
{
|
||||
await bot.AnswerCallbackQuery(command.CallbackQueryId, "Сессия не найдена.", cancellationToken: ct);
|
||||
await messenger.AnswerInteractionAsync(
|
||||
new PlatformInteractionReply(command.CallbackQueryId, result.ReplyText!, result.ReplyText!.Contains("owner")),
|
||||
ct);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!session.CanManage)
|
||||
{
|
||||
await bot.AnswerCallbackQuery(command.CallbackQueryId, "Только owner или co-GM может удалять сессию.", showAlert: true, cancellationToken: ct);
|
||||
return;
|
||||
}
|
||||
|
||||
// 2. Delete session
|
||||
await connection.ExecuteAsync("DELETE FROM sessions WHERE id = @Id", new { Id = command.SessionId }, transaction);
|
||||
|
||||
var remainingInTopic = session.ThreadId.HasValue
|
||||
? await connection.ExecuteScalarAsync<int>(
|
||||
"""
|
||||
SELECT COUNT(*)
|
||||
FROM sessions
|
||||
WHERE group_id = @GroupId
|
||||
AND thread_id = @ThreadId
|
||||
""",
|
||||
new { session.GroupId, ThreadId = session.ThreadId.Value },
|
||||
transaction)
|
||||
: 0;
|
||||
|
||||
await transaction.CommitAsync(ct);
|
||||
|
||||
// 4. If no sessions are left in a bot-owned forum topic, delete the topic.
|
||||
if (session.ThreadId.HasValue &&
|
||||
TelegramTopicRouting.ShouldDeleteForumTopic(session.TopicCreatedByBot, remainingInTopic))
|
||||
if (result.ThreadId.HasValue &&
|
||||
TelegramTopicRouting.ShouldDeleteForumTopic(result.TopicCreatedByBot, result.RemainingInTopic))
|
||||
{
|
||||
try
|
||||
{
|
||||
await bot.DeleteForumTopic(command.ChatId, session.ThreadId.Value, cancellationToken: ct);
|
||||
logger.LogInformation("Deleted forum topic {ThreadId} for batch {BatchId} as no sessions remained.", session.ThreadId.Value, session.BatchId);
|
||||
await messenger.DeleteThreadAsync(
|
||||
new PlatformGroup(PlatformKind.Telegram, command.ChatId.ToString(), string.Empty, null, result.ThreadId.Value.ToString(System.Globalization.CultureInfo.InvariantCulture)),
|
||||
ct);
|
||||
logger.LogInformation("Deleted forum topic {ThreadId} for batch {BatchId} as no sessions remained.", result.ThreadId.Value, result.GroupId);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogWarning(ex, "Failed to delete forum topic {ThreadId}", session.ThreadId.Value);
|
||||
logger.LogWarning(ex, "Failed to delete forum topic {ThreadId}", result.ThreadId.Value);
|
||||
}
|
||||
}
|
||||
|
||||
await bot.AnswerCallbackQuery(command.CallbackQueryId, "Сессия удалена!", cancellationToken: ct);
|
||||
await messenger.AnswerInteractionAsync(
|
||||
new PlatformInteractionReply(command.CallbackQueryId, result.ReplyText!),
|
||||
ct);
|
||||
|
||||
// 5. Update the /listsessions message (we delete the message or edit it to remove the button)
|
||||
// A simple way is to re-render the list:
|
||||
await using var readConnection = await dataSource.OpenConnectionAsync(ct);
|
||||
var sessions = await readConnection.QueryAsync<SessionListItemDto>(
|
||||
@"SELECT s.id as Id, s.title as Title, s.scheduled_at as ScheduledAt, s.status as Status, s.max_players as MaxPlayers,
|
||||
COUNT(sp.id) FILTER (WHERE sp.is_gm = false AND sp.registration_status = @Active) as PlayerCount,
|
||||
COUNT(sp.id) FILTER (WHERE sp.is_gm = false AND sp.registration_status = @Waitlisted) as WaitlistCount,
|
||||
EXISTS (
|
||||
SELECT 1
|
||||
FROM group_managers gm
|
||||
JOIN players manager_player ON manager_player.id = gm.player_id
|
||||
WHERE gm.group_id = s.group_id
|
||||
AND manager_player.telegram_id = @TelegramUserId
|
||||
) AS CanManage
|
||||
FROM sessions s
|
||||
JOIN game_groups g ON s.group_id = g.id
|
||||
LEFT JOIN session_participants sp ON s.id = sp.session_id
|
||||
WHERE g.telegram_chat_id = @ChatId AND s.status != @Cancelled AND s.scheduled_at > NOW()
|
||||
GROUP BY s.id, s.title, s.scheduled_at, s.status, s.max_players, s.group_id
|
||||
ORDER BY s.scheduled_at ASC",
|
||||
new
|
||||
{
|
||||
ChatId = command.ChatId,
|
||||
command.TelegramUserId,
|
||||
Cancelled = SessionStatus.Cancelled,
|
||||
Active = ParticipantRegistrationStatus.Active,
|
||||
Waitlisted = ParticipantRegistrationStatus.Waitlisted
|
||||
});
|
||||
// 5. Update the /listsessions message
|
||||
var listCommand = new GmRelay.Shared.Features.Sessions.ListSessions.ListSessionsCommand(platformGroup, platformUser);
|
||||
var listResult = await listSessionsHandler.HandleAsync(listCommand, ct);
|
||||
|
||||
var sessionsList = sessions.ToList();
|
||||
|
||||
if (sessionsList.Count == 0)
|
||||
if (listResult.Sessions.Count == 0)
|
||||
{
|
||||
try { await bot.EditMessageText(command.ChatId, command.MessageId, "📭 В этой группе нет предстоящих игр.", cancellationToken: ct); } catch { }
|
||||
try
|
||||
{
|
||||
await messenger.UpdateGroupMessageAsync(
|
||||
scheduleMessage,
|
||||
"📭 В этой группе нет предстоящих игр.",
|
||||
[],
|
||||
ct);
|
||||
}
|
||||
catch { }
|
||||
return;
|
||||
}
|
||||
|
||||
var renderResult = SessionListMessageRenderer.Render(sessionsList);
|
||||
var text = SessionListMessageRenderer.RenderText(listResult.Sessions);
|
||||
var actions = listResult.CanManage ? SessionListMessageRenderer.RenderActions(listResult.Sessions) : [];
|
||||
|
||||
try
|
||||
{
|
||||
await bot.EditMessageText(
|
||||
command.ChatId,
|
||||
command.MessageId,
|
||||
renderResult.Text,
|
||||
parseMode: Telegram.Bot.Types.Enums.ParseMode.Html,
|
||||
replyMarkup: renderResult.Markup,
|
||||
cancellationToken: ct);
|
||||
await messenger.UpdateGroupMessageAsync(scheduleMessage, text, actions, ct);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
||||
@@ -1,114 +1,37 @@
|
||||
using Dapper;
|
||||
using GmRelay.Shared.Domain;
|
||||
using Npgsql;
|
||||
using Telegram.Bot;
|
||||
using GmRelay.Shared.Platform;
|
||||
using Telegram.Bot.Types;
|
||||
using Telegram.Bot.Types.ReplyMarkups;
|
||||
|
||||
namespace GmRelay.Bot.Features.Sessions.ListSessions;
|
||||
|
||||
internal sealed record SessionListItemDto(Guid Id, string Title, DateTime ScheduledAt, string Status, int? MaxPlayers, int PlayerCount, int WaitlistCount, bool CanManage);
|
||||
|
||||
internal static class SessionListMessageRenderer
|
||||
{
|
||||
public static (string Text, InlineKeyboardMarkup? Markup) Render(IReadOnlyList<SessionListItemDto> sessions)
|
||||
{
|
||||
var text = "📅 <b>Ближайшие игры:</b>\n\n";
|
||||
foreach (var session in sessions)
|
||||
{
|
||||
var seats = session.MaxPlayers.HasValue
|
||||
? $"{session.PlayerCount}/{session.MaxPlayers.Value}"
|
||||
: session.PlayerCount.ToString(System.Globalization.CultureInfo.InvariantCulture);
|
||||
var waitlist = session.WaitlistCount > 0 ? $", ожидание: {session.WaitlistCount}" : string.Empty;
|
||||
text += $"🔹 <b>{session.ScheduledAt.FormatMoscow()}</b> — {System.Net.WebUtility.HtmlEncode(session.Title)} (Места: {seats}{waitlist})\n";
|
||||
}
|
||||
|
||||
var canManage = sessions.Count > 0 && sessions.First().CanManage;
|
||||
if (!canManage)
|
||||
{
|
||||
return (text, null);
|
||||
}
|
||||
|
||||
var buttons = new List<InlineKeyboardButton[]>();
|
||||
foreach (var session in sessions)
|
||||
{
|
||||
var dateTitle = session.ScheduledAt.FormatMoscowShort();
|
||||
buttons.Add(
|
||||
[
|
||||
InlineKeyboardButton.WithCallbackData($"❌ {dateTitle}", $"cancel_session:{session.Id}"),
|
||||
InlineKeyboardButton.WithCallbackData($"⏰ {dateTitle}", $"reschedule_session:{session.Id}")
|
||||
]);
|
||||
|
||||
if (SessionCapacityRules.CanPromoteWaitlistedPlayer(session.MaxPlayers, session.PlayerCount, session.WaitlistCount))
|
||||
{
|
||||
buttons.Add(
|
||||
[
|
||||
InlineKeyboardButton.WithCallbackData($"⬆️ Из ожидания {dateTitle}", $"promote_waitlist:{session.Id}")
|
||||
]);
|
||||
}
|
||||
|
||||
buttons.Add(
|
||||
[
|
||||
InlineKeyboardButton.WithCallbackData($"🗑 Удалить {dateTitle}", $"delete_session:{session.Id}")
|
||||
]);
|
||||
}
|
||||
|
||||
return (text, new InlineKeyboardMarkup(buttons));
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class ListSessionsHandler(
|
||||
NpgsqlDataSource dataSource,
|
||||
ITelegramBotClient botClient)
|
||||
GmRelay.Shared.Features.Sessions.ListSessions.ListSessionsHandler sharedHandler,
|
||||
IPlatformMessenger messenger)
|
||||
{
|
||||
public async Task HandleAsync(Message message, CancellationToken cancellationToken)
|
||||
{
|
||||
await using var connection = await dataSource.OpenConnectionAsync(cancellationToken);
|
||||
var command = new GmRelay.Shared.Features.Sessions.ListSessions.ListSessionsCommand(
|
||||
new PlatformGroup(
|
||||
PlatformKind.Telegram,
|
||||
message.Chat.Id.ToString(),
|
||||
message.Chat.Title ?? "Private Chat",
|
||||
message.MessageThreadId?.ToString()),
|
||||
new PlatformUser(
|
||||
PlatformKind.Telegram,
|
||||
message.From?.Id.ToString() ?? string.Empty,
|
||||
message.From?.FirstName ?? string.Empty,
|
||||
message.From?.Username));
|
||||
|
||||
var sessions = await connection.QueryAsync<SessionListItemDto>(
|
||||
@"SELECT s.id as Id, s.title as Title, s.scheduled_at as ScheduledAt, s.status as Status, s.max_players as MaxPlayers,
|
||||
COUNT(sp.id) FILTER (WHERE sp.is_gm = false AND sp.registration_status = @Active) as PlayerCount,
|
||||
COUNT(sp.id) FILTER (WHERE sp.is_gm = false AND sp.registration_status = @Waitlisted) as WaitlistCount,
|
||||
EXISTS (
|
||||
SELECT 1
|
||||
FROM group_managers gm
|
||||
JOIN players manager_player ON manager_player.id = gm.player_id
|
||||
WHERE gm.group_id = s.group_id
|
||||
AND manager_player.telegram_id = @TelegramUserId
|
||||
) AS CanManage
|
||||
FROM sessions s
|
||||
JOIN game_groups g ON s.group_id = g.id
|
||||
LEFT JOIN session_participants sp ON s.id = sp.session_id
|
||||
WHERE g.telegram_chat_id = @ChatId AND s.status != @Cancelled AND s.scheduled_at > NOW()
|
||||
GROUP BY s.id, s.title, s.scheduled_at, s.status, s.max_players, s.group_id
|
||||
ORDER BY s.scheduled_at ASC",
|
||||
new
|
||||
{
|
||||
ChatId = message.Chat.Id,
|
||||
TelegramUserId = message.From?.Id,
|
||||
Cancelled = SessionStatus.Cancelled,
|
||||
Active = ParticipantRegistrationStatus.Active,
|
||||
Waitlisted = ParticipantRegistrationStatus.Waitlisted
|
||||
});
|
||||
var result = await sharedHandler.HandleAsync(command, cancellationToken);
|
||||
|
||||
var sessionsList = sessions.ToList();
|
||||
|
||||
if (sessionsList.Count == 0)
|
||||
if (result.Sessions.Count == 0)
|
||||
{
|
||||
await botClient.SendMessage(
|
||||
chatId: message.Chat.Id,
|
||||
text: "📭 В этой группе нет предстоящих игр.",
|
||||
cancellationToken: cancellationToken);
|
||||
await messenger.SendGroupMessageAsync(command.Group, "📭 В этой группе нет предстоящих игр.", cancellationToken);
|
||||
return;
|
||||
}
|
||||
|
||||
var renderResult = SessionListMessageRenderer.Render(sessionsList);
|
||||
var text = SessionListMessageRenderer.RenderText(result.Sessions);
|
||||
var actions = result.CanManage ? SessionListMessageRenderer.RenderActions(result.Sessions) : [];
|
||||
|
||||
await botClient.SendMessage(
|
||||
chatId: message.Chat.Id,
|
||||
text: renderResult.Text,
|
||||
parseMode: Telegram.Bot.Types.Enums.ParseMode.Html,
|
||||
replyMarkup: renderResult.Markup,
|
||||
cancellationToken: cancellationToken);
|
||||
await messenger.SendGroupMessageAsync(command.Group, text, actions, cancellationToken);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
using GmRelay.Shared.Domain;
|
||||
using GmRelay.Shared.Platform;
|
||||
using GmRelay.Shared.Features.Sessions.ListSessions;
|
||||
|
||||
namespace GmRelay.Bot.Features.Sessions.ListSessions;
|
||||
|
||||
internal static class SessionListMessageRenderer
|
||||
{
|
||||
public static string RenderText(IReadOnlyList<SessionListItemDto> sessions)
|
||||
{
|
||||
var text = "📅 <b>Ближайшие игры:</b>\n\n";
|
||||
foreach (var session in sessions)
|
||||
{
|
||||
var seats = session.MaxPlayers.HasValue
|
||||
? $"{session.PlayerCount}/{session.MaxPlayers.Value}"
|
||||
: session.PlayerCount.ToString(System.Globalization.CultureInfo.InvariantCulture);
|
||||
var waitlist = session.WaitlistCount > 0 ? $", ожидание: {session.WaitlistCount}" : string.Empty;
|
||||
text += $"🔹 <b>{session.ScheduledAt.FormatMoscow()}</b> — {System.Net.WebUtility.HtmlEncode(session.Title)} (Места: {seats}{waitlist})\n";
|
||||
}
|
||||
|
||||
return text;
|
||||
}
|
||||
|
||||
public static IReadOnlyList<PlatformMessageAction> RenderActions(IReadOnlyList<SessionListItemDto> sessions)
|
||||
{
|
||||
if (sessions.Count == 0 || !sessions.First().CanManage)
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
var actions = new List<PlatformMessageAction>();
|
||||
|
||||
foreach (var session in sessions)
|
||||
{
|
||||
var dateTitle = session.ScheduledAt.FormatMoscowShort();
|
||||
|
||||
actions.Add(new PlatformMessageAction(
|
||||
$"cancel_session:{session.Id}",
|
||||
$"❌ {dateTitle}",
|
||||
$"cancel_session:{session.Id}"));
|
||||
|
||||
actions.Add(new PlatformMessageAction(
|
||||
$"reschedule_session:{session.Id}",
|
||||
$"⏰ {dateTitle}",
|
||||
$"reschedule_session:{session.Id}"));
|
||||
|
||||
if (SessionCapacityRules.CanPromoteWaitlistedPlayer(session.MaxPlayers, session.PlayerCount, session.WaitlistCount))
|
||||
{
|
||||
actions.Add(new PlatformMessageAction(
|
||||
$"promote_waitlist:{session.Id}",
|
||||
$"⬆️ Из ожидания {dateTitle}",
|
||||
$"promote_waitlist:{session.Id}"));
|
||||
}
|
||||
|
||||
actions.Add(new PlatformMessageAction(
|
||||
$"delete_session:{session.Id}",
|
||||
$"🗑 Удалить {dateTitle}",
|
||||
$"delete_session:{session.Id}"));
|
||||
}
|
||||
|
||||
return actions;
|
||||
}
|
||||
}
|
||||
+81
-217
@@ -1,6 +1,7 @@
|
||||
using Dapper;
|
||||
using GmRelay.Bot.Features.Notifications;
|
||||
using GmRelay.Shared.Domain;
|
||||
using GmRelay.Shared.Features.Sessions.RescheduleSession;
|
||||
using GmRelay.Shared.Platform;
|
||||
using GmRelay.Shared.Rendering;
|
||||
using Npgsql;
|
||||
@@ -11,247 +12,156 @@ using GmRelay.Bot.Infrastructure.Telegram;
|
||||
|
||||
namespace GmRelay.Bot.Features.Sessions.RescheduleSession;
|
||||
|
||||
// ── DTOs ─────────────────────────────────────────────────────────────
|
||||
|
||||
internal sealed record AwaitingProposalDto(
|
||||
Guid Id, Guid SessionId, string Title, DateTime CurrentScheduledAt,
|
||||
Guid BatchId, int? BatchMessageId, long TelegramChatId, int? ThreadId, string NotificationMode);
|
||||
|
||||
internal sealed record VoteParticipantDto(
|
||||
Guid PlayerId,
|
||||
string DisplayName,
|
||||
string? TelegramUsername,
|
||||
long TelegramId = 0);
|
||||
|
||||
// ── Handler ──────────────────────────────────────────────────────────
|
||||
|
||||
/// <summary>
|
||||
/// Handles text input from the GM who has an AwaitingTime proposal.
|
||||
/// Parses reschedule options with a voting deadline, creates a voting message,
|
||||
/// and tags all participants.
|
||||
/// If no participants are registered, reschedules immediately.
|
||||
/// Telegram adapter for reschedule time input.
|
||||
/// Delegates core logic to the shared handler, then performs Telegram-specific
|
||||
/// message sending, DM notifications, vote_message_id storage, and cleanup.
|
||||
/// </summary>
|
||||
public sealed class HandleRescheduleTimeInputHandler(
|
||||
GmRelay.Shared.Features.Sessions.RescheduleSession.HandleRescheduleTimeInputHandler sharedHandler,
|
||||
NpgsqlDataSource dataSource,
|
||||
ITelegramBotClient bot,
|
||||
IPlatformMessenger messenger,
|
||||
DirectSessionNotificationSender directSender,
|
||||
ILogger<HandleRescheduleTimeInputHandler> logger)
|
||||
{
|
||||
/// <summary>
|
||||
/// Attempts to handle a text message as reschedule time input.
|
||||
/// Returns true if it was handled (i.e. user had an AwaitingTime proposal).
|
||||
/// </summary>
|
||||
public async Task<bool> TryHandleAsync(Message message, CancellationToken ct)
|
||||
{
|
||||
if (message.From is null || string.IsNullOrWhiteSpace(message.Text))
|
||||
return false;
|
||||
|
||||
var gmTelegramId = message.From.Id;
|
||||
var chatId = message.Chat.Id;
|
||||
var text = message.Text.Trim();
|
||||
var command = new GmRelay.Shared.Features.Sessions.RescheduleSession.HandleRescheduleTimeInputCommand(
|
||||
new PlatformUser(
|
||||
PlatformKind.Telegram,
|
||||
message.From.Id.ToString(),
|
||||
message.From.FirstName + (string.IsNullOrEmpty(message.From.LastName) ? "" : $" {message.From.LastName}"),
|
||||
message.From.Username),
|
||||
TelegramPlatformIds.Group(message.Chat.Id, message.MessageThreadId, message.Chat.Title),
|
||||
message.Text.Trim());
|
||||
|
||||
await using var connection = await dataSource.OpenConnectionAsync(ct);
|
||||
|
||||
// 1. Check if this GM has an AwaitingTime proposal in this chat
|
||||
var proposal = await connection.QuerySingleOrDefaultAsync<AwaitingProposalDto>(
|
||||
"""
|
||||
SELECT rp.id AS Id, rp.session_id AS SessionId, s.title AS Title, s.scheduled_at AS CurrentScheduledAt,
|
||||
s.batch_id AS BatchId, s.batch_message_id AS BatchMessageId,
|
||||
g.telegram_chat_id AS TelegramChatId,
|
||||
s.thread_id AS ThreadId,
|
||||
s.notification_mode AS NotificationMode
|
||||
FROM reschedule_proposals rp
|
||||
JOIN sessions s ON s.id = rp.session_id
|
||||
JOIN game_groups g ON g.id = s.group_id
|
||||
WHERE rp.proposed_by = @GmId
|
||||
AND rp.status = 'AwaitingTime'
|
||||
AND g.telegram_chat_id = @ChatId
|
||||
AND EXISTS (
|
||||
SELECT 1
|
||||
FROM group_managers gm
|
||||
JOIN players manager_player ON manager_player.id = gm.player_id
|
||||
WHERE gm.group_id = s.group_id
|
||||
AND manager_player.telegram_id = @GmId
|
||||
)
|
||||
ORDER BY rp.created_at DESC
|
||||
LIMIT 1
|
||||
""",
|
||||
new { GmId = gmTelegramId, ChatId = chatId });
|
||||
|
||||
if (proposal is null)
|
||||
var result = await sharedHandler.HandleAsync(command, ct);
|
||||
if (!result.Handled)
|
||||
return false;
|
||||
|
||||
// 2. Parse voting input
|
||||
if (!RescheduleVotingInput.TryParse(text, DateTimeOffset.UtcNow, out var votingInput, out var parseError))
|
||||
if (!string.IsNullOrEmpty(result.ReplyText) && !result.IsRescheduledImmediately)
|
||||
{
|
||||
await messenger.SendGroupMessageAsync(
|
||||
TelegramPlatformIds.Group(chatId, proposal.ThreadId),
|
||||
$"⚠️ {parseError}\n\nИспользуйте формат:\n<code>25.04.2026 19:30\n26.04.2026 18:00\nДедлайн: 25.04.2026 12:00</code>",
|
||||
command.Group,
|
||||
$"""⚠️ {result.ReplyText}\n\nИспользуйте формат:\n<code>25.04.2026 19:30\n26.04.2026 18:00\nДедлайн: 25.04.2026 12:00</code>""",
|
||||
ct);
|
||||
return true;
|
||||
}
|
||||
|
||||
// 3. Load participants (non-GM) signed up for this session
|
||||
var participants = (await connection.QueryAsync<VoteParticipantDto>(
|
||||
"""
|
||||
SELECT p.id AS PlayerId,
|
||||
p.display_name AS DisplayName,
|
||||
p.telegram_username AS TelegramUsername,
|
||||
p.telegram_id AS TelegramId
|
||||
FROM session_participants sp
|
||||
JOIN players p ON p.id = sp.player_id
|
||||
WHERE sp.session_id = @SessionId
|
||||
AND sp.is_gm = false
|
||||
AND sp.registration_status = @Active
|
||||
""",
|
||||
new { proposal.SessionId, Active = ParticipantRegistrationStatus.Active })).ToList();
|
||||
|
||||
// 4. If no participants — reschedule immediately
|
||||
if (participants.Count == 0)
|
||||
if (result.IsRescheduledImmediately)
|
||||
{
|
||||
await RescheduleImmediately(connection, proposal, votingInput.Options[0], chatId, ct);
|
||||
await TryDeleteMessage(chatId, message.MessageId, ct);
|
||||
if (result.UpdatedView is not null && result.BatchMessageId.HasValue)
|
||||
{
|
||||
await TryUpdateBatchMessage(
|
||||
command.Group,
|
||||
result.UpdatedView,
|
||||
TelegramPlatformIds.Message(message.Chat.Id, message.MessageThreadId, result.BatchMessageId.Value),
|
||||
ct);
|
||||
}
|
||||
|
||||
await messenger.SendGroupMessageAsync(command.Group, result.ReplyText!, ct);
|
||||
await TryDeleteMessage(message.Chat.Id, message.MessageId, ct);
|
||||
return true;
|
||||
}
|
||||
|
||||
// 5. Create voting message
|
||||
await using var transaction = await connection.BeginTransactionAsync(ct);
|
||||
var options = votingInput.Options
|
||||
.Select((proposedAt, index) => new RescheduleOptionDto(
|
||||
Guid.NewGuid(),
|
||||
index + 1,
|
||||
proposedAt))
|
||||
.ToList();
|
||||
|
||||
await connection.ExecuteAsync(
|
||||
"""
|
||||
UPDATE reschedule_proposals
|
||||
SET voting_deadline_at = @Deadline, status = 'Voting', vote_chat_id = @ChatId
|
||||
WHERE id = @Id
|
||||
""",
|
||||
new { votingInput.Deadline, ChatId = chatId, Id = proposal.Id },
|
||||
transaction);
|
||||
|
||||
foreach (var option in options)
|
||||
{
|
||||
await connection.ExecuteAsync(
|
||||
"""
|
||||
INSERT INTO reschedule_options (id, proposal_id, proposed_at, display_order)
|
||||
VALUES (@OptionId, @ProposalId, @ProposedAt, @DisplayOrder)
|
||||
""",
|
||||
new
|
||||
{
|
||||
option.OptionId,
|
||||
ProposalId = proposal.Id,
|
||||
option.ProposedAt,
|
||||
option.DisplayOrder
|
||||
},
|
||||
transaction);
|
||||
}
|
||||
|
||||
await transaction.CommitAsync(ct);
|
||||
|
||||
// Voting mode
|
||||
var voteText = BuildVotingMessage(
|
||||
proposal.Title,
|
||||
proposal.CurrentScheduledAt,
|
||||
votingInput.Deadline,
|
||||
options,
|
||||
participants,
|
||||
result.Title!,
|
||||
result.CurrentScheduledAt,
|
||||
result.VotingDeadlineAt!.Value,
|
||||
result.Options,
|
||||
result.Participants,
|
||||
[]);
|
||||
var keyboard = BuildVotingKeyboard(options);
|
||||
|
||||
var keyboard = BuildVotingKeyboard(result.Options);
|
||||
|
||||
var voteMsg = await bot.SendMessage(
|
||||
chatId: chatId,
|
||||
messageThreadId: proposal.ThreadId,
|
||||
chatId: message.Chat.Id,
|
||||
messageThreadId: message.MessageThreadId,
|
||||
text: voteText,
|
||||
parseMode: Telegram.Bot.Types.Enums.ParseMode.Html,
|
||||
replyMarkup: keyboard,
|
||||
cancellationToken: ct);
|
||||
|
||||
var mode = SessionNotificationModeExtensions.FromDatabaseValue(proposal.NotificationMode);
|
||||
var mode = await GetNotificationModeAsync(result.ProposalId!.Value, ct);
|
||||
if (mode.ShouldSendDirectMessages())
|
||||
{
|
||||
var optionsText = string.Join(
|
||||
"\n",
|
||||
options.Select(option => $"{option.DisplayOrder}. <b>{option.ProposedAt.FormatMoscow()}</b> (МСК)"));
|
||||
result.Options.Select(option => $"{option.DisplayOrder}. <b>{option.ProposedAt.FormatMoscow()}</b> (МСК)"));
|
||||
var directText = $"""
|
||||
🔄 <b>Голосование за перенос сессии</b>
|
||||
|
||||
📌 <b>{System.Net.WebUtility.HtmlEncode(proposal.Title)}</b>
|
||||
📅 Текущее время: <b>{proposal.CurrentScheduledAt.FormatMoscow()}</b> (МСК)
|
||||
📌 <b>{System.Net.WebUtility.HtmlEncode(result.Title)}</b>
|
||||
📅 Текущее время: <b>{result.CurrentScheduledAt.FormatMoscow()}</b> (МСК)
|
||||
🗳 Варианты:
|
||||
{optionsText}
|
||||
|
||||
⏳ Дедлайн: <b>{votingInput.Deadline.FormatMoscow()}</b> (МСК)
|
||||
⏳ Дедлайн: <b>{result.VotingDeadlineAt.Value.FormatMoscow()}</b> (МСК)
|
||||
|
||||
Проголосуйте кнопкой в групповом сообщении.
|
||||
""";
|
||||
|
||||
await directSender.SendAsync(
|
||||
participants.Select(p => new DirectNotificationRecipient(
|
||||
result.Participants.Select(p => new DirectNotificationRecipient(
|
||||
p.TelegramId,
|
||||
p.DisplayName)),
|
||||
directText,
|
||||
"reschedule-vote",
|
||||
proposal.SessionId,
|
||||
result.ProposalId.Value,
|
||||
ct);
|
||||
}
|
||||
|
||||
// Store vote message ID
|
||||
await using var connection = await dataSource.OpenConnectionAsync(ct);
|
||||
await connection.ExecuteAsync(
|
||||
"UPDATE reschedule_proposals SET vote_message_id = @MsgId WHERE id = @Id",
|
||||
new { MsgId = voteMsg.MessageId, Id = proposal.Id });
|
||||
new { MsgId = voteMsg.MessageId, Id = result.ProposalId.Value });
|
||||
|
||||
logger.LogInformation(
|
||||
"Reschedule voting started for session {SessionId}, proposal {ProposalId}, options {OptionCount}, deadline {Deadline}",
|
||||
proposal.SessionId,
|
||||
proposal.Id,
|
||||
options.Count,
|
||||
votingInput.Deadline);
|
||||
|
||||
// Delete GM's time input message
|
||||
await TryDeleteMessage(chatId, message.MessageId, ct);
|
||||
result.ProposalId.Value,
|
||||
result.ProposalId.Value,
|
||||
result.Options.Count,
|
||||
result.VotingDeadlineAt.Value);
|
||||
|
||||
await TryDeleteMessage(message.Chat.Id, message.MessageId, ct);
|
||||
return true;
|
||||
}
|
||||
|
||||
private async Task RescheduleImmediately(
|
||||
NpgsqlConnection connection, AwaitingProposalDto proposal,
|
||||
DateTimeOffset newTime, long chatId, CancellationToken ct)
|
||||
private async Task<SessionNotificationMode> GetNotificationModeAsync(Guid proposalId, CancellationToken ct)
|
||||
{
|
||||
await using var transaction = await connection.BeginTransactionAsync(ct);
|
||||
|
||||
await connection.ExecuteAsync(
|
||||
await using var connection = await dataSource.OpenConnectionAsync(ct);
|
||||
var raw = await connection.QuerySingleOrDefaultAsync<string?>(
|
||||
"""
|
||||
UPDATE sessions
|
||||
SET scheduled_at = @NewTime,
|
||||
status = @Status,
|
||||
confirmation_message_id = NULL,
|
||||
confirmation_sent_at = NULL,
|
||||
one_hour_reminder_processed_at = NULL,
|
||||
updated_at = now()
|
||||
WHERE id = @SessionId
|
||||
SELECT s.notification_mode
|
||||
FROM sessions s
|
||||
JOIN reschedule_proposals rp ON rp.session_id = s.id
|
||||
WHERE rp.id = @Id
|
||||
""",
|
||||
new { NewTime = newTime, proposal.SessionId, Status = SessionStatus.Planned },
|
||||
transaction);
|
||||
new { Id = proposalId });
|
||||
return SessionNotificationModeExtensions.FromDatabaseValue(raw ?? string.Empty);
|
||||
}
|
||||
|
||||
await connection.ExecuteAsync(
|
||||
"UPDATE reschedule_proposals SET proposed_at = @NewTime, status = 'Approved' WHERE id = @Id",
|
||||
new { NewTime = newTime, Id = proposal.Id },
|
||||
transaction);
|
||||
|
||||
await transaction.CommitAsync(ct);
|
||||
|
||||
await messenger.SendGroupMessageAsync(
|
||||
TelegramPlatformIds.Group(chatId, proposal.ThreadId),
|
||||
$"✅ Сессия «{proposal.Title}» перенесена!\n\n📅 Новое время: <b>{newTime.ToOffset(TimeSpan.FromHours(3)).ToString("d MMMM yyyy, HH:mm", System.Globalization.CultureInfo.GetCultureInfo("ru-RU"))}</b> (МСК)\n\n<i>Участников нет — голосование не требуется.</i>",
|
||||
ct);
|
||||
|
||||
// Re-render batch message with updated time
|
||||
await TryUpdateBatchMessage(proposal, ct);
|
||||
|
||||
logger.LogInformation("Session {SessionId} rescheduled immediately (no participants)", proposal.SessionId);
|
||||
private async Task TryUpdateBatchMessage(
|
||||
PlatformGroup group,
|
||||
SessionBatchViewModel view,
|
||||
PlatformMessageRef scheduleMessage,
|
||||
CancellationToken ct)
|
||||
{
|
||||
try
|
||||
{
|
||||
await messenger.UpdateScheduleAsync(
|
||||
new PlatformScheduleMessage(group, view, scheduleMessage),
|
||||
ct);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogWarning(ex, "Failed to update batch message after immediate reschedule");
|
||||
}
|
||||
}
|
||||
|
||||
internal static string BuildVotingMessage(
|
||||
@@ -273,7 +183,7 @@ public sealed class HandleRescheduleTimeInputHandler(
|
||||
|
||||
var lines = new List<string>
|
||||
{
|
||||
$"🔄 <b>Перенос сессии «{System.Net.WebUtility.HtmlEncode(title)}»</b>",
|
||||
$"""🔄 <b>Перенос сессии «{System.Net.WebUtility.HtmlEncode(title)}»</b>""",
|
||||
"",
|
||||
$"📅 Текущее время: <b>{currentTime.FormatMoscow()}</b> (МСК)",
|
||||
$"⏳ Дедлайн: <b>{deadline.FormatMoscow()}</b> (МСК)",
|
||||
@@ -354,52 +264,6 @@ public sealed class HandleRescheduleTimeInputHandler(
|
||||
"dd.MM HH:mm",
|
||||
System.Globalization.CultureInfo.InvariantCulture);
|
||||
|
||||
private async Task TryUpdateBatchMessage(AwaitingProposalDto proposal, CancellationToken ct)
|
||||
{
|
||||
try
|
||||
{
|
||||
await using var conn = await dataSource.OpenConnectionAsync(ct);
|
||||
|
||||
var batchSessions = (await conn.QueryAsync<SessionBatchDto>(
|
||||
"SELECT id AS SessionId, scheduled_at AS ScheduledAt, status AS Status, max_players AS MaxPlayers, join_link AS JoinLink FROM sessions WHERE batch_id = @BatchId ORDER BY scheduled_at",
|
||||
new { proposal.BatchId })).ToList();
|
||||
|
||||
var batchParticipants = (await conn.QueryAsync<ParticipantBatchDto>(
|
||||
"""
|
||||
SELECT sp.session_id AS SessionId,
|
||||
p.display_name AS DisplayName,
|
||||
p.telegram_username AS TelegramUsername,
|
||||
sp.registration_status AS RegistrationStatus
|
||||
FROM session_participants sp
|
||||
JOIN players p ON sp.player_id = p.id
|
||||
JOIN sessions s ON sp.session_id = s.id
|
||||
WHERE s.batch_id = @BatchId AND sp.is_gm = false
|
||||
ORDER BY sp.registration_status ASC, sp.created_at ASC, sp.responded_at ASC, p.created_at ASC
|
||||
""",
|
||||
new { proposal.BatchId })).ToList();
|
||||
|
||||
if (proposal.BatchMessageId.HasValue)
|
||||
{
|
||||
var view = SessionBatchViewBuilder.Build(proposal.Title, batchSessions, batchParticipants);
|
||||
|
||||
await messenger.UpdateScheduleAsync(
|
||||
new PlatformScheduleMessage(
|
||||
TelegramPlatformIds.Group(proposal.TelegramChatId, proposal.ThreadId),
|
||||
view,
|
||||
TelegramPlatformIds.Message(proposal.TelegramChatId, proposal.ThreadId, proposal.BatchMessageId.Value)),
|
||||
ct);
|
||||
}
|
||||
else
|
||||
{
|
||||
logger.LogWarning("No batch_message_id stored for session {SessionId}, cannot edit batch message in-place", proposal.SessionId);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogWarning(ex, "Failed to update batch message after immediate reschedule for session {SessionId}", proposal.SessionId);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task TryDeleteMessage(long chatId, int messageId, CancellationToken ct)
|
||||
{
|
||||
try
|
||||
|
||||
+36
-125
@@ -1,7 +1,7 @@
|
||||
using Dapper;
|
||||
using GmRelay.Bot.Infrastructure.Telegram;
|
||||
using GmRelay.Shared.Domain;
|
||||
using GmRelay.Shared.Features.Sessions.RescheduleSession;
|
||||
using GmRelay.Shared.Platform;
|
||||
using Npgsql;
|
||||
using Telegram.Bot;
|
||||
|
||||
namespace GmRelay.Bot.Features.Sessions.RescheduleSession;
|
||||
@@ -13,138 +13,50 @@ public sealed record HandleRescheduleVoteCommand(
|
||||
long ChatId,
|
||||
int MessageId);
|
||||
|
||||
internal sealed record VoteProposalDto(
|
||||
Guid Id,
|
||||
Guid SessionId,
|
||||
DateTimeOffset VotingDeadlineAt,
|
||||
string Title,
|
||||
DateTime CurrentScheduledAt);
|
||||
|
||||
public sealed class HandleRescheduleVoteHandler(
|
||||
NpgsqlDataSource dataSource,
|
||||
GmRelay.Shared.Features.Sessions.RescheduleSession.HandleRescheduleVoteHandler sharedHandler,
|
||||
ITelegramBotClient bot,
|
||||
IPlatformMessenger messenger,
|
||||
ILogger<HandleRescheduleVoteHandler> logger)
|
||||
{
|
||||
public async Task HandleAsync(HandleRescheduleVoteCommand command, CancellationToken ct)
|
||||
{
|
||||
await using var connection = await dataSource.OpenConnectionAsync(ct);
|
||||
await using var transaction = await connection.BeginTransactionAsync(ct);
|
||||
var platformUser = new PlatformUser(
|
||||
PlatformKind.Telegram,
|
||||
command.TelegramUserId.ToString(),
|
||||
string.Empty,
|
||||
null);
|
||||
|
||||
var proposal = await connection.QuerySingleOrDefaultAsync<VoteProposalDto>(
|
||||
"""
|
||||
SELECT rp.id AS Id,
|
||||
rp.session_id AS SessionId,
|
||||
rp.voting_deadline_at AS VotingDeadlineAt,
|
||||
s.title AS Title,
|
||||
s.scheduled_at AS CurrentScheduledAt
|
||||
FROM reschedule_options ro
|
||||
JOIN reschedule_proposals rp ON rp.id = ro.proposal_id
|
||||
JOIN sessions s ON s.id = rp.session_id
|
||||
WHERE ro.id = @OptionId AND rp.status = 'Voting'
|
||||
""",
|
||||
new { command.OptionId },
|
||||
transaction);
|
||||
var platformGroup = new PlatformGroup(
|
||||
PlatformKind.Telegram,
|
||||
command.ChatId.ToString(),
|
||||
string.Empty);
|
||||
|
||||
if (proposal is null)
|
||||
var sharedCommand = new GmRelay.Shared.Features.Sessions.RescheduleSession.HandleRescheduleVoteCommand(
|
||||
command.OptionId,
|
||||
platformUser,
|
||||
platformGroup,
|
||||
command.CallbackQueryId,
|
||||
TelegramPlatformIds.Message(command.ChatId, null, command.MessageId));
|
||||
|
||||
var result = await sharedHandler.HandleAsync(sharedCommand, ct);
|
||||
|
||||
if (!result.Success)
|
||||
{
|
||||
await AnswerAsync(command.CallbackQueryId, "Голосование уже завершено или не найдено.", ct);
|
||||
await messenger.AnswerInteractionAsync(
|
||||
new PlatformInteractionReply(command.CallbackQueryId, result.ReplyText!, result.ReplyText!.Contains("дедлайн")),
|
||||
ct);
|
||||
return;
|
||||
}
|
||||
|
||||
if (proposal.VotingDeadlineAt <= DateTimeOffset.UtcNow)
|
||||
{
|
||||
await AnswerAsync(command.CallbackQueryId, "Дедлайн уже прошёл. Результаты скоро будут применены.", ct, showAlert: true);
|
||||
return;
|
||||
}
|
||||
|
||||
var playerId = await connection.ExecuteScalarAsync<Guid?>(
|
||||
"""
|
||||
SELECT p.id
|
||||
FROM session_participants sp
|
||||
JOIN players p ON p.id = sp.player_id
|
||||
WHERE sp.session_id = @SessionId
|
||||
AND p.telegram_id = @TelegramUserId
|
||||
AND sp.is_gm = false
|
||||
AND sp.registration_status = @Active
|
||||
""",
|
||||
new { proposal.SessionId, command.TelegramUserId, Active = ParticipantRegistrationStatus.Active },
|
||||
transaction);
|
||||
|
||||
if (playerId is null)
|
||||
{
|
||||
await AnswerAsync(command.CallbackQueryId, "Вы не являетесь участником этой сессии.", ct);
|
||||
return;
|
||||
}
|
||||
|
||||
await connection.ExecuteAsync(
|
||||
"""
|
||||
INSERT INTO reschedule_option_votes (proposal_id, player_id, option_id)
|
||||
VALUES (@ProposalId, @PlayerId, @OptionId)
|
||||
ON CONFLICT (proposal_id, player_id) DO UPDATE
|
||||
SET option_id = EXCLUDED.option_id,
|
||||
voted_at = now()
|
||||
""",
|
||||
new
|
||||
{
|
||||
ProposalId = proposal.Id,
|
||||
PlayerId = playerId.Value,
|
||||
command.OptionId
|
||||
},
|
||||
transaction);
|
||||
|
||||
var participants = (await connection.QueryAsync<VoteParticipantDto>(
|
||||
"""
|
||||
SELECT p.id AS PlayerId,
|
||||
p.display_name AS DisplayName,
|
||||
p.telegram_username AS TelegramUsername,
|
||||
p.telegram_id AS TelegramId
|
||||
FROM session_participants sp
|
||||
JOIN players p ON p.id = sp.player_id
|
||||
WHERE sp.session_id = @SessionId
|
||||
AND sp.is_gm = false
|
||||
AND sp.registration_status = @Active
|
||||
ORDER BY p.display_name
|
||||
""",
|
||||
new { proposal.SessionId, Active = ParticipantRegistrationStatus.Active },
|
||||
transaction)).ToList();
|
||||
|
||||
var options = (await connection.QueryAsync<RescheduleOptionDto>(
|
||||
"""
|
||||
SELECT id AS OptionId,
|
||||
display_order AS DisplayOrder,
|
||||
proposed_at AS ProposedAt
|
||||
FROM reschedule_options
|
||||
WHERE proposal_id = @ProposalId
|
||||
ORDER BY display_order
|
||||
""",
|
||||
new { ProposalId = proposal.Id },
|
||||
transaction)).ToList();
|
||||
|
||||
var votes = (await connection.QueryAsync<RescheduleOptionVoteDto>(
|
||||
"""
|
||||
SELECT rov.option_id AS OptionId,
|
||||
p.id AS PlayerId,
|
||||
p.display_name AS DisplayName,
|
||||
p.telegram_username AS TelegramUsername
|
||||
FROM reschedule_option_votes rov
|
||||
JOIN players p ON p.id = rov.player_id
|
||||
WHERE rov.proposal_id = @ProposalId
|
||||
ORDER BY rov.voted_at, p.display_name
|
||||
""",
|
||||
new { ProposalId = proposal.Id },
|
||||
transaction)).ToList();
|
||||
|
||||
await transaction.CommitAsync(ct);
|
||||
|
||||
var voteText = HandleRescheduleTimeInputHandler.BuildVotingMessage(
|
||||
proposal.Title,
|
||||
proposal.CurrentScheduledAt,
|
||||
proposal.VotingDeadlineAt,
|
||||
options,
|
||||
participants,
|
||||
votes);
|
||||
var keyboard = HandleRescheduleTimeInputHandler.BuildVotingKeyboard(options);
|
||||
result.Title!,
|
||||
result.CurrentScheduledAt,
|
||||
result.VotingDeadlineAt,
|
||||
result.Options,
|
||||
result.Participants,
|
||||
result.Votes);
|
||||
var keyboard = HandleRescheduleTimeInputHandler.BuildVotingKeyboard(result.Options);
|
||||
|
||||
try
|
||||
{
|
||||
@@ -158,12 +70,11 @@ public sealed class HandleRescheduleVoteHandler(
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogWarning(ex, "Failed to update reschedule vote message for proposal {ProposalId}", proposal.Id);
|
||||
logger.LogWarning(ex, "Failed to update reschedule vote message for proposal {ProposalId}", result.ProposalId);
|
||||
}
|
||||
|
||||
await AnswerAsync(command.CallbackQueryId, "Ваш голос учтён. До дедлайна его можно изменить.", ct);
|
||||
await messenger.AnswerInteractionAsync(
|
||||
new PlatformInteractionReply(command.CallbackQueryId, result.ReplyText!),
|
||||
ct);
|
||||
}
|
||||
|
||||
private Task AnswerAsync(string callbackQueryId, string text, CancellationToken ct, bool showAlert = false) =>
|
||||
messenger.AnswerInteractionAsync(new PlatformInteractionReply(callbackQueryId, text, showAlert), ct);
|
||||
}
|
||||
|
||||
@@ -45,12 +45,13 @@ public sealed class InitiateRescheduleHandler(
|
||||
FROM group_managers gm
|
||||
JOIN players p ON p.id = gm.player_id
|
||||
WHERE gm.group_id = s.group_id
|
||||
AND p.telegram_id = @TelegramUserId
|
||||
AND p.platform = 'Telegram'
|
||||
AND p.external_user_id = @ExternalUserId
|
||||
) AS CanManage
|
||||
FROM sessions s
|
||||
WHERE s.id = @SessionId AND s.status != @Cancelled
|
||||
""",
|
||||
new { command.SessionId, command.TelegramUserId, Cancelled = SessionStatus.Cancelled });
|
||||
new { command.SessionId, ExternalUserId = command.TelegramUserId.ToString(), Cancelled = SessionStatus.Cancelled });
|
||||
|
||||
if (session is null)
|
||||
{
|
||||
@@ -83,10 +84,10 @@ public sealed class InitiateRescheduleHandler(
|
||||
// 3. Create proposal in AwaitingTime status
|
||||
await connection.ExecuteAsync(
|
||||
"""
|
||||
INSERT INTO reschedule_proposals (session_id, proposed_by, status)
|
||||
VALUES (@SessionId, @GmId, 'AwaitingTime')
|
||||
INSERT INTO reschedule_proposals (session_id, proposed_by_external_user_id, source_platform, status)
|
||||
VALUES (@SessionId, @ProposedBy, 'Telegram', 'AwaitingTime')
|
||||
""",
|
||||
new { command.SessionId, GmId = command.TelegramUserId });
|
||||
new { command.SessionId, ProposedBy = command.TelegramUserId.ToString() });
|
||||
|
||||
logger.LogInformation("Reschedule initiated for session {SessionId} by GM {GmId}", command.SessionId, command.TelegramUserId);
|
||||
|
||||
|
||||
+87
-230
@@ -1,35 +1,25 @@
|
||||
using Dapper;
|
||||
using GmRelay.Bot.Features.Notifications;
|
||||
using GmRelay.Bot.Infrastructure.Scheduling;
|
||||
using GmRelay.Bot.Infrastructure.Telegram;
|
||||
using GmRelay.Shared.Domain;
|
||||
using GmRelay.Shared.Features.Notifications;
|
||||
using GmRelay.Shared.Features.Sessions.RescheduleSession;
|
||||
using GmRelay.Shared.Platform;
|
||||
using GmRelay.Shared.Rendering;
|
||||
using Npgsql;
|
||||
using Telegram.Bot;
|
||||
using Telegram.Bot.Types.Enums;
|
||||
using GmRelay.Bot.Infrastructure.Telegram;
|
||||
|
||||
namespace GmRelay.Bot.Features.Sessions.RescheduleSession;
|
||||
|
||||
internal sealed record DueRescheduleProposalDto(
|
||||
Guid Id,
|
||||
Guid SessionId,
|
||||
DateTimeOffset VotingDeadlineAt,
|
||||
string Title,
|
||||
DateTime CurrentScheduledAt,
|
||||
Guid BatchId,
|
||||
int? BatchMessageId,
|
||||
internal sealed record TelegramProposalFieldsDto(
|
||||
int? VoteMessageId,
|
||||
int? BatchMessageId,
|
||||
long TelegramChatId,
|
||||
int? ThreadId,
|
||||
string NotificationMode);
|
||||
int? ThreadId);
|
||||
|
||||
public sealed class RescheduleVotingDeadlineService(
|
||||
NpgsqlDataSource dataSource,
|
||||
ITelegramBotClient bot,
|
||||
IPlatformMessenger messenger,
|
||||
DirectSessionNotificationSender directSender,
|
||||
ISystemClock clock,
|
||||
PlatformDirectNotificationSender directSender,
|
||||
RescheduleVotingFinalizer finalizer,
|
||||
ILogger<RescheduleVotingDeadlineService> logger) : BackgroundService
|
||||
{
|
||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
@@ -53,18 +43,7 @@ public sealed class RescheduleVotingDeadlineService(
|
||||
{
|
||||
try
|
||||
{
|
||||
await using var connection = await dataSource.OpenConnectionAsync(ct);
|
||||
var proposalIds = (await connection.QueryAsync<Guid>(
|
||||
"""
|
||||
SELECT id
|
||||
FROM reschedule_proposals
|
||||
WHERE status = 'Voting'
|
||||
AND voting_deadline_at IS NOT NULL
|
||||
AND voting_deadline_at <= @Now
|
||||
ORDER BY voting_deadline_at
|
||||
LIMIT 25
|
||||
""",
|
||||
new { Now = clock.UtcNow.UtcDateTime })).ToList();
|
||||
var proposalIds = await finalizer.GetDueProposalIdsAsync("Telegram", ct);
|
||||
|
||||
foreach (var proposalId in proposalIds)
|
||||
{
|
||||
@@ -82,212 +61,101 @@ public sealed class RescheduleVotingDeadlineService(
|
||||
|
||||
private async Task FinalizeProposal(Guid proposalId, CancellationToken ct)
|
||||
{
|
||||
await using var connection = await dataSource.OpenConnectionAsync(ct);
|
||||
await using var transaction = await connection.BeginTransactionAsync(ct);
|
||||
var result = await finalizer.FinalizeAsync(proposalId, ct);
|
||||
if (result is null)
|
||||
return;
|
||||
|
||||
var proposal = await connection.QuerySingleOrDefaultAsync<DueRescheduleProposalDto>(
|
||||
if (result.SourcePlatform != "Telegram")
|
||||
{
|
||||
logger.LogInformation(
|
||||
"Skipping Telegram message handling for proposal {ProposalId} with source platform {SourcePlatform}",
|
||||
proposalId,
|
||||
result.SourcePlatform);
|
||||
return;
|
||||
}
|
||||
|
||||
await using var connection = await dataSource.OpenConnectionAsync(ct);
|
||||
var telegramFields = await connection.QuerySingleOrDefaultAsync<TelegramProposalFieldsDto>(
|
||||
"""
|
||||
SELECT rp.id AS Id,
|
||||
rp.session_id AS SessionId,
|
||||
rp.voting_deadline_at AS VotingDeadlineAt,
|
||||
rp.vote_message_id AS VoteMessageId,
|
||||
s.title AS Title,
|
||||
s.scheduled_at AS CurrentScheduledAt,
|
||||
s.batch_id AS BatchId,
|
||||
SELECT rp.vote_message_id AS VoteMessageId,
|
||||
s.batch_message_id AS BatchMessageId,
|
||||
s.notification_mode AS NotificationMode,
|
||||
s.thread_id AS ThreadId,
|
||||
g.telegram_chat_id AS TelegramChatId
|
||||
g.external_group_id::BIGINT AS TelegramChatId,
|
||||
s.thread_id AS ThreadId
|
||||
FROM reschedule_proposals rp
|
||||
JOIN sessions s ON s.id = rp.session_id
|
||||
JOIN game_groups g ON g.id = s.group_id
|
||||
WHERE rp.id = @ProposalId
|
||||
AND rp.status = 'Voting'
|
||||
AND rp.voting_deadline_at IS NOT NULL
|
||||
AND rp.voting_deadline_at <= @Now
|
||||
FOR UPDATE
|
||||
""",
|
||||
new { ProposalId = proposalId, Now = clock.UtcNow.UtcDateTime },
|
||||
transaction);
|
||||
new { ProposalId = proposalId });
|
||||
|
||||
if (proposal is null)
|
||||
if (telegramFields is null)
|
||||
{
|
||||
logger.LogWarning("Could not find Telegram fields for proposal {ProposalId}", proposalId);
|
||||
return;
|
||||
|
||||
var participants = (await connection.QueryAsync<VoteParticipantDto>(
|
||||
"""
|
||||
SELECT p.id AS PlayerId,
|
||||
p.display_name AS DisplayName,
|
||||
p.telegram_username AS TelegramUsername,
|
||||
p.telegram_id AS TelegramId
|
||||
FROM session_participants sp
|
||||
JOIN players p ON p.id = sp.player_id
|
||||
WHERE sp.session_id = @SessionId
|
||||
AND sp.is_gm = false
|
||||
AND sp.registration_status = @Active
|
||||
ORDER BY p.display_name
|
||||
""",
|
||||
new { proposal.SessionId, Active = ParticipantRegistrationStatus.Active },
|
||||
transaction)).ToList();
|
||||
|
||||
var options = (await connection.QueryAsync<RescheduleOptionDto>(
|
||||
"""
|
||||
SELECT id AS OptionId,
|
||||
display_order AS DisplayOrder,
|
||||
proposed_at AS ProposedAt
|
||||
FROM reschedule_options
|
||||
WHERE proposal_id = @ProposalId
|
||||
ORDER BY display_order
|
||||
""",
|
||||
new { ProposalId = proposal.Id },
|
||||
transaction)).ToList();
|
||||
|
||||
var votes = (await connection.QueryAsync<RescheduleOptionVoteDto>(
|
||||
"""
|
||||
SELECT rov.option_id AS OptionId,
|
||||
p.id AS PlayerId,
|
||||
p.display_name AS DisplayName,
|
||||
p.telegram_username AS TelegramUsername
|
||||
FROM reschedule_option_votes rov
|
||||
JOIN players p ON p.id = rov.player_id
|
||||
WHERE rov.proposal_id = @ProposalId
|
||||
ORDER BY rov.voted_at, p.display_name
|
||||
""",
|
||||
new { ProposalId = proposal.Id },
|
||||
transaction)).ToList();
|
||||
|
||||
var voteCounts = options
|
||||
.Select(option => new RescheduleOptionVoteCount(
|
||||
option.OptionId,
|
||||
votes.Count(vote => vote.OptionId == option.OptionId)))
|
||||
.ToList();
|
||||
var decision = RescheduleVoteRules.SelectWinner(voteCounts);
|
||||
var selectedOption = decision.SelectedOptionId is { } selectedOptionId
|
||||
? options.Single(x => x.OptionId == selectedOptionId)
|
||||
: null;
|
||||
|
||||
if (selectedOption is not null)
|
||||
{
|
||||
await connection.ExecuteAsync(
|
||||
"""
|
||||
UPDATE sessions
|
||||
SET scheduled_at = @NewTime,
|
||||
status = @Status,
|
||||
confirmation_message_id = NULL,
|
||||
confirmation_sent_at = NULL,
|
||||
link_message_id = NULL,
|
||||
one_hour_reminder_processed_at = NULL,
|
||||
updated_at = now()
|
||||
WHERE id = @SessionId
|
||||
""",
|
||||
new { NewTime = selectedOption.ProposedAt, proposal.SessionId, Status = SessionStatus.Planned },
|
||||
transaction);
|
||||
|
||||
await connection.ExecuteAsync(
|
||||
"""
|
||||
UPDATE session_participants
|
||||
SET rsvp_status = 'Pending',
|
||||
responded_at = NULL
|
||||
WHERE session_id = @SessionId
|
||||
AND is_gm = false
|
||||
AND registration_status = @Active
|
||||
""",
|
||||
new { proposal.SessionId, Active = ParticipantRegistrationStatus.Active },
|
||||
transaction);
|
||||
|
||||
await connection.ExecuteAsync(
|
||||
"""
|
||||
UPDATE reschedule_proposals
|
||||
SET status = 'Approved',
|
||||
selected_option_id = @SelectedOptionId,
|
||||
proposed_at = @ProposedAt
|
||||
WHERE id = @ProposalId
|
||||
""",
|
||||
new
|
||||
{
|
||||
ProposalId = proposal.Id,
|
||||
SelectedOptionId = selectedOption.OptionId,
|
||||
ProposedAt = selectedOption.ProposedAt
|
||||
},
|
||||
transaction);
|
||||
}
|
||||
else
|
||||
{
|
||||
await connection.ExecuteAsync(
|
||||
"UPDATE reschedule_proposals SET status = 'Rejected' WHERE id = @ProposalId",
|
||||
new { ProposalId = proposal.Id },
|
||||
transaction);
|
||||
}
|
||||
|
||||
var directRecipients = participants
|
||||
.Select(p => new DirectNotificationRecipient(p.TelegramId, p.DisplayName))
|
||||
var directRecipients = result.Participants
|
||||
.Select(p => TelegramPlatformIds.User(p.TelegramId, p.DisplayName))
|
||||
.ToList();
|
||||
|
||||
await transaction.CommitAsync(ct);
|
||||
await TryUpdateVoteMessage(result, telegramFields, ct);
|
||||
|
||||
await TryUpdateVoteMessage(proposal, options, participants, votes, decision, selectedOption, ct);
|
||||
|
||||
if (selectedOption is not null)
|
||||
if (result.SelectedOption is not null)
|
||||
{
|
||||
await TryUpdateBatchMessage(proposal, ct);
|
||||
await TryUpdateBatchMessage(result, telegramFields, ct);
|
||||
}
|
||||
|
||||
var mode = SessionNotificationModeExtensions.FromDatabaseValue(proposal.NotificationMode);
|
||||
var mode = SessionNotificationModeExtensions.FromDatabaseValue(result.NotificationMode);
|
||||
if (mode.ShouldSendDirectMessages())
|
||||
{
|
||||
await SendDirectResult(proposal, directRecipients, decision, selectedOption, ct);
|
||||
await SendDirectResult(result, directRecipients, ct);
|
||||
}
|
||||
|
||||
logger.LogInformation(
|
||||
"Finalized reschedule proposal {ProposalId} for session {SessionId} with outcome {Outcome}",
|
||||
proposal.Id,
|
||||
proposal.SessionId,
|
||||
decision.Outcome);
|
||||
"Updated Telegram messages for finalized reschedule proposal {ProposalId} for session {SessionId}",
|
||||
result.ProposalId,
|
||||
result.SessionId);
|
||||
}
|
||||
|
||||
private async Task TryUpdateVoteMessage(
|
||||
DueRescheduleProposalDto proposal,
|
||||
IReadOnlyList<RescheduleOptionDto> options,
|
||||
IReadOnlyList<VoteParticipantDto> participants,
|
||||
IReadOnlyList<RescheduleOptionVoteDto> votes,
|
||||
RescheduleVoteDecision decision,
|
||||
RescheduleOptionDto? selectedOption,
|
||||
RescheduleVotingFinalizerResult result,
|
||||
TelegramProposalFieldsDto telegramFields,
|
||||
CancellationToken ct)
|
||||
{
|
||||
if (proposal.VoteMessageId is null)
|
||||
if (telegramFields.VoteMessageId is null)
|
||||
return;
|
||||
|
||||
try
|
||||
{
|
||||
var resultText = selectedOption is not null
|
||||
? $"✅ <b>Голосование завершено.</b>\nПобедил вариант {selectedOption.DisplayOrder}: <b>{selectedOption.ProposedAt.FormatMoscow()}</b> (МСК)."
|
||||
: $"❌ <b>Голосование завершено.</b>\n{System.Net.WebUtility.HtmlEncode(decision.Reason)}";
|
||||
|
||||
var text = $"""
|
||||
{HandleRescheduleTimeInputHandler.BuildVotingMessage(
|
||||
proposal.Title,
|
||||
proposal.CurrentScheduledAt,
|
||||
proposal.VotingDeadlineAt,
|
||||
options,
|
||||
participants,
|
||||
votes)}
|
||||
|
||||
{resultText}
|
||||
""";
|
||||
|
||||
await bot.EditMessageText(
|
||||
chatId: proposal.TelegramChatId,
|
||||
messageId: proposal.VoteMessageId.Value,
|
||||
text: text,
|
||||
parseMode: ParseMode.Html,
|
||||
cancellationToken: ct);
|
||||
await messenger.UpdateRescheduleVoteAsync(
|
||||
new PlatformRescheduleVoteUpdate(
|
||||
TelegramPlatformIds.Group(telegramFields.TelegramChatId, telegramFields.ThreadId),
|
||||
TelegramPlatformIds.Message(
|
||||
telegramFields.TelegramChatId,
|
||||
telegramFields.ThreadId,
|
||||
telegramFields.VoteMessageId.Value),
|
||||
result.ProposalId,
|
||||
result.SessionId,
|
||||
result.Title,
|
||||
result.CurrentScheduledAt,
|
||||
result.VotingDeadlineAt,
|
||||
result.Decision,
|
||||
result.SelectedOption,
|
||||
result.Options,
|
||||
result.Votes,
|
||||
result.Participants),
|
||||
ct);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogWarning(ex, "Failed to update finalized reschedule vote message for proposal {ProposalId}", proposal.Id);
|
||||
logger.LogWarning(ex, "Failed to update finalized reschedule vote message for proposal {ProposalId}", result.ProposalId);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task TryUpdateBatchMessage(DueRescheduleProposalDto proposal, CancellationToken ct)
|
||||
private async Task TryUpdateBatchMessage(
|
||||
RescheduleVotingFinalizerResult result,
|
||||
TelegramProposalFieldsDto telegramFields,
|
||||
CancellationToken ct)
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -295,13 +163,13 @@ public sealed class RescheduleVotingDeadlineService(
|
||||
|
||||
var batchSessions = (await connection.QueryAsync<SessionBatchDto>(
|
||||
"SELECT id AS SessionId, scheduled_at AS ScheduledAt, status AS Status, max_players AS MaxPlayers, join_link AS JoinLink FROM sessions WHERE batch_id = @BatchId ORDER BY scheduled_at",
|
||||
new { proposal.BatchId })).ToList();
|
||||
new { result.BatchId })).ToList();
|
||||
|
||||
var batchParticipants = (await connection.QueryAsync<ParticipantBatchDto>(
|
||||
"""
|
||||
SELECT sp.session_id AS SessionId,
|
||||
p.display_name AS DisplayName,
|
||||
p.telegram_username AS TelegramUsername,
|
||||
p.external_username AS TelegramUsername,
|
||||
sp.registration_status AS RegistrationStatus
|
||||
FROM session_participants sp
|
||||
JOIN players p ON sp.player_id = p.id
|
||||
@@ -309,60 +177,49 @@ public sealed class RescheduleVotingDeadlineService(
|
||||
WHERE s.batch_id = @BatchId AND sp.is_gm = false
|
||||
ORDER BY sp.registration_status ASC, sp.created_at ASC, sp.responded_at ASC, p.created_at ASC
|
||||
""",
|
||||
new { proposal.BatchId })).ToList();
|
||||
new { result.BatchId })).ToList();
|
||||
|
||||
if (proposal.BatchMessageId.HasValue)
|
||||
if (telegramFields.BatchMessageId.HasValue)
|
||||
{
|
||||
var view = SessionBatchViewBuilder.Build(proposal.Title, batchSessions, batchParticipants);
|
||||
var view = SessionBatchViewBuilder.Build(result.Title, batchSessions, batchParticipants);
|
||||
|
||||
await messenger.UpdateScheduleAsync(
|
||||
new PlatformScheduleMessage(
|
||||
TelegramPlatformIds.Group(proposal.TelegramChatId, proposal.ThreadId),
|
||||
TelegramPlatformIds.Group(telegramFields.TelegramChatId, telegramFields.ThreadId),
|
||||
view,
|
||||
TelegramPlatformIds.Message(proposal.TelegramChatId, proposal.ThreadId, proposal.BatchMessageId.Value)),
|
||||
TelegramPlatformIds.Message(telegramFields.TelegramChatId, telegramFields.ThreadId, telegramFields.BatchMessageId.Value)),
|
||||
ct);
|
||||
}
|
||||
else
|
||||
{
|
||||
await messenger.SendGroupMessageAsync(
|
||||
TelegramPlatformIds.Group(proposal.TelegramChatId, proposal.ThreadId),
|
||||
$"📣 Расписание обновлено после голосования за перенос сессии «{System.Net.WebUtility.HtmlEncode(proposal.Title)}».",
|
||||
TelegramPlatformIds.Group(telegramFields.TelegramChatId, telegramFields.ThreadId),
|
||||
$"Расписание обновлено после голосования за перенос сессии \"{System.Net.WebUtility.HtmlEncode(result.Title)}\".",
|
||||
ct);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogWarning(ex, "Failed to update batch message for finalized proposal {ProposalId}", proposal.Id);
|
||||
logger.LogWarning(ex, "Failed to update batch message for finalized proposal {ProposalId}", result.ProposalId);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task SendDirectResult(
|
||||
DueRescheduleProposalDto proposal,
|
||||
IReadOnlyList<DirectNotificationRecipient> recipients,
|
||||
RescheduleVoteDecision decision,
|
||||
RescheduleOptionDto? selectedOption,
|
||||
RescheduleVotingFinalizerResult result,
|
||||
IReadOnlyList<PlatformUser> recipients,
|
||||
CancellationToken ct)
|
||||
{
|
||||
var htmlText = selectedOption is not null
|
||||
? $"""
|
||||
✅ <b>Сессия перенесена по итогам голосования</b>
|
||||
|
||||
📌 <b>{System.Net.WebUtility.HtmlEncode(proposal.Title)}</b>
|
||||
📅 Новое время: <b>{selectedOption.ProposedAt.FormatMoscow()}</b> (МСК)
|
||||
"""
|
||||
: $"""
|
||||
❌ <b>Перенос сессии отклонён по итогам голосования</b>
|
||||
|
||||
📌 <b>{System.Net.WebUtility.HtmlEncode(proposal.Title)}</b>
|
||||
📅 Время остаётся прежним: <b>{proposal.CurrentScheduledAt.FormatMoscow()}</b> (МСК)
|
||||
Причина: {System.Net.WebUtility.HtmlEncode(decision.Reason)}
|
||||
""";
|
||||
|
||||
await directSender.SendAsync(
|
||||
result.SelectedOption is not null
|
||||
? PlatformDirectSessionNotificationKind.RescheduleApproved
|
||||
: PlatformDirectSessionNotificationKind.RescheduleRejected,
|
||||
recipients,
|
||||
htmlText,
|
||||
selectedOption is not null ? "reschedule-vote-approved" : "reschedule-vote-rejected",
|
||||
proposal.SessionId,
|
||||
result.SessionId,
|
||||
result.Title,
|
||||
result.SelectedOption?.ProposedAt.UtcDateTime ?? result.CurrentScheduledAt,
|
||||
joinLink: null,
|
||||
actorDisplayName: null,
|
||||
reason: result.SelectedOption is null ? result.Decision.Reason : null,
|
||||
ct);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,6 @@
|
||||
namespace GmRelay.Bot.Infrastructure.Scheduling;
|
||||
using GmRelay.Shared.Platform;
|
||||
|
||||
public interface ISystemClock
|
||||
{
|
||||
DateTimeOffset UtcNow { get; }
|
||||
}
|
||||
namespace GmRelay.Bot.Infrastructure.Scheduling;
|
||||
|
||||
public sealed class SystemClock : ISystemClock
|
||||
{
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
using System.Globalization;
|
||||
using GmRelay.Bot.Features.Sessions.RescheduleSession;
|
||||
using GmRelay.Shared.Domain;
|
||||
using GmRelay.Shared.Platform;
|
||||
using Telegram.Bot;
|
||||
using Telegram.Bot.Types;
|
||||
@@ -93,6 +95,68 @@ public sealed class TelegramPlatformMessenger(
|
||||
cancellationToken: ct);
|
||||
}
|
||||
|
||||
public async Task SendGroupMessageAsync(PlatformGroup group, string htmlText, IReadOnlyList<PlatformMessageAction> actions, CancellationToken ct)
|
||||
{
|
||||
EnsureTelegram(group.Platform);
|
||||
await bot.SendMessage(
|
||||
chatId: ParseLong(group.ExternalGroupId),
|
||||
messageThreadId: ParseNullableInt(group.ExternalThreadId),
|
||||
text: htmlText,
|
||||
parseMode: ParseMode.Html,
|
||||
replyMarkup: BuildActionsMarkup(actions),
|
||||
cancellationToken: ct);
|
||||
}
|
||||
|
||||
public async Task UpdateGroupMessageAsync(PlatformMessageRef messageRef, string htmlText, IReadOnlyList<PlatformMessageAction> actions, CancellationToken ct)
|
||||
{
|
||||
EnsureTelegram(messageRef.Platform);
|
||||
await bot.EditMessageText(
|
||||
chatId: ParseLong(messageRef.ExternalGroupId),
|
||||
messageId: ParseInt(messageRef.ExternalMessageId),
|
||||
text: htmlText,
|
||||
parseMode: ParseMode.Html,
|
||||
replyMarkup: BuildActionsMarkup(actions),
|
||||
cancellationToken: ct);
|
||||
}
|
||||
|
||||
public async Task<PlatformMessageRef> CreateThreadAsync(PlatformGroup group, string title, CancellationToken ct)
|
||||
{
|
||||
EnsureTelegram(group.Platform);
|
||||
var topic = await bot.CreateForumTopic(
|
||||
chatId: ParseLong(group.ExternalGroupId),
|
||||
name: title,
|
||||
cancellationToken: ct);
|
||||
|
||||
return new PlatformMessageRef(
|
||||
PlatformKind.Telegram,
|
||||
group.ExternalGroupId,
|
||||
topic.MessageThreadId.ToString(CultureInfo.InvariantCulture),
|
||||
string.Empty);
|
||||
}
|
||||
|
||||
public Task DeleteThreadAsync(PlatformGroup group, CancellationToken ct)
|
||||
{
|
||||
EnsureTelegram(group.Platform);
|
||||
if (string.IsNullOrWhiteSpace(group.ExternalThreadId))
|
||||
{
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
return bot.DeleteForumTopic(
|
||||
ParseLong(group.ExternalGroupId),
|
||||
ParseInt(group.ExternalThreadId),
|
||||
cancellationToken: ct);
|
||||
}
|
||||
|
||||
public Task DeleteMessageAsync(PlatformMessageRef messageRef, CancellationToken ct)
|
||||
{
|
||||
EnsureTelegram(messageRef.Platform);
|
||||
return bot.DeleteMessage(
|
||||
ParseLong(messageRef.ExternalGroupId),
|
||||
ParseInt(messageRef.ExternalMessageId),
|
||||
cancellationToken: ct);
|
||||
}
|
||||
|
||||
public Task SendPrivateMessageAsync(PlatformPrivateMessage message, CancellationToken ct)
|
||||
{
|
||||
EnsureTelegram(message.Recipient.Platform);
|
||||
@@ -125,6 +189,135 @@ public sealed class TelegramPlatformMessenger(
|
||||
cancellationToken: ct);
|
||||
}
|
||||
|
||||
public async Task<PlatformMessageRef> SendConfirmationRequestAsync(PlatformConfirmationRequest request, CancellationToken ct)
|
||||
{
|
||||
EnsureTelegram(request.Group.Platform);
|
||||
|
||||
var chatId = ParseLong(request.Group.ExternalGroupId);
|
||||
var threadId = ParseNullableInt(request.Group.ExternalThreadId);
|
||||
var message = await bot.SendMessage(
|
||||
chatId: chatId,
|
||||
messageThreadId: threadId,
|
||||
text: BuildConfirmationText(request),
|
||||
parseMode: ParseMode.Html,
|
||||
replyMarkup: BuildRsvpKeyboard(request.SessionId),
|
||||
cancellationToken: ct);
|
||||
|
||||
return TelegramPlatformIds.Message(chatId, threadId, message.MessageId);
|
||||
}
|
||||
|
||||
public async Task UpdateConfirmationRequestAsync(PlatformRsvpMessageUpdate update, CancellationToken ct)
|
||||
{
|
||||
var request = update.Request;
|
||||
EnsureTelegram(request.Group.Platform);
|
||||
var existingMessage = request.ExistingMessage
|
||||
?? throw new ArgumentException("Existing confirmation message reference is required.", nameof(update));
|
||||
|
||||
EnsureTelegram(existingMessage.Platform);
|
||||
await bot.EditMessageText(
|
||||
chatId: ParseLong(existingMessage.ExternalGroupId),
|
||||
messageId: ParseInt(existingMessage.ExternalMessageId),
|
||||
text: BuildConfirmationText(request),
|
||||
parseMode: ParseMode.Html,
|
||||
replyMarkup: update.DisableActions ? null : BuildRsvpKeyboard(request.SessionId),
|
||||
cancellationToken: ct);
|
||||
}
|
||||
|
||||
public async Task<PlatformMessageRef> SendJoinLinkNotificationAsync(
|
||||
PlatformJoinLinkNotification notification,
|
||||
CancellationToken ct)
|
||||
{
|
||||
EnsureTelegram(notification.Group.Platform);
|
||||
|
||||
var chatId = ParseLong(notification.Group.ExternalGroupId);
|
||||
var threadId = ParseNullableInt(notification.Group.ExternalThreadId);
|
||||
var message = await bot.SendMessage(
|
||||
chatId: chatId,
|
||||
messageThreadId: threadId,
|
||||
text: BuildJoinLinkText(notification),
|
||||
cancellationToken: ct);
|
||||
|
||||
return TelegramPlatformIds.Message(chatId, threadId, message.MessageId);
|
||||
}
|
||||
|
||||
public Task SendDirectSessionNotificationAsync(
|
||||
PlatformDirectSessionNotification notification,
|
||||
CancellationToken ct)
|
||||
{
|
||||
EnsureTelegram(notification.Recipient.Platform);
|
||||
return bot.SendMessage(
|
||||
chatId: ParseLong(notification.Recipient.ExternalUserId),
|
||||
text: BuildDirectNotificationText(notification),
|
||||
parseMode: ParseMode.Html,
|
||||
cancellationToken: ct);
|
||||
}
|
||||
|
||||
public async Task SendRsvpOutcomeAsync(PlatformRsvpOutcomeNotification notification, CancellationToken ct)
|
||||
{
|
||||
switch (notification.Kind)
|
||||
{
|
||||
case PlatformRsvpOutcomeKind.GroupAllConfirmed:
|
||||
if (notification.Group is null)
|
||||
{
|
||||
throw new ArgumentException("Group notification requires a group.", nameof(notification));
|
||||
}
|
||||
|
||||
EnsureTelegram(notification.Group.Platform);
|
||||
await bot.SendMessage(
|
||||
chatId: ParseLong(notification.Group.ExternalGroupId),
|
||||
messageThreadId: ParseNullableInt(notification.Group.ExternalThreadId),
|
||||
text: $"🎉 Игра «{notification.Title}» подтверждена! Все участники на месте.",
|
||||
cancellationToken: ct);
|
||||
break;
|
||||
|
||||
case PlatformRsvpOutcomeKind.GmAllConfirmed:
|
||||
case PlatformRsvpOutcomeKind.GmPlayerDeclined:
|
||||
foreach (var recipient in notification.Recipients)
|
||||
{
|
||||
EnsureTelegram(recipient.Platform);
|
||||
await bot.SendMessage(
|
||||
chatId: ParseLong(recipient.ExternalUserId),
|
||||
text: BuildRsvpOutcomeDirectText(notification),
|
||||
parseMode: ParseMode.Html,
|
||||
cancellationToken: ct);
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException(nameof(notification), notification.Kind, "Unknown RSVP outcome kind.");
|
||||
}
|
||||
}
|
||||
|
||||
public Task UpdateRescheduleVoteAsync(PlatformRescheduleVoteUpdate update, CancellationToken ct)
|
||||
{
|
||||
EnsureTelegram(update.Group.Platform);
|
||||
EnsureTelegram(update.ExistingMessage.Platform);
|
||||
|
||||
var resultText = update.SelectedOption is not null
|
||||
? $"✅ <b>Голосование завершено.</b>\nПобедил вариант {update.SelectedOption.DisplayOrder}: <b>{update.SelectedOption.ProposedAt.FormatMoscow()}</b> (МСК)."
|
||||
: $"❌ <b>Голосование завершено.</b>\n{System.Net.WebUtility.HtmlEncode(update.Decision.Reason)}";
|
||||
|
||||
var text = $"""
|
||||
{HandleRescheduleTimeInputHandler.BuildVotingMessage(
|
||||
update.Title,
|
||||
update.CurrentScheduledAt,
|
||||
update.VotingDeadlineAt,
|
||||
update.Options,
|
||||
update.Participants,
|
||||
update.Votes)}
|
||||
|
||||
{resultText}
|
||||
""";
|
||||
|
||||
return bot.EditMessageText(
|
||||
chatId: ParseLong(update.ExistingMessage.ExternalGroupId),
|
||||
messageId: ParseInt(update.ExistingMessage.ExternalMessageId),
|
||||
text: text,
|
||||
parseMode: ParseMode.Html,
|
||||
cancellationToken: ct);
|
||||
}
|
||||
|
||||
private async Task<Message> SendScheduleTextMessage(
|
||||
long chatId,
|
||||
int? threadId,
|
||||
@@ -139,6 +332,134 @@ public sealed class TelegramPlatformMessenger(
|
||||
replyMarkup: markup,
|
||||
cancellationToken: ct);
|
||||
|
||||
private static string BuildConfirmationText(PlatformConfirmationRequest request)
|
||||
{
|
||||
var confirmed = request.Participants.Where(p => p.RsvpStatus == RsvpStatus.Confirmed).ToList();
|
||||
var declined = request.Participants.Where(p => p.RsvpStatus == RsvpStatus.Declined).ToList();
|
||||
var pending = request.Participants.Where(p => p.RsvpStatus == RsvpStatus.Pending).ToList();
|
||||
|
||||
var lines = new List<string>
|
||||
{
|
||||
$"🎲 Подтвердите участие в «{System.Net.WebUtility.HtmlEncode(request.Title)}»",
|
||||
$"📅 {request.ScheduledAt.FormatMoscow()} (МСК)",
|
||||
string.Empty
|
||||
};
|
||||
|
||||
foreach (var participant in confirmed)
|
||||
{
|
||||
lines.Add($" ✅ {FormatTelegramParticipant(participant)}");
|
||||
}
|
||||
|
||||
foreach (var participant in declined)
|
||||
{
|
||||
lines.Add($" ❌ <s>{FormatTelegramParticipant(participant)}</s>");
|
||||
}
|
||||
|
||||
foreach (var participant in pending)
|
||||
{
|
||||
lines.Add($" ⏳ {FormatTelegramParticipant(participant)}");
|
||||
}
|
||||
|
||||
lines.Add(string.Empty);
|
||||
|
||||
if (request.Participants.Count > 0 && confirmed.Count == request.Participants.Count)
|
||||
{
|
||||
lines.Add($"Статус: ✅ все подтвердили ({confirmed.Count}/{request.Participants.Count})");
|
||||
}
|
||||
else if (declined.Count > 0)
|
||||
{
|
||||
lines.Add($"Статус: ⚠️ есть отказы ({confirmed.Count}/{request.Participants.Count} подтвердили)");
|
||||
}
|
||||
else
|
||||
{
|
||||
lines.Add($"Статус: ожидаем подтверждения ({confirmed.Count}/{request.Participants.Count})");
|
||||
}
|
||||
|
||||
return string.Join("\n", lines);
|
||||
}
|
||||
|
||||
private static string BuildJoinLinkText(PlatformJoinLinkNotification notification)
|
||||
{
|
||||
var mentions = string.Join(", ", notification.ConfirmedPlayers.Select(FormatTelegramParticipant));
|
||||
|
||||
return $"""
|
||||
🎮 Игра «{notification.Title}» начинается через 5 минут!
|
||||
|
||||
🔗 Ссылка на подключение:
|
||||
{notification.JoinLink}
|
||||
|
||||
Участники: {mentions}
|
||||
|
||||
Хорошей игры! 🎲
|
||||
""";
|
||||
}
|
||||
|
||||
private static string BuildDirectNotificationText(PlatformDirectSessionNotification notification) =>
|
||||
notification.Kind switch
|
||||
{
|
||||
PlatformDirectSessionNotificationKind.ConfirmationRequest => $"""
|
||||
🎲 <b>Подтвердите участие в игре</b>
|
||||
|
||||
📌 <b>{System.Net.WebUtility.HtmlEncode(notification.Title)}</b>
|
||||
📅 {notification.ScheduledAt.FormatMoscow()} (МСК)
|
||||
|
||||
Ответьте кнопкой в групповом сообщении расписания.
|
||||
""",
|
||||
PlatformDirectSessionNotificationKind.OneHourReminder => $"""
|
||||
⏰ <b>Игра начнётся примерно через 1 час</b>
|
||||
|
||||
📌 <b>{System.Net.WebUtility.HtmlEncode(notification.Title)}</b>
|
||||
📅 {notification.ScheduledAt.FormatMoscow()} (МСК)
|
||||
🔗 {System.Net.WebUtility.HtmlEncode(notification.JoinLink ?? string.Empty)}
|
||||
""",
|
||||
PlatformDirectSessionNotificationKind.JoinLink => $"""
|
||||
🎮 <b>Игра начинается через 5 минут</b>
|
||||
|
||||
📌 <b>{System.Net.WebUtility.HtmlEncode(notification.Title)}</b>
|
||||
🔗 {System.Net.WebUtility.HtmlEncode(notification.JoinLink ?? string.Empty)}
|
||||
""",
|
||||
PlatformDirectSessionNotificationKind.RescheduleApproved => $"""
|
||||
✅ <b>Сессия перенесена по итогам голосования</b>
|
||||
|
||||
📌 <b>{System.Net.WebUtility.HtmlEncode(notification.Title)}</b>
|
||||
📅 Новое время: <b>{notification.ScheduledAt.FormatMoscow()}</b> (МСК)
|
||||
""",
|
||||
PlatformDirectSessionNotificationKind.RescheduleRejected => $"""
|
||||
❌ <b>Перенос сессии отклонён по итогам голосования</b>
|
||||
|
||||
📌 <b>{System.Net.WebUtility.HtmlEncode(notification.Title)}</b>
|
||||
📅 Время остаётся прежним: <b>{notification.ScheduledAt.FormatMoscow()}</b> (МСК)
|
||||
Причина: {System.Net.WebUtility.HtmlEncode(notification.Reason ?? string.Empty)}
|
||||
""",
|
||||
_ => BuildFallbackDirectText(notification)
|
||||
};
|
||||
|
||||
private static string BuildFallbackDirectText(PlatformDirectSessionNotification notification) =>
|
||||
$"<b>{System.Net.WebUtility.HtmlEncode(notification.Title)}</b>\n{notification.ScheduledAt.FormatMoscow()} (МСК)";
|
||||
|
||||
private static string BuildRsvpOutcomeDirectText(PlatformRsvpOutcomeNotification notification) =>
|
||||
notification.Kind switch
|
||||
{
|
||||
PlatformRsvpOutcomeKind.GmAllConfirmed =>
|
||||
$"✅ Все подтвердили участие в «{System.Net.WebUtility.HtmlEncode(notification.Title)}» ({notification.ScheduledAt.FormatMoscow()} МСК).",
|
||||
PlatformRsvpOutcomeKind.GmPlayerDeclined =>
|
||||
$"🚨 Отмена! {System.Net.WebUtility.HtmlEncode(notification.ActorDisplayName ?? "Игрок")} не сможет прийти на игру «{System.Net.WebUtility.HtmlEncode(notification.Title)}».",
|
||||
_ => System.Net.WebUtility.HtmlEncode(notification.Title)
|
||||
};
|
||||
|
||||
private static InlineKeyboardMarkup BuildRsvpKeyboard(Guid sessionId) =>
|
||||
new([
|
||||
[
|
||||
InlineKeyboardButton.WithCallbackData("✅ Буду", $"rsvp:confirm:{sessionId}"),
|
||||
InlineKeyboardButton.WithCallbackData("❌ Не смогу", $"rsvp:decline:{sessionId}")
|
||||
]
|
||||
]);
|
||||
|
||||
private static string FormatTelegramParticipant(PlatformSessionParticipant participant) =>
|
||||
participant.User.ExternalUsername is not null
|
||||
? $"@{participant.User.ExternalUsername}"
|
||||
: System.Net.WebUtility.HtmlEncode(participant.User.DisplayName);
|
||||
|
||||
private async Task TrySendScheduleImageOnly(
|
||||
long chatId,
|
||||
int? threadId,
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
// ... UpdateRouter will have CancelSessionHandler and cancel_session route instead of close_recruitment
|
||||
using GmRelay.Shared.Domain;
|
||||
using GmRelay.Shared.Features.Confirmation.HandleRsvp;
|
||||
using GmRelay.Shared.Features.Sessions.CreateSession;
|
||||
using GmRelay.Shared.Rendering;
|
||||
using GmRelay.Bot.Features.Confirmation.HandleRsvp;
|
||||
using GmRelay.Bot.Features.Sessions.CreateSession;
|
||||
using GmRelay.Bot.Features.Sessions.ListSessions;
|
||||
using BotCreateSessionHandler = GmRelay.Bot.Features.Sessions.CreateSession.CreateSessionHandler;
|
||||
using BotRescheduleTimeInputHandler = GmRelay.Bot.Features.Sessions.RescheduleSession.HandleRescheduleTimeInputHandler;
|
||||
using BotRescheduleVoteHandler = GmRelay.Bot.Features.Sessions.RescheduleSession.HandleRescheduleVoteHandler;
|
||||
using GmRelay.Bot.Features.Sessions.ExportCalendar;
|
||||
using GmRelay.Bot.Features.Sessions.RescheduleSession;
|
||||
using Telegram.Bot;
|
||||
@@ -20,7 +23,7 @@ namespace GmRelay.Bot.Infrastructure.Telegram;
|
||||
/// </summary>
|
||||
public sealed class UpdateRouter(
|
||||
HandleRsvpHandler rsvpHandler,
|
||||
CreateSessionHandler createSessionHandler,
|
||||
BotCreateSessionHandler createSessionHandler,
|
||||
JoinSessionHandler joinSessionHandler,
|
||||
LeaveSessionHandler leaveSessionHandler,
|
||||
PromoteWaitlistedPlayerHandler promoteWaitlistedPlayerHandler,
|
||||
@@ -29,8 +32,8 @@ public sealed class UpdateRouter(
|
||||
ListSessionsHandler listSessionsHandler,
|
||||
ExportCalendarHandler exportCalendarHandler,
|
||||
InitiateRescheduleHandler initiateRescheduleHandler,
|
||||
HandleRescheduleTimeInputHandler rescheduleTimeInputHandler,
|
||||
HandleRescheduleVoteHandler rescheduleVoteHandler,
|
||||
BotRescheduleTimeInputHandler rescheduleTimeInputHandler,
|
||||
BotRescheduleVoteHandler rescheduleVoteHandler,
|
||||
ITelegramBotClient bot,
|
||||
IConfiguration configuration,
|
||||
ILogger<UpdateRouter> logger) : ITelegramUpdateHandler
|
||||
@@ -187,11 +190,11 @@ public sealed class UpdateRouter(
|
||||
|
||||
var command = new HandleRsvpCommand(
|
||||
SessionId: sessionId,
|
||||
TelegramUserId: query.From.Id,
|
||||
User: user,
|
||||
Status: status,
|
||||
CallbackQueryId: query.Id,
|
||||
ChatId: message.Chat.Id,
|
||||
MessageId: message.MessageId);
|
||||
InteractionId: query.Id,
|
||||
Group: group,
|
||||
ConfirmationMessage: scheduleMessage);
|
||||
|
||||
await rsvpHandler.HandleAsync(command, ct);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
-- =============================================================
|
||||
-- V018: Add platform columns to reschedule_proposals
|
||||
-- =============================================================
|
||||
-- Add platform columns to reschedule_proposals to support Discord reschedule voting.
|
||||
-- proposed_by is made nullable so Discord proposals can leave it NULL
|
||||
-- (Discord snowflakes don't fit in BIGINT safely).
|
||||
-- =============================================================
|
||||
|
||||
ALTER TABLE reschedule_proposals
|
||||
ALTER COLUMN proposed_by DROP NOT NULL;
|
||||
|
||||
ALTER TABLE reschedule_proposals
|
||||
ADD COLUMN source_platform VARCHAR(50),
|
||||
ADD COLUMN proposed_by_external_user_id VARCHAR(255);
|
||||
|
||||
UPDATE reschedule_proposals
|
||||
SET source_platform = 'Telegram',
|
||||
proposed_by_external_user_id = proposed_by::TEXT
|
||||
WHERE source_platform IS NULL;
|
||||
@@ -0,0 +1,18 @@
|
||||
-- =============================================================
|
||||
-- V019: Rename session_audit_log.actor_telegram_id to actor_external_user_id
|
||||
-- =============================================================
|
||||
-- Scope: Support platform-agnostic audit log identity.
|
||||
-- =============================================================
|
||||
|
||||
ALTER TABLE session_audit_log
|
||||
ADD COLUMN actor_external_user_id VARCHAR(255);
|
||||
|
||||
UPDATE session_audit_log
|
||||
SET actor_external_user_id = actor_telegram_id::TEXT
|
||||
WHERE actor_external_user_id IS NULL;
|
||||
|
||||
ALTER TABLE session_audit_log
|
||||
ALTER COLUMN actor_external_user_id SET NOT NULL;
|
||||
|
||||
ALTER TABLE session_audit_log
|
||||
DROP COLUMN actor_telegram_id;
|
||||
@@ -0,0 +1,37 @@
|
||||
-- =============================================================
|
||||
-- V020: Player identity linking for unified multi-platform accounts
|
||||
-- =============================================================
|
||||
-- Scope: Allow linking multiple platform identities (Telegram, Discord)
|
||||
-- to a single "primary" player account. All group/session permissions
|
||||
-- resolve through the effective (primary) player id.
|
||||
-- =============================================================
|
||||
|
||||
-- player_links: secondary player → primary player (1:1 on secondary)
|
||||
CREATE TABLE player_links (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
primary_player_id UUID NOT NULL REFERENCES players(id) ON DELETE CASCADE,
|
||||
secondary_player_id UUID NOT NULL UNIQUE REFERENCES players(id) ON DELETE CASCADE,
|
||||
linked_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||
linked_by_player_id UUID REFERENCES players(id) ON DELETE SET NULL,
|
||||
-- Prevent self-linking at the DB level
|
||||
CONSTRAINT no_self_link CHECK (primary_player_id <> secondary_player_id)
|
||||
);
|
||||
|
||||
CREATE INDEX ix_player_links_primary_player_id
|
||||
ON player_links(primary_player_id);
|
||||
|
||||
-- identity_audit_log: security-sensitive link/unlink actions
|
||||
CREATE TABLE identity_audit_log (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
player_id UUID NOT NULL REFERENCES players(id) ON DELETE CASCADE,
|
||||
action VARCHAR(50) NOT NULL, -- 'link', 'unlink', 'link_attempt_conflict'
|
||||
target_platform VARCHAR(50),
|
||||
target_external_user_id VARCHAR(255),
|
||||
performed_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||
performed_by_player_id UUID REFERENCES players(id) ON DELETE SET NULL
|
||||
);
|
||||
|
||||
CREATE INDEX ix_identity_audit_log_player_id
|
||||
ON identity_audit_log(player_id);
|
||||
CREATE INDEX ix_identity_audit_log_performed_at
|
||||
ON identity_audit_log(performed_at DESC);
|
||||
@@ -0,0 +1,8 @@
|
||||
-- =============================================================
|
||||
-- V021: Add avatar_url column to players table
|
||||
-- =============================================================
|
||||
-- Scope: Support storing avatar URLs for Discord and other platforms.
|
||||
-- =============================================================
|
||||
|
||||
ALTER TABLE players
|
||||
ADD COLUMN avatar_url VARCHAR(500);
|
||||
@@ -0,0 +1,16 @@
|
||||
-- =============================================================
|
||||
-- V022: Fix incorrectly oriented player_links for Discord↔Telegram
|
||||
-- =============================================================
|
||||
-- Scope: Reverse player_links where Discord was incorrectly made primary
|
||||
-- and Telegram secondary. Telegram (with historical group/session data)
|
||||
-- must always be the primary account.
|
||||
-- =============================================================
|
||||
|
||||
UPDATE player_links pl
|
||||
SET primary_player_id = pl.secondary_player_id,
|
||||
secondary_player_id = pl.primary_player_id
|
||||
FROM players p1, players p2
|
||||
WHERE pl.primary_player_id = p1.id
|
||||
AND pl.secondary_player_id = p2.id
|
||||
AND p1.platform = 'Discord'
|
||||
AND p2.platform = 'Telegram';
|
||||
@@ -0,0 +1,14 @@
|
||||
-- =============================================================
|
||||
-- V023: Make legacy Telegram columns nullable for multi-platform
|
||||
-- =============================================================
|
||||
-- Scope: Allow Discord (and future platforms) to create players
|
||||
-- and game_groups without legacy telegram_* values.
|
||||
-- Existing Telegram data was backfilled in V016.
|
||||
-- =============================================================
|
||||
|
||||
ALTER TABLE game_groups
|
||||
ALTER COLUMN telegram_chat_id DROP NOT NULL,
|
||||
ALTER COLUMN gm_telegram_id DROP NOT NULL;
|
||||
|
||||
ALTER TABLE players
|
||||
ALTER COLUMN telegram_id DROP NOT NULL;
|
||||
@@ -0,0 +1,41 @@
|
||||
-- =============================================================
|
||||
-- V024: Deprecate legacy Telegram-specific columns
|
||||
-- =============================================================
|
||||
-- Scope: Complete platform migration by backfilling any remaining
|
||||
-- external_* gaps and officially deprecating telegram_* columns.
|
||||
-- No columns are dropped — rollback-safe.
|
||||
-- =============================================================
|
||||
|
||||
-- 1. Backfill players platform identity (safeguard for any rows missed in V016)
|
||||
UPDATE players
|
||||
SET platform = 'Telegram',
|
||||
external_user_id = telegram_id::TEXT,
|
||||
external_username = telegram_username
|
||||
WHERE platform IS NULL;
|
||||
|
||||
-- 2. Backfill game_groups platform identity (safeguard for any rows missed in V016)
|
||||
UPDATE game_groups
|
||||
SET platform = 'Telegram',
|
||||
external_group_id = telegram_chat_id::TEXT
|
||||
WHERE platform IS NULL;
|
||||
|
||||
-- 3. Add platform identity to calendar_subscriptions
|
||||
ALTER TABLE calendar_subscriptions
|
||||
ADD COLUMN user_platform VARCHAR(50),
|
||||
ADD COLUMN user_external_id VARCHAR(255);
|
||||
|
||||
UPDATE calendar_subscriptions
|
||||
SET user_external_id = user_telegram_id::TEXT,
|
||||
user_platform = 'Telegram'
|
||||
WHERE user_platform IS NULL;
|
||||
|
||||
-- 4. Migrate calendar subscription index
|
||||
DROP INDEX IF EXISTS ix_calendar_subscriptions_user_telegram_id;
|
||||
CREATE INDEX ix_calendar_subscriptions_user_external_id ON calendar_subscriptions (user_external_id);
|
||||
|
||||
-- 5. Deprecation comments on legacy columns
|
||||
COMMENT ON COLUMN players.telegram_id IS 'DEPRECATED: use platform + external_user_id';
|
||||
COMMENT ON COLUMN players.telegram_username IS 'DEPRECATED: use external_username';
|
||||
COMMENT ON COLUMN game_groups.telegram_chat_id IS 'DEPRECATED: use platform + external_group_id';
|
||||
COMMENT ON COLUMN game_groups.gm_telegram_id IS 'DEPRECATED: group ownership is tracked in group_managers';
|
||||
COMMENT ON COLUMN calendar_subscriptions.user_telegram_id IS 'DEPRECATED: use user_platform + user_external_id';
|
||||
@@ -0,0 +1,11 @@
|
||||
-- =============================================================
|
||||
-- V025: Backfill proposed_by_external_user_id for Telegram proposals
|
||||
-- =============================================================
|
||||
-- Scope: Ensure all reschedule_proposals have proposed_by_external_user_id
|
||||
-- populated so that InitiateRescheduleHandler can stop writing proposed_by.
|
||||
-- =============================================================
|
||||
|
||||
UPDATE reschedule_proposals
|
||||
SET proposed_by_external_user_id = proposed_by::TEXT
|
||||
WHERE proposed_by_external_user_id IS NULL
|
||||
AND proposed_by IS NOT NULL;
|
||||
@@ -0,0 +1,17 @@
|
||||
-- Public club pages and read-only schedule publication controls.
|
||||
|
||||
ALTER TABLE game_groups
|
||||
ADD COLUMN public_slug VARCHAR(120),
|
||||
ADD COLUMN public_schedule_enabled BOOLEAN NOT NULL DEFAULT false,
|
||||
ADD COLUMN public_schedule_updated_at TIMESTAMPTZ;
|
||||
|
||||
ALTER TABLE sessions
|
||||
ADD COLUMN is_public BOOLEAN NOT NULL DEFAULT false;
|
||||
|
||||
CREATE UNIQUE INDEX ux_game_groups_public_slug
|
||||
ON game_groups (lower(public_slug))
|
||||
WHERE public_slug IS NOT NULL;
|
||||
|
||||
CREATE INDEX ix_sessions_public_schedule
|
||||
ON sessions (group_id, scheduled_at)
|
||||
WHERE is_public = true AND status <> 'Cancelled';
|
||||
@@ -0,0 +1,14 @@
|
||||
-- Showcase fields for game catalog / public session browsing.
|
||||
|
||||
ALTER TABLE sessions
|
||||
ADD COLUMN is_one_shot BOOLEAN NOT NULL DEFAULT false,
|
||||
ADD COLUMN system VARCHAR(50),
|
||||
ADD COLUMN description TEXT,
|
||||
ADD COLUMN cover_image_url TEXT,
|
||||
ADD COLUMN duration_minutes INTEGER,
|
||||
ADD COLUMN format VARCHAR(20) CHECK (format IN ('Online', 'Offline', 'Hybrid')),
|
||||
ADD COLUMN allow_direct_registration BOOLEAN NOT NULL DEFAULT false;
|
||||
|
||||
CREATE INDEX ix_sessions_showcase
|
||||
ON sessions (scheduled_at, system, is_one_shot, format)
|
||||
WHERE is_public = true AND status <> 'Cancelled';
|
||||
@@ -0,0 +1,20 @@
|
||||
-- Public GM profiles for catalog and club trust pages.
|
||||
|
||||
CREATE TABLE master_profiles (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
player_id UUID NOT NULL UNIQUE REFERENCES players(id) ON DELETE CASCADE,
|
||||
public_slug VARCHAR(120),
|
||||
is_public BOOLEAN NOT NULL DEFAULT false,
|
||||
display_name VARCHAR(255) NOT NULL,
|
||||
bio TEXT,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||
updated_at TIMESTAMPTZ NOT NULL DEFAULT now()
|
||||
);
|
||||
|
||||
CREATE UNIQUE INDEX ux_master_profiles_public_slug
|
||||
ON master_profiles (lower(public_slug))
|
||||
WHERE public_slug IS NOT NULL;
|
||||
|
||||
CREATE INDEX ix_master_profiles_public
|
||||
ON master_profiles (lower(public_slug))
|
||||
WHERE is_public = true AND public_slug IS NOT NULL;
|
||||
+22
-10
@@ -1,16 +1,18 @@
|
||||
using GmRelay.Bot.Features.Confirmation.HandleRsvp;
|
||||
using GmRelay.Bot.Features.Confirmation.SendConfirmation;
|
||||
using GmRelay.Bot.Features.Notifications;
|
||||
using GmRelay.Bot.Features.Reminders.SendJoinLink;
|
||||
using GmRelay.Bot.Features.Reminders.SendOneHourReminder;
|
||||
using GmRelay.Bot.Features.Sessions.CreateSession;
|
||||
using GmRelay.Bot.Features.Sessions.RescheduleSession;
|
||||
using GmRelay.Bot.Infrastructure.Database;
|
||||
using GmRelay.Shared.Features.Sessions.RescheduleSession;
|
||||
using GmRelay.Bot.Infrastructure.Health;
|
||||
using GmRelay.Bot.Infrastructure.Logging;
|
||||
using GmRelay.Bot.Infrastructure.Scheduling;
|
||||
using GmRelay.Bot.Infrastructure.Telegram;
|
||||
using GmRelay.Shared.Features.Confirmation.HandleRsvp;
|
||||
using GmRelay.Shared.Features.Confirmation.SendConfirmation;
|
||||
using GmRelay.Shared.Features.Notifications;
|
||||
using GmRelay.Shared.Features.Reminders.SendJoinLink;
|
||||
using GmRelay.Shared.Features.Reminders.SendOneHourReminder;
|
||||
using GmRelay.Shared.Features.Sessions.CreateSession;
|
||||
using GmRelay.Shared.Infrastructure.Scheduling;
|
||||
using GmRelay.Shared.Platform;
|
||||
using Npgsql;
|
||||
using Telegram.Bot;
|
||||
@@ -53,28 +55,38 @@ builder.Services.AddSingleton<ITelegramBotClient>(sp =>
|
||||
});
|
||||
builder.Services.AddSingleton<ITelegramUpdateSource, TelegramUpdateSource>();
|
||||
builder.Services.AddSingleton<IPlatformMessenger, TelegramPlatformMessenger>();
|
||||
builder.Services.AddSingleton(new PlatformSchedulerOptions(PlatformKind.Telegram));
|
||||
|
||||
// ── Feature handlers (explicit registration — AOT safe) ──────────────
|
||||
builder.Services.AddSingleton<SendConfirmationHandler>();
|
||||
builder.Services.AddSingleton<ISendConfirmationHandler>(sp => sp.GetRequiredService<SendConfirmationHandler>());
|
||||
builder.Services.AddSingleton<DirectSessionNotificationSender>();
|
||||
builder.Services.AddSingleton<PlatformDirectNotificationSender>();
|
||||
builder.Services.AddSingleton<HandleRsvpHandler>();
|
||||
builder.Services.AddSingleton<SendJoinLinkHandler>();
|
||||
builder.Services.AddSingleton<ISendJoinLinkHandler>(sp => sp.GetRequiredService<SendJoinLinkHandler>());
|
||||
builder.Services.AddSingleton<SendOneHourReminderHandler>();
|
||||
builder.Services.AddSingleton<ISendOneHourReminderHandler>(sp => sp.GetRequiredService<SendOneHourReminderHandler>());
|
||||
builder.Services.AddSingleton<CreateSessionHandler>();
|
||||
builder.Services.AddSingleton<GmRelay.Shared.Features.Sessions.CreateSession.CreateSessionHandler>();
|
||||
builder.Services.AddSingleton<GmRelay.Bot.Features.Sessions.CreateSession.CreateSessionHandler>();
|
||||
builder.Services.AddSingleton<IScheduleMessageUpdateLock, ScheduleMessageUpdateLock>();
|
||||
builder.Services.AddSingleton<JoinSessionHandler>();
|
||||
builder.Services.AddSingleton<LeaveSessionHandler>();
|
||||
builder.Services.AddSingleton<PromoteWaitlistedPlayerHandler>();
|
||||
builder.Services.AddSingleton<CancelSessionHandler>();
|
||||
builder.Services.AddSingleton<GmRelay.Shared.Features.Sessions.ListSessions.DeleteSessionHandler>();
|
||||
builder.Services.AddSingleton<GmRelay.Bot.Features.Sessions.ListSessions.DeleteSessionHandler>();
|
||||
builder.Services.AddSingleton<GmRelay.Shared.Features.Sessions.ListSessions.ListSessionsHandler>();
|
||||
builder.Services.AddSingleton<GmRelay.Bot.Features.Sessions.ListSessions.ListSessionsHandler>();
|
||||
builder.Services.AddSingleton<GmRelay.Shared.Features.Sessions.ExportCalendar.ExportCalendarHandler>();
|
||||
builder.Services.AddSingleton<GmRelay.Bot.Features.Sessions.ExportCalendar.ExportCalendarHandler>();
|
||||
builder.Services.AddSingleton<InitiateRescheduleHandler>();
|
||||
builder.Services.AddSingleton<HandleRescheduleTimeInputHandler>();
|
||||
builder.Services.AddSingleton<HandleRescheduleVoteHandler>();
|
||||
builder.Services.AddSingleton<GmRelay.Shared.Features.Sessions.RescheduleSession.HandleRescheduleTimeInputHandler>();
|
||||
builder.Services.AddSingleton<GmRelay.Bot.Features.Sessions.RescheduleSession.HandleRescheduleTimeInputHandler>();
|
||||
builder.Services.AddSingleton<GmRelay.Shared.Features.Sessions.RescheduleSession.HandleRescheduleVoteHandler>();
|
||||
builder.Services.AddSingleton<GmRelay.Bot.Features.Sessions.RescheduleSession.HandleRescheduleVoteHandler>();
|
||||
builder.Services.AddSingleton<RescheduleVotingFinalizer>();
|
||||
|
||||
builder.Services.AddSingleton<DirectSessionNotificationSender>();
|
||||
|
||||
// ── Telegram infrastructure ──────────────────────────────────────────
|
||||
builder.Services.AddSingleton<UpdateRouter>();
|
||||
@@ -83,7 +95,7 @@ builder.Services.AddHostedService<TelegramMiniAppMenuButtonService>();
|
||||
builder.Services.AddHostedService<TelegramBotService>();
|
||||
|
||||
// ── Clock and scheduling ──────────────────────────────────────────────
|
||||
builder.Services.AddSingleton<ISystemClock, SystemClock>();
|
||||
builder.Services.AddSingleton<ISystemClock, GmRelay.Bot.Infrastructure.Scheduling.SystemClock>();
|
||||
builder.Services.AddSingleton<ISessionTriggerStore, DbSessionTriggerStore>();
|
||||
|
||||
// ── Session scheduler ────────────────────────────────────────────────
|
||||
|
||||
@@ -664,6 +664,7 @@
|
||||
"type": "Project",
|
||||
"dependencies": {
|
||||
"Dapper": "[2.1.72, )",
|
||||
"Microsoft.Extensions.Hosting.Abstractions": "[10.0.5, )",
|
||||
"Microsoft.Extensions.Logging.Abstractions": "[10.0.5, )",
|
||||
"Npgsql": "[10.0.2, )"
|
||||
}
|
||||
|
||||
@@ -13,8 +13,13 @@ WORKDIR /src/src/GmRelay.DiscordBot
|
||||
RUN dotnet publish "GmRelay.DiscordBot.csproj" -c Release -o /app/publish /p:UseAppHost=false
|
||||
|
||||
# Stage 2: Runtime
|
||||
FROM mcr.microsoft.com/dotnet/runtime:10.0-noble AS final
|
||||
FROM mcr.microsoft.com/dotnet/aspnet:10.0-noble AS final
|
||||
WORKDIR /app
|
||||
|
||||
# Install wget for healthcheck
|
||||
RUN apt-get update && apt-get install -y --no-install-recommends wget \
|
||||
&& rm -rf /var/lib/apt/lists/*
|
||||
|
||||
COPY --from=build /app/publish .
|
||||
USER $APP_UID
|
||||
ENTRYPOINT ["dotnet", "GmRelay.DiscordBot.dll"]
|
||||
|
||||
@@ -0,0 +1,84 @@
|
||||
using Dapper;
|
||||
using GmRelay.DiscordBot.Infrastructure.Discord;
|
||||
using GmRelay.Shared.Rendering;
|
||||
using Npgsql;
|
||||
|
||||
namespace GmRelay.DiscordBot.Features.Sessions;
|
||||
|
||||
public sealed record DiscordDeleteSessionResult(
|
||||
string ReplyText,
|
||||
SessionBatchViewModel? UpdatedView,
|
||||
string? EmptyMessage = null);
|
||||
|
||||
public sealed class DiscordDeleteSessionHandler(
|
||||
NpgsqlDataSource dataSource,
|
||||
DiscordPermissionChecker permissionChecker,
|
||||
DiscordListSessionsHandler listSessionsHandler,
|
||||
ILogger<DiscordDeleteSessionHandler> logger)
|
||||
{
|
||||
public async Task<DiscordDeleteSessionResult> HandleAsync(
|
||||
string guildId,
|
||||
string channelId,
|
||||
ulong userId,
|
||||
ulong resolvedPermissions,
|
||||
ulong guildOwnerId,
|
||||
Guid sessionId,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
await using var connection = await dataSource.OpenConnectionAsync(cancellationToken);
|
||||
|
||||
var dbManagerUserIds = await connection.QueryAsync<ulong>(
|
||||
@"SELECT CAST(p.external_user_id AS BIGINT)
|
||||
FROM group_managers gm
|
||||
JOIN players p ON p.id = gm.player_id
|
||||
JOIN game_groups g ON g.id = gm.group_id
|
||||
WHERE g.platform = 'Discord' AND g.external_group_id = @GuildId",
|
||||
new { GuildId = guildId });
|
||||
|
||||
if (!permissionChecker.CanManageSchedule(guildOwnerId, userId, dbManagerUserIds, resolvedPermissions))
|
||||
{
|
||||
return new DiscordDeleteSessionResult(
|
||||
"Только owner, администратор или manager могут удалять сессии.",
|
||||
UpdatedView: null);
|
||||
}
|
||||
|
||||
await using var transaction = await connection.BeginTransactionAsync(cancellationToken);
|
||||
var deletedRows = await connection.ExecuteAsync(
|
||||
"""
|
||||
DELETE FROM sessions s
|
||||
USING game_groups g
|
||||
WHERE s.group_id = g.id
|
||||
AND s.id = @SessionId
|
||||
AND g.platform = 'Discord'
|
||||
AND g.external_group_id = @GuildId
|
||||
""",
|
||||
new { SessionId = sessionId, GuildId = guildId },
|
||||
transaction);
|
||||
|
||||
await transaction.CommitAsync(cancellationToken);
|
||||
|
||||
if (deletedRows == 0)
|
||||
{
|
||||
return new DiscordDeleteSessionResult(
|
||||
"Сессия не найдена или уже удалена.",
|
||||
UpdatedView: null);
|
||||
}
|
||||
|
||||
logger.LogInformation("Deleted Discord session {SessionId} in guild {GuildId}", sessionId, guildId);
|
||||
|
||||
var updatedView = await listSessionsHandler.BuildScheduleAsync(
|
||||
guildId,
|
||||
channelId,
|
||||
userId,
|
||||
resolvedPermissions,
|
||||
guildOwnerId,
|
||||
cancellationToken);
|
||||
|
||||
return updatedView is null
|
||||
? new DiscordDeleteSessionResult(
|
||||
"Сессия удалена.",
|
||||
UpdatedView: null,
|
||||
EmptyMessage: "В этом сервере нет предстоящих игр.")
|
||||
: new DiscordDeleteSessionResult("Сессия удалена.", updatedView);
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,9 @@
|
||||
using NetCord;
|
||||
using NetCord.Rest;
|
||||
using NetCord.Services.ApplicationCommands;
|
||||
|
||||
namespace GmRelay.DiscordBot.Features.Sessions;
|
||||
|
||||
[SlashCommand("listsessions", "Show upcoming game sessions in this server")]
|
||||
public class DiscordListSessionsCommand : ApplicationCommandModule<SlashCommandContext>
|
||||
{
|
||||
private readonly DiscordListSessionsHandler _handler;
|
||||
@@ -13,13 +13,23 @@ public class DiscordListSessionsCommand : ApplicationCommandModule<SlashCommandC
|
||||
_handler = handler;
|
||||
}
|
||||
|
||||
[SlashCommand("listsessions", "Show upcoming game sessions in this server")]
|
||||
public async Task ExecuteAsync()
|
||||
{
|
||||
var guildId = Context.Guild?.Id.ToString()
|
||||
var guildId = Context.Interaction.GuildId?.ToString()
|
||||
?? throw new InvalidOperationException("This command can only be used in a guild.");
|
||||
var channelId = Context.Channel.Id.ToString();
|
||||
var member = Context.User as GuildInteractionUser;
|
||||
var resolvedPermissions = member is null ? 0UL : (ulong)member.Permissions;
|
||||
var guildOwnerId = 0UL;
|
||||
|
||||
var view = await _handler.BuildScheduleAsync(guildId, channelId, CancellationToken.None);
|
||||
var view = await _handler.BuildScheduleAsync(
|
||||
guildId,
|
||||
channelId,
|
||||
Context.User.Id,
|
||||
resolvedPermissions,
|
||||
guildOwnerId,
|
||||
CancellationToken.None);
|
||||
|
||||
if (view is null)
|
||||
{
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using Dapper;
|
||||
using GmRelay.DiscordBot.Infrastructure.Discord;
|
||||
using GmRelay.Shared.Domain;
|
||||
using GmRelay.Shared.Rendering;
|
||||
using Npgsql;
|
||||
@@ -9,11 +10,22 @@ internal sealed record DiscordSessionListItemDto(
|
||||
Guid Id, string Title, DateTime ScheduledAt, string Status, int? MaxPlayers,
|
||||
int PlayerCount, int WaitlistCount);
|
||||
|
||||
public sealed class DiscordListSessionsHandler(NpgsqlDataSource dataSource)
|
||||
public sealed class DiscordListSessionsHandler(
|
||||
NpgsqlDataSource dataSource,
|
||||
DiscordPermissionChecker permissionChecker)
|
||||
{
|
||||
public Task<SessionBatchViewModel?> BuildScheduleAsync(
|
||||
string guildId,
|
||||
string channelId,
|
||||
CancellationToken cancellationToken) =>
|
||||
BuildScheduleAsync(guildId, channelId, 0, 0, 0, cancellationToken);
|
||||
|
||||
public async Task<SessionBatchViewModel?> BuildScheduleAsync(
|
||||
string guildId,
|
||||
string channelId,
|
||||
ulong userId,
|
||||
ulong resolvedPermissions,
|
||||
ulong guildOwnerId,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
await using var connection = await dataSource.OpenConnectionAsync(cancellationToken);
|
||||
@@ -21,15 +33,15 @@ public sealed class DiscordListSessionsHandler(NpgsqlDataSource dataSource)
|
||||
var sessions = await connection.QueryAsync<DiscordSessionListItemDto>(
|
||||
@"SELECT s.id as Id, s.title as Title, s.scheduled_at as ScheduledAt, s.status as Status,
|
||||
s.max_players as MaxPlayers,
|
||||
COUNT(sp.id) FILTER (WHERE sp.is_gm = false AND sp.registration_status = @Active) as PlayerCount,
|
||||
COUNT(sp.id) FILTER (WHERE sp.is_gm = false AND sp.registration_status = @Waitlisted) as WaitlistCount
|
||||
COUNT(sp.id) FILTER (WHERE sp.is_gm = false AND sp.registration_status = @Active)::int as PlayerCount,
|
||||
COUNT(sp.id) FILTER (WHERE sp.is_gm = false AND sp.registration_status = @Waitlisted)::int as WaitlistCount
|
||||
FROM sessions s
|
||||
JOIN game_groups g ON s.group_id = g.id
|
||||
LEFT JOIN session_participants sp ON s.id = sp.session_id
|
||||
WHERE g.platform = 'Discord'
|
||||
AND g.external_group_id = @GuildId
|
||||
AND s.status != @Cancelled
|
||||
AND s.scheduled_at > NOW()
|
||||
AND s.scheduled_at > now() - interval '4 hours'
|
||||
GROUP BY s.id, s.title, s.scheduled_at, s.status, s.max_players
|
||||
ORDER BY s.scheduled_at ASC",
|
||||
new
|
||||
@@ -44,11 +56,25 @@ public sealed class DiscordListSessionsHandler(NpgsqlDataSource dataSource)
|
||||
if (sessionList.Count == 0)
|
||||
return null;
|
||||
|
||||
var dbManagerUserIds = await connection.QueryAsync<ulong>(
|
||||
@"SELECT CAST(p.external_user_id AS BIGINT)
|
||||
FROM group_managers gm
|
||||
JOIN players p ON p.id = gm.player_id
|
||||
JOIN game_groups g ON g.id = gm.group_id
|
||||
WHERE g.platform = 'Discord' AND g.external_group_id = @GuildId",
|
||||
new { GuildId = guildId });
|
||||
|
||||
var canManage = permissionChecker.CanManageSchedule(
|
||||
guildOwnerId,
|
||||
userId,
|
||||
dbManagerUserIds,
|
||||
resolvedPermissions);
|
||||
|
||||
var sessionIds = sessionList.Select(s => s.Id).ToList();
|
||||
var participants = await connection.QueryAsync<ParticipantBatchDto>(
|
||||
@"SELECT sp.session_id as SessionId,
|
||||
p.display_name as DisplayName,
|
||||
COALESCE(p.external_username, p.telegram_username) as TelegramUsername,
|
||||
p.external_username as TelegramUsername,
|
||||
sp.registration_status as RegistrationStatus
|
||||
FROM session_participants sp
|
||||
JOIN players p ON p.id = sp.player_id
|
||||
@@ -60,6 +86,25 @@ public sealed class DiscordListSessionsHandler(NpgsqlDataSource dataSource)
|
||||
var batchDtos = sessionList.Select(s => new SessionBatchDto(
|
||||
s.Id, s.ScheduledAt, s.Status, s.MaxPlayers, "")).ToList();
|
||||
|
||||
return SessionBatchViewBuilder.Build(firstTitle, batchDtos, participants.ToList());
|
||||
var view = SessionBatchViewBuilder.Build(firstTitle, batchDtos, participants.ToList());
|
||||
return canManage ? AddManagerActions(view) : view;
|
||||
}
|
||||
|
||||
internal static SessionBatchViewModel AddManagerActions(SessionBatchViewModel view) =>
|
||||
view with
|
||||
{
|
||||
Sessions = view.Sessions
|
||||
.Select(session =>
|
||||
{
|
||||
if (SessionStatus.IsCancelled(session.Status))
|
||||
return session;
|
||||
|
||||
var actions = session.AvailableActions
|
||||
.Concat([new AvailableAction("delete_session", $"Удалить {session.ScheduledAt.FormatMoscowShort()}", session.SessionId)])
|
||||
.ToList();
|
||||
|
||||
return session with { AvailableActions = actions };
|
||||
})
|
||||
.ToList()
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
using GmRelay.DiscordBot.Rendering;
|
||||
using GmRelay.DiscordBot.Rendering;
|
||||
using NetCord;
|
||||
using NetCord.Rest;
|
||||
using NetCord.Services.ApplicationCommands;
|
||||
|
||||
namespace GmRelay.DiscordBot.Features.Sessions;
|
||||
|
||||
[SlashCommand("newsession", "Create a new game session")]
|
||||
public class DiscordNewSessionCommand : ApplicationCommandModule<SlashCommandContext>
|
||||
{
|
||||
private readonly DiscordNewSessionHandler _handler;
|
||||
@@ -16,15 +16,54 @@ public class DiscordNewSessionCommand : ApplicationCommandModule<SlashCommandCon
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
[SlashCommand("newsession", "Create a new game session")]
|
||||
public async Task ExecuteAsync(
|
||||
[SlashCommandParameter(Name = "title", Description = "Game title")] string title,
|
||||
[SlashCommandParameter(Name = "time", Description = "Session time (YYYY-MM-DD HH:mm or DD.MM.YYYY HH:mm)")] string time,
|
||||
[SlashCommandParameter(Name = "seats", Description = "Maximum number of players")] long? seats = null,
|
||||
[SlashCommandParameter(Name = "link", Description = "Join link")] string? link = null)
|
||||
{
|
||||
var guild = Context.Guild
|
||||
_logger.LogInformation(
|
||||
"newsession called by user {UserId} ({UserType}) in guild {GuildId}, channel {ChannelId}",
|
||||
Context.User.Id,
|
||||
Context.User.GetType().Name,
|
||||
Context.Interaction.GuildId,
|
||||
Context.Channel?.Id);
|
||||
|
||||
var guildId = Context.Interaction.GuildId
|
||||
?? throw new InvalidOperationException("This command can only be used in a guild.");
|
||||
|
||||
var member = Context.User as GuildInteractionUser;
|
||||
if (member is null)
|
||||
{
|
||||
_logger.LogError("Context.User is not GuildInteractionUser. Actual type: {ActualType}", Context.User.GetType().Name);
|
||||
throw new InvalidOperationException("Guild member data not available in interaction.");
|
||||
}
|
||||
|
||||
var resolvedPermissions = (ulong)member.Permissions;
|
||||
_logger.LogInformation("Resolved permissions for user {UserId}: {Permissions}", Context.User.Id, resolvedPermissions);
|
||||
|
||||
ulong guildOwnerId = 0;
|
||||
var guildName = guildId.ToString();
|
||||
try
|
||||
{
|
||||
var guild = await Context.Client.Rest.GetGuildAsync(guildId);
|
||||
guildOwnerId = guild.OwnerId;
|
||||
guildName = guild.Name;
|
||||
_logger.LogInformation("Guild owner id: {OwnerId}", guildOwnerId);
|
||||
}
|
||||
catch (RestException ex) when (ex.StatusCode == System.Net.HttpStatusCode.NotFound)
|
||||
{
|
||||
_logger.LogWarning(
|
||||
ex,
|
||||
"Bot is not a REST member of guild {GuildId}; using resolved permissions from interaction payload",
|
||||
guildId);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Unexpected error fetching guild {GuildId}", guildId);
|
||||
}
|
||||
|
||||
var timeResult = DiscordNewSessionHandler.ParseTimeInput(time);
|
||||
if (!timeResult.IsSuccess)
|
||||
{
|
||||
@@ -33,55 +72,57 @@ public class DiscordNewSessionCommand : ApplicationCommandModule<SlashCommandCon
|
||||
return;
|
||||
}
|
||||
|
||||
var resolvedPermissions = GetResolvedPermissions(guild, Context.User.Id);
|
||||
// Defer the response to avoid Discord 3-second interaction timeout
|
||||
await Context.Interaction.SendResponseAsync(InteractionCallback.DeferredMessage());
|
||||
|
||||
try
|
||||
{
|
||||
_logger.LogInformation("Creating session for guild {GuildId}, user {UserId}", guildId, Context.User.Id);
|
||||
|
||||
var view = await _handler.HandleAsync(
|
||||
guildId: guild.Id.ToString(),
|
||||
channelId: Context.Channel.Id.ToString(),
|
||||
guildId: guildId.ToString(),
|
||||
channelId: Context.Channel!.Id.ToString(),
|
||||
groupName: guildName,
|
||||
userId: Context.User.Id,
|
||||
userDisplayName: Context.User.GlobalName ?? Context.User.Username,
|
||||
resolvedPermissions: resolvedPermissions,
|
||||
guildOwnerId: guild.OwnerId,
|
||||
guildOwnerId: guildOwnerId,
|
||||
title: title,
|
||||
scheduledAt: timeResult.Value,
|
||||
maxPlayers: seats is null ? null : (int)seats.Value,
|
||||
joinLink: link,
|
||||
CancellationToken.None);
|
||||
|
||||
_logger.LogInformation("Session created successfully. Building render.");
|
||||
|
||||
var (embeds, actionRows) = DiscordSessionBatchRenderer.Render(view);
|
||||
await Context.Interaction.SendResponseAsync(
|
||||
InteractionCallback.Message(new InteractionMessageProperties()
|
||||
.WithContent(":white_check_mark: **Session created successfully!**")
|
||||
.WithEmbeds(embeds)
|
||||
.WithComponents(actionRows)));
|
||||
|
||||
_logger.LogInformation("Sending success response.");
|
||||
|
||||
await Context.Interaction.ModifyResponseAsync(message =>
|
||||
{
|
||||
message.Content = ":white_check_mark: **Session created successfully!**";
|
||||
message.Embeds = embeds;
|
||||
message.Components = actionRows;
|
||||
});
|
||||
|
||||
_logger.LogInformation("Success response sent.");
|
||||
}
|
||||
catch (UnauthorizedAccessException ex)
|
||||
{
|
||||
await Context.Interaction.SendResponseAsync(
|
||||
InteractionCallback.Message($":no_entry: {ex.Message}"));
|
||||
_logger.LogWarning(ex, "Unauthorized session creation attempt by user {UserId}", Context.User.Id);
|
||||
await Context.Interaction.ModifyResponseAsync(message =>
|
||||
{
|
||||
message.Content = $":no_entry: {ex.Message}";
|
||||
});
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Failed to create session for user {UserId} in guild {GuildId}", Context.User.Id, guild.Id);
|
||||
await Context.Interaction.SendResponseAsync(
|
||||
InteractionCallback.Message(":boom: An error occurred while creating the session."));
|
||||
_logger.LogError(ex, "Failed to create session for user {UserId} in guild {GuildId}", Context.User.Id, guildId);
|
||||
await Context.Interaction.ModifyResponseAsync(message =>
|
||||
{
|
||||
message.Content = ":boom: An error occurred while creating the session.";
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private static ulong GetResolvedPermissions(NetCord.Gateway.Guild guild, ulong userId)
|
||||
{
|
||||
if (!guild.Users.TryGetValue(userId, out var guildUser))
|
||||
return 0;
|
||||
|
||||
ulong resolved = 0;
|
||||
foreach (var roleId in guildUser.RoleIds)
|
||||
{
|
||||
if (guild.Roles.TryGetValue(roleId, out var role))
|
||||
resolved |= (ulong)role.Permissions;
|
||||
}
|
||||
|
||||
return resolved;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
using Dapper;
|
||||
using GmRelay.DiscordBot.Infrastructure.Discord;
|
||||
using GmRelay.Shared.Domain;
|
||||
using GmRelay.Shared.Platform;
|
||||
using GmRelay.Shared.Rendering;
|
||||
using Npgsql;
|
||||
using System.Globalization;
|
||||
|
||||
namespace GmRelay.DiscordBot.Features.Sessions;
|
||||
|
||||
@@ -12,35 +12,40 @@ public sealed record TimeParseResult(bool IsSuccess, DateTimeOffset Value, strin
|
||||
public sealed class DiscordNewSessionHandler(
|
||||
NpgsqlDataSource dataSource,
|
||||
DiscordPermissionChecker permissionChecker,
|
||||
IPlatformMessenger messenger,
|
||||
ILogger<DiscordNewSessionHandler> logger)
|
||||
{
|
||||
private static readonly TimeSpan MoscowOffset = TimeSpan.FromHours(3);
|
||||
|
||||
public static TimeParseResult ParseTimeInput(string input)
|
||||
{
|
||||
if (DateTimeOffset.TryParseExact(
|
||||
input.Trim(),
|
||||
var trimmed = input.Trim();
|
||||
|
||||
if (DateTime.TryParseExact(
|
||||
trimmed,
|
||||
"yyyy-MM-dd HH:mm",
|
||||
System.Globalization.CultureInfo.InvariantCulture,
|
||||
System.Globalization.DateTimeStyles.AssumeUniversal,
|
||||
out var result))
|
||||
CultureInfo.InvariantCulture,
|
||||
DateTimeStyles.None,
|
||||
out var dt1))
|
||||
{
|
||||
if (result < DateTimeOffset.UtcNow)
|
||||
var offset = new DateTimeOffset(dt1, MoscowOffset).ToUniversalTime();
|
||||
if (offset < DateTimeOffset.UtcNow)
|
||||
return new TimeParseResult(false, default, "Дата находится в прошлом.");
|
||||
|
||||
return new TimeParseResult(true, result.ToUniversalTime(), null);
|
||||
return new TimeParseResult(true, offset, null);
|
||||
}
|
||||
|
||||
if (DateTimeOffset.TryParseExact(
|
||||
input.Trim(),
|
||||
if (DateTime.TryParseExact(
|
||||
trimmed,
|
||||
"dd.MM.yyyy HH:mm",
|
||||
System.Globalization.CultureInfo.InvariantCulture,
|
||||
System.Globalization.DateTimeStyles.AssumeUniversal,
|
||||
out var altResult))
|
||||
CultureInfo.InvariantCulture,
|
||||
DateTimeStyles.None,
|
||||
out var dt2))
|
||||
{
|
||||
if (altResult < DateTimeOffset.UtcNow)
|
||||
var offset = new DateTimeOffset(dt2, MoscowOffset).ToUniversalTime();
|
||||
if (offset < DateTimeOffset.UtcNow)
|
||||
return new TimeParseResult(false, default, "Дата находится в прошлом.");
|
||||
|
||||
return new TimeParseResult(true, altResult.ToUniversalTime(), null);
|
||||
return new TimeParseResult(true, offset, null);
|
||||
}
|
||||
|
||||
return new TimeParseResult(false, default, "Некорректный формат даты. Используйте YYYY-MM-DD HH:mm или DD.MM.YYYY HH:mm");
|
||||
@@ -49,6 +54,7 @@ public sealed class DiscordNewSessionHandler(
|
||||
public async Task<SessionBatchViewModel> HandleAsync(
|
||||
string guildId,
|
||||
string channelId,
|
||||
string groupName,
|
||||
ulong userId,
|
||||
string userDisplayName,
|
||||
ulong resolvedPermissions,
|
||||
@@ -60,13 +66,18 @@ public sealed class DiscordNewSessionHandler(
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
await using var connection = await dataSource.OpenConnectionAsync(cancellationToken);
|
||||
var displayGroupName = string.IsNullOrWhiteSpace(groupName) || string.Equals(groupName, guildId, StringComparison.Ordinal)
|
||||
? title
|
||||
: groupName.Trim();
|
||||
|
||||
var dbManagerUserIds = await connection.QueryAsync<ulong>(
|
||||
@"SELECT CAST(p.external_user_id AS BIGINT)
|
||||
FROM group_managers gm
|
||||
JOIN players p ON p.id = gm.player_id
|
||||
JOIN game_groups g ON g.id = gm.group_id
|
||||
WHERE g.platform = 'Discord' AND g.external_group_id = @GuildId",
|
||||
WHERE g.platform = 'Discord'
|
||||
AND p.platform = 'Discord'
|
||||
AND g.external_group_id = @GuildId",
|
||||
new { GuildId = guildId });
|
||||
|
||||
if (!permissionChecker.CanManageSchedule(guildOwnerId, userId, dbManagerUserIds, resolvedPermissions))
|
||||
@@ -75,6 +86,7 @@ public sealed class DiscordNewSessionHandler(
|
||||
}
|
||||
|
||||
await using var transaction = await connection.BeginTransactionAsync(cancellationToken);
|
||||
var transactionCommitted = false;
|
||||
try
|
||||
{
|
||||
await connection.ExecuteAsync(
|
||||
@@ -89,13 +101,13 @@ public sealed class DiscordNewSessionHandler(
|
||||
|
||||
var groupId = await connection.ExecuteScalarAsync<Guid>(
|
||||
@"INSERT INTO game_groups (name, platform, external_group_id, external_channel_id)
|
||||
VALUES (@GuildId, 'Discord', @GuildId, @ChannelId)
|
||||
VALUES (@GroupName, 'Discord', @GuildId, @ChannelId)
|
||||
ON CONFLICT (platform, external_group_id)
|
||||
WHERE platform IS NOT NULL AND external_group_id IS NOT NULL
|
||||
DO UPDATE SET name = EXCLUDED.name,
|
||||
external_channel_id = COALESCE(EXCLUDED.external_channel_id, game_groups.external_channel_id)
|
||||
RETURNING id",
|
||||
new { GuildId = guildId, ChannelId = channelId },
|
||||
new { GroupName = displayGroupName, GuildId = guildId, ChannelId = channelId },
|
||||
transaction);
|
||||
|
||||
await connection.ExecuteAsync(
|
||||
@@ -125,23 +137,19 @@ public sealed class DiscordNewSessionHandler(
|
||||
transaction);
|
||||
|
||||
await transaction.CommitAsync(cancellationToken);
|
||||
transactionCommitted = true;
|
||||
logger.LogInformation("Created session {SessionId} in guild {GuildId}", sessionId, guildId);
|
||||
|
||||
var sessions = new[] { new SessionBatchDto(sessionId, scheduledAt.UtcDateTime, SessionStatus.Planned, maxPlayers, joinLink ?? string.Empty) };
|
||||
var view = SessionBatchViewBuilder.Build(title, sessions, Array.Empty<ParticipantBatchDto>());
|
||||
|
||||
await messenger.SendScheduleAsync(
|
||||
new PlatformScheduleMessage(
|
||||
new PlatformGroup(PlatformKind.Discord, guildId, guildId, channelId),
|
||||
view,
|
||||
null),
|
||||
cancellationToken);
|
||||
|
||||
return view;
|
||||
return SessionBatchViewBuilder.Build(title, sessions, Array.Empty<ParticipantBatchDto>());
|
||||
}
|
||||
catch
|
||||
{
|
||||
await transaction.RollbackAsync(cancellationToken);
|
||||
if (!transactionCommitted)
|
||||
{
|
||||
await transaction.RollbackAsync(cancellationToken);
|
||||
}
|
||||
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,154 @@
|
||||
namespace GmRelay.DiscordBot.Features.Sessions;
|
||||
|
||||
using NetCord;
|
||||
using NetCord.Rest;
|
||||
using NetCord.Services.ApplicationCommands;
|
||||
|
||||
public class DiscordRescheduleCommand : ApplicationCommandModule<SlashCommandContext>
|
||||
{
|
||||
private readonly DiscordRescheduleHandler _handler;
|
||||
private readonly ILogger<DiscordRescheduleCommand> _logger;
|
||||
|
||||
public DiscordRescheduleCommand(DiscordRescheduleHandler handler, ILogger<DiscordRescheduleCommand> logger)
|
||||
{
|
||||
_handler = handler;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
[SlashCommand("reschedule", "Initiate reschedule voting for a session")]
|
||||
public async Task ExecuteAsync(
|
||||
[SlashCommandParameter(Name = "session", Description = "Session ID to reschedule")] string sessionIdText,
|
||||
[SlashCommandParameter(Name = "option1", Description = "First time option (YYYY-MM-DD HH:mm)")] string option1,
|
||||
[SlashCommandParameter(Name = "option2", Description = "Second time option (YYYY-MM-DD HH:mm)")] string option2,
|
||||
[SlashCommandParameter(Name = "option3", Description = "Third time option (optional)")] string? option3 = null,
|
||||
[SlashCommandParameter(Name = "deadline", Description = "Voting deadline (YYYY-MM-DD HH:mm)")] string deadline = "")
|
||||
{
|
||||
_logger.LogInformation(
|
||||
"reschedule called by user {UserId} ({UserType}) in guild {GuildId}",
|
||||
Context.User.Id,
|
||||
Context.User.GetType().Name,
|
||||
Context.Interaction.GuildId);
|
||||
|
||||
var guildId = Context.Interaction.GuildId
|
||||
?? throw new InvalidOperationException("This command can only be used in a guild.");
|
||||
|
||||
var member = Context.User as GuildInteractionUser;
|
||||
if (member is null)
|
||||
{
|
||||
_logger.LogError("Context.User is not GuildInteractionUser. Actual type: {ActualType}", Context.User.GetType().Name);
|
||||
throw new InvalidOperationException("Guild member data not available in interaction.");
|
||||
}
|
||||
|
||||
var resolvedPermissions = (ulong)member.Permissions;
|
||||
_logger.LogInformation("Resolved permissions for user {UserId}: {Permissions}", Context.User.Id, resolvedPermissions);
|
||||
|
||||
ulong guildOwnerId = 0;
|
||||
try
|
||||
{
|
||||
var guild = await Context.Client.Rest.GetGuildAsync(guildId);
|
||||
guildOwnerId = guild.OwnerId;
|
||||
_logger.LogInformation("Guild owner id: {OwnerId}", guildOwnerId);
|
||||
}
|
||||
catch (RestException ex) when (ex.StatusCode == System.Net.HttpStatusCode.NotFound)
|
||||
{
|
||||
_logger.LogWarning(
|
||||
ex,
|
||||
"Bot is not a REST member of guild {GuildId}; using resolved permissions from interaction payload",
|
||||
guildId);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Unexpected error fetching guild {GuildId}", guildId);
|
||||
}
|
||||
|
||||
if (!Guid.TryParse(sessionIdText, out var sessionId))
|
||||
{
|
||||
await Context.Interaction.SendResponseAsync(
|
||||
InteractionCallback.Message("❌ Некорректный ID сессии."));
|
||||
return;
|
||||
}
|
||||
|
||||
var options = new List<string> { option1, option2 };
|
||||
if (!string.IsNullOrWhiteSpace(option3))
|
||||
options.Add(option3);
|
||||
|
||||
var parsedOptions = new List<DateTimeOffset>();
|
||||
foreach (var opt in options)
|
||||
{
|
||||
var result = DiscordNewSessionHandler.ParseTimeInput(opt);
|
||||
if (!result.IsSuccess)
|
||||
{
|
||||
await Context.Interaction.SendResponseAsync(
|
||||
InteractionCallback.Message($"❌ {opt}: {result.Error}"));
|
||||
return;
|
||||
}
|
||||
parsedOptions.Add(result.Value);
|
||||
}
|
||||
|
||||
var deadlineResult = DiscordNewSessionHandler.ParseTimeInput(deadline);
|
||||
if (!deadlineResult.IsSuccess)
|
||||
{
|
||||
await Context.Interaction.SendResponseAsync(
|
||||
InteractionCallback.Message($"❌ Дедлайн: {deadlineResult.Error}"));
|
||||
return;
|
||||
}
|
||||
|
||||
if (deadlineResult.Value >= parsedOptions.Min())
|
||||
{
|
||||
await Context.Interaction.SendResponseAsync(
|
||||
InteractionCallback.Message("❌ Дедлайн должен быть раньше первого варианта времени."));
|
||||
return;
|
||||
}
|
||||
|
||||
// Defer the response to avoid Discord 3-second interaction timeout
|
||||
await Context.Interaction.SendResponseAsync(InteractionCallback.DeferredMessage());
|
||||
|
||||
try
|
||||
{
|
||||
_logger.LogInformation("Initiating reschedule for session {SessionId} in guild {GuildId}", sessionId, guildId);
|
||||
|
||||
var result = await _handler.HandleAsync(
|
||||
guildId: guildId.ToString(),
|
||||
channelId: Context.Channel!.Id.ToString(),
|
||||
userId: Context.User.Id,
|
||||
userDisplayName: Context.User.GlobalName ?? Context.User.Username,
|
||||
resolvedPermissions: resolvedPermissions,
|
||||
guildOwnerId: guildOwnerId,
|
||||
sessionId: sessionId,
|
||||
options: parsedOptions,
|
||||
deadline: deadlineResult.Value,
|
||||
CancellationToken.None);
|
||||
|
||||
_logger.LogInformation("Reschedule voting started for session {SessionId}, proposal {ProposalId}", sessionId, result.ProposalId);
|
||||
|
||||
await Context.Interaction.ModifyResponseAsync(message =>
|
||||
{
|
||||
message.Content = $"🗳 Голосование за перенос запущено! Дедлайн: {deadlineResult.Value:yyyy-MM-dd HH:mm} UTC.";
|
||||
});
|
||||
}
|
||||
catch (UnauthorizedAccessException ex)
|
||||
{
|
||||
_logger.LogWarning(ex, "Unauthorized reschedule attempt by user {UserId}", Context.User.Id);
|
||||
await Context.Interaction.ModifyResponseAsync(message =>
|
||||
{
|
||||
message.Content = $":no_entry: {ex.Message}";
|
||||
});
|
||||
}
|
||||
catch (InvalidOperationException ex)
|
||||
{
|
||||
_logger.LogWarning(ex, "Invalid reschedule request by user {UserId}", Context.User.Id);
|
||||
await Context.Interaction.ModifyResponseAsync(message =>
|
||||
{
|
||||
message.Content = $":warning: {ex.Message}";
|
||||
});
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Failed to initiate reschedule for session {SessionId}", sessionId);
|
||||
await Context.Interaction.ModifyResponseAsync(message =>
|
||||
{
|
||||
message.Content = ":boom: Ошибка при запуске голосования.";
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,155 @@
|
||||
namespace GmRelay.DiscordBot.Features.Sessions;
|
||||
|
||||
using Dapper;
|
||||
using GmRelay.DiscordBot.Infrastructure.Discord;
|
||||
using GmRelay.DiscordBot.Rendering;
|
||||
using GmRelay.Shared.Domain;
|
||||
using GmRelay.Shared.Features.Sessions.RescheduleSession;
|
||||
using GmRelay.Shared.Platform;
|
||||
using NetCord;
|
||||
using NetCord.Rest;
|
||||
using Npgsql;
|
||||
|
||||
public sealed record DiscordRescheduleResult(Guid ProposalId, IReadOnlyList<RescheduleOptionDto> Options, DateTimeOffset Deadline);
|
||||
|
||||
public sealed class DiscordRescheduleHandler(
|
||||
NpgsqlDataSource dataSource,
|
||||
DiscordPermissionChecker permissionChecker,
|
||||
RestClient restClient,
|
||||
ILogger<DiscordRescheduleHandler> logger)
|
||||
{
|
||||
public async Task<DiscordRescheduleResult> HandleAsync(
|
||||
string guildId,
|
||||
string channelId,
|
||||
ulong userId,
|
||||
string userDisplayName,
|
||||
ulong resolvedPermissions,
|
||||
ulong guildOwnerId,
|
||||
Guid sessionId,
|
||||
IReadOnlyList<DateTimeOffset> options,
|
||||
DateTimeOffset deadline,
|
||||
CancellationToken ct)
|
||||
{
|
||||
// 1. Permission check + read-only validation (before Discord message)
|
||||
await using var readConnection = await dataSource.OpenConnectionAsync(ct);
|
||||
|
||||
var dbManagerUserIds = await readConnection.QueryAsync<ulong>(
|
||||
@"SELECT CAST(p.external_user_id AS BIGINT)
|
||||
FROM group_managers gm
|
||||
JOIN players p ON p.id = gm.player_id
|
||||
JOIN game_groups g ON g.id = gm.group_id
|
||||
WHERE g.platform = 'Discord' AND g.external_group_id = @GuildId",
|
||||
new { GuildId = guildId });
|
||||
|
||||
if (!permissionChecker.CanManageSchedule(guildOwnerId, userId, dbManagerUserIds, resolvedPermissions))
|
||||
{
|
||||
throw new UnauthorizedAccessException("⛔ Только owner, администратор или manager могут переносить сессии.");
|
||||
}
|
||||
|
||||
// 2. Ensure player exists
|
||||
await readConnection.ExecuteAsync(
|
||||
@"INSERT INTO players (display_name, platform, external_user_id, external_username)
|
||||
VALUES (@Name, 'Discord', @UserId, @Name)
|
||||
ON CONFLICT (platform, external_user_id)
|
||||
WHERE platform IS NOT NULL AND external_user_id IS NOT NULL
|
||||
DO UPDATE SET display_name = EXCLUDED.display_name",
|
||||
new { Name = userDisplayName, UserId = userId.ToString() });
|
||||
|
||||
// 3. Verify session exists
|
||||
var session = await readConnection.QuerySingleOrDefaultAsync<RescheduleSessionInfoDto>(
|
||||
"""
|
||||
SELECT s.title AS Title, s.scheduled_at AS CurrentScheduledAt
|
||||
FROM sessions s
|
||||
WHERE s.id = @SessionId AND s.status != @Cancelled
|
||||
""",
|
||||
new { SessionId = sessionId, Cancelled = SessionStatus.Cancelled });
|
||||
|
||||
if (session is null)
|
||||
throw new InvalidOperationException("Сессия не найдена или отменена.");
|
||||
|
||||
// 4. Check no active proposal
|
||||
var hasActive = await readConnection.ExecuteScalarAsync<bool>(
|
||||
"SELECT EXISTS (SELECT 1 FROM reschedule_proposals WHERE session_id = @SessionId AND status IN ('AwaitingTime', 'Voting'))",
|
||||
new { SessionId = sessionId });
|
||||
|
||||
if (hasActive)
|
||||
throw new InvalidOperationException("Уже есть активный запрос на перенос этой сессии.");
|
||||
|
||||
// 5. Load participants for rendering
|
||||
var participants = (await readConnection.QueryAsync<VoteParticipantDto>(
|
||||
"""
|
||||
SELECT p.id AS PlayerId, p.display_name AS DisplayName, p.external_username AS TelegramUsername, 0 AS TelegramId
|
||||
FROM session_participants sp
|
||||
JOIN players p ON p.id = sp.player_id
|
||||
WHERE sp.session_id = @SessionId AND sp.is_gm = false AND sp.registration_status = @Active
|
||||
""",
|
||||
new { SessionId = sessionId, Active = ParticipantRegistrationStatus.Active })).ToList();
|
||||
|
||||
// 6. Prepare proposal data
|
||||
var proposalId = Guid.NewGuid();
|
||||
var optionDtos = options.Select((o, i) => new RescheduleOptionDto(Guid.NewGuid(), i + 1, o)).ToList();
|
||||
|
||||
// 7. Build and send Discord vote message BEFORE transaction
|
||||
var (embed, actionRow) = DiscordRescheduleVotingRenderer.Render(session.Title, session.CurrentScheduledAt, deadline, optionDtos, participants, []);
|
||||
|
||||
var channelIdUlong = ulong.Parse(channelId);
|
||||
|
||||
// NOTE: Discord message is sent before DB transaction to avoid orphaned proposals
|
||||
// if the send fails. There is a negligible race window where the message is visible
|
||||
// before the DB commit; in practice users cannot click faster than the transaction commits.
|
||||
var sentMessage = await restClient.SendMessageAsync(
|
||||
channelIdUlong,
|
||||
new MessageProperties()
|
||||
.WithEmbeds(new[] { embed })
|
||||
.WithComponents(new[] { actionRow }));
|
||||
|
||||
// 8. Create proposal + options + platform_messages in transaction
|
||||
try
|
||||
{
|
||||
await using var connection = await dataSource.OpenConnectionAsync(ct);
|
||||
await using var transaction = await connection.BeginTransactionAsync(ct);
|
||||
|
||||
await connection.ExecuteAsync(
|
||||
"""
|
||||
INSERT INTO reschedule_proposals (id, session_id, proposed_by, source_platform, proposed_by_external_user_id, status, voting_deadline_at)
|
||||
VALUES (@Id, @SessionId, NULL, 'Discord', @ProposedBy, 'Voting', @Deadline)
|
||||
""",
|
||||
new { Id = proposalId, SessionId = sessionId, ProposedBy = userId.ToString(), Deadline = deadline.UtcDateTime },
|
||||
transaction);
|
||||
|
||||
foreach (var option in optionDtos)
|
||||
{
|
||||
await connection.ExecuteAsync(
|
||||
"""
|
||||
INSERT INTO reschedule_options (id, proposal_id, proposed_at, display_order)
|
||||
VALUES (@OptionId, @ProposalId, @ProposedAt, @DisplayOrder)
|
||||
""",
|
||||
new { option.OptionId, ProposalId = proposalId, option.ProposedAt, option.DisplayOrder },
|
||||
transaction);
|
||||
}
|
||||
|
||||
await connection.ExecuteAsync(
|
||||
"""
|
||||
INSERT INTO platform_messages (platform, group_id, session_id, external_channel_id, external_message_id, purpose)
|
||||
VALUES ('Discord', (SELECT id FROM game_groups WHERE platform = 'Discord' AND external_group_id = @GuildId), @SessionId, @ChannelId, @MessageId, 'reschedule_vote')
|
||||
""",
|
||||
new { GuildId = guildId, SessionId = sessionId, ChannelId = channelId, MessageId = sentMessage.Id.ToString() },
|
||||
transaction);
|
||||
|
||||
await transaction.CommitAsync(ct);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, "Transaction failed after Discord message sent; deleting orphaned message");
|
||||
try { await restClient.DeleteMessageAsync(channelIdUlong, sentMessage.Id); } catch { /* best effort */ }
|
||||
throw;
|
||||
}
|
||||
|
||||
logger.LogInformation("Discord reschedule voting started for session {SessionId}, proposal {ProposalId}", sessionId, proposalId);
|
||||
|
||||
return new DiscordRescheduleResult(proposalId, optionDtos, deadline);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
internal sealed record RescheduleSessionInfoDto(string Title, DateTime CurrentScheduledAt);
|
||||
@@ -0,0 +1,63 @@
|
||||
using GmRelay.DiscordBot.Rendering;
|
||||
using GmRelay.Shared.Features.Sessions.RescheduleSession;
|
||||
using GmRelay.Shared.Platform;
|
||||
using NetCord.Rest;
|
||||
|
||||
namespace GmRelay.DiscordBot.Features.Sessions;
|
||||
|
||||
public sealed record DiscordRescheduleVoteInput(
|
||||
Guid OptionId,
|
||||
ulong UserId,
|
||||
string InteractionId,
|
||||
string GuildId,
|
||||
string ChannelId,
|
||||
string MessageId);
|
||||
|
||||
public sealed class DiscordRescheduleVoteHandler(
|
||||
GmRelay.Shared.Features.Sessions.RescheduleSession.HandleRescheduleVoteHandler sharedHandler,
|
||||
RestClient restClient,
|
||||
ILogger<DiscordRescheduleVoteHandler> logger)
|
||||
{
|
||||
public async Task<string> HandleAsync(DiscordRescheduleVoteInput input, CancellationToken ct)
|
||||
{
|
||||
var command = new HandleRescheduleVoteCommand(
|
||||
input.OptionId,
|
||||
new PlatformUser(PlatformKind.Discord, input.UserId.ToString(), string.Empty, null),
|
||||
new PlatformGroup(PlatformKind.Discord, input.GuildId, string.Empty, input.ChannelId),
|
||||
input.InteractionId,
|
||||
new PlatformMessageRef(PlatformKind.Discord, input.ChannelId, null, input.MessageId));
|
||||
|
||||
var result = await sharedHandler.HandleAsync(command, ct);
|
||||
|
||||
if (!result.Success)
|
||||
{
|
||||
return result.ReplyText!;
|
||||
}
|
||||
|
||||
var (embed, actionRow) = DiscordRescheduleVotingRenderer.Render(
|
||||
result.Title!,
|
||||
result.CurrentScheduledAt,
|
||||
result.VotingDeadlineAt,
|
||||
result.Options,
|
||||
result.Participants,
|
||||
result.Votes);
|
||||
|
||||
var channelIdUlong = ulong.Parse(input.ChannelId);
|
||||
var messageIdUlong = ulong.Parse(input.MessageId);
|
||||
|
||||
try
|
||||
{
|
||||
await restClient.ModifyMessageAsync(channelIdUlong, messageIdUlong, options =>
|
||||
{
|
||||
options.Embeds = new[] { embed };
|
||||
options.Components = new[] { actionRow };
|
||||
});
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogWarning(ex, "Failed to update Discord vote message for proposal {ProposalId}", result.ProposalId);
|
||||
}
|
||||
|
||||
return result.ReplyText!;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,196 @@
|
||||
namespace GmRelay.DiscordBot.Features.Sessions;
|
||||
|
||||
using Dapper;
|
||||
using GmRelay.Shared.Features.Sessions.RescheduleSession;
|
||||
using GmRelay.Shared.Platform;
|
||||
using GmRelay.Shared.Rendering;
|
||||
using Npgsql;
|
||||
|
||||
public sealed class DiscordRescheduleVotingDeadlineService(
|
||||
NpgsqlDataSource dataSource,
|
||||
RescheduleVotingFinalizer finalizer,
|
||||
IPlatformMessenger messenger,
|
||||
ILogger<DiscordRescheduleVotingDeadlineService> logger) : BackgroundService
|
||||
{
|
||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
await ProcessDueProposals(stoppingToken);
|
||||
using var timer = new PeriodicTimer(TimeSpan.FromMinutes(1));
|
||||
while (await timer.WaitForNextTickAsync(stoppingToken))
|
||||
{
|
||||
await ProcessDueProposals(stoppingToken);
|
||||
}
|
||||
}
|
||||
catch (OperationCanceledException) when (stoppingToken.IsCancellationRequested)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
private async Task ProcessDueProposals(CancellationToken ct)
|
||||
{
|
||||
try
|
||||
{
|
||||
var proposalIds = await finalizer.GetDueProposalIdsAsync("Discord", ct);
|
||||
foreach (var id in proposalIds)
|
||||
{
|
||||
await TryFinalizeAsync(id, ct);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, "Failed to process Discord reschedule proposals");
|
||||
}
|
||||
}
|
||||
|
||||
private async Task TryFinalizeAsync(Guid proposalId, CancellationToken ct)
|
||||
{
|
||||
try
|
||||
{
|
||||
var result = await finalizer.FinalizeAsync(proposalId, ct);
|
||||
if (result is null)
|
||||
return;
|
||||
|
||||
if (result.SourcePlatform != "Discord")
|
||||
return;
|
||||
|
||||
await TryUpdateDiscordVoteMessage(result, ct);
|
||||
|
||||
if (result.SelectedOption is not null)
|
||||
{
|
||||
await TryUpdateBatchScheduleAsync(result, ct);
|
||||
}
|
||||
|
||||
logger.LogInformation(
|
||||
"Finalized Discord reschedule proposal {ProposalId} for session {SessionId} with outcome {Outcome}",
|
||||
proposalId,
|
||||
result.SessionId,
|
||||
result.Decision.Outcome);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, "Failed to finalize Discord proposal {ProposalId}", proposalId);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task TryUpdateDiscordVoteMessage(RescheduleVotingFinalizerResult result, CancellationToken ct)
|
||||
{
|
||||
try
|
||||
{
|
||||
await using var connection = await dataSource.OpenConnectionAsync(ct);
|
||||
var msgRef = await connection.QuerySingleOrDefaultAsync<PlatformMessageRefDto>(
|
||||
"""
|
||||
SELECT g.external_group_id AS ExternalGroupId,
|
||||
COALESCE(pm.external_channel_id, g.external_channel_id, g.external_group_id) AS ExternalChannelId,
|
||||
pm.external_message_id AS ExternalMessageId
|
||||
FROM platform_messages pm
|
||||
JOIN game_groups g ON g.id = pm.group_id
|
||||
WHERE pm.session_id = @SessionId AND pm.purpose = 'reschedule_vote' AND pm.platform = 'Discord'
|
||||
ORDER BY pm.created_at DESC
|
||||
LIMIT 1
|
||||
""",
|
||||
new { result.SessionId });
|
||||
|
||||
if (msgRef is null)
|
||||
return;
|
||||
|
||||
var group = CreateDiscordGroup(msgRef);
|
||||
|
||||
await messenger.UpdateRescheduleVoteAsync(
|
||||
new PlatformRescheduleVoteUpdate(
|
||||
group,
|
||||
new PlatformMessageRef(
|
||||
PlatformKind.Discord,
|
||||
msgRef.ExternalGroupId,
|
||||
null,
|
||||
msgRef.ExternalMessageId),
|
||||
result.ProposalId,
|
||||
result.SessionId,
|
||||
result.Title,
|
||||
result.CurrentScheduledAt,
|
||||
result.VotingDeadlineAt,
|
||||
result.Decision,
|
||||
result.SelectedOption,
|
||||
result.Options,
|
||||
result.Votes,
|
||||
result.Participants),
|
||||
ct);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogWarning(ex, "Failed to update Discord vote message for session {SessionId}", result.SessionId);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task TryUpdateBatchScheduleAsync(RescheduleVotingFinalizerResult result, CancellationToken ct)
|
||||
{
|
||||
try
|
||||
{
|
||||
await using var connection = await dataSource.OpenConnectionAsync(ct);
|
||||
var batchRef = await connection.QuerySingleOrDefaultAsync<PlatformMessageRefDto>(
|
||||
"""
|
||||
SELECT g.external_group_id AS ExternalGroupId,
|
||||
COALESCE(pm.external_channel_id, g.external_channel_id, g.external_group_id) AS ExternalChannelId,
|
||||
pm.external_message_id AS ExternalMessageId
|
||||
FROM platform_messages pm
|
||||
JOIN game_groups g ON g.id = pm.group_id
|
||||
WHERE pm.batch_id = @BatchId AND pm.purpose = 'schedule' AND pm.platform = 'Discord'
|
||||
ORDER BY pm.created_at DESC
|
||||
LIMIT 1
|
||||
""",
|
||||
new { result.BatchId });
|
||||
|
||||
if (batchRef is null)
|
||||
return;
|
||||
|
||||
var sessions = (await connection.QueryAsync<SessionBatchDto>(
|
||||
"SELECT id AS SessionId, scheduled_at AS ScheduledAt, status AS Status, max_players AS MaxPlayers, join_link AS JoinLink FROM sessions WHERE batch_id = @BatchId ORDER BY scheduled_at",
|
||||
new { result.BatchId })).ToList();
|
||||
|
||||
var participants = (await connection.QueryAsync<ParticipantBatchDto>(
|
||||
"""
|
||||
SELECT sp.session_id AS SessionId,
|
||||
p.display_name AS DisplayName,
|
||||
p.external_username AS TelegramUsername,
|
||||
sp.registration_status AS RegistrationStatus
|
||||
FROM session_participants sp
|
||||
JOIN players p ON p.id = sp.player_id
|
||||
JOIN sessions s ON sp.session_id = s.id
|
||||
WHERE s.batch_id = @BatchId AND sp.is_gm = false
|
||||
ORDER BY sp.registration_status ASC, sp.created_at ASC
|
||||
""",
|
||||
new { result.BatchId })).ToList();
|
||||
|
||||
var view = SessionBatchViewBuilder.Build(result.Title, sessions, participants);
|
||||
var group = CreateDiscordGroup(batchRef);
|
||||
|
||||
await messenger.UpdateScheduleAsync(
|
||||
new PlatformScheduleMessage(
|
||||
group,
|
||||
view,
|
||||
new PlatformMessageRef(
|
||||
PlatformKind.Discord,
|
||||
batchRef.ExternalGroupId,
|
||||
null,
|
||||
batchRef.ExternalMessageId)),
|
||||
ct);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogWarning(ex, "Failed to update Discord batch schedule for session {SessionId}", result.SessionId);
|
||||
}
|
||||
}
|
||||
|
||||
private static PlatformGroup CreateDiscordGroup(PlatformMessageRefDto message) =>
|
||||
new(
|
||||
PlatformKind.Discord,
|
||||
message.ExternalGroupId,
|
||||
message.ExternalGroupId,
|
||||
message.ExternalChannelId);
|
||||
|
||||
internal sealed record PlatformMessageRefDto(
|
||||
string ExternalGroupId,
|
||||
string ExternalChannelId,
|
||||
string ExternalMessageId);
|
||||
}
|
||||
@@ -1,5 +1,11 @@
|
||||
using GmRelay.DiscordBot.Infrastructure.Discord;
|
||||
using GmRelay.DiscordBot.Rendering;
|
||||
using GmRelay.Shared.Domain;
|
||||
using GmRelay.Shared.Features.Confirmation.HandleRsvp;
|
||||
using GmRelay.Shared.Features.Sessions.CreateSession;
|
||||
using GmRelay.Shared.Platform;
|
||||
using System.Collections;
|
||||
using System.Globalization;
|
||||
using NetCord;
|
||||
using NetCord.Rest;
|
||||
using NetCord.Services.ComponentInteractions;
|
||||
@@ -9,6 +15,9 @@ namespace GmRelay.DiscordBot.Features.Sessions;
|
||||
public sealed class DiscordSessionInteractionModule(
|
||||
JoinSessionHandler joinSessionHandler,
|
||||
LeaveSessionHandler leaveSessionHandler,
|
||||
HandleRsvpHandler rsvpHandler,
|
||||
DiscordDeleteSessionHandler deleteSessionHandler,
|
||||
DiscordRescheduleVoteHandler voteHandler,
|
||||
DiscordInteractionReplyCache interactionReplies,
|
||||
ILogger<DiscordSessionInteractionModule> logger) : ComponentInteractionModule<ButtonInteractionContext>
|
||||
{
|
||||
@@ -22,21 +31,22 @@ public sealed class DiscordSessionInteractionModule(
|
||||
}
|
||||
|
||||
var input = CreateInput(parsedSessionId);
|
||||
await RespondAsync(InteractionCallback.DeferredMessage(MessageFlags.Ephemeral));
|
||||
await RespondAsync(InteractionCallback.DeferredModifyMessage);
|
||||
SessionInteractionResult result;
|
||||
try
|
||||
{
|
||||
await joinSessionHandler.HandleAsync(
|
||||
DiscordSessionInteractionMapper.CreateJoinCommand(input),
|
||||
result = await joinSessionHandler.HandleAsync(
|
||||
DiscordSessionInteractionMapper.CreateJoinCommand(input) with { DeferScheduleUpdate = true },
|
||||
CancellationToken.None);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, "Failed to handle Discord join interaction for session {SessionId}", parsedSessionId);
|
||||
await CompleteResponseAsync("Не удалось обработать кнопку.");
|
||||
await FollowupEphemeralAsync("Не удалось обработать кнопку.");
|
||||
return;
|
||||
}
|
||||
|
||||
await CompleteWithStoredReplyAsync(input.InteractionId);
|
||||
await CompleteScheduleUpdateResponseAsync(input.InteractionId, result);
|
||||
}
|
||||
|
||||
[ComponentInteraction("leave_session")]
|
||||
@@ -49,16 +59,110 @@ public sealed class DiscordSessionInteractionModule(
|
||||
}
|
||||
|
||||
var input = CreateInput(parsedSessionId);
|
||||
await RespondAsync(InteractionCallback.DeferredMessage(MessageFlags.Ephemeral));
|
||||
await RespondAsync(InteractionCallback.DeferredModifyMessage);
|
||||
SessionInteractionResult result;
|
||||
try
|
||||
{
|
||||
await leaveSessionHandler.HandleAsync(
|
||||
DiscordSessionInteractionMapper.CreateLeaveCommand(input),
|
||||
result = await leaveSessionHandler.HandleAsync(
|
||||
DiscordSessionInteractionMapper.CreateLeaveCommand(input) with { DeferScheduleUpdate = true },
|
||||
CancellationToken.None);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, "Failed to handle Discord leave interaction for session {SessionId}", parsedSessionId);
|
||||
await FollowupEphemeralAsync("Не удалось обработать кнопку.");
|
||||
return;
|
||||
}
|
||||
|
||||
await CompleteScheduleUpdateResponseAsync(input.InteractionId, result);
|
||||
}
|
||||
|
||||
[ComponentInteraction("delete_session")]
|
||||
public async Task DeleteAsync(string sessionId)
|
||||
{
|
||||
if (!Guid.TryParse(sessionId, out var parsedSessionId))
|
||||
{
|
||||
await RespondAsync(CreateEphemeralReply("Session button is outdated."));
|
||||
return;
|
||||
}
|
||||
|
||||
var input = CreateInput(parsedSessionId);
|
||||
var member = Context.User as GuildInteractionUser;
|
||||
var resolvedPermissions = member is null ? 0UL : (ulong)member.Permissions;
|
||||
|
||||
await RespondAsync(InteractionCallback.DeferredModifyMessage);
|
||||
try
|
||||
{
|
||||
var result = await deleteSessionHandler.HandleAsync(
|
||||
guildId: input.GuildId,
|
||||
channelId: input.ChannelId,
|
||||
userId: input.UserId,
|
||||
resolvedPermissions: resolvedPermissions,
|
||||
guildOwnerId: 0,
|
||||
sessionId: parsedSessionId,
|
||||
CancellationToken.None);
|
||||
|
||||
await CompleteDeleteResponseAsync(result);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, "Failed to handle Discord delete interaction for session {SessionId}", parsedSessionId);
|
||||
await FollowupEphemeralAsync("Не удалось удалить сессию.");
|
||||
}
|
||||
}
|
||||
|
||||
[ComponentInteraction("rsvp")]
|
||||
public async Task RsvpAsync(string status, string sessionId)
|
||||
{
|
||||
if (!Guid.TryParse(sessionId, out var parsedSessionId))
|
||||
{
|
||||
await RespondAsync(CreateEphemeralReply("Session button is outdated."));
|
||||
return;
|
||||
}
|
||||
|
||||
var rsvpStatus = status switch
|
||||
{
|
||||
"confirm" => RsvpStatus.Confirmed,
|
||||
"decline" => RsvpStatus.Declined,
|
||||
_ => null
|
||||
};
|
||||
|
||||
if (rsvpStatus is null)
|
||||
{
|
||||
await RespondAsync(CreateEphemeralReply("Session button is outdated."));
|
||||
return;
|
||||
}
|
||||
|
||||
var input = CreateInput(parsedSessionId);
|
||||
await RespondAsync(InteractionCallback.DeferredMessage(MessageFlags.Ephemeral));
|
||||
|
||||
try
|
||||
{
|
||||
await rsvpHandler.HandleAsync(
|
||||
new HandleRsvpCommand(
|
||||
parsedSessionId,
|
||||
new PlatformUser(
|
||||
PlatformKind.Discord,
|
||||
Context.User.Id.ToString(CultureInfo.InvariantCulture),
|
||||
string.IsNullOrWhiteSpace(Context.User.GlobalName) ? Context.User.Username : Context.User.GlobalName,
|
||||
Context.User.Username),
|
||||
rsvpStatus,
|
||||
input.InteractionId,
|
||||
new PlatformGroup(
|
||||
PlatformKind.Discord,
|
||||
input.GuildId,
|
||||
input.GuildId,
|
||||
input.ChannelId),
|
||||
new PlatformMessageRef(
|
||||
PlatformKind.Discord,
|
||||
input.GuildId,
|
||||
null,
|
||||
input.MessageId)),
|
||||
CancellationToken.None);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, "Failed to handle Discord RSVP interaction for session {SessionId}", parsedSessionId);
|
||||
await CompleteResponseAsync("Не удалось обработать кнопку.");
|
||||
return;
|
||||
}
|
||||
@@ -66,9 +170,44 @@ public sealed class DiscordSessionInteractionModule(
|
||||
await CompleteWithStoredReplyAsync(input.InteractionId);
|
||||
}
|
||||
|
||||
[ComponentInteraction("reschedule_vote")]
|
||||
public async Task RescheduleVoteAsync(string optionId)
|
||||
{
|
||||
if (!Guid.TryParse(optionId, out var parsedOptionId))
|
||||
{
|
||||
await RespondAsync(CreateEphemeralReply("Vote button is outdated."));
|
||||
return;
|
||||
}
|
||||
|
||||
var input = CreateInput(Guid.Empty); // sessionId not needed for vote routing
|
||||
var voteInput = new DiscordRescheduleVoteInput(
|
||||
parsedOptionId,
|
||||
Context.User.Id,
|
||||
Context.Interaction.Id.ToString(System.Globalization.CultureInfo.InvariantCulture),
|
||||
input.GuildId,
|
||||
input.ChannelId,
|
||||
input.MessageId);
|
||||
|
||||
await RespondAsync(InteractionCallback.DeferredMessage(MessageFlags.Ephemeral));
|
||||
|
||||
string replyText;
|
||||
try
|
||||
{
|
||||
replyText = await voteHandler.HandleAsync(voteInput, CancellationToken.None);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, "Failed to handle Discord reschedule vote for option {OptionId}", parsedOptionId);
|
||||
await CompleteResponseAsync("Не удалось обработать голос.");
|
||||
return;
|
||||
}
|
||||
|
||||
await CompleteResponseAsync(replyText);
|
||||
}
|
||||
|
||||
private DiscordSessionInteractionInput CreateInput(Guid sessionId)
|
||||
{
|
||||
var guild = Context.Guild
|
||||
var guildId = Context.Interaction.GuildId?.ToString(CultureInfo.InvariantCulture)
|
||||
?? throw new InvalidOperationException("Session buttons can only be used in a guild.");
|
||||
var message = Context.Interaction.Message
|
||||
?? throw new InvalidOperationException("Session button interaction must include a message.");
|
||||
@@ -76,9 +215,9 @@ public sealed class DiscordSessionInteractionModule(
|
||||
return new DiscordSessionInteractionInput(
|
||||
SessionId: sessionId,
|
||||
InteractionId: Context.Interaction.Id.ToString(System.Globalization.CultureInfo.InvariantCulture),
|
||||
GuildId: guild.Id.ToString(System.Globalization.CultureInfo.InvariantCulture),
|
||||
ChannelId: Context.Channel.Id.ToString(System.Globalization.CultureInfo.InvariantCulture),
|
||||
MessageId: message.Id.ToString(System.Globalization.CultureInfo.InvariantCulture),
|
||||
GuildId: guildId,
|
||||
ChannelId: Context.Channel.Id.ToString(CultureInfo.InvariantCulture),
|
||||
MessageId: message.Id.ToString(CultureInfo.InvariantCulture),
|
||||
UserId: Context.User.Id,
|
||||
Username: Context.User.Username,
|
||||
DisplayName: Context.User.GlobalName);
|
||||
@@ -90,9 +229,85 @@ public sealed class DiscordSessionInteractionModule(
|
||||
await CompleteResponseAsync(reply?.Text ?? "Session updated.");
|
||||
}
|
||||
|
||||
private async Task CompleteScheduleUpdateResponseAsync(string interactionId, SessionInteractionResult result)
|
||||
{
|
||||
var updatedView = result.UpdatedView;
|
||||
if (updatedView is not null && SourceMessageHasDeleteAction())
|
||||
{
|
||||
updatedView = DiscordListSessionsHandler.AddManagerActions(updatedView);
|
||||
}
|
||||
|
||||
if (updatedView is not null)
|
||||
{
|
||||
var (embeds, actionRows) = DiscordSessionBatchRenderer.Render(updatedView);
|
||||
await ModifyResponseAsync(options =>
|
||||
{
|
||||
options.Embeds = embeds;
|
||||
options.Components = actionRows;
|
||||
});
|
||||
}
|
||||
|
||||
var reply = interactionReplies.Take(interactionId);
|
||||
await FollowupEphemeralAsync(reply?.Text ?? result.ReplyText);
|
||||
}
|
||||
|
||||
private async Task CompleteDeleteResponseAsync(DiscordDeleteSessionResult result)
|
||||
{
|
||||
if (result.UpdatedView is not null)
|
||||
{
|
||||
var (embeds, actionRows) = DiscordSessionBatchRenderer.Render(result.UpdatedView);
|
||||
await ModifyResponseAsync(options =>
|
||||
{
|
||||
options.Embeds = embeds;
|
||||
options.Components = actionRows;
|
||||
});
|
||||
}
|
||||
else if (result.EmptyMessage is not null)
|
||||
{
|
||||
await ModifyResponseAsync(options =>
|
||||
{
|
||||
options.Content = result.EmptyMessage;
|
||||
options.Embeds = [];
|
||||
options.Components = [];
|
||||
});
|
||||
}
|
||||
|
||||
await FollowupEphemeralAsync(result.ReplyText);
|
||||
}
|
||||
|
||||
private Task CompleteResponseAsync(string text) =>
|
||||
ModifyResponseAsync(options => options.Content = text);
|
||||
|
||||
private Task FollowupEphemeralAsync(string text) =>
|
||||
FollowupAsync(new InteractionMessageProperties()
|
||||
.WithContent(text)
|
||||
.WithFlags(MessageFlags.Ephemeral));
|
||||
|
||||
private bool SourceMessageHasDeleteAction() =>
|
||||
Context.Interaction.Message?.Components.Any(ComponentContainsDeleteAction) == true;
|
||||
|
||||
private static bool ComponentContainsDeleteAction(object? component)
|
||||
{
|
||||
if (component is null)
|
||||
return false;
|
||||
|
||||
if (component is IInteractiveComponent interactive
|
||||
&& interactive.CustomId.StartsWith("delete_session:", StringComparison.Ordinal))
|
||||
return true;
|
||||
|
||||
var nestedComponents = component.GetType().GetProperty("Components")?.GetValue(component) as IEnumerable;
|
||||
if (nestedComponents is null)
|
||||
return false;
|
||||
|
||||
foreach (var nestedComponent in nestedComponents)
|
||||
{
|
||||
if (ComponentContainsDeleteAction(nestedComponent))
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private static InteractionCallbackProperties CreateEphemeralReply(string text) =>
|
||||
InteractionCallback.Message(
|
||||
new InteractionMessageProperties()
|
||||
|
||||
@@ -6,11 +6,14 @@
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<UserSecretsId>dotnet-GmRelay.DiscordBot-issue-26</UserSecretsId>
|
||||
<!-- DiscordBot uses vanilla Dapper in its own handlers; DAP005 requires AOT-enabled Dapper -->
|
||||
<NoWarn>$(NoWarn);DAP005</NoWarn>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Aspire.Npgsql" Version="13.2.2" />
|
||||
<PackageReference Include="Dapper" Version="2.1.72" />
|
||||
<PackageReference Include="Dapper.AOT" Version="1.0.48" />
|
||||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="10.0.5" />
|
||||
<PackageReference Include="NetCord.Hosting" Version="1.0.0-alpha.489" />
|
||||
<PackageReference Include="NetCord.Hosting.Services" Version="1.0.0-alpha.489" />
|
||||
|
||||
@@ -1,21 +1,43 @@
|
||||
using System.Globalization;
|
||||
using System.Text;
|
||||
using GmRelay.DiscordBot.Rendering;
|
||||
using GmRelay.Shared.Domain;
|
||||
using GmRelay.Shared.Platform;
|
||||
using GmRelay.Shared.Rendering;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using NetCord;
|
||||
using NetCord.Rest;
|
||||
|
||||
namespace GmRelay.DiscordBot.Infrastructure.Discord;
|
||||
|
||||
public sealed class DiscordPlatformMessenger(
|
||||
RestClient restClient,
|
||||
DiscordInteractionReplyCache interactionReplies) : IPlatformMessenger
|
||||
public sealed class DiscordPlatformMessenger : IPlatformMessenger
|
||||
{
|
||||
private readonly RestClient restClient;
|
||||
private readonly DiscordInteractionReplyCache interactionReplies;
|
||||
private readonly ILogger<DiscordPlatformMessenger>? logger;
|
||||
|
||||
public DiscordPlatformMessenger(
|
||||
RestClient restClient,
|
||||
DiscordInteractionReplyCache interactionReplies)
|
||||
: this(restClient, interactionReplies, logger: null)
|
||||
{
|
||||
}
|
||||
|
||||
public DiscordPlatformMessenger(
|
||||
RestClient restClient,
|
||||
DiscordInteractionReplyCache interactionReplies,
|
||||
ILogger<DiscordPlatformMessenger>? logger)
|
||||
{
|
||||
this.restClient = restClient;
|
||||
this.interactionReplies = interactionReplies;
|
||||
this.logger = logger;
|
||||
}
|
||||
|
||||
public async Task<PlatformMessageRef> SendScheduleAsync(PlatformScheduleMessage message, CancellationToken ct)
|
||||
{
|
||||
var (embeds, actionRows) = DiscordSessionBatchRenderer.Render(message.View);
|
||||
|
||||
var channelId = ulong.Parse(message.Group.ExternalChannelId
|
||||
?? message.Group.ExternalGroupId);
|
||||
var channelId = GetChannelId(message.Group);
|
||||
|
||||
var msg = await restClient.SendMessageAsync(
|
||||
channelId,
|
||||
@@ -27,7 +49,7 @@ public sealed class DiscordPlatformMessenger(
|
||||
PlatformKind.Discord,
|
||||
message.Group.ExternalGroupId,
|
||||
null,
|
||||
msg.Id.ToString());
|
||||
msg.Id.ToString(CultureInfo.InvariantCulture));
|
||||
}
|
||||
|
||||
public async Task UpdateScheduleAsync(PlatformScheduleMessage message, CancellationToken ct)
|
||||
@@ -37,9 +59,8 @@ public sealed class DiscordPlatformMessenger(
|
||||
|
||||
var (embeds, actionRows) = DiscordSessionBatchRenderer.Render(message.View);
|
||||
|
||||
var channelId = ulong.Parse(message.Group.ExternalChannelId
|
||||
?? message.Group.ExternalGroupId);
|
||||
var messageId = ulong.Parse(message.ExistingMessage.ExternalMessageId);
|
||||
var channelId = GetChannelId(message.Group);
|
||||
var messageId = ParseSnowflake(message.ExistingMessage.ExternalMessageId);
|
||||
|
||||
await restClient.ModifyMessageAsync(
|
||||
channelId,
|
||||
@@ -51,14 +72,46 @@ public sealed class DiscordPlatformMessenger(
|
||||
});
|
||||
}
|
||||
|
||||
public Task SendGroupMessageAsync(PlatformGroup group, string htmlText, CancellationToken ct)
|
||||
public async Task SendGroupMessageAsync(PlatformGroup group, string htmlText, CancellationToken ct)
|
||||
{
|
||||
return Task.CompletedTask;
|
||||
await restClient.SendMessageAsync(GetChannelId(group), htmlText);
|
||||
}
|
||||
|
||||
public Task SendPrivateMessageAsync(PlatformPrivateMessage message, CancellationToken ct)
|
||||
public async Task SendGroupMessageAsync(PlatformGroup group, string htmlText, IReadOnlyList<PlatformMessageAction> actions, CancellationToken ct)
|
||||
{
|
||||
return Task.CompletedTask;
|
||||
var rows = BuildActionRows(actions);
|
||||
await restClient.SendMessageAsync(GetChannelId(group), new MessageProperties().WithContent(htmlText).WithComponents(rows));
|
||||
}
|
||||
|
||||
public async Task UpdateGroupMessageAsync(PlatformMessageRef messageRef, string htmlText, IReadOnlyList<PlatformMessageAction> actions, CancellationToken ct)
|
||||
{
|
||||
var channelId = GetChannelId(new PlatformGroup(messageRef.Platform, messageRef.ExternalGroupId, string.Empty, messageRef.ExternalThreadId));
|
||||
var messageId = ParseSnowflake(messageRef.ExternalMessageId);
|
||||
var rows = BuildActionRows(actions);
|
||||
await restClient.ModifyMessageAsync(channelId, messageId, options =>
|
||||
{
|
||||
options.Content = htmlText;
|
||||
options.Components = rows;
|
||||
});
|
||||
}
|
||||
|
||||
public Task<PlatformMessageRef> CreateThreadAsync(PlatformGroup group, string title, CancellationToken ct)
|
||||
{
|
||||
// Discord thread creation is not implemented in this adapter
|
||||
return Task.FromResult(new PlatformMessageRef(PlatformKind.Discord, group.ExternalGroupId, group.ExternalThreadId, string.Empty));
|
||||
}
|
||||
|
||||
public Task DeleteThreadAsync(PlatformGroup group, CancellationToken ct) => Task.CompletedTask;
|
||||
|
||||
public async Task DeleteMessageAsync(PlatformMessageRef messageRef, CancellationToken ct)
|
||||
{
|
||||
var channelId = GetChannelId(new PlatformGroup(messageRef.Platform, messageRef.ExternalGroupId, string.Empty, messageRef.ExternalThreadId));
|
||||
await restClient.DeleteMessageAsync(channelId, ParseSnowflake(messageRef.ExternalMessageId));
|
||||
}
|
||||
|
||||
public async Task SendPrivateMessageAsync(PlatformPrivateMessage message, CancellationToken ct)
|
||||
{
|
||||
await SendDirectContentAsync(message.Recipient, message.HtmlText, ct);
|
||||
}
|
||||
|
||||
public Task AnswerInteractionAsync(PlatformInteractionReply reply, CancellationToken ct)
|
||||
@@ -71,4 +124,341 @@ public sealed class DiscordPlatformMessenger(
|
||||
{
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public async Task<PlatformMessageRef> SendConfirmationRequestAsync(
|
||||
PlatformConfirmationRequest request,
|
||||
CancellationToken ct)
|
||||
{
|
||||
var channelId = GetChannelId(request.Group);
|
||||
try
|
||||
{
|
||||
var message = await restClient.SendMessageAsync(
|
||||
channelId,
|
||||
new MessageProperties()
|
||||
.WithEmbeds([BuildConfirmationEmbed(request)])
|
||||
.WithComponents(BuildRsvpRows(request.SessionId, disabled: false)));
|
||||
|
||||
logger?.LogInformation(
|
||||
"Confirmation request sent to Discord channel {ChannelId}, message id {MessageId}",
|
||||
channelId,
|
||||
message.Id);
|
||||
|
||||
return new PlatformMessageRef(
|
||||
PlatformKind.Discord,
|
||||
request.Group.ExternalGroupId,
|
||||
null,
|
||||
message.Id.ToString(CultureInfo.InvariantCulture));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger?.LogError(
|
||||
ex,
|
||||
"Failed to send confirmation request to Discord channel {ChannelId} for session {SessionId}",
|
||||
channelId,
|
||||
request.SessionId);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task UpdateConfirmationRequestAsync(PlatformRsvpMessageUpdate update, CancellationToken ct)
|
||||
{
|
||||
if (update.Request.ExistingMessage is null)
|
||||
return;
|
||||
|
||||
var channelId = GetChannelId(update.Request.Group);
|
||||
var messageId = ParseSnowflake(update.Request.ExistingMessage.ExternalMessageId);
|
||||
var components = BuildRsvpRows(update.Request.SessionId, update.DisableActions);
|
||||
|
||||
await restClient.ModifyMessageAsync(
|
||||
channelId,
|
||||
messageId,
|
||||
options =>
|
||||
{
|
||||
options.Embeds = [BuildConfirmationEmbed(update.Request)];
|
||||
options.Components = components;
|
||||
});
|
||||
}
|
||||
|
||||
public async Task<PlatformMessageRef> SendJoinLinkNotificationAsync(
|
||||
PlatformJoinLinkNotification notification,
|
||||
CancellationToken ct)
|
||||
{
|
||||
var channelId = GetChannelId(notification.Group);
|
||||
try
|
||||
{
|
||||
var message = await restClient.SendMessageAsync(
|
||||
channelId,
|
||||
new MessageProperties().WithEmbeds([BuildJoinLinkEmbed(notification)]));
|
||||
|
||||
logger?.LogInformation(
|
||||
"Join link sent to Discord channel {ChannelId}, message id {MessageId}",
|
||||
channelId,
|
||||
message.Id);
|
||||
|
||||
return new PlatformMessageRef(
|
||||
PlatformKind.Discord,
|
||||
notification.Group.ExternalGroupId,
|
||||
null,
|
||||
message.Id.ToString(CultureInfo.InvariantCulture));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger?.LogError(
|
||||
ex,
|
||||
"Failed to send join link to Discord channel {ChannelId} for session {SessionId}",
|
||||
channelId,
|
||||
notification.SessionId);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task SendDirectSessionNotificationAsync(
|
||||
PlatformDirectSessionNotification notification,
|
||||
CancellationToken ct)
|
||||
{
|
||||
try
|
||||
{
|
||||
await SendDirectContentAsync(
|
||||
notification.Recipient,
|
||||
BuildDirectContent(notification),
|
||||
ct);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger?.LogWarning(
|
||||
ex,
|
||||
"Failed to send Discord direct notification {NotificationKind} for session {SessionId} to user {ExternalUserId}",
|
||||
notification.Kind,
|
||||
notification.SessionId,
|
||||
notification.Recipient.ExternalUserId);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task SendRsvpOutcomeAsync(PlatformRsvpOutcomeNotification notification, CancellationToken ct)
|
||||
{
|
||||
if (notification.Kind == PlatformRsvpOutcomeKind.GroupAllConfirmed && notification.Group is not null)
|
||||
{
|
||||
await restClient.SendMessageAsync(
|
||||
GetChannelId(notification.Group),
|
||||
BuildRsvpGroupOutcomeContent(notification));
|
||||
return;
|
||||
}
|
||||
|
||||
var directKind = notification.Kind == PlatformRsvpOutcomeKind.GmPlayerDeclined
|
||||
? PlatformDirectSessionNotificationKind.RsvpDeclined
|
||||
: PlatformDirectSessionNotificationKind.RsvpAllConfirmed;
|
||||
|
||||
foreach (var recipient in notification.Recipients)
|
||||
{
|
||||
await SendDirectSessionNotificationAsync(
|
||||
new PlatformDirectSessionNotification(
|
||||
directKind,
|
||||
recipient,
|
||||
notification.SessionId,
|
||||
notification.Title,
|
||||
notification.ScheduledAt,
|
||||
ActorDisplayName: notification.ActorDisplayName),
|
||||
ct);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task UpdateRescheduleVoteAsync(PlatformRescheduleVoteUpdate update, CancellationToken ct)
|
||||
{
|
||||
var (embed, actionRow) = DiscordRescheduleVotingRenderer.Render(
|
||||
update.Title,
|
||||
update.CurrentScheduledAt,
|
||||
update.VotingDeadlineAt,
|
||||
update.Options,
|
||||
update.Participants,
|
||||
update.Votes);
|
||||
|
||||
var disabledRow = new ActionRowProperties();
|
||||
foreach (var button in actionRow.OfType<ButtonProperties>())
|
||||
{
|
||||
disabledRow.Add(new ButtonProperties(
|
||||
button.CustomId,
|
||||
button.Label ?? string.Empty,
|
||||
ButtonStyle.Secondary)
|
||||
{
|
||||
Disabled = true
|
||||
});
|
||||
}
|
||||
|
||||
var updatedEmbed = embed.WithDescription(
|
||||
$"{embed.Description}\n\n{BuildRescheduleResultText(update)}");
|
||||
|
||||
await restClient.ModifyMessageAsync(
|
||||
GetChannelId(update.Group),
|
||||
ParseSnowflake(update.ExistingMessage.ExternalMessageId),
|
||||
options =>
|
||||
{
|
||||
options.Embeds = [updatedEmbed];
|
||||
options.Components = [disabledRow];
|
||||
});
|
||||
}
|
||||
|
||||
private static EmbedProperties BuildConfirmationEmbed(PlatformConfirmationRequest request)
|
||||
{
|
||||
var embed = new EmbedProperties()
|
||||
.WithTitle($"Подтверждение: {request.Title}")
|
||||
.WithDescription(BuildConfirmationDescription(request))
|
||||
.WithColor(new Color(0x5865F2));
|
||||
|
||||
return embed.AddFields(
|
||||
[
|
||||
BuildParticipantField("Подтвердили", request.Participants, RsvpStatus.Confirmed),
|
||||
BuildParticipantField("Отказались", request.Participants, RsvpStatus.Declined),
|
||||
BuildParticipantField("Ожидаем ответ", request.Participants, RsvpStatus.Pending)
|
||||
]);
|
||||
}
|
||||
|
||||
private static string BuildConfirmationDescription(PlatformConfirmationRequest request) =>
|
||||
$"Время: **{request.ScheduledAt.FormatMoscow()}** (МСК)\n" +
|
||||
"Подтвердите участие кнопкой ниже.";
|
||||
|
||||
private static EmbedFieldProperties BuildParticipantField(
|
||||
string title,
|
||||
IReadOnlyList<PlatformSessionParticipant> participants,
|
||||
string status)
|
||||
{
|
||||
var values = participants
|
||||
.Where(participant => participant.RsvpStatus == status)
|
||||
.Select(FormatDiscordParticipant)
|
||||
.ToList();
|
||||
|
||||
return new EmbedFieldProperties()
|
||||
.WithName(title)
|
||||
.WithValue(values.Count == 0 ? "—" : string.Join("\n", values))
|
||||
.WithInline();
|
||||
}
|
||||
|
||||
private static EmbedProperties BuildJoinLinkEmbed(PlatformJoinLinkNotification notification)
|
||||
{
|
||||
var mentions = notification.ConfirmedPlayers.Count == 0
|
||||
? "—"
|
||||
: string.Join(", ", notification.ConfirmedPlayers.Select(p => Mention(p.User)));
|
||||
|
||||
var embed = new EmbedProperties()
|
||||
.WithTitle($"Ссылка на игру: {notification.Title}")
|
||||
.WithDescription(
|
||||
$"Время: **{notification.ScheduledAt.FormatMoscow()}** (МСК)\n" +
|
||||
$"Ссылка: {notification.JoinLink}\n\n" +
|
||||
$"Участники: {mentions}")
|
||||
.WithColor(new Color(0x57F287));
|
||||
|
||||
var embedUrl = DiscordEmbedUrls.NormalizeHttpUrl(notification.JoinLink);
|
||||
return embedUrl is null ? embed : embed.WithUrl(embedUrl);
|
||||
}
|
||||
|
||||
private static IReadOnlyList<ActionRowProperties> BuildRsvpRows(Guid sessionId, bool disabled)
|
||||
{
|
||||
var row = new ActionRowProperties();
|
||||
row.Add(new ButtonProperties($"rsvp:confirm:{sessionId}", "Буду", ButtonStyle.Success)
|
||||
{
|
||||
Disabled = disabled
|
||||
});
|
||||
row.Add(new ButtonProperties($"rsvp:decline:{sessionId}", "Не смогу", ButtonStyle.Danger)
|
||||
{
|
||||
Disabled = disabled
|
||||
});
|
||||
|
||||
return [row];
|
||||
}
|
||||
|
||||
private static string BuildDirectContent(PlatformDirectSessionNotification notification)
|
||||
{
|
||||
var builder = new StringBuilder();
|
||||
builder.AppendLine(notification.Kind switch
|
||||
{
|
||||
PlatformDirectSessionNotificationKind.ConfirmationRequest => "Нужно подтвердить участие",
|
||||
PlatformDirectSessionNotificationKind.OneHourReminder => "Напоминание: сессия через час",
|
||||
PlatformDirectSessionNotificationKind.JoinLink => "Ссылка на игру",
|
||||
PlatformDirectSessionNotificationKind.RsvpAllConfirmed => "Все игроки подтвердили участие",
|
||||
PlatformDirectSessionNotificationKind.RsvpDeclined => "Игрок отказался от участия",
|
||||
PlatformDirectSessionNotificationKind.RescheduleApproved => "Сессия перенесена",
|
||||
PlatformDirectSessionNotificationKind.RescheduleRejected => "Перенос сессии отклонен",
|
||||
_ => "Уведомление по сессии"
|
||||
});
|
||||
|
||||
builder.AppendLine();
|
||||
builder.AppendLine($"**{notification.Title}**");
|
||||
builder.AppendLine($"Время: **{notification.ScheduledAt.FormatMoscow()}** (МСК)");
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(notification.JoinLink))
|
||||
builder.AppendLine($"Ссылка: {notification.JoinLink}");
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(notification.ActorDisplayName))
|
||||
builder.AppendLine($"Игрок: {notification.ActorDisplayName}");
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(notification.Reason))
|
||||
builder.AppendLine($"Причина: {notification.Reason}");
|
||||
|
||||
return builder.ToString();
|
||||
}
|
||||
|
||||
private static string BuildRsvpGroupOutcomeContent(PlatformRsvpOutcomeNotification notification) =>
|
||||
$"Все участники подтвердили сессию **{notification.Title}** на " +
|
||||
$"**{notification.ScheduledAt.FormatMoscow()}** (МСК).";
|
||||
|
||||
private static string BuildRescheduleResultText(PlatformRescheduleVoteUpdate update)
|
||||
{
|
||||
if (update.SelectedOption is not null)
|
||||
{
|
||||
return "Голосование завершено. " +
|
||||
$"Победил вариант {update.SelectedOption.DisplayOrder}: " +
|
||||
$"**{update.SelectedOption.ProposedAt.FormatMoscow()}** (МСК).";
|
||||
}
|
||||
|
||||
return $"Голосование завершено. {update.Decision.Reason}";
|
||||
}
|
||||
|
||||
private async Task SendDirectContentAsync(PlatformUser recipient, string content, CancellationToken ct)
|
||||
{
|
||||
var userId = ParseSnowflake(recipient.ExternalUserId);
|
||||
var dm = await restClient.GetDMChannelAsync(userId, cancellationToken: ct);
|
||||
await restClient.SendMessageAsync(
|
||||
dm.Id,
|
||||
new MessageProperties().WithContent(content),
|
||||
cancellationToken: ct);
|
||||
}
|
||||
|
||||
private static string FormatDiscordParticipant(PlatformSessionParticipant participant) =>
|
||||
$"{Mention(participant.User)} ({participant.User.DisplayName})";
|
||||
|
||||
private static string Mention(PlatformUser user) => $"<@{user.ExternalUserId}>";
|
||||
|
||||
private static ulong GetChannelId(PlatformGroup group)
|
||||
{
|
||||
var channelId = group.ExternalChannelId ?? group.ExternalGroupId
|
||||
?? throw new InvalidOperationException("Discord group has no channel or group identifier.");
|
||||
|
||||
return ParseSnowflake(channelId);
|
||||
}
|
||||
|
||||
private static IReadOnlyList<ActionRowProperties> BuildActionRows(IReadOnlyList<PlatformMessageAction> actions)
|
||||
{
|
||||
if (actions.Count == 0)
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
var rows = new List<ActionRowProperties>();
|
||||
foreach (var chunk in actions.Chunk(5))
|
||||
{
|
||||
var row = new ActionRowProperties();
|
||||
foreach (var action in chunk)
|
||||
{
|
||||
row.Add(new ButtonProperties(action.Key, action.Label, ButtonStyle.Secondary)
|
||||
{
|
||||
CustomId = action.Payload
|
||||
});
|
||||
}
|
||||
rows.Add(row);
|
||||
}
|
||||
|
||||
return rows;
|
||||
}
|
||||
|
||||
private static ulong ParseSnowflake(string value) =>
|
||||
ulong.Parse(value, CultureInfo.InvariantCulture);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,101 @@
|
||||
using System.Net;
|
||||
|
||||
namespace GmRelay.DiscordBot.Infrastructure.Health;
|
||||
|
||||
public sealed class DiscordHealthCheckHostedService : IHostedService
|
||||
{
|
||||
private readonly ILogger<DiscordHealthCheckHostedService> _logger;
|
||||
private readonly string _prefix;
|
||||
private HttpListener? _listener;
|
||||
private CancellationTokenSource? _cts;
|
||||
private Task? _listenerTask;
|
||||
|
||||
public DiscordHealthCheckHostedService(
|
||||
ILogger<DiscordHealthCheckHostedService> logger,
|
||||
IConfiguration configuration)
|
||||
{
|
||||
_logger = logger;
|
||||
_prefix = configuration.GetValue("HealthCheck:Prefix", "http://+:8082/")!;
|
||||
}
|
||||
|
||||
public Task StartAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
_cts = new CancellationTokenSource();
|
||||
_listener = new HttpListener();
|
||||
_listener.Prefixes.Add(_prefix);
|
||||
_listener.Start();
|
||||
|
||||
_logger.LogInformation("Discord health check server started on {Prefix}", _prefix);
|
||||
|
||||
_listenerTask = Task.Run(async () => await ListenAsync(_cts.Token), cancellationToken);
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public async Task StopAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
_cts?.Cancel();
|
||||
_listener?.Stop();
|
||||
|
||||
if (_listenerTask != null)
|
||||
{
|
||||
await Task.WhenAny(_listenerTask, Task.Delay(TimeSpan.FromSeconds(5), cancellationToken));
|
||||
}
|
||||
|
||||
_listener?.Close();
|
||||
_logger.LogInformation("Discord health check server stopped");
|
||||
}
|
||||
|
||||
private async Task ListenAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
while (_listener?.IsListening == true && !cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
try
|
||||
{
|
||||
var context = await _listener.GetContextAsync();
|
||||
_ = Task.Run(() => HandleRequestAsync(context), cancellationToken);
|
||||
}
|
||||
catch (HttpListenerException) when (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
break;
|
||||
}
|
||||
catch (ObjectDisposedException)
|
||||
{
|
||||
break;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error in Discord health check listener");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async Task HandleRequestAsync(HttpListenerContext context)
|
||||
{
|
||||
var response = context.Response;
|
||||
try
|
||||
{
|
||||
var request = context.Request;
|
||||
|
||||
if (request.Url?.AbsolutePath == "/health")
|
||||
{
|
||||
response.StatusCode = (int)HttpStatusCode.OK;
|
||||
response.ContentType = "application/json";
|
||||
var body = "{\"status\":\"healthy\"}"u8.ToArray();
|
||||
await response.OutputStream.WriteAsync(body);
|
||||
}
|
||||
else
|
||||
{
|
||||
response.StatusCode = (int)HttpStatusCode.NotFound;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error handling Discord health check request");
|
||||
}
|
||||
finally
|
||||
{
|
||||
response.Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace GmRelay.DiscordBot.Infrastructure;
|
||||
|
||||
public sealed class SystemClock : GmRelay.Shared.Platform.ISystemClock
|
||||
{
|
||||
public DateTimeOffset UtcNow => DateTimeOffset.UtcNow;
|
||||
}
|
||||
@@ -1,16 +1,27 @@
|
||||
using GmRelay.DiscordBot;
|
||||
using GmRelay.DiscordBot.Features.Sessions;
|
||||
using GmRelay.DiscordBot.Infrastructure;
|
||||
using GmRelay.DiscordBot.Infrastructure.Discord;
|
||||
using GmRelay.DiscordBot.Infrastructure.Health;
|
||||
using GmRelay.DiscordBot.Infrastructure.Logging;
|
||||
using GmRelay.Shared.Features.Confirmation.HandleRsvp;
|
||||
using GmRelay.Shared.Features.Confirmation.SendConfirmation;
|
||||
using GmRelay.Shared.Features.Notifications;
|
||||
using GmRelay.Shared.Features.Reminders.SendJoinLink;
|
||||
using GmRelay.Shared.Features.Reminders.SendOneHourReminder;
|
||||
using GmRelay.Shared.Features.Sessions.CreateSession;
|
||||
using GmRelay.Shared.Features.Sessions.RescheduleSession;
|
||||
using GmRelay.Shared.Infrastructure.Scheduling;
|
||||
using GmRelay.Shared.Platform;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using NetCord;
|
||||
using NetCord.Gateway;
|
||||
using NetCord.Hosting.Gateway;
|
||||
using NetCord.Hosting.Services;
|
||||
using NetCord.Hosting.Services.ApplicationCommands;
|
||||
using NetCord.Hosting.Services.ComponentInteractions;
|
||||
using NetCord.Services.ApplicationCommands;
|
||||
using NetCord.Services.ComponentInteractions;
|
||||
using Npgsql;
|
||||
|
||||
@@ -25,6 +36,8 @@ discordOptions.Validate();
|
||||
|
||||
builder.Services.AddSingleton(discordOptions);
|
||||
|
||||
builder.Logging.AddConsole();
|
||||
|
||||
builder.Services.AddSingleton<NpgsqlDataSource>(sp =>
|
||||
{
|
||||
var config = sp.GetRequiredService<IConfiguration>();
|
||||
@@ -43,12 +56,31 @@ builder.Services.AddSingleton<NpgsqlDataSource>(sp =>
|
||||
|
||||
builder.Services.AddSingleton<DiscordPermissionChecker>();
|
||||
builder.Services.AddSingleton<DiscordListSessionsHandler>();
|
||||
builder.Services.AddSingleton<DiscordDeleteSessionHandler>();
|
||||
builder.Services.AddSingleton<DiscordNewSessionHandler>();
|
||||
builder.Services.AddSingleton<DiscordRescheduleHandler>();
|
||||
builder.Services.AddSingleton<GmRelay.Shared.Features.Sessions.RescheduleSession.HandleRescheduleVoteHandler>();
|
||||
builder.Services.AddSingleton<DiscordRescheduleVoteHandler>();
|
||||
builder.Services.AddSingleton<IScheduleMessageUpdateLock, ScheduleMessageUpdateLock>();
|
||||
builder.Services.AddSingleton<JoinSessionHandler>();
|
||||
builder.Services.AddSingleton<LeaveSessionHandler>();
|
||||
builder.Services.AddSingleton<DiscordInteractionReplyCache>();
|
||||
builder.Services.AddSingleton<IPlatformMessenger, DiscordPlatformMessenger>();
|
||||
builder.Services.AddSingleton<ISystemClock, SystemClock>();
|
||||
builder.Services.AddSingleton(new PlatformSchedulerOptions(PlatformKind.Discord));
|
||||
builder.Services.AddSingleton<ISessionTriggerStore, DbSessionTriggerStore>();
|
||||
builder.Services.AddSingleton<PlatformDirectNotificationSender>();
|
||||
builder.Services.AddSingleton<SendConfirmationHandler>();
|
||||
builder.Services.AddSingleton<ISendConfirmationHandler>(sp => sp.GetRequiredService<SendConfirmationHandler>());
|
||||
builder.Services.AddSingleton<SendJoinLinkHandler>();
|
||||
builder.Services.AddSingleton<ISendJoinLinkHandler>(sp => sp.GetRequiredService<SendJoinLinkHandler>());
|
||||
builder.Services.AddSingleton<SendOneHourReminderHandler>();
|
||||
builder.Services.AddSingleton<ISendOneHourReminderHandler>(sp => sp.GetRequiredService<SendOneHourReminderHandler>());
|
||||
builder.Services.AddSingleton<HandleRsvpHandler>();
|
||||
builder.Services.AddSingleton<RescheduleVotingFinalizer>();
|
||||
builder.Services.AddHostedService<SessionSchedulerService>();
|
||||
builder.Services.AddHostedService<DiscordRescheduleVotingDeadlineService>();
|
||||
builder.Services.AddHostedService<DiscordHealthCheckHostedService>();
|
||||
|
||||
builder.Services
|
||||
.AddDiscordGateway(options =>
|
||||
@@ -56,12 +88,13 @@ builder.Services
|
||||
options.Token = discordOptions.Token;
|
||||
options.Intents = GatewayIntents.Guilds;
|
||||
})
|
||||
.AddApplicationCommands()
|
||||
.AddApplicationCommands<SlashCommandInteraction, SlashCommandContext>()
|
||||
.AddComponentInteractions<ButtonInteraction, ButtonInteractionContext>()
|
||||
.AddGatewayHandlers(typeof(Program).Assembly);
|
||||
|
||||
var host = builder.Build();
|
||||
|
||||
host.AddSlashCommand("ping", "Checks whether GM-Relay Discord is online.", () => "Pong!");
|
||||
host.AddModules(typeof(Program).Assembly);
|
||||
|
||||
await host.RunAsync();
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
namespace GmRelay.DiscordBot.Rendering;
|
||||
|
||||
public static class DiscordEmbedUrls
|
||||
{
|
||||
public static string? NormalizeHttpUrl(string? value)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(value))
|
||||
return null;
|
||||
|
||||
var candidate = value.Trim();
|
||||
if (IsSupportedHttpUrl(candidate, out var normalized))
|
||||
return normalized;
|
||||
|
||||
if (candidate.Contains("://", StringComparison.Ordinal))
|
||||
return null;
|
||||
|
||||
return IsSupportedHttpUrl($"https://{candidate}", out normalized)
|
||||
&& HasPublicHost(normalized)
|
||||
? normalized
|
||||
: null;
|
||||
}
|
||||
|
||||
private static bool IsSupportedHttpUrl(string value, out string normalized)
|
||||
{
|
||||
normalized = string.Empty;
|
||||
|
||||
if (!Uri.TryCreate(value, UriKind.Absolute, out var uri))
|
||||
return false;
|
||||
|
||||
if (!string.Equals(uri.Scheme, Uri.UriSchemeHttp, StringComparison.OrdinalIgnoreCase)
|
||||
&& !string.Equals(uri.Scheme, Uri.UriSchemeHttps, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
normalized = uri.ToString();
|
||||
return true;
|
||||
}
|
||||
|
||||
private static bool HasPublicHost(string value) =>
|
||||
Uri.TryCreate(value, UriKind.Absolute, out var uri)
|
||||
&& uri.Host.Contains('.', StringComparison.Ordinal);
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
namespace GmRelay.DiscordBot.Rendering;
|
||||
|
||||
using GmRelay.Shared.Domain;
|
||||
using GmRelay.Shared.Features.Sessions.RescheduleSession;
|
||||
using NetCord;
|
||||
using NetCord.Rest;
|
||||
|
||||
public static class DiscordRescheduleVotingRenderer
|
||||
{
|
||||
public static (EmbedProperties Embed, ActionRowProperties ActionRow) Render(
|
||||
string title,
|
||||
DateTime currentTime,
|
||||
DateTimeOffset deadline,
|
||||
IReadOnlyList<RescheduleOptionDto> options,
|
||||
IReadOnlyList<VoteParticipantDto> participants,
|
||||
IReadOnlyList<RescheduleOptionVoteDto> votes)
|
||||
{
|
||||
var votesByOption = votes.GroupBy(v => v.OptionId).ToDictionary(g => g.Key, g => g.ToList());
|
||||
var votedPlayerIds = votes.Select(v => v.PlayerId).ToHashSet();
|
||||
var pending = participants.Where(p => !votedPlayerIds.Contains(p.PlayerId)).Select(p => p.DisplayName).ToList();
|
||||
|
||||
var sb = new System.Text.StringBuilder();
|
||||
sb.AppendLine($"📅 Текущее время: {currentTime.FormatMoscow()} (МСК)");
|
||||
sb.AppendLine($"⏳ Дедлайн: {deadline.FormatMoscow()} (МСК)");
|
||||
sb.AppendLine();
|
||||
sb.AppendLine("Выберите один из вариантов:");
|
||||
|
||||
foreach (var option in options.OrderBy(o => o.DisplayOrder))
|
||||
{
|
||||
var optionVotes = votesByOption.GetValueOrDefault(option.OptionId, []);
|
||||
sb.AppendLine($"{option.DisplayOrder}. **{option.ProposedAt.FormatMoscow()}** (МСК) — {optionVotes.Count} голосов");
|
||||
if (optionVotes.Count > 0)
|
||||
{
|
||||
sb.AppendLine($" {string.Join(", ", optionVotes.Select(v => v.DisplayName))}");
|
||||
}
|
||||
}
|
||||
|
||||
if (pending.Count > 0)
|
||||
{
|
||||
sb.AppendLine();
|
||||
sb.AppendLine($"Не проголосовали: {string.Join(", ", pending)}");
|
||||
}
|
||||
|
||||
sb.AppendLine();
|
||||
sb.AppendLine($"Голосов: {votedPlayerIds.Count}/{participants.Count}");
|
||||
sb.AppendLine("Правило: побеждает вариант с большинством голосов к дедлайну; при ничьей перенос не применяется.");
|
||||
|
||||
var embed = new EmbedProperties()
|
||||
.WithTitle($"🔄 Перенос сессии «{title}»")
|
||||
.WithDescription(sb.ToString())
|
||||
.WithColor(new Color(0xFEE75C));
|
||||
|
||||
var actionRow = new ActionRowProperties();
|
||||
foreach (var option in options.OrderBy(o => o.DisplayOrder))
|
||||
{
|
||||
actionRow.Add(new ButtonProperties(
|
||||
$"reschedule_vote:{option.OptionId}",
|
||||
$"{option.DisplayOrder}. {FormatButtonTime(option.ProposedAt)}",
|
||||
ButtonStyle.Primary));
|
||||
}
|
||||
|
||||
return (embed, actionRow);
|
||||
}
|
||||
|
||||
private static string FormatButtonTime(DateTimeOffset utc)
|
||||
=> utc.ToOffset(TimeSpan.FromHours(3)).ToString("dd.MM HH:mm", System.Globalization.CultureInfo.InvariantCulture);
|
||||
}
|
||||
@@ -70,9 +70,10 @@ public static class DiscordSessionBatchRenderer
|
||||
.WithInline()
|
||||
};
|
||||
|
||||
if (!string.IsNullOrEmpty(session.JoinLink))
|
||||
var embedUrl = DiscordEmbedUrls.NormalizeHttpUrl(session.JoinLink);
|
||||
if (embedUrl is not null)
|
||||
{
|
||||
embed = embed.WithUrl(session.JoinLink);
|
||||
embed = embed.WithUrl(embedUrl);
|
||||
}
|
||||
|
||||
embed = embed.WithColor(GetColor(session));
|
||||
|
||||
@@ -28,6 +28,12 @@
|
||||
"resolved": "2.1.72",
|
||||
"contentHash": "ns4mGqQd9a/MhP8m6w556vVlZIa0/MfUu03zrxjZC/jlr1uVCsUac8bkdB+Fs98Llbd56rRSo1eZH5VVmeGZyw=="
|
||||
},
|
||||
"Dapper.AOT": {
|
||||
"type": "Direct",
|
||||
"requested": "[1.0.48, )",
|
||||
"resolved": "1.0.48",
|
||||
"contentHash": "rsLM3yKr4g+YKKox9lhc8D+kz67P7Q9+xdyn1LmCsoYr1kYpJSm+Nt6slo5UrfUrcTiGJ57zUlyO8XUdV7G7iA=="
|
||||
},
|
||||
"Microsoft.Extensions.Hosting": {
|
||||
"type": "Direct",
|
||||
"requested": "[10.0.5, )",
|
||||
@@ -669,6 +675,7 @@
|
||||
"type": "Project",
|
||||
"dependencies": {
|
||||
"Dapper": "[2.1.72, )",
|
||||
"Microsoft.Extensions.Hosting.Abstractions": "[10.0.5, )",
|
||||
"Microsoft.Extensions.Logging.Abstractions": "[10.0.5, )",
|
||||
"Npgsql": "[10.0.2, )"
|
||||
}
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
using System.Collections.Frozen;
|
||||
|
||||
namespace GmRelay.Shared.Domain;
|
||||
|
||||
public enum GameSystem
|
||||
{
|
||||
Dnd5e,
|
||||
Pathfinder2e,
|
||||
CallOfCthulhu7e,
|
||||
Shadowdark,
|
||||
OldSchoolEssentials,
|
||||
Dragonbane,
|
||||
BladesInTheDark,
|
||||
Daggerheart,
|
||||
CyberpunkRed,
|
||||
Mothership,
|
||||
AlienRpg,
|
||||
WarhammerFantasy,
|
||||
VampireMasquerade5e,
|
||||
StarWarsFfg,
|
||||
Genesys,
|
||||
SavageWorlds,
|
||||
GURPS,
|
||||
Fate,
|
||||
DungeonWorld,
|
||||
Ironsworn,
|
||||
Other
|
||||
}
|
||||
|
||||
public static class GameSystemExtensions
|
||||
{
|
||||
private static readonly FrozenDictionary<GameSystem, string> DisplayNames =
|
||||
new Dictionary<GameSystem, string>
|
||||
{
|
||||
[GameSystem.Dnd5e] = "D&D 5e",
|
||||
[GameSystem.Pathfinder2e] = "Pathfinder 2e",
|
||||
[GameSystem.CallOfCthulhu7e] = "Call of Cthulhu 7e",
|
||||
[GameSystem.Shadowdark] = "Shadowdark",
|
||||
[GameSystem.OldSchoolEssentials] = "Old School Essentials",
|
||||
[GameSystem.Dragonbane] = "Dragonbane",
|
||||
[GameSystem.BladesInTheDark] = "Blades in the Dark",
|
||||
[GameSystem.Daggerheart] = "Daggerheart",
|
||||
[GameSystem.CyberpunkRed] = "Cyberpunk RED",
|
||||
[GameSystem.Mothership] = "Mothership",
|
||||
[GameSystem.AlienRpg] = "Alien RPG",
|
||||
[GameSystem.WarhammerFantasy] = "Warhammer Fantasy",
|
||||
[GameSystem.VampireMasquerade5e] = "Vampire: The Masquerade 5e",
|
||||
[GameSystem.StarWarsFfg] = "Star Wars (FFG)",
|
||||
[GameSystem.Genesys] = "Genesys",
|
||||
[GameSystem.SavageWorlds] = "Savage Worlds",
|
||||
[GameSystem.GURPS] = "GURPS",
|
||||
[GameSystem.Fate] = "Fate",
|
||||
[GameSystem.DungeonWorld] = "Dungeon World",
|
||||
[GameSystem.Ironsworn] = "Ironsworn",
|
||||
[GameSystem.Other] = "Другое"
|
||||
}.ToFrozenDictionary();
|
||||
|
||||
public static string ToDisplayName(this GameSystem system) =>
|
||||
DisplayNames.TryGetValue(system, out var name) ? name : "Другое";
|
||||
|
||||
public static GameSystem? TryParseFuzzy(string input)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(input))
|
||||
return null;
|
||||
|
||||
var normalized = input.Trim().ToLowerInvariant();
|
||||
|
||||
if (Enum.TryParse<GameSystem>(normalized, true, out var exact))
|
||||
return exact;
|
||||
|
||||
foreach (var value in Enum.GetValues<GameSystem>())
|
||||
{
|
||||
if (value == GameSystem.Other)
|
||||
continue;
|
||||
|
||||
var display = value.ToDisplayName().ToLowerInvariant();
|
||||
if (display == normalized || display.Contains(normalized) || normalized.Contains(display))
|
||||
return value;
|
||||
}
|
||||
|
||||
return GameSystem.Other;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,346 @@
|
||||
using System.Globalization;
|
||||
using Dapper;
|
||||
using GmRelay.Shared.Domain;
|
||||
using GmRelay.Shared.Features.Notifications;
|
||||
using GmRelay.Shared.Platform;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Npgsql;
|
||||
|
||||
namespace GmRelay.Shared.Features.Confirmation.HandleRsvp;
|
||||
|
||||
public sealed record HandleRsvpCommand(
|
||||
Guid SessionId,
|
||||
PlatformUser User,
|
||||
string Status,
|
||||
string InteractionId,
|
||||
PlatformGroup Group,
|
||||
PlatformMessageRef ConfirmationMessage);
|
||||
|
||||
internal sealed record RsvpCounts(int Total, int Confirmed, int Declined);
|
||||
|
||||
internal sealed record RsvpSessionContext(
|
||||
Guid GroupId,
|
||||
string Title,
|
||||
DateTime ScheduledAt,
|
||||
string Status);
|
||||
|
||||
internal sealed record ParticipantRsvpRow(
|
||||
string Platform,
|
||||
string ExternalUserId,
|
||||
string DisplayName,
|
||||
string? ExternalUsername,
|
||||
string RsvpStatus,
|
||||
string RegistrationStatus,
|
||||
bool IsGm);
|
||||
|
||||
internal sealed record RsvpRecipientRow(
|
||||
string Platform,
|
||||
string ExternalUserId,
|
||||
string DisplayName,
|
||||
string? ExternalUsername);
|
||||
|
||||
public sealed class HandleRsvpHandler(
|
||||
NpgsqlDataSource dataSource,
|
||||
IPlatformMessenger messenger,
|
||||
ILogger<HandleRsvpHandler> logger)
|
||||
{
|
||||
public async Task HandleAsync(HandleRsvpCommand command, CancellationToken ct)
|
||||
{
|
||||
await using var connection = await dataSource.OpenConnectionAsync(ct);
|
||||
await using var transaction = await connection.BeginTransactionAsync(ct);
|
||||
|
||||
var participantExists = await connection.ExecuteScalarAsync<bool>(
|
||||
"""
|
||||
SELECT EXISTS (
|
||||
SELECT 1
|
||||
FROM session_participants sp
|
||||
JOIN players p ON p.id = sp.player_id
|
||||
WHERE sp.session_id = @SessionId
|
||||
AND p.platform = @Platform
|
||||
AND p.external_user_id = @ExternalUserId
|
||||
AND sp.is_gm = false
|
||||
AND sp.registration_status = @Active
|
||||
)
|
||||
""",
|
||||
new
|
||||
{
|
||||
command.SessionId,
|
||||
Platform = command.User.Platform.ToString(),
|
||||
command.User.ExternalUserId,
|
||||
Active = ParticipantRegistrationStatus.Active
|
||||
},
|
||||
transaction);
|
||||
|
||||
if (!participantExists)
|
||||
{
|
||||
await messenger.AnswerInteractionAsync(
|
||||
new PlatformInteractionReply(
|
||||
command.InteractionId,
|
||||
"Вы не являетесь участником этой сессии."),
|
||||
ct);
|
||||
return;
|
||||
}
|
||||
|
||||
var updated = await connection.ExecuteAsync(
|
||||
"""
|
||||
UPDATE session_participants
|
||||
SET rsvp_status = @Status,
|
||||
responded_at = now()
|
||||
WHERE session_id = @SessionId
|
||||
AND player_id = (
|
||||
SELECT id
|
||||
FROM players
|
||||
WHERE platform = @Platform
|
||||
AND external_user_id = @ExternalUserId
|
||||
LIMIT 1
|
||||
)
|
||||
AND registration_status = @Active
|
||||
AND rsvp_status != @Status
|
||||
""",
|
||||
new
|
||||
{
|
||||
command.SessionId,
|
||||
command.Status,
|
||||
Platform = command.User.Platform.ToString(),
|
||||
command.User.ExternalUserId,
|
||||
Active = ParticipantRegistrationStatus.Active
|
||||
},
|
||||
transaction);
|
||||
|
||||
if (updated == 0)
|
||||
{
|
||||
var alreadyText = command.Status == RsvpStatus.Confirmed
|
||||
? "Вы уже подтвердили участие."
|
||||
: "Вы уже отказались от участия.";
|
||||
|
||||
await messenger.AnswerInteractionAsync(
|
||||
new PlatformInteractionReply(command.InteractionId, alreadyText),
|
||||
ct);
|
||||
return;
|
||||
}
|
||||
|
||||
var session = await connection.QuerySingleAsync<RsvpSessionContext>(
|
||||
"""
|
||||
SELECT s.group_id AS GroupId,
|
||||
s.title,
|
||||
s.scheduled_at AS ScheduledAt,
|
||||
s.status AS Status
|
||||
FROM sessions s
|
||||
WHERE s.id = @SessionId
|
||||
""",
|
||||
new { command.SessionId },
|
||||
transaction);
|
||||
|
||||
if (command.Status == RsvpStatus.Declined)
|
||||
{
|
||||
var decision = RsvpFlowRules.Evaluate(
|
||||
command.Status,
|
||||
session.Status,
|
||||
totalParticipants: 0,
|
||||
confirmedParticipants: 0);
|
||||
|
||||
if (decision.ShouldRevertSessionToConfirmationSent)
|
||||
{
|
||||
await connection.ExecuteAsync(
|
||||
"""
|
||||
UPDATE sessions
|
||||
SET status = @ConfirmationSent, updated_at = now()
|
||||
WHERE id = @SessionId AND status = @Confirmed
|
||||
""",
|
||||
new
|
||||
{
|
||||
command.SessionId,
|
||||
ConfirmationSent = SessionStatus.ConfirmationSent,
|
||||
Confirmed = SessionStatus.Confirmed
|
||||
},
|
||||
transaction);
|
||||
}
|
||||
|
||||
var gmRecipients = (await GetGmRecipientsAsync(connection, session.GroupId, transaction))
|
||||
.ToList();
|
||||
|
||||
await transaction.CommitAsync(ct);
|
||||
|
||||
if (gmRecipients.Count > 0)
|
||||
{
|
||||
await messenger.SendRsvpOutcomeAsync(
|
||||
new PlatformRsvpOutcomeNotification(
|
||||
PlatformRsvpOutcomeKind.GmPlayerDeclined,
|
||||
Group: null,
|
||||
gmRecipients,
|
||||
command.SessionId,
|
||||
session.Title,
|
||||
session.ScheduledAt,
|
||||
ActorDisplayName: command.User.DisplayName),
|
||||
ct);
|
||||
}
|
||||
|
||||
await messenger.AnswerInteractionAsync(
|
||||
new PlatformInteractionReply(command.InteractionId, decision.CallbackText),
|
||||
ct);
|
||||
}
|
||||
else
|
||||
{
|
||||
var counts = await connection.QuerySingleAsync<RsvpCounts>(
|
||||
"""
|
||||
SELECT
|
||||
count(*) AS Total,
|
||||
count(*) FILTER (WHERE rsvp_status = @Confirmed) AS Confirmed,
|
||||
count(*) FILTER (WHERE rsvp_status = @Declined) AS Declined
|
||||
FROM session_participants
|
||||
WHERE session_id = @SessionId AND is_gm = false
|
||||
AND registration_status = @Active
|
||||
""",
|
||||
new
|
||||
{
|
||||
command.SessionId,
|
||||
Confirmed = RsvpStatus.Confirmed,
|
||||
Declined = RsvpStatus.Declined,
|
||||
Active = ParticipantRegistrationStatus.Active
|
||||
},
|
||||
transaction);
|
||||
|
||||
var decision = RsvpFlowRules.Evaluate(command.Status, session.Status, counts.Total, counts.Confirmed);
|
||||
|
||||
if (decision.ShouldMarkSessionConfirmed)
|
||||
{
|
||||
await connection.ExecuteAsync(
|
||||
"""
|
||||
UPDATE sessions
|
||||
SET status = @Confirmed, updated_at = now()
|
||||
WHERE id = @SessionId
|
||||
""",
|
||||
new { command.SessionId, Confirmed = SessionStatus.Confirmed },
|
||||
transaction);
|
||||
}
|
||||
|
||||
var gmRecipients = decision.ShouldNotifyGm
|
||||
? (await GetGmRecipientsAsync(connection, session.GroupId, transaction)).ToList()
|
||||
: [];
|
||||
|
||||
await transaction.CommitAsync(ct);
|
||||
|
||||
if (decision.ShouldNotifyGroup)
|
||||
{
|
||||
await messenger.SendRsvpOutcomeAsync(
|
||||
new PlatformRsvpOutcomeNotification(
|
||||
PlatformRsvpOutcomeKind.GroupAllConfirmed,
|
||||
command.Group,
|
||||
[],
|
||||
command.SessionId,
|
||||
session.Title,
|
||||
session.ScheduledAt),
|
||||
ct);
|
||||
}
|
||||
|
||||
if (decision.ShouldNotifyGm && gmRecipients.Count > 0)
|
||||
{
|
||||
await messenger.SendRsvpOutcomeAsync(
|
||||
new PlatformRsvpOutcomeNotification(
|
||||
PlatformRsvpOutcomeKind.GmAllConfirmed,
|
||||
Group: null,
|
||||
gmRecipients,
|
||||
command.SessionId,
|
||||
session.Title,
|
||||
session.ScheduledAt),
|
||||
ct);
|
||||
}
|
||||
|
||||
await messenger.AnswerInteractionAsync(
|
||||
new PlatformInteractionReply(command.InteractionId, decision.CallbackText),
|
||||
ct);
|
||||
}
|
||||
|
||||
await UpdateConfirmationMessage(command, session, ct);
|
||||
}
|
||||
|
||||
private async Task UpdateConfirmationMessage(
|
||||
HandleRsvpCommand command,
|
||||
RsvpSessionContext session,
|
||||
CancellationToken ct)
|
||||
{
|
||||
try
|
||||
{
|
||||
await using var connection = await dataSource.OpenConnectionAsync(ct);
|
||||
|
||||
var participants = (await connection.QueryAsync<ParticipantRsvpRow>(
|
||||
"""
|
||||
SELECT p.platform AS Platform,
|
||||
p.external_user_id AS ExternalUserId,
|
||||
p.display_name AS DisplayName,
|
||||
p.external_username AS ExternalUsername,
|
||||
sp.rsvp_status AS RsvpStatus,
|
||||
sp.registration_status AS RegistrationStatus,
|
||||
sp.is_gm AS IsGm
|
||||
FROM session_participants sp
|
||||
JOIN players p ON p.id = sp.player_id
|
||||
WHERE sp.session_id = @SessionId
|
||||
AND sp.is_gm = false
|
||||
AND sp.registration_status = @Active
|
||||
ORDER BY sp.responded_at NULLS LAST
|
||||
""",
|
||||
new { command.SessionId, Active = ParticipantRegistrationStatus.Active }))
|
||||
.Select(ToParticipant)
|
||||
.ToList();
|
||||
|
||||
var disableActions = participants.Count > 0 &&
|
||||
participants.All(participant => participant.RsvpStatus == RsvpStatus.Confirmed);
|
||||
|
||||
await messenger.UpdateConfirmationRequestAsync(
|
||||
new PlatformRsvpMessageUpdate(
|
||||
new PlatformConfirmationRequest(
|
||||
command.Group,
|
||||
command.SessionId,
|
||||
session.Title,
|
||||
session.ScheduledAt,
|
||||
participants,
|
||||
command.ConfirmationMessage),
|
||||
disableActions),
|
||||
ct);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogWarning(ex, "Failed to update confirmation message for session {SessionId}", command.SessionId);
|
||||
}
|
||||
}
|
||||
|
||||
private static async Task<IEnumerable<PlatformUser>> GetGmRecipientsAsync(
|
||||
NpgsqlConnection connection,
|
||||
Guid groupId,
|
||||
NpgsqlTransaction transaction)
|
||||
{
|
||||
var rows = await connection.QueryAsync<RsvpRecipientRow>(
|
||||
"""
|
||||
SELECT DISTINCT
|
||||
p.platform AS Platform,
|
||||
p.external_user_id AS ExternalUserId,
|
||||
p.display_name AS DisplayName,
|
||||
p.external_username AS ExternalUsername
|
||||
FROM group_managers gm
|
||||
JOIN players p ON p.id = gm.player_id
|
||||
WHERE gm.group_id = @GroupId
|
||||
""",
|
||||
new { GroupId = groupId },
|
||||
transaction);
|
||||
|
||||
return rows.Select(row => new PlatformUser(
|
||||
ParsePlatform(row.Platform),
|
||||
row.ExternalUserId,
|
||||
row.DisplayName,
|
||||
row.ExternalUsername));
|
||||
}
|
||||
|
||||
private static PlatformSessionParticipant ToParticipant(ParticipantRsvpRow row) =>
|
||||
new(
|
||||
new PlatformUser(
|
||||
ParsePlatform(row.Platform),
|
||||
row.ExternalUserId,
|
||||
row.DisplayName,
|
||||
row.ExternalUsername),
|
||||
row.RsvpStatus,
|
||||
row.RegistrationStatus,
|
||||
row.IsGm);
|
||||
|
||||
private static PlatformKind ParsePlatform(string platform) =>
|
||||
Enum.Parse<PlatformKind>(platform, ignoreCase: true);
|
||||
}
|
||||
+5
-5
@@ -1,8 +1,8 @@
|
||||
using GmRelay.Shared.Domain;
|
||||
|
||||
namespace GmRelay.Bot.Features.Confirmation.HandleRsvp;
|
||||
namespace GmRelay.Shared.Features.Confirmation.HandleRsvp;
|
||||
|
||||
internal sealed record RsvpFlowDecision(
|
||||
public sealed record RsvpFlowDecision(
|
||||
string CallbackText,
|
||||
bool ShouldAlertGm,
|
||||
bool ShouldRevertSessionToConfirmationSent,
|
||||
@@ -10,7 +10,7 @@ internal sealed record RsvpFlowDecision(
|
||||
bool ShouldNotifyGroup,
|
||||
bool ShouldNotifyGm);
|
||||
|
||||
internal static class RsvpFlowRules
|
||||
public static class RsvpFlowRules
|
||||
{
|
||||
public static RsvpFlowDecision Evaluate(
|
||||
string requestedStatus,
|
||||
@@ -21,7 +21,7 @@ internal static class RsvpFlowRules
|
||||
if (requestedStatus == RsvpStatus.Declined)
|
||||
{
|
||||
return new RsvpFlowDecision(
|
||||
CallbackText: "\u0412\u044b \u043e\u0442\u043a\u0430\u0437\u0430\u043b\u0438\u0441\u044c \u043e\u0442 \u0443\u0447\u0430\u0441\u0442\u0438\u044f.",
|
||||
CallbackText: "Вы отказались от участия.",
|
||||
ShouldAlertGm: true,
|
||||
ShouldRevertSessionToConfirmationSent: currentSessionStatus == SessionStatus.Confirmed,
|
||||
ShouldMarkSessionConfirmed: false,
|
||||
@@ -32,7 +32,7 @@ internal static class RsvpFlowRules
|
||||
var everyoneConfirmed = confirmedParticipants == totalParticipants;
|
||||
|
||||
return new RsvpFlowDecision(
|
||||
CallbackText: "\u0412\u044b \u043f\u043e\u0434\u0442\u0432\u0435\u0440\u0434\u0438\u043b\u0438 \u0443\u0447\u0430\u0441\u0442\u0438\u0435!",
|
||||
CallbackText: "Вы подтвердили участие!",
|
||||
ShouldAlertGm: false,
|
||||
ShouldRevertSessionToConfirmationSent: false,
|
||||
ShouldMarkSessionConfirmed: everyoneConfirmed,
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
namespace GmRelay.Bot.Features.Confirmation.SendConfirmation;
|
||||
namespace GmRelay.Shared.Features.Confirmation.SendConfirmation;
|
||||
|
||||
public interface ISendConfirmationHandler
|
||||
{
|
||||
@@ -0,0 +1,217 @@
|
||||
using System.Globalization;
|
||||
using Dapper;
|
||||
using GmRelay.Shared.Domain;
|
||||
using GmRelay.Shared.Features.Notifications;
|
||||
using GmRelay.Shared.Platform;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Npgsql;
|
||||
|
||||
namespace GmRelay.Shared.Features.Confirmation.SendConfirmation;
|
||||
|
||||
internal sealed record ConfirmationSessionRow(
|
||||
Guid Id,
|
||||
string Title,
|
||||
DateTime ScheduledAt,
|
||||
Guid GroupId,
|
||||
string Platform,
|
||||
string ExternalGroupId,
|
||||
string DisplayName,
|
||||
string? ExternalChannelId,
|
||||
int? ThreadId,
|
||||
string NotificationMode);
|
||||
|
||||
internal sealed record ConfirmationParticipantRow(
|
||||
string Platform,
|
||||
string ExternalUserId,
|
||||
string DisplayName,
|
||||
string? ExternalUsername,
|
||||
string RsvpStatus,
|
||||
string RegistrationStatus,
|
||||
bool IsGm);
|
||||
|
||||
public sealed class SendConfirmationHandler(
|
||||
NpgsqlDataSource dataSource,
|
||||
IPlatformMessenger messenger,
|
||||
PlatformDirectNotificationSender directSender,
|
||||
ILogger<SendConfirmationHandler> logger) : ISendConfirmationHandler
|
||||
{
|
||||
public async Task HandleAsync(Guid sessionId, CancellationToken ct)
|
||||
{
|
||||
await using var connection = await dataSource.OpenConnectionAsync(ct);
|
||||
|
||||
var session = await connection.QuerySingleOrDefaultAsync<ConfirmationSessionRow>(
|
||||
"""
|
||||
SELECT s.id,
|
||||
s.title,
|
||||
s.scheduled_at AS ScheduledAt,
|
||||
s.group_id AS GroupId,
|
||||
g.platform AS Platform,
|
||||
g.external_group_id AS ExternalGroupId,
|
||||
g.name AS DisplayName,
|
||||
g.external_channel_id AS ExternalChannelId,
|
||||
s.thread_id AS ThreadId,
|
||||
s.notification_mode AS NotificationMode
|
||||
FROM sessions s
|
||||
JOIN game_groups g ON g.id = s.group_id
|
||||
WHERE s.id = @SessionId AND s.status = @Planned
|
||||
""",
|
||||
new { SessionId = sessionId, Planned = SessionStatus.Planned });
|
||||
|
||||
if (session is null)
|
||||
{
|
||||
logger.LogWarning("Session {SessionId} not found or not in Planned status", sessionId);
|
||||
return;
|
||||
}
|
||||
|
||||
var participants = (await connection.QueryAsync<ConfirmationParticipantRow>(
|
||||
"""
|
||||
SELECT p.platform AS Platform,
|
||||
p.external_user_id AS ExternalUserId,
|
||||
p.display_name AS DisplayName,
|
||||
p.external_username AS ExternalUsername,
|
||||
sp.rsvp_status AS RsvpStatus,
|
||||
sp.registration_status AS RegistrationStatus,
|
||||
sp.is_gm AS IsGm
|
||||
FROM session_participants sp
|
||||
JOIN players p ON p.id = sp.player_id
|
||||
WHERE sp.session_id = @SessionId
|
||||
AND sp.is_gm = false
|
||||
AND sp.registration_status = @Active
|
||||
ORDER BY sp.created_at ASC
|
||||
""",
|
||||
new { SessionId = sessionId, Active = ParticipantRegistrationStatus.Active }))
|
||||
.Select(ToParticipant)
|
||||
.ToList();
|
||||
|
||||
if (participants.Count == 0)
|
||||
{
|
||||
logger.LogWarning("Session {SessionId} has no non-GM participants", sessionId);
|
||||
return;
|
||||
}
|
||||
|
||||
var group = CreateGroup(session);
|
||||
var message = await messenger.SendConfirmationRequestAsync(
|
||||
new PlatformConfirmationRequest(
|
||||
group,
|
||||
session.Id,
|
||||
session.Title,
|
||||
session.ScheduledAt,
|
||||
participants),
|
||||
ct);
|
||||
|
||||
await connection.ExecuteAsync(
|
||||
"""
|
||||
UPDATE sessions
|
||||
SET status = @Status,
|
||||
confirmation_message_id = @MessageId,
|
||||
confirmation_sent_at = now(),
|
||||
updated_at = now()
|
||||
WHERE id = @SessionId
|
||||
AND confirmation_sent_at IS NULL
|
||||
""",
|
||||
new
|
||||
{
|
||||
SessionId = sessionId,
|
||||
Status = SessionStatus.ConfirmationSent,
|
||||
MessageId = TryGetTelegramMessageId(message)
|
||||
});
|
||||
|
||||
await PersistPlatformMessageAsync(
|
||||
connection,
|
||||
message,
|
||||
session.GroupId,
|
||||
session.Id,
|
||||
batchId: null,
|
||||
purpose: "confirmation");
|
||||
|
||||
var mode = SessionNotificationModeExtensions.FromDatabaseValue(session.NotificationMode);
|
||||
if (mode.ShouldSendDirectMessages())
|
||||
{
|
||||
await directSender.SendAsync(
|
||||
PlatformDirectSessionNotificationKind.ConfirmationRequest,
|
||||
participants.Select(p => p.User),
|
||||
session.Id,
|
||||
session.Title,
|
||||
session.ScheduledAt,
|
||||
joinLink: null,
|
||||
actorDisplayName: null,
|
||||
reason: null,
|
||||
ct);
|
||||
}
|
||||
|
||||
logger.LogInformation(
|
||||
"Confirmation sent for session {SessionId} ({Title}), platform={Platform}, message_id={MessageId}",
|
||||
sessionId,
|
||||
session.Title,
|
||||
message.Platform,
|
||||
message.ExternalMessageId);
|
||||
}
|
||||
|
||||
private static PlatformSessionParticipant ToParticipant(ConfirmationParticipantRow row) =>
|
||||
new(
|
||||
new PlatformUser(
|
||||
ParsePlatform(row.Platform),
|
||||
row.ExternalUserId,
|
||||
row.DisplayName,
|
||||
row.ExternalUsername),
|
||||
row.RsvpStatus,
|
||||
row.RegistrationStatus,
|
||||
row.IsGm);
|
||||
|
||||
private static PlatformGroup CreateGroup(ConfirmationSessionRow row) =>
|
||||
new(
|
||||
ParsePlatform(row.Platform),
|
||||
row.ExternalGroupId,
|
||||
row.DisplayName,
|
||||
row.ExternalChannelId,
|
||||
row.ThreadId?.ToString(CultureInfo.InvariantCulture));
|
||||
|
||||
private static PlatformKind ParsePlatform(string platform) =>
|
||||
Enum.Parse<PlatformKind>(platform, ignoreCase: true);
|
||||
|
||||
private static int? TryGetTelegramMessageId(PlatformMessageRef message) =>
|
||||
message.Platform == PlatformKind.Telegram &&
|
||||
int.TryParse(message.ExternalMessageId, NumberStyles.Integer, CultureInfo.InvariantCulture, out var messageId)
|
||||
? messageId
|
||||
: null;
|
||||
|
||||
private static Task PersistPlatformMessageAsync(
|
||||
NpgsqlConnection connection,
|
||||
PlatformMessageRef message,
|
||||
Guid groupId,
|
||||
Guid? sessionId,
|
||||
Guid? batchId,
|
||||
string purpose) =>
|
||||
connection.ExecuteAsync(
|
||||
"""
|
||||
INSERT INTO platform_messages (
|
||||
platform,
|
||||
group_id,
|
||||
batch_id,
|
||||
session_id,
|
||||
external_channel_id,
|
||||
external_thread_id,
|
||||
external_message_id,
|
||||
purpose)
|
||||
VALUES (
|
||||
@Platform,
|
||||
@GroupId,
|
||||
@BatchId,
|
||||
@SessionId,
|
||||
@ExternalChannelId,
|
||||
@ExternalThreadId,
|
||||
@ExternalMessageId,
|
||||
@Purpose)
|
||||
""",
|
||||
new
|
||||
{
|
||||
Platform = message.Platform.ToString(),
|
||||
GroupId = groupId,
|
||||
BatchId = batchId,
|
||||
SessionId = sessionId,
|
||||
ExternalChannelId = message.ExternalGroupId,
|
||||
message.ExternalThreadId,
|
||||
message.ExternalMessageId,
|
||||
Purpose = purpose
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
using GmRelay.Shared.Platform;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace GmRelay.Shared.Features.Notifications;
|
||||
|
||||
public sealed class PlatformDirectNotificationSender(
|
||||
IPlatformMessenger messenger,
|
||||
ILogger<PlatformDirectNotificationSender> logger)
|
||||
{
|
||||
public async Task SendAsync(
|
||||
PlatformDirectSessionNotificationKind kind,
|
||||
IEnumerable<PlatformUser> recipients,
|
||||
Guid sessionId,
|
||||
string title,
|
||||
DateTime scheduledAt,
|
||||
string? joinLink,
|
||||
string? actorDisplayName,
|
||||
string? reason,
|
||||
CancellationToken ct)
|
||||
{
|
||||
foreach (var recipient in recipients)
|
||||
{
|
||||
try
|
||||
{
|
||||
await messenger.SendDirectSessionNotificationAsync(
|
||||
new PlatformDirectSessionNotification(
|
||||
kind,
|
||||
recipient,
|
||||
sessionId,
|
||||
title,
|
||||
scheduledAt,
|
||||
joinLink,
|
||||
actorDisplayName,
|
||||
reason),
|
||||
ct);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogWarning(
|
||||
ex,
|
||||
"Failed to send {NotificationKind} notification for session {SessionId} to {Platform} user {ExternalUserId} ({DisplayName})",
|
||||
kind,
|
||||
sessionId,
|
||||
recipient.Platform,
|
||||
recipient.ExternalUserId,
|
||||
recipient.DisplayName);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
namespace GmRelay.Bot.Features.Reminders.SendJoinLink;
|
||||
namespace GmRelay.Shared.Features.Reminders.SendJoinLink;
|
||||
|
||||
public interface ISendJoinLinkHandler
|
||||
{
|
||||
@@ -0,0 +1,228 @@
|
||||
using System.Globalization;
|
||||
using Dapper;
|
||||
using GmRelay.Shared.Domain;
|
||||
using GmRelay.Shared.Features.Notifications;
|
||||
using GmRelay.Shared.Platform;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Npgsql;
|
||||
|
||||
namespace GmRelay.Shared.Features.Reminders.SendJoinLink;
|
||||
|
||||
internal sealed record JoinLinkSessionRow(
|
||||
Guid Id,
|
||||
Guid GroupId,
|
||||
string Title,
|
||||
string JoinLink,
|
||||
DateTime ScheduledAt,
|
||||
string Platform,
|
||||
string ExternalGroupId,
|
||||
string DisplayName,
|
||||
string? ExternalChannelId,
|
||||
int? ThreadId,
|
||||
string NotificationMode);
|
||||
|
||||
internal sealed record JoinLinkPlayerRow(
|
||||
string Platform,
|
||||
string ExternalUserId,
|
||||
string DisplayName,
|
||||
string? ExternalUsername,
|
||||
string RsvpStatus,
|
||||
string RegistrationStatus,
|
||||
bool IsGm);
|
||||
|
||||
public sealed class SendJoinLinkHandler(
|
||||
NpgsqlDataSource dataSource,
|
||||
IPlatformMessenger messenger,
|
||||
PlatformDirectNotificationSender directSender,
|
||||
ILogger<SendJoinLinkHandler> logger) : ISendJoinLinkHandler
|
||||
{
|
||||
public async Task HandleAsync(Guid sessionId, CancellationToken ct)
|
||||
{
|
||||
await using var connection = await dataSource.OpenConnectionAsync(ct);
|
||||
|
||||
var session = await connection.QuerySingleOrDefaultAsync<JoinLinkSessionRow>(
|
||||
"""
|
||||
SELECT s.id,
|
||||
s.group_id AS GroupId,
|
||||
s.title,
|
||||
s.join_link AS JoinLink,
|
||||
s.scheduled_at AS ScheduledAt,
|
||||
g.platform AS Platform,
|
||||
g.external_group_id AS ExternalGroupId,
|
||||
g.name AS DisplayName,
|
||||
g.external_channel_id AS ExternalChannelId,
|
||||
s.thread_id AS ThreadId,
|
||||
s.notification_mode AS NotificationMode
|
||||
FROM sessions s
|
||||
JOIN game_groups g ON g.id = s.group_id
|
||||
WHERE s.id = @SessionId
|
||||
AND s.status = @Confirmed
|
||||
AND (
|
||||
(g.platform = 'Telegram' AND s.link_message_id IS NULL)
|
||||
OR (
|
||||
g.platform <> 'Telegram'
|
||||
AND NOT EXISTS (
|
||||
SELECT 1
|
||||
FROM platform_messages pm
|
||||
WHERE pm.session_id = s.id
|
||||
AND pm.platform = g.platform
|
||||
AND pm.purpose = 'join_link'
|
||||
)
|
||||
)
|
||||
)
|
||||
""",
|
||||
new { SessionId = sessionId, Confirmed = SessionStatus.Confirmed });
|
||||
|
||||
if (session is null)
|
||||
{
|
||||
logger.LogWarning("Session {SessionId} not eligible for join link", sessionId);
|
||||
return;
|
||||
}
|
||||
|
||||
var players = (await connection.QueryAsync<JoinLinkPlayerRow>(
|
||||
"""
|
||||
SELECT p.platform AS Platform,
|
||||
p.external_user_id AS ExternalUserId,
|
||||
p.display_name AS DisplayName,
|
||||
p.external_username AS ExternalUsername,
|
||||
sp.rsvp_status AS RsvpStatus,
|
||||
sp.registration_status AS RegistrationStatus,
|
||||
sp.is_gm AS IsGm
|
||||
FROM session_participants sp
|
||||
JOIN players p ON p.id = sp.player_id
|
||||
WHERE sp.session_id = @SessionId
|
||||
AND sp.rsvp_status = @Confirmed
|
||||
AND sp.registration_status = @Active
|
||||
ORDER BY sp.created_at ASC
|
||||
""",
|
||||
new
|
||||
{
|
||||
SessionId = sessionId,
|
||||
Confirmed = RsvpStatus.Confirmed,
|
||||
Active = ParticipantRegistrationStatus.Active
|
||||
}))
|
||||
.Select(ToParticipant)
|
||||
.ToList();
|
||||
|
||||
var group = CreateGroup(session);
|
||||
var message = await messenger.SendJoinLinkNotificationAsync(
|
||||
new PlatformJoinLinkNotification(
|
||||
group,
|
||||
session.Id,
|
||||
session.Title,
|
||||
session.ScheduledAt,
|
||||
session.JoinLink,
|
||||
players),
|
||||
ct);
|
||||
|
||||
await connection.ExecuteAsync(
|
||||
"""
|
||||
UPDATE sessions
|
||||
SET link_message_id = @MessageId, updated_at = now()
|
||||
WHERE id = @SessionId
|
||||
""",
|
||||
new
|
||||
{
|
||||
SessionId = sessionId,
|
||||
MessageId = TryGetTelegramMessageId(message)
|
||||
});
|
||||
|
||||
await PersistPlatformMessageAsync(
|
||||
connection,
|
||||
message,
|
||||
session.GroupId,
|
||||
session.Id,
|
||||
batchId: null,
|
||||
purpose: "join_link");
|
||||
|
||||
var mode = SessionNotificationModeExtensions.FromDatabaseValue(session.NotificationMode);
|
||||
if (mode.ShouldSendDirectMessages())
|
||||
{
|
||||
await directSender.SendAsync(
|
||||
PlatformDirectSessionNotificationKind.JoinLink,
|
||||
players.Select(p => p.User),
|
||||
session.Id,
|
||||
session.Title,
|
||||
session.ScheduledAt,
|
||||
session.JoinLink,
|
||||
actorDisplayName: null,
|
||||
reason: null,
|
||||
ct);
|
||||
}
|
||||
|
||||
logger.LogInformation(
|
||||
"Join link sent for session {SessionId} ({Title}), platform={Platform}, message_id={MessageId}",
|
||||
sessionId,
|
||||
session.Title,
|
||||
message.Platform,
|
||||
message.ExternalMessageId);
|
||||
}
|
||||
|
||||
private static PlatformSessionParticipant ToParticipant(JoinLinkPlayerRow row) =>
|
||||
new(
|
||||
new PlatformUser(
|
||||
ParsePlatform(row.Platform),
|
||||
row.ExternalUserId,
|
||||
row.DisplayName,
|
||||
row.ExternalUsername),
|
||||
row.RsvpStatus,
|
||||
row.RegistrationStatus,
|
||||
row.IsGm);
|
||||
|
||||
private static PlatformGroup CreateGroup(JoinLinkSessionRow row) =>
|
||||
new(
|
||||
ParsePlatform(row.Platform),
|
||||
row.ExternalGroupId,
|
||||
row.DisplayName,
|
||||
row.ExternalChannelId,
|
||||
row.ThreadId?.ToString(CultureInfo.InvariantCulture));
|
||||
|
||||
private static PlatformKind ParsePlatform(string platform) =>
|
||||
Enum.Parse<PlatformKind>(platform, ignoreCase: true);
|
||||
|
||||
private static int? TryGetTelegramMessageId(PlatformMessageRef message) =>
|
||||
message.Platform == PlatformKind.Telegram &&
|
||||
int.TryParse(message.ExternalMessageId, NumberStyles.Integer, CultureInfo.InvariantCulture, out var messageId)
|
||||
? messageId
|
||||
: null;
|
||||
|
||||
private static Task PersistPlatformMessageAsync(
|
||||
NpgsqlConnection connection,
|
||||
PlatformMessageRef message,
|
||||
Guid groupId,
|
||||
Guid? sessionId,
|
||||
Guid? batchId,
|
||||
string purpose) =>
|
||||
connection.ExecuteAsync(
|
||||
"""
|
||||
INSERT INTO platform_messages (
|
||||
platform,
|
||||
group_id,
|
||||
batch_id,
|
||||
session_id,
|
||||
external_channel_id,
|
||||
external_thread_id,
|
||||
external_message_id,
|
||||
purpose)
|
||||
VALUES (
|
||||
@Platform,
|
||||
@GroupId,
|
||||
@BatchId,
|
||||
@SessionId,
|
||||
@ExternalChannelId,
|
||||
@ExternalThreadId,
|
||||
@ExternalMessageId,
|
||||
@Purpose)
|
||||
""",
|
||||
new
|
||||
{
|
||||
Platform = message.Platform.ToString(),
|
||||
GroupId = groupId,
|
||||
BatchId = batchId,
|
||||
SessionId = sessionId,
|
||||
ExternalChannelId = message.ExternalGroupId,
|
||||
message.ExternalThreadId,
|
||||
message.ExternalMessageId,
|
||||
Purpose = purpose
|
||||
});
|
||||
}
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
namespace GmRelay.Bot.Features.Reminders.SendOneHourReminder;
|
||||
namespace GmRelay.Shared.Features.Reminders.SendOneHourReminder;
|
||||
|
||||
public interface ISendOneHourReminderHandler
|
||||
{
|
||||
+38
-18
@@ -1,27 +1,35 @@
|
||||
using Dapper;
|
||||
using GmRelay.Bot.Features.Notifications;
|
||||
using GmRelay.Shared.Domain;
|
||||
using GmRelay.Shared.Features.Notifications;
|
||||
using GmRelay.Shared.Platform;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Npgsql;
|
||||
|
||||
namespace GmRelay.Bot.Features.Reminders.SendOneHourReminder;
|
||||
namespace GmRelay.Shared.Features.Reminders.SendOneHourReminder;
|
||||
|
||||
internal sealed record OneHourReminderSession(
|
||||
internal sealed record OneHourReminderSessionRow(
|
||||
Guid Id,
|
||||
string Title,
|
||||
string JoinLink,
|
||||
DateTime ScheduledAt,
|
||||
string NotificationMode);
|
||||
|
||||
internal sealed record OneHourReminderRecipientRow(
|
||||
string Platform,
|
||||
string ExternalUserId,
|
||||
string DisplayName,
|
||||
string? ExternalUsername);
|
||||
|
||||
public sealed class SendOneHourReminderHandler(
|
||||
NpgsqlDataSource dataSource,
|
||||
DirectSessionNotificationSender directSender,
|
||||
PlatformDirectNotificationSender directSender,
|
||||
ILogger<SendOneHourReminderHandler> logger) : ISendOneHourReminderHandler
|
||||
{
|
||||
public async Task HandleAsync(Guid sessionId, CancellationToken ct)
|
||||
{
|
||||
await using var connection = await dataSource.OpenConnectionAsync(ct);
|
||||
|
||||
var session = await connection.QuerySingleOrDefaultAsync<OneHourReminderSession>(
|
||||
var session = await connection.QuerySingleOrDefaultAsync<OneHourReminderSessionRow>(
|
||||
"""
|
||||
SELECT id,
|
||||
title,
|
||||
@@ -46,10 +54,12 @@ public sealed class SendOneHourReminderHandler(
|
||||
return;
|
||||
}
|
||||
|
||||
var recipients = (await connection.QueryAsync<DirectNotificationRecipient>(
|
||||
var recipients = (await connection.QueryAsync<OneHourReminderRecipientRow>(
|
||||
"""
|
||||
SELECT p.telegram_id AS TelegramId,
|
||||
p.display_name AS DisplayName
|
||||
SELECT p.platform AS Platform,
|
||||
p.external_user_id AS ExternalUserId,
|
||||
p.display_name AS DisplayName,
|
||||
p.external_username AS ExternalUsername
|
||||
FROM session_participants sp
|
||||
JOIN players p ON p.id = sp.player_id
|
||||
WHERE sp.session_id = @SessionId
|
||||
@@ -62,20 +72,27 @@ public sealed class SendOneHourReminderHandler(
|
||||
SessionId = sessionId,
|
||||
Active = ParticipantRegistrationStatus.Active,
|
||||
Declined = RsvpStatus.Declined
|
||||
})).ToList();
|
||||
}))
|
||||
.Select(row => new PlatformUser(
|
||||
ParsePlatform(row.Platform),
|
||||
row.ExternalUserId,
|
||||
row.DisplayName,
|
||||
row.ExternalUsername))
|
||||
.ToList();
|
||||
|
||||
var mode = SessionNotificationModeExtensions.FromDatabaseValue(session.NotificationMode);
|
||||
if (mode.ShouldSendDirectMessages() && recipients.Count > 0)
|
||||
{
|
||||
var text = $"""
|
||||
⏰ <b>Игра начнётся примерно через 1 час</b>
|
||||
|
||||
📌 <b>{System.Net.WebUtility.HtmlEncode(session.Title)}</b>
|
||||
📅 {session.ScheduledAt.FormatMoscow()} (МСК)
|
||||
🔗 {System.Net.WebUtility.HtmlEncode(session.JoinLink)}
|
||||
""";
|
||||
|
||||
await directSender.SendAsync(recipients, text, "one-hour-reminder", session.Id, ct);
|
||||
await directSender.SendAsync(
|
||||
PlatformDirectSessionNotificationKind.OneHourReminder,
|
||||
recipients,
|
||||
session.Id,
|
||||
session.Title,
|
||||
session.ScheduledAt,
|
||||
session.JoinLink,
|
||||
actorDisplayName: null,
|
||||
reason: null,
|
||||
ct);
|
||||
}
|
||||
|
||||
await connection.ExecuteAsync(
|
||||
@@ -94,4 +111,7 @@ public sealed class SendOneHourReminderHandler(
|
||||
session.Title,
|
||||
session.NotificationMode);
|
||||
}
|
||||
|
||||
private static PlatformKind ParsePlatform(string platform) =>
|
||||
Enum.Parse<PlatformKind>(platform, ignoreCase: true);
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
using GmRelay.Shared.Domain;
|
||||
using GmRelay.Shared.Platform;
|
||||
|
||||
namespace GmRelay.Shared.Features.Sessions.CreateSession;
|
||||
|
||||
public sealed record CreateSessionCommand(
|
||||
PlatformUser User,
|
||||
PlatformGroup Group,
|
||||
string Title,
|
||||
string Link,
|
||||
IReadOnlyList<DateTimeOffset> ScheduledTimes,
|
||||
int? MaxPlayers,
|
||||
string? ImageReference,
|
||||
GameSystem? System = null,
|
||||
string? Description = null,
|
||||
string? Format = null,
|
||||
int? DurationMinutes = null,
|
||||
bool IsOneShot = false);
|
||||
@@ -0,0 +1,168 @@
|
||||
using Dapper;
|
||||
using GmRelay.Shared.Domain;
|
||||
using GmRelay.Shared.Platform;
|
||||
using GmRelay.Shared.Rendering;
|
||||
using Npgsql;
|
||||
|
||||
namespace GmRelay.Shared.Features.Sessions.CreateSession;
|
||||
|
||||
internal sealed record SessionCreationGroupAccessDto(Guid GroupId, bool CanManage);
|
||||
|
||||
public sealed class CreateSessionHandler(
|
||||
NpgsqlDataSource dataSource)
|
||||
{
|
||||
public async Task<CreateSessionResult> HandleAsync(CreateSessionCommand command, CancellationToken ct)
|
||||
{
|
||||
await using var connection = await dataSource.OpenConnectionAsync(ct);
|
||||
await using var transaction = await connection.BeginTransactionAsync(ct);
|
||||
|
||||
var transactionCommitted = false;
|
||||
try
|
||||
{
|
||||
var platform = command.User.Platform.ToString();
|
||||
var externalUserId = command.User.ExternalUserId;
|
||||
var displayName = command.User.DisplayName;
|
||||
var externalUsername = command.User.ExternalUsername;
|
||||
|
||||
await connection.ExecuteAsync(
|
||||
"""
|
||||
INSERT INTO players (display_name, platform, external_user_id, external_username)
|
||||
VALUES (@Name, @Platform, @ExternalId, @Username)
|
||||
ON CONFLICT (platform, external_user_id)
|
||||
WHERE platform IS NOT NULL AND external_user_id IS NOT NULL
|
||||
DO UPDATE
|
||||
SET display_name = EXCLUDED.display_name,
|
||||
external_username = EXCLUDED.external_username;
|
||||
""",
|
||||
new { ExternalId = externalUserId, Name = displayName, Username = externalUsername, Platform = platform },
|
||||
transaction);
|
||||
|
||||
var existingGroup = await connection.QuerySingleOrDefaultAsync<SessionCreationGroupAccessDto>(
|
||||
"""
|
||||
SELECT g.id AS GroupId,
|
||||
EXISTS (
|
||||
SELECT 1
|
||||
FROM group_managers gm
|
||||
JOIN players p ON p.id = gm.player_id
|
||||
WHERE gm.group_id = g.id
|
||||
AND p.platform = @Platform
|
||||
AND p.external_user_id = @ExternalGmId
|
||||
) AS CanManage
|
||||
FROM game_groups g
|
||||
WHERE g.platform = @Platform
|
||||
AND g.external_group_id = @ExternalGroupId
|
||||
""",
|
||||
new { Platform = platform, ExternalGroupId = command.Group.ExternalGroupId, ExternalGmId = externalUserId },
|
||||
transaction);
|
||||
|
||||
Guid groupId;
|
||||
if (existingGroup is null)
|
||||
{
|
||||
groupId = await connection.ExecuteScalarAsync<Guid>(
|
||||
"""
|
||||
INSERT INTO game_groups (name, platform, external_group_id, external_channel_id)
|
||||
VALUES (@ChatName, @Platform, @ExternalGroupId, @ExternalChannelId)
|
||||
RETURNING id;
|
||||
""",
|
||||
new
|
||||
{
|
||||
Platform = platform,
|
||||
ExternalGroupId = command.Group.ExternalGroupId,
|
||||
ExternalChannelId = command.Group.ExternalChannelId,
|
||||
ChatName = command.Group.DisplayName
|
||||
},
|
||||
transaction);
|
||||
|
||||
await connection.ExecuteAsync(
|
||||
"""
|
||||
INSERT INTO group_managers (group_id, player_id, role)
|
||||
SELECT @GroupId, p.id, @OwnerRole
|
||||
FROM players p
|
||||
WHERE p.platform = @Platform
|
||||
AND p.external_user_id = @ExternalGmId
|
||||
ON CONFLICT (group_id, player_id) DO NOTHING
|
||||
""",
|
||||
new { GroupId = groupId, ExternalGmId = externalUserId, OwnerRole = GroupManagerRoleExtensions.OwnerValue },
|
||||
transaction);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!existingGroup.CanManage)
|
||||
{
|
||||
await transaction.RollbackAsync(ct);
|
||||
return new CreateSessionResult(
|
||||
false,
|
||||
"⛔ Только owner или co-GM этой группы может создавать игровые сессии.",
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
Array.Empty<string>());
|
||||
}
|
||||
|
||||
groupId = existingGroup.GroupId;
|
||||
await connection.ExecuteAsync(
|
||||
"""
|
||||
UPDATE game_groups
|
||||
SET name = @ChatName
|
||||
WHERE id = @GroupId
|
||||
""",
|
||||
new { ChatName = command.Group.DisplayName, GroupId = groupId },
|
||||
transaction);
|
||||
}
|
||||
|
||||
var batchId = Guid.NewGuid();
|
||||
var sessions = new List<SessionBatchDto>();
|
||||
var orderedTimes = command.ScheduledTimes.OrderBy(v => v).ToList();
|
||||
|
||||
foreach (var scheduledAt in orderedTimes)
|
||||
{
|
||||
var sessionId = await connection.ExecuteScalarAsync<Guid>(
|
||||
"""
|
||||
INSERT INTO sessions (batch_id, group_id, title, join_link, scheduled_at, status, max_players, system, description, format, duration_minutes, is_one_shot, cover_image_url)
|
||||
VALUES (@BatchId, @GroupId, @Title, @Link, @ScheduledAt, @Status, @MaxPlayers, @System, @Description, @Format, @DurationMinutes, @IsOneShot, @CoverImageUrl)
|
||||
RETURNING id;
|
||||
""",
|
||||
new
|
||||
{
|
||||
BatchId = batchId,
|
||||
GroupId = groupId,
|
||||
command.Title,
|
||||
Link = command.Link,
|
||||
ScheduledAt = scheduledAt,
|
||||
Status = SessionStatus.Planned,
|
||||
MaxPlayers = command.MaxPlayers,
|
||||
System = command.System?.ToString(),
|
||||
command.Description,
|
||||
command.Format,
|
||||
DurationMinutes = command.DurationMinutes,
|
||||
IsOneShot = command.IsOneShot,
|
||||
CoverImageUrl = command.ImageReference
|
||||
},
|
||||
transaction);
|
||||
|
||||
sessions.Add(new SessionBatchDto(sessionId, scheduledAt.UtcDateTime, SessionStatus.Planned, command.MaxPlayers, command.Link));
|
||||
}
|
||||
|
||||
await transaction.CommitAsync(ct);
|
||||
transactionCommitted = true;
|
||||
|
||||
var view = SessionBatchViewBuilder.Build(command.Title, sessions, Array.Empty<ParticipantBatchDto>());
|
||||
|
||||
return new CreateSessionResult(
|
||||
true,
|
||||
null,
|
||||
view,
|
||||
batchId,
|
||||
groupId,
|
||||
Array.Empty<string>());
|
||||
}
|
||||
catch
|
||||
{
|
||||
if (!transactionCommitted)
|
||||
{
|
||||
await transaction.RollbackAsync(ct);
|
||||
}
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
using GmRelay.Shared.Rendering;
|
||||
|
||||
namespace GmRelay.Shared.Features.Sessions.CreateSession;
|
||||
|
||||
public sealed record CreateSessionResult(
|
||||
bool Success,
|
||||
string? ErrorMessage,
|
||||
SessionBatchViewModel? View,
|
||||
Guid? BatchId,
|
||||
Guid? GroupId,
|
||||
IReadOnlyList<string> Warnings);
|
||||
@@ -13,7 +13,12 @@ public sealed record JoinSessionCommand(
|
||||
PlatformUser User,
|
||||
string InteractionId,
|
||||
PlatformGroup Group,
|
||||
PlatformMessageRef ScheduleMessage);
|
||||
PlatformMessageRef ScheduleMessage,
|
||||
bool DeferScheduleUpdate = false);
|
||||
|
||||
public sealed record SessionInteractionResult(
|
||||
string ReplyText,
|
||||
SessionBatchViewModel? UpdatedView = null);
|
||||
|
||||
// DTOs for AOT compilation
|
||||
internal sealed record JoinSessionBatchDto(Guid BatchId, string Title, string Status, int? MaxPlayers);
|
||||
@@ -24,7 +29,7 @@ public sealed class JoinSessionHandler(
|
||||
IScheduleMessageUpdateLock scheduleUpdateLock,
|
||||
ILogger<JoinSessionHandler> logger)
|
||||
{
|
||||
public async Task HandleAsync(JoinSessionCommand command, CancellationToken ct)
|
||||
public async Task<SessionInteractionResult> HandleAsync(JoinSessionCommand command, CancellationToken ct)
|
||||
{
|
||||
await using var updateLock = await scheduleUpdateLock.AcquireAsync(command.ScheduleMessage, ct);
|
||||
await using var connection = await dataSource.OpenConnectionAsync(ct);
|
||||
@@ -35,30 +40,19 @@ public sealed class JoinSessionHandler(
|
||||
{
|
||||
// 1. Убеждаемся, что игрок есть в базе
|
||||
var platform = command.User.Platform.ToString();
|
||||
var legacyTelegramId = command.User.Platform == PlatformKind.Telegram
|
||||
? long.Parse(command.User.ExternalUserId, CultureInfo.InvariantCulture)
|
||||
: (long?)null;
|
||||
var legacyTelegramUsername = command.User.Platform == PlatformKind.Telegram
|
||||
? command.User.ExternalUsername
|
||||
: null;
|
||||
|
||||
var playerId = await connection.ExecuteScalarAsync<Guid>(
|
||||
@"INSERT INTO players (telegram_id, display_name, telegram_username, platform, external_user_id, external_username)
|
||||
VALUES (@LegacyTelegramId, @Name, @LegacyTelegramUsername, @Platform, @ExternalUserId, @ExternalUsername)
|
||||
@"INSERT INTO players (display_name, platform, external_user_id, external_username)
|
||||
VALUES (@Name, @Platform, @ExternalUserId, @ExternalUsername)
|
||||
ON CONFLICT (platform, external_user_id)
|
||||
WHERE platform IS NOT NULL AND external_user_id IS NOT NULL
|
||||
DO UPDATE
|
||||
SET display_name = EXCLUDED.display_name,
|
||||
telegram_username = COALESCE(EXCLUDED.telegram_username, players.telegram_username),
|
||||
platform = EXCLUDED.platform,
|
||||
external_user_id = EXCLUDED.external_user_id,
|
||||
external_username = EXCLUDED.external_username
|
||||
RETURNING id;",
|
||||
new
|
||||
{
|
||||
LegacyTelegramId = legacyTelegramId,
|
||||
Name = command.User.DisplayName,
|
||||
LegacyTelegramUsername = legacyTelegramUsername,
|
||||
Platform = platform,
|
||||
command.User.ExternalUserId,
|
||||
command.User.ExternalUsername
|
||||
@@ -77,15 +71,13 @@ public sealed class JoinSessionHandler(
|
||||
if (batchInfo is null)
|
||||
{
|
||||
await transaction.RollbackAsync(ct);
|
||||
await AnswerAsync(command.InteractionId, "Сессия не найдена.", ct);
|
||||
return;
|
||||
return await AnswerAsync(command.InteractionId, "Сессия не найдена.", ct);
|
||||
}
|
||||
|
||||
if (SessionStatus.IsCancelled(batchInfo.Status))
|
||||
{
|
||||
await transaction.RollbackAsync(ct);
|
||||
await AnswerAsync(command.InteractionId, "Сессия уже отменена.", ct);
|
||||
return;
|
||||
return await AnswerAsync(command.InteractionId, "Сессия уже отменена.", ct);
|
||||
}
|
||||
|
||||
var existingRegistrationStatus = await connection.ExecuteScalarAsync<string?>(
|
||||
@@ -105,8 +97,7 @@ public sealed class JoinSessionHandler(
|
||||
var alreadyText = existingRegistrationStatus == ParticipantRegistrationStatus.Waitlisted
|
||||
? "Вы уже в листе ожидания!"
|
||||
: "Вы уже записаны!";
|
||||
await AnswerAsync(command.InteractionId, alreadyText, ct);
|
||||
return;
|
||||
return await AnswerAsync(command.InteractionId, alreadyText, ct);
|
||||
}
|
||||
|
||||
var activeParticipants = await connection.ExecuteScalarAsync<int>(
|
||||
@@ -139,8 +130,7 @@ public sealed class JoinSessionHandler(
|
||||
if (inserted == 0)
|
||||
{
|
||||
await transaction.RollbackAsync(ct);
|
||||
await AnswerAsync(command.InteractionId, "Вы уже записаны!", ct);
|
||||
return;
|
||||
return await AnswerAsync(command.InteractionId, "Вы уже записаны!", ct);
|
||||
}
|
||||
|
||||
// Загружаем весь батч для перерисовки
|
||||
@@ -154,7 +144,7 @@ public sealed class JoinSessionHandler(
|
||||
var batchParticipants = await connection.QueryAsync<ParticipantBatchDto>(
|
||||
@"SELECT sp.session_id as SessionId,
|
||||
p.display_name as DisplayName,
|
||||
COALESCE(p.external_username, p.telegram_username) as TelegramUsername,
|
||||
p.external_username as TelegramUsername,
|
||||
sp.registration_status as RegistrationStatus
|
||||
FROM session_participants sp
|
||||
JOIN players p ON sp.player_id = p.id
|
||||
@@ -168,17 +158,20 @@ public sealed class JoinSessionHandler(
|
||||
|
||||
// 4. Перерисовываем сообщение
|
||||
var view = SessionBatchViewBuilder.Build(batchInfo.Title, batchSessions.ToList(), batchParticipants.ToList());
|
||||
await messenger.UpdateScheduleAsync(
|
||||
new PlatformScheduleMessage(
|
||||
command.Group,
|
||||
view,
|
||||
command.ScheduleMessage),
|
||||
ct);
|
||||
if (!command.DeferScheduleUpdate)
|
||||
{
|
||||
await messenger.UpdateScheduleAsync(
|
||||
new PlatformScheduleMessage(
|
||||
command.Group,
|
||||
view,
|
||||
command.ScheduleMessage),
|
||||
ct);
|
||||
}
|
||||
|
||||
var callbackText = registrationStatus == ParticipantRegistrationStatus.Waitlisted
|
||||
? "Основной состав заполнен. Вы добавлены в лист ожидания."
|
||||
: "Вы успешно записаны!";
|
||||
await AnswerAsync(command.InteractionId, callbackText, ct);
|
||||
return await AnswerAsync(command.InteractionId, callbackText, ct, view);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -191,10 +184,17 @@ public sealed class JoinSessionHandler(
|
||||
var errorText = transactionCommitted
|
||||
? "Регистрация сохранена, но не удалось обновить сообщение расписания."
|
||||
: "Произошла ошибка при регистрации.";
|
||||
await AnswerAsync(command.InteractionId, errorText, ct);
|
||||
return await AnswerAsync(command.InteractionId, errorText, ct);
|
||||
}
|
||||
}
|
||||
|
||||
private Task AnswerAsync(string interactionId, string text, CancellationToken ct) =>
|
||||
messenger.AnswerInteractionAsync(new PlatformInteractionReply(interactionId, text), ct);
|
||||
private async Task<SessionInteractionResult> AnswerAsync(
|
||||
string interactionId,
|
||||
string text,
|
||||
CancellationToken ct,
|
||||
SessionBatchViewModel? updatedView = null)
|
||||
{
|
||||
await messenger.AnswerInteractionAsync(new PlatformInteractionReply(interactionId, text), ct);
|
||||
return new SessionInteractionResult(text, updatedView);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,7 +12,8 @@ public sealed record LeaveSessionCommand(
|
||||
PlatformUser User,
|
||||
string InteractionId,
|
||||
PlatformGroup Group,
|
||||
PlatformMessageRef ScheduleMessage);
|
||||
PlatformMessageRef ScheduleMessage,
|
||||
bool DeferScheduleUpdate = false);
|
||||
|
||||
internal sealed record LeaveSessionInfoDto(string Title, Guid BatchId, string Status, int? MaxPlayers);
|
||||
internal sealed record LeaveSessionParticipantDto(Guid ParticipantRowId, string DisplayName, string RegistrationStatus);
|
||||
@@ -24,7 +25,7 @@ public sealed class LeaveSessionHandler(
|
||||
IScheduleMessageUpdateLock scheduleUpdateLock,
|
||||
ILogger<LeaveSessionHandler> logger)
|
||||
{
|
||||
public async Task HandleAsync(LeaveSessionCommand command, CancellationToken ct)
|
||||
public async Task<SessionInteractionResult> HandleAsync(LeaveSessionCommand command, CancellationToken ct)
|
||||
{
|
||||
await using var updateLock = await scheduleUpdateLock.AcquireAsync(command.ScheduleMessage, ct);
|
||||
await using var connection = await dataSource.OpenConnectionAsync(ct);
|
||||
@@ -49,15 +50,13 @@ public sealed class LeaveSessionHandler(
|
||||
if (session is null)
|
||||
{
|
||||
await transaction.RollbackAsync(ct);
|
||||
await AnswerAsync(command.InteractionId, "Сессия не найдена.", ct);
|
||||
return;
|
||||
return await AnswerAsync(command.InteractionId, "Сессия не найдена.", ct);
|
||||
}
|
||||
|
||||
if (SessionStatus.IsCancelled(session.Status))
|
||||
{
|
||||
await transaction.RollbackAsync(ct);
|
||||
await AnswerAsync(command.InteractionId, "Сессия уже отменена.", ct);
|
||||
return;
|
||||
return await AnswerAsync(command.InteractionId, "Сессия уже отменена.", ct);
|
||||
}
|
||||
|
||||
var platform = command.User.Platform.ToString();
|
||||
@@ -81,8 +80,7 @@ public sealed class LeaveSessionHandler(
|
||||
if (participant is null)
|
||||
{
|
||||
await transaction.RollbackAsync(ct);
|
||||
await AnswerAsync(command.InteractionId, "Вы не записаны на эту сессию.", ct);
|
||||
return;
|
||||
return await AnswerAsync(command.InteractionId, "Вы не записаны на эту сессию.", ct);
|
||||
}
|
||||
|
||||
await connection.ExecuteAsync(
|
||||
@@ -175,7 +173,7 @@ public sealed class LeaveSessionHandler(
|
||||
"""
|
||||
SELECT sp.session_id AS SessionId,
|
||||
p.display_name AS DisplayName,
|
||||
COALESCE(p.external_username, p.telegram_username) AS TelegramUsername,
|
||||
p.external_username AS TelegramUsername,
|
||||
sp.registration_status AS RegistrationStatus
|
||||
FROM session_participants sp
|
||||
JOIN players p ON sp.player_id = p.id
|
||||
@@ -190,12 +188,15 @@ public sealed class LeaveSessionHandler(
|
||||
transactionCommitted = true;
|
||||
|
||||
var view = SessionBatchViewBuilder.Build(session.Title, batchSessions, batchParticipants);
|
||||
await messenger.UpdateScheduleAsync(
|
||||
new PlatformScheduleMessage(
|
||||
command.Group,
|
||||
view,
|
||||
command.ScheduleMessage),
|
||||
ct);
|
||||
if (!command.DeferScheduleUpdate)
|
||||
{
|
||||
await messenger.UpdateScheduleAsync(
|
||||
new PlatformScheduleMessage(
|
||||
command.Group,
|
||||
view,
|
||||
command.ScheduleMessage),
|
||||
ct);
|
||||
}
|
||||
|
||||
var callbackText = participant.RegistrationStatus == ParticipantRegistrationStatus.Waitlisted
|
||||
? "Вы удалены из листа ожидания."
|
||||
@@ -203,7 +204,7 @@ public sealed class LeaveSessionHandler(
|
||||
? "Вы отписались от сессии."
|
||||
: $"Вы отписались от сессии. Место получил(а) {promotedDisplayName}.";
|
||||
|
||||
await AnswerAsync(command.InteractionId, callbackText, ct);
|
||||
return await AnswerAsync(command.InteractionId, callbackText, ct, view);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -216,10 +217,17 @@ public sealed class LeaveSessionHandler(
|
||||
var errorText = transactionCommitted
|
||||
? "Запись снята, но не удалось обновить сообщение расписания."
|
||||
: "Произошла ошибка при отмене записи.";
|
||||
await AnswerAsync(command.InteractionId, errorText, ct);
|
||||
return await AnswerAsync(command.InteractionId, errorText, ct);
|
||||
}
|
||||
}
|
||||
|
||||
private Task AnswerAsync(string interactionId, string text, CancellationToken ct) =>
|
||||
messenger.AnswerInteractionAsync(new PlatformInteractionReply(interactionId, text), ct);
|
||||
private async Task<SessionInteractionResult> AnswerAsync(
|
||||
string interactionId,
|
||||
string text,
|
||||
CancellationToken ct,
|
||||
SessionBatchViewModel? updatedView = null)
|
||||
{
|
||||
await messenger.AnswerInteractionAsync(new PlatformInteractionReply(interactionId, text), ct);
|
||||
return new SessionInteractionResult(text, updatedView);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
using GmRelay.Shared.Platform;
|
||||
|
||||
namespace GmRelay.Shared.Features.Sessions.ExportCalendar;
|
||||
|
||||
public sealed record ExportCalendarCommand(
|
||||
PlatformGroup Group,
|
||||
PlatformUser User);
|
||||
@@ -0,0 +1,111 @@
|
||||
using System.Text;
|
||||
using Dapper;
|
||||
using GmRelay.Shared.Domain;
|
||||
using GmRelay.Shared.Platform;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Npgsql;
|
||||
|
||||
namespace GmRelay.Shared.Features.Sessions.ExportCalendar;
|
||||
|
||||
internal sealed record CalendarSessionDto(Guid Id, string Title, DateTime ScheduledAt);
|
||||
|
||||
public sealed class ExportCalendarHandler(
|
||||
NpgsqlDataSource dataSource,
|
||||
IPlatformMessenger messenger,
|
||||
IConfiguration configuration)
|
||||
{
|
||||
public async Task HandleAsync(ExportCalendarCommand command, CancellationToken cancellationToken)
|
||||
{
|
||||
await using var connection = await dataSource.OpenConnectionAsync(cancellationToken);
|
||||
|
||||
var sessions = await connection.QueryAsync<CalendarSessionDto>(
|
||||
@"SELECT s.id as Id, s.title as Title, s.scheduled_at as ScheduledAt"
|
||||
+ " FROM sessions s"
|
||||
+ " JOIN game_groups g ON s.group_id = g.id"
|
||||
+ " WHERE g.platform = @Platform"
|
||||
+ " AND g.external_group_id = @ExternalGroupId"
|
||||
+ " AND s.status = @Planned"
|
||||
+ " AND s.scheduled_at > NOW()"
|
||||
+ " ORDER BY s.scheduled_at ASC",
|
||||
new { Platform = command.Group.Platform.ToString(), ExternalGroupId = command.Group.ExternalGroupId, Planned = SessionStatus.Planned });
|
||||
|
||||
var sessionsList = sessions.ToList();
|
||||
|
||||
if (sessionsList.Count == 0)
|
||||
{
|
||||
await messenger.SendGroupMessageAsync(
|
||||
command.Group,
|
||||
"📭 У этой группы нет запланированных сессий для экспорта.",
|
||||
cancellationToken);
|
||||
return;
|
||||
}
|
||||
|
||||
var sb = new StringBuilder();
|
||||
sb.AppendLine("BEGIN:VCALENDAR");
|
||||
sb.AppendLine("VERSION:2.0");
|
||||
sb.AppendLine("PRODID:-//GM-Relay//TTRPG Schedule//EN");
|
||||
|
||||
foreach (var s in sessionsList)
|
||||
{
|
||||
var dtStart = s.ScheduledAt.ToString("yyyyMMddTHHmmssZ");
|
||||
var dtEnd = s.ScheduledAt.AddHours(4).ToString("yyyyMMddTHHmmssZ");
|
||||
|
||||
sb.AppendLine("BEGIN:VEVENT");
|
||||
sb.AppendLine($"UID:{s.Id}@gmrelay");
|
||||
sb.AppendLine($"DTSTAMP:{DateTime.UtcNow:yyyyMMddTHHmmssZ}");
|
||||
sb.AppendLine($"DTSTART:{dtStart}");
|
||||
sb.AppendLine($"DTEND:{dtEnd}");
|
||||
sb.AppendLine($"SUMMARY:{s.Title}");
|
||||
sb.AppendLine("END:VEVENT");
|
||||
}
|
||||
|
||||
sb.AppendLine("END:VCALENDAR");
|
||||
|
||||
var bytes = Encoding.UTF8.GetBytes(sb.ToString());
|
||||
|
||||
// Create calendar subscription
|
||||
string? subscriptionUrl = null;
|
||||
var baseUrl = configuration["Web:BaseUrl"];
|
||||
var senderId = command.User.ExternalUserId;
|
||||
if (!string.IsNullOrWhiteSpace(baseUrl) && !string.IsNullOrWhiteSpace(senderId))
|
||||
{
|
||||
try
|
||||
{
|
||||
var token = Guid.NewGuid().ToString("N");
|
||||
var groupId = await connection.QueryFirstOrDefaultAsync<Guid?>(
|
||||
@"SELECT id FROM game_groups WHERE platform = @Platform AND external_group_id = @ExternalGroupId",
|
||||
new { Platform = command.Group.Platform.ToString(), ExternalGroupId = command.Group.ExternalGroupId });
|
||||
|
||||
await connection.ExecuteAsync(
|
||||
@"INSERT INTO calendar_subscriptions (id, token, user_platform, user_external_id, group_id, filter_type, created_at, expires_at)
|
||||
VALUES (gen_random_uuid(), @token, @userPlatform, @userExternalId, @groupId, @filterType, now(), NULL)",
|
||||
new { token, userPlatform = command.Group.Platform.ToString(), userExternalId = senderId, groupId, filterType = (int)CalendarSubscriptionFilter.SpecificGroup });
|
||||
|
||||
subscriptionUrl = $"{baseUrl.TrimEnd('/')}/calendar/{token}.ics";
|
||||
}
|
||||
catch
|
||||
{
|
||||
// Non-critical: if subscription creation fails, still send the file
|
||||
}
|
||||
}
|
||||
|
||||
var actions = subscriptionUrl is not null
|
||||
? new[]
|
||||
{
|
||||
new PlatformMessageAction(
|
||||
"calendar-subscription",
|
||||
"🔗 Подписаться на календарь",
|
||||
subscriptionUrl)
|
||||
}
|
||||
: Array.Empty<PlatformMessageAction>();
|
||||
|
||||
await messenger.SendCalendarFileAsync(
|
||||
new PlatformCalendarFile(
|
||||
command.Group,
|
||||
"schedule.ics",
|
||||
bytes,
|
||||
"📅 <b>Ваш календарь игр!</b>\nОткройте файл на устройстве, чтобы добавить события в свой календарь.",
|
||||
actions),
|
||||
cancellationToken);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
using Dapper;
|
||||
using GmRelay.Shared.Domain;
|
||||
using GmRelay.Shared.Platform;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Npgsql;
|
||||
|
||||
namespace GmRelay.Shared.Features.Sessions.ListSessions;
|
||||
|
||||
internal sealed record DeleteSessionInfoDto(
|
||||
string Title,
|
||||
Guid BatchId,
|
||||
Guid GroupId,
|
||||
bool CanManage,
|
||||
int? ThreadId,
|
||||
bool TopicCreatedByBot);
|
||||
|
||||
public sealed record DeleteSessionResult(
|
||||
bool Success,
|
||||
string? ReplyText,
|
||||
string? Title,
|
||||
Guid? GroupId,
|
||||
int? ThreadId,
|
||||
bool TopicCreatedByBot,
|
||||
int RemainingInTopic);
|
||||
|
||||
public sealed class DeleteSessionHandler(
|
||||
NpgsqlDataSource dataSource)
|
||||
{
|
||||
public async Task<DeleteSessionResult> HandleAsync(DeleteSessionCommand command, CancellationToken ct)
|
||||
{
|
||||
await using var connection = await dataSource.OpenConnectionAsync(ct);
|
||||
await using var transaction = await connection.BeginTransactionAsync(ct);
|
||||
|
||||
// 1. Fetch session and verify group manager.
|
||||
var session = await connection.QuerySingleOrDefaultAsync<DeleteSessionInfoDto>(
|
||||
"""
|
||||
SELECT s.title AS Title,
|
||||
s.batch_id AS BatchId,
|
||||
s.group_id AS GroupId,
|
||||
s.thread_id AS ThreadId,
|
||||
s.topic_created_by_bot AS TopicCreatedByBot,
|
||||
EXISTS (
|
||||
SELECT 1
|
||||
FROM group_managers gm
|
||||
JOIN players p ON p.id = gm.player_id
|
||||
WHERE gm.group_id = s.group_id
|
||||
AND p.platform = @Platform
|
||||
AND p.external_user_id = @ExternalUserId
|
||||
) AS CanManage
|
||||
FROM sessions s
|
||||
WHERE s.id = @SessionId
|
||||
""",
|
||||
new { command.SessionId, Platform = command.User.Platform.ToString(), ExternalUserId = command.User.ExternalUserId }, transaction);
|
||||
|
||||
if (session == null)
|
||||
{
|
||||
return new DeleteSessionResult(false, "Сессия не найдена.", null, null, null, false, 0);
|
||||
}
|
||||
|
||||
if (!session.CanManage)
|
||||
{
|
||||
return new DeleteSessionResult(false, "Только owner или co-GM может удалять сессию.", null, null, null, false, 0);
|
||||
}
|
||||
|
||||
// 2. Delete session
|
||||
await connection.ExecuteAsync("DELETE FROM sessions WHERE id = @Id", new { Id = command.SessionId }, transaction);
|
||||
|
||||
var remainingInTopic = session.ThreadId.HasValue
|
||||
? await connection.ExecuteScalarAsync<int>(
|
||||
"""
|
||||
SELECT COUNT(*)
|
||||
FROM sessions
|
||||
WHERE group_id = @GroupId
|
||||
AND thread_id = @ThreadId
|
||||
""",
|
||||
new { session.GroupId, ThreadId = session.ThreadId.Value },
|
||||
transaction)
|
||||
: 0;
|
||||
|
||||
await transaction.CommitAsync(ct);
|
||||
|
||||
return new DeleteSessionResult(
|
||||
true,
|
||||
"Сессия удалена!",
|
||||
session.Title,
|
||||
session.GroupId,
|
||||
session.ThreadId,
|
||||
session.TopicCreatedByBot,
|
||||
remainingInTopic);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
using GmRelay.Shared.Platform;
|
||||
|
||||
namespace GmRelay.Shared.Features.Sessions.ListSessions;
|
||||
|
||||
public sealed record ListSessionsCommand(
|
||||
PlatformGroup Group,
|
||||
PlatformUser User);
|
||||
|
||||
public sealed record DeleteSessionCommand(
|
||||
Guid SessionId,
|
||||
PlatformUser User,
|
||||
PlatformGroup Group,
|
||||
PlatformMessageRef ScheduleMessage);
|
||||
@@ -0,0 +1,57 @@
|
||||
using Dapper;
|
||||
using GmRelay.Shared.Domain;
|
||||
using GmRelay.Shared.Platform;
|
||||
using Npgsql;
|
||||
|
||||
namespace GmRelay.Shared.Features.Sessions.ListSessions;
|
||||
|
||||
public sealed record SessionListItemDto(Guid Id, string Title, DateTime ScheduledAt, string Status, int? MaxPlayers, int PlayerCount, int WaitlistCount, bool CanManage);
|
||||
|
||||
public sealed record SessionListResult(
|
||||
IReadOnlyList<SessionListItemDto> Sessions,
|
||||
bool CanManage);
|
||||
|
||||
public sealed class ListSessionsHandler(
|
||||
NpgsqlDataSource dataSource)
|
||||
{
|
||||
public async Task<SessionListResult> HandleAsync(ListSessionsCommand command, CancellationToken cancellationToken)
|
||||
{
|
||||
await using var connection = await dataSource.OpenConnectionAsync(cancellationToken);
|
||||
|
||||
var sessions = await connection.QueryAsync<SessionListItemDto>(
|
||||
@"SELECT s.id as Id, s.title as Title, s.scheduled_at as ScheduledAt, s.status as Status, s.max_players as MaxPlayers,
|
||||
COUNT(sp.id) FILTER (WHERE sp.is_gm = false AND sp.registration_status = @Active) as PlayerCount,
|
||||
COUNT(sp.id) FILTER (WHERE sp.is_gm = false AND sp.registration_status = @Waitlisted) as WaitlistCount,
|
||||
EXISTS (
|
||||
SELECT 1
|
||||
FROM group_managers gm
|
||||
JOIN players manager_player ON manager_player.id = gm.player_id
|
||||
WHERE gm.group_id = s.group_id
|
||||
AND manager_player.platform = @Platform
|
||||
AND manager_player.external_user_id = @ExternalUserId
|
||||
) AS CanManage
|
||||
FROM sessions s
|
||||
JOIN game_groups g ON s.group_id = g.id
|
||||
LEFT JOIN session_participants sp ON s.id = sp.session_id
|
||||
WHERE g.platform = @Platform
|
||||
AND g.external_group_id = @ExternalGroupId
|
||||
AND s.status != @Cancelled
|
||||
AND s.scheduled_at > NOW()
|
||||
GROUP BY s.id, s.title, s.scheduled_at, s.status, s.max_players, s.group_id
|
||||
ORDER BY s.scheduled_at ASC",
|
||||
new
|
||||
{
|
||||
Platform = command.Group.Platform.ToString(),
|
||||
ExternalGroupId = command.Group.ExternalGroupId,
|
||||
ExternalUserId = command.User.ExternalUserId,
|
||||
Cancelled = SessionStatus.Cancelled,
|
||||
Active = ParticipantRegistrationStatus.Active,
|
||||
Waitlisted = ParticipantRegistrationStatus.Waitlisted
|
||||
});
|
||||
|
||||
var sessionsList = sessions.ToList();
|
||||
var canManage = sessionsList.Count > 0 && sessionsList.First().CanManage;
|
||||
|
||||
return new SessionListResult(sessionsList, canManage);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
namespace GmRelay.Shared.Features.Sessions.RescheduleSession;
|
||||
|
||||
internal sealed record AwaitingProposalDto(
|
||||
Guid Id,
|
||||
Guid SessionId,
|
||||
string Title,
|
||||
DateTime CurrentScheduledAt,
|
||||
Guid BatchId,
|
||||
int? BatchMessageId,
|
||||
string ExternalGroupId,
|
||||
int? ThreadId,
|
||||
string NotificationMode);
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
using GmRelay.Shared.Platform;
|
||||
|
||||
namespace GmRelay.Shared.Features.Sessions.RescheduleSession;
|
||||
|
||||
public sealed record HandleRescheduleTimeInputCommand(
|
||||
PlatformUser User,
|
||||
PlatformGroup Group,
|
||||
string Text);
|
||||
|
||||
public sealed record HandleRescheduleVoteCommand(
|
||||
Guid OptionId,
|
||||
PlatformUser User,
|
||||
PlatformGroup Group,
|
||||
string InteractionId,
|
||||
PlatformMessageRef ScheduleMessage);
|
||||
+181
@@ -0,0 +1,181 @@
|
||||
using Dapper;
|
||||
using GmRelay.Shared.Domain;
|
||||
using GmRelay.Shared.Platform;
|
||||
using GmRelay.Shared.Rendering;
|
||||
using Npgsql;
|
||||
|
||||
namespace GmRelay.Shared.Features.Sessions.RescheduleSession;
|
||||
|
||||
public sealed class HandleRescheduleTimeInputHandler(
|
||||
NpgsqlDataSource dataSource)
|
||||
{
|
||||
public async Task<HandleRescheduleTimeInputResult> HandleAsync(
|
||||
HandleRescheduleTimeInputCommand command, CancellationToken ct)
|
||||
{
|
||||
await using var connection = await dataSource.OpenConnectionAsync(ct);
|
||||
|
||||
var platform = command.User.Platform.ToString();
|
||||
var externalGmId = command.User.ExternalUserId;
|
||||
var externalGroupId = command.Group.ExternalGroupId;
|
||||
|
||||
var proposal = await connection.QuerySingleOrDefaultAsync<AwaitingProposalDto>(
|
||||
"""
|
||||
SELECT rp.id AS Id, rp.session_id AS SessionId, s.title AS Title, s.scheduled_at AS CurrentScheduledAt,
|
||||
s.batch_id AS BatchId, s.batch_message_id AS BatchMessageId,
|
||||
g.external_group_id AS ExternalGroupId,
|
||||
s.thread_id AS ThreadId,
|
||||
s.notification_mode AS NotificationMode
|
||||
FROM reschedule_proposals rp
|
||||
JOIN sessions s ON s.id = rp.session_id
|
||||
JOIN game_groups g ON g.id = s.group_id
|
||||
WHERE rp.proposed_by_external_user_id = @ExternalGmId
|
||||
AND rp.status = 'AwaitingTime'
|
||||
AND g.platform = @Platform
|
||||
AND g.external_group_id = @ExternalGroupId
|
||||
AND EXISTS (
|
||||
SELECT 1
|
||||
FROM group_managers gm
|
||||
JOIN players manager_player ON manager_player.id = gm.player_id
|
||||
WHERE gm.group_id = s.group_id
|
||||
AND manager_player.platform = @Platform
|
||||
AND manager_player.external_user_id = @ExternalGmId
|
||||
)
|
||||
ORDER BY rp.created_at DESC
|
||||
LIMIT 1
|
||||
""",
|
||||
new { ExternalGmId = externalGmId, Platform = platform, ExternalGroupId = externalGroupId });
|
||||
|
||||
if (proposal is null)
|
||||
return new HandleRescheduleTimeInputResult(false, false, null, null, null, null, [], [], [], null, default, null);
|
||||
|
||||
if (!RescheduleVotingInput.TryParse(command.Text, DateTimeOffset.UtcNow, out var votingInput, out var parseError))
|
||||
{
|
||||
return new HandleRescheduleTimeInputResult(
|
||||
true, false, parseError, null, null, null, [], [], [], proposal.Title, proposal.CurrentScheduledAt, null);
|
||||
}
|
||||
|
||||
var participants = (await connection.QueryAsync<VoteParticipantDto>(
|
||||
"""
|
||||
SELECT p.id AS PlayerId,
|
||||
p.display_name AS DisplayName,
|
||||
p.external_username AS TelegramUsername,
|
||||
p.external_user_id::BIGINT AS TelegramId
|
||||
FROM session_participants sp
|
||||
JOIN players p ON p.id = sp.player_id
|
||||
WHERE sp.session_id = @SessionId
|
||||
AND sp.is_gm = false
|
||||
AND sp.registration_status = @Active
|
||||
""",
|
||||
new { proposal.SessionId, Active = ParticipantRegistrationStatus.Active })).ToList();
|
||||
|
||||
if (participants.Count == 0)
|
||||
{
|
||||
var newTime = votingInput.Options[0];
|
||||
var view = await RescheduleImmediatelyAsync(connection, proposal, newTime, ct);
|
||||
var replyText =
|
||||
$"""✅ Сессия «{proposal.Title}» перенесена!\n\n📅 Новое время: <b>{newTime.ToOffset(TimeSpan.FromHours(3)).ToString("d MMMM yyyy, HH:mm", System.Globalization.CultureInfo.GetCultureInfo("ru-RU"))}</b> (МСК)\n\n<i>Участников нет — голосование не требуется.</i>""";
|
||||
return new HandleRescheduleTimeInputResult(
|
||||
true, true, replyText, view, null, null, [], [], [], proposal.Title, proposal.CurrentScheduledAt, proposal.BatchMessageId);
|
||||
}
|
||||
|
||||
await using var transaction = await connection.BeginTransactionAsync(ct);
|
||||
var options = votingInput.Options
|
||||
.Select((proposedAt, index) => new RescheduleOptionDto(
|
||||
Guid.NewGuid(),
|
||||
index + 1,
|
||||
proposedAt))
|
||||
.ToList();
|
||||
|
||||
await connection.ExecuteAsync(
|
||||
"""
|
||||
UPDATE reschedule_proposals
|
||||
SET voting_deadline_at = @Deadline, status = 'Voting', vote_chat_id = @VoteChatId
|
||||
WHERE id = @Id
|
||||
""",
|
||||
new { votingInput.Deadline, VoteChatId = externalGroupId, Id = proposal.Id },
|
||||
transaction);
|
||||
|
||||
foreach (var option in options)
|
||||
{
|
||||
await connection.ExecuteAsync(
|
||||
"""
|
||||
INSERT INTO reschedule_options (id, proposal_id, proposed_at, display_order)
|
||||
VALUES (@OptionId, @ProposalId, @ProposedAt, @DisplayOrder)
|
||||
""",
|
||||
new
|
||||
{
|
||||
option.OptionId,
|
||||
ProposalId = proposal.Id,
|
||||
option.ProposedAt,
|
||||
option.DisplayOrder
|
||||
},
|
||||
transaction);
|
||||
}
|
||||
|
||||
await transaction.CommitAsync(ct);
|
||||
|
||||
return new HandleRescheduleTimeInputResult(
|
||||
true,
|
||||
false,
|
||||
null,
|
||||
null,
|
||||
proposal.Id,
|
||||
votingInput.Deadline,
|
||||
options,
|
||||
participants,
|
||||
[],
|
||||
proposal.Title,
|
||||
proposal.CurrentScheduledAt,
|
||||
null);
|
||||
}
|
||||
|
||||
private static async Task<SessionBatchViewModel?> RescheduleImmediatelyAsync(
|
||||
NpgsqlConnection connection,
|
||||
AwaitingProposalDto proposal,
|
||||
DateTimeOffset newTime,
|
||||
CancellationToken ct)
|
||||
{
|
||||
await using var transaction = await connection.BeginTransactionAsync(ct);
|
||||
|
||||
await connection.ExecuteAsync(
|
||||
"""
|
||||
UPDATE sessions
|
||||
SET scheduled_at = @NewTime,
|
||||
status = @Status,
|
||||
confirmation_message_id = NULL,
|
||||
confirmation_sent_at = NULL,
|
||||
one_hour_reminder_processed_at = NULL,
|
||||
updated_at = now()
|
||||
WHERE id = @SessionId
|
||||
""",
|
||||
new { NewTime = newTime, proposal.SessionId, Status = SessionStatus.Planned },
|
||||
transaction);
|
||||
|
||||
await connection.ExecuteAsync(
|
||||
"UPDATE reschedule_proposals SET proposed_at = @NewTime, status = 'Approved' WHERE id = @Id",
|
||||
new { NewTime = newTime, Id = proposal.Id },
|
||||
transaction);
|
||||
|
||||
await transaction.CommitAsync(ct);
|
||||
|
||||
var batchSessions = (await connection.QueryAsync<SessionBatchDto>(
|
||||
"SELECT id AS SessionId, scheduled_at AS ScheduledAt, status AS Status, max_players AS MaxPlayers, join_link AS JoinLink FROM sessions WHERE batch_id = @BatchId ORDER BY scheduled_at",
|
||||
new { proposal.BatchId })).ToList();
|
||||
|
||||
var batchParticipants = (await connection.QueryAsync<ParticipantBatchDto>(
|
||||
"""
|
||||
SELECT sp.session_id AS SessionId,
|
||||
p.display_name AS DisplayName,
|
||||
p.external_username AS TelegramUsername,
|
||||
sp.registration_status AS RegistrationStatus
|
||||
FROM session_participants sp
|
||||
JOIN players p ON sp.player_id = p.id
|
||||
JOIN sessions s ON sp.session_id = s.id
|
||||
WHERE s.batch_id = @BatchId AND sp.is_gm = false
|
||||
ORDER BY sp.registration_status ASC, sp.created_at ASC, sp.responded_at ASC, p.created_at ASC
|
||||
""",
|
||||
new { proposal.BatchId })).ToList();
|
||||
|
||||
return SessionBatchViewBuilder.Build(proposal.Title, batchSessions, batchParticipants);
|
||||
}
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
using GmRelay.Shared.Rendering;
|
||||
|
||||
namespace GmRelay.Shared.Features.Sessions.RescheduleSession;
|
||||
|
||||
public sealed record HandleRescheduleTimeInputResult(
|
||||
bool Handled,
|
||||
bool IsRescheduledImmediately,
|
||||
string? ReplyText,
|
||||
SessionBatchViewModel? UpdatedView,
|
||||
Guid? ProposalId,
|
||||
DateTimeOffset? VotingDeadlineAt,
|
||||
IReadOnlyList<RescheduleOptionDto> Options,
|
||||
IReadOnlyList<VoteParticipantDto> Participants,
|
||||
IReadOnlyList<RescheduleOptionVoteDto> Votes,
|
||||
string? Title,
|
||||
DateTime CurrentScheduledAt,
|
||||
int? BatchMessageId);
|
||||
+156
@@ -0,0 +1,156 @@
|
||||
using Dapper;
|
||||
using GmRelay.Shared.Domain;
|
||||
using GmRelay.Shared.Platform;
|
||||
using Npgsql;
|
||||
|
||||
namespace GmRelay.Shared.Features.Sessions.RescheduleSession;
|
||||
|
||||
public sealed class HandleRescheduleVoteHandler(
|
||||
NpgsqlDataSource dataSource)
|
||||
{
|
||||
public async Task<HandleRescheduleVoteResult> HandleAsync(HandleRescheduleVoteCommand command, CancellationToken ct)
|
||||
{
|
||||
await using var connection = await dataSource.OpenConnectionAsync(ct);
|
||||
await using var transaction = await connection.BeginTransactionAsync(ct);
|
||||
|
||||
var proposal = await connection.QuerySingleOrDefaultAsync<VoteProposalDto>(
|
||||
"""
|
||||
SELECT rp.id AS Id,
|
||||
rp.session_id AS SessionId,
|
||||
rp.voting_deadline_at AS VotingDeadlineAt,
|
||||
s.title AS Title,
|
||||
s.scheduled_at AS CurrentScheduledAt
|
||||
FROM reschedule_options ro
|
||||
JOIN reschedule_proposals rp ON rp.id = ro.proposal_id
|
||||
JOIN sessions s ON s.id = rp.session_id
|
||||
WHERE ro.id = @OptionId AND rp.status = 'Voting'
|
||||
""",
|
||||
new { command.OptionId },
|
||||
transaction);
|
||||
|
||||
if (proposal is null)
|
||||
{
|
||||
return new HandleRescheduleVoteResult(
|
||||
false,
|
||||
"Голосование уже завершено или не найдено.",
|
||||
null, null, null, default, default,
|
||||
Array.Empty<VoteParticipantDto>(),
|
||||
Array.Empty<RescheduleOptionDto>(),
|
||||
Array.Empty<RescheduleOptionVoteDto>());
|
||||
}
|
||||
|
||||
if (proposal.VotingDeadlineAt <= DateTimeOffset.UtcNow)
|
||||
{
|
||||
return new HandleRescheduleVoteResult(
|
||||
false,
|
||||
"Дедлайн уже прошёл. Результаты скоро будут применены.",
|
||||
null, null, null, default, default,
|
||||
Array.Empty<VoteParticipantDto>(),
|
||||
Array.Empty<RescheduleOptionDto>(),
|
||||
Array.Empty<RescheduleOptionVoteDto>());
|
||||
}
|
||||
|
||||
var playerId = await connection.ExecuteScalarAsync<Guid?>(
|
||||
"""
|
||||
SELECT p.id
|
||||
FROM session_participants sp
|
||||
JOIN players p ON p.id = sp.player_id
|
||||
WHERE sp.session_id = @SessionId
|
||||
AND p.platform = @Platform
|
||||
AND p.external_user_id = @ExternalUserId
|
||||
AND sp.is_gm = false
|
||||
AND sp.registration_status = @Active
|
||||
""",
|
||||
new
|
||||
{
|
||||
proposal.SessionId,
|
||||
Platform = command.User.Platform.ToString(),
|
||||
ExternalUserId = command.User.ExternalUserId,
|
||||
Active = ParticipantRegistrationStatus.Active
|
||||
},
|
||||
transaction);
|
||||
|
||||
if (playerId is null)
|
||||
{
|
||||
return new HandleRescheduleVoteResult(
|
||||
false,
|
||||
"Вы не являетесь участником этой сессии.",
|
||||
null, null, null, default, default,
|
||||
Array.Empty<VoteParticipantDto>(),
|
||||
Array.Empty<RescheduleOptionDto>(),
|
||||
Array.Empty<RescheduleOptionVoteDto>());
|
||||
}
|
||||
|
||||
await connection.ExecuteAsync(
|
||||
"""
|
||||
INSERT INTO reschedule_option_votes (proposal_id, player_id, option_id)
|
||||
VALUES (@ProposalId, @PlayerId, @OptionId)
|
||||
ON CONFLICT (proposal_id, player_id) DO UPDATE
|
||||
SET option_id = EXCLUDED.option_id,
|
||||
voted_at = now()
|
||||
""",
|
||||
new
|
||||
{
|
||||
ProposalId = proposal.Id,
|
||||
PlayerId = playerId.Value,
|
||||
command.OptionId
|
||||
},
|
||||
transaction);
|
||||
|
||||
var participants = (await connection.QueryAsync<VoteParticipantDto>(
|
||||
"""
|
||||
SELECT p.id AS PlayerId,
|
||||
p.display_name AS DisplayName,
|
||||
p.external_username AS TelegramUsername,
|
||||
p.external_user_id AS TelegramId
|
||||
FROM session_participants sp
|
||||
JOIN players p ON p.id = sp.player_id
|
||||
WHERE sp.session_id = @SessionId
|
||||
AND sp.is_gm = false
|
||||
AND sp.registration_status = @Active
|
||||
ORDER BY p.display_name
|
||||
""",
|
||||
new { proposal.SessionId, Active = ParticipantRegistrationStatus.Active },
|
||||
transaction)).ToList();
|
||||
|
||||
var options = (await connection.QueryAsync<RescheduleOptionDto>(
|
||||
"""
|
||||
SELECT id AS OptionId,
|
||||
display_order AS DisplayOrder,
|
||||
proposed_at AS ProposedAt
|
||||
FROM reschedule_options
|
||||
WHERE proposal_id = @ProposalId
|
||||
ORDER BY display_order
|
||||
""",
|
||||
new { ProposalId = proposal.Id },
|
||||
transaction)).ToList();
|
||||
|
||||
var votes = (await connection.QueryAsync<RescheduleOptionVoteDto>(
|
||||
"""
|
||||
SELECT rov.option_id AS OptionId,
|
||||
p.id AS PlayerId,
|
||||
p.display_name AS DisplayName,
|
||||
p.external_username AS TelegramUsername
|
||||
FROM reschedule_option_votes rov
|
||||
JOIN players p ON p.id = rov.player_id
|
||||
WHERE rov.proposal_id = @ProposalId
|
||||
ORDER BY rov.voted_at, p.display_name
|
||||
""",
|
||||
new { ProposalId = proposal.Id },
|
||||
transaction)).ToList();
|
||||
|
||||
await transaction.CommitAsync(ct);
|
||||
|
||||
return new HandleRescheduleVoteResult(
|
||||
true,
|
||||
"Ваш голос учтён. До дедлайна его можно изменить.",
|
||||
proposal.Id,
|
||||
proposal.SessionId,
|
||||
proposal.Title,
|
||||
proposal.CurrentScheduledAt,
|
||||
proposal.VotingDeadlineAt,
|
||||
participants,
|
||||
options,
|
||||
votes);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
using GmRelay.Shared.Features.Sessions.RescheduleSession;
|
||||
|
||||
namespace GmRelay.Shared.Features.Sessions.RescheduleSession;
|
||||
|
||||
public sealed record HandleRescheduleVoteResult(
|
||||
bool Success,
|
||||
string? ReplyText,
|
||||
Guid? ProposalId,
|
||||
Guid? SessionId,
|
||||
string? Title,
|
||||
DateTime CurrentScheduledAt,
|
||||
DateTimeOffset VotingDeadlineAt,
|
||||
IReadOnlyList<VoteParticipantDto> Participants,
|
||||
IReadOnlyList<RescheduleOptionDto> Options,
|
||||
IReadOnlyList<RescheduleOptionVoteDto> Votes);
|
||||
@@ -0,0 +1,29 @@
|
||||
namespace GmRelay.Shared.Features.Sessions.RescheduleSession;
|
||||
|
||||
public sealed record RescheduleOptionDto(
|
||||
Guid OptionId,
|
||||
int DisplayOrder,
|
||||
DateTimeOffset ProposedAt);
|
||||
|
||||
public sealed record VoteProposalDto(
|
||||
Guid Id,
|
||||
Guid SessionId,
|
||||
DateTimeOffset VotingDeadlineAt,
|
||||
string Title,
|
||||
DateTime CurrentScheduledAt);
|
||||
|
||||
public sealed record RescheduleOptionVoteDto(
|
||||
Guid OptionId,
|
||||
Guid PlayerId,
|
||||
string DisplayName,
|
||||
string? TelegramUsername);
|
||||
|
||||
public sealed record RescheduleOptionVoteCount(
|
||||
Guid OptionId,
|
||||
int VoteCount);
|
||||
|
||||
public sealed record VoteParticipantDto(
|
||||
Guid PlayerId,
|
||||
string DisplayName,
|
||||
string? TelegramUsername,
|
||||
long TelegramId = 0);
|
||||
+10
-10
@@ -1,13 +1,13 @@
|
||||
namespace GmRelay.Bot.Features.Sessions.RescheduleSession;
|
||||
namespace GmRelay.Shared.Features.Sessions.RescheduleSession;
|
||||
|
||||
internal enum RescheduleVoteOutcome
|
||||
public enum RescheduleVoteOutcome
|
||||
{
|
||||
Pending,
|
||||
Rejected,
|
||||
Approved
|
||||
}
|
||||
|
||||
internal sealed record RescheduleVoteDecision(
|
||||
public sealed record RescheduleVoteDecision(
|
||||
RescheduleVoteOutcome Outcome,
|
||||
string Reason,
|
||||
Guid? SelectedOptionId = null,
|
||||
@@ -15,7 +15,7 @@ internal sealed record RescheduleVoteDecision(
|
||||
bool ShouldRescheduleSession = false,
|
||||
bool ShouldResetParticipantRsvps = false);
|
||||
|
||||
internal static class RescheduleVoteRules
|
||||
public static class RescheduleVoteRules
|
||||
{
|
||||
public static RescheduleVoteDecision SelectWinner(IReadOnlyList<RescheduleOptionVoteCount> voteCounts)
|
||||
{
|
||||
@@ -49,8 +49,8 @@ internal static class RescheduleVoteRules
|
||||
{
|
||||
return new RescheduleVoteDecision(
|
||||
Outcome: RescheduleVoteOutcome.Rejected,
|
||||
Reason: "\u041e\u0434\u0438\u043d \u0438\u0437 \u0443\u0447\u0430\u0441\u0442\u043d\u0438\u043a\u043e\u0432 \u043e\u0442\u043a\u043b\u043e\u043d\u0438\u043b \u043f\u0435\u0440\u0435\u043d\u043e\u0441.",
|
||||
CallbackText: "\u0412\u044b \u043f\u0440\u043e\u0433\u043e\u043b\u043e\u0441\u043e\u0432\u0430\u043b\u0438 \u043f\u0440\u043e\u0442\u0438\u0432 \u043f\u0435\u0440\u0435\u043d\u043e\u0441\u0430.");
|
||||
Reason: "Один из участников отклонил перенос.",
|
||||
CallbackText: "Вы проголосовали против переноса.");
|
||||
}
|
||||
|
||||
var everyoneApproved = approvedParticipants == totalParticipants;
|
||||
@@ -58,11 +58,11 @@ internal static class RescheduleVoteRules
|
||||
return new RescheduleVoteDecision(
|
||||
Outcome: everyoneApproved ? RescheduleVoteOutcome.Approved : RescheduleVoteOutcome.Pending,
|
||||
Reason: everyoneApproved
|
||||
? "\u0412\u0441\u0435 \u0443\u0447\u0430\u0441\u0442\u043d\u0438\u043a\u0438 \u0441\u043e\u0433\u043b\u0430\u0441\u043d\u044b."
|
||||
: "\u0413\u043e\u043b\u043e\u0441\u043e\u0432\u0430\u043d\u0438\u0435 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0435\u0442\u0441\u044f.",
|
||||
? "Все участники согласны."
|
||||
: "Голосование продолжается.",
|
||||
CallbackText: everyoneApproved
|
||||
? "\u0412\u044b \u043f\u043e\u0434\u0442\u0432\u0435\u0440\u0434\u0438\u043b\u0438 \u043f\u0435\u0440\u0435\u043d\u043e\u0441! \u0412\u0441\u0435 \u0441\u043e\u0433\u043b\u0430\u0441\u043d\u044b \u2014 \u0432\u0440\u0435\u043c\u044f \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u043e."
|
||||
: "\u0412\u044b \u043f\u043e\u0434\u0442\u0432\u0435\u0440\u0434\u0438\u043b\u0438 \u043f\u0435\u0440\u0435\u043d\u043e\u0441!",
|
||||
? "Вы подтвердили перенос! Все согласны — время обновлено."
|
||||
: "Вы подтвердили перенос!",
|
||||
ShouldRescheduleSession: everyoneApproved,
|
||||
ShouldResetParticipantRsvps: everyoneApproved);
|
||||
}
|
||||
@@ -0,0 +1,215 @@
|
||||
using Dapper;
|
||||
using GmRelay.Shared.Domain;
|
||||
using GmRelay.Shared.Platform;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Npgsql;
|
||||
|
||||
namespace GmRelay.Shared.Features.Sessions.RescheduleSession;
|
||||
|
||||
public sealed record RescheduleVotingFinalizerResult(
|
||||
Guid ProposalId,
|
||||
Guid SessionId,
|
||||
string Title,
|
||||
DateTime CurrentScheduledAt,
|
||||
Guid BatchId,
|
||||
string NotificationMode,
|
||||
string SourcePlatform,
|
||||
DateTimeOffset VotingDeadlineAt,
|
||||
RescheduleVoteDecision Decision,
|
||||
RescheduleOptionDto? SelectedOption,
|
||||
IReadOnlyList<RescheduleOptionDto> Options,
|
||||
IReadOnlyList<RescheduleOptionVoteDto> Votes,
|
||||
IReadOnlyList<VoteParticipantDto> Participants);
|
||||
|
||||
public sealed class RescheduleVotingFinalizer(
|
||||
NpgsqlDataSource dataSource,
|
||||
ISystemClock clock,
|
||||
ILogger<RescheduleVotingFinalizer> logger)
|
||||
{
|
||||
public async Task<IReadOnlyList<Guid>> GetDueProposalIdsAsync(string sourcePlatform, CancellationToken ct)
|
||||
{
|
||||
await using var connection = await dataSource.OpenConnectionAsync(ct);
|
||||
var proposalIds = (await connection.QueryAsync<Guid>(
|
||||
"""
|
||||
SELECT id
|
||||
FROM reschedule_proposals
|
||||
WHERE status = 'Voting'
|
||||
AND voting_deadline_at IS NOT NULL
|
||||
AND voting_deadline_at <= @Now
|
||||
AND source_platform = @SourcePlatform
|
||||
ORDER BY voting_deadline_at
|
||||
LIMIT 25
|
||||
""",
|
||||
new { Now = clock.UtcNow.UtcDateTime, SourcePlatform = sourcePlatform })).ToList();
|
||||
|
||||
return proposalIds;
|
||||
}
|
||||
|
||||
public async Task<RescheduleVotingFinalizerResult?> FinalizeAsync(Guid proposalId, CancellationToken ct)
|
||||
{
|
||||
await using var connection = await dataSource.OpenConnectionAsync(ct);
|
||||
await using var transaction = await connection.BeginTransactionAsync(ct);
|
||||
|
||||
var proposal = await connection.QuerySingleOrDefaultAsync<ProposalRow>(
|
||||
"""
|
||||
SELECT rp.id AS ProposalId,
|
||||
rp.session_id AS SessionId,
|
||||
rp.voting_deadline_at AS VotingDeadlineAt,
|
||||
rp.source_platform AS SourcePlatform,
|
||||
s.title AS Title,
|
||||
s.scheduled_at AS CurrentScheduledAt,
|
||||
s.batch_id AS BatchId,
|
||||
s.notification_mode AS NotificationMode
|
||||
FROM reschedule_proposals rp
|
||||
JOIN sessions s ON s.id = rp.session_id
|
||||
WHERE rp.id = @ProposalId
|
||||
AND rp.status = 'Voting'
|
||||
AND rp.voting_deadline_at IS NOT NULL
|
||||
AND rp.voting_deadline_at <= @Now
|
||||
FOR UPDATE
|
||||
""",
|
||||
new { ProposalId = proposalId, Now = clock.UtcNow.UtcDateTime },
|
||||
transaction);
|
||||
|
||||
if (proposal is null)
|
||||
return null;
|
||||
|
||||
var participants = (await connection.QueryAsync<VoteParticipantDto>(
|
||||
"""
|
||||
SELECT p.id AS PlayerId,
|
||||
p.display_name AS DisplayName,
|
||||
p.external_username AS TelegramUsername,
|
||||
p.external_user_id::BIGINT AS TelegramId
|
||||
FROM session_participants sp
|
||||
JOIN players p ON p.id = sp.player_id
|
||||
WHERE sp.session_id = @SessionId
|
||||
AND sp.is_gm = false
|
||||
AND sp.registration_status = @Active
|
||||
ORDER BY p.display_name
|
||||
""",
|
||||
new { proposal.SessionId, Active = ParticipantRegistrationStatus.Active },
|
||||
transaction)).ToList();
|
||||
|
||||
var options = (await connection.QueryAsync<RescheduleOptionDto>(
|
||||
"""
|
||||
SELECT id AS OptionId,
|
||||
display_order AS DisplayOrder,
|
||||
proposed_at AS ProposedAt
|
||||
FROM reschedule_options
|
||||
WHERE proposal_id = @ProposalId
|
||||
ORDER BY display_order
|
||||
""",
|
||||
new { ProposalId = proposal.ProposalId },
|
||||
transaction)).ToList();
|
||||
|
||||
var votes = (await connection.QueryAsync<RescheduleOptionVoteDto>(
|
||||
"""
|
||||
SELECT rov.option_id AS OptionId,
|
||||
p.id AS PlayerId,
|
||||
p.display_name AS DisplayName,
|
||||
p.external_username AS TelegramUsername
|
||||
FROM reschedule_option_votes rov
|
||||
JOIN players p ON p.id = rov.player_id
|
||||
WHERE rov.proposal_id = @ProposalId
|
||||
ORDER BY rov.voted_at, p.display_name
|
||||
""",
|
||||
new { ProposalId = proposal.ProposalId },
|
||||
transaction)).ToList();
|
||||
|
||||
var voteCounts = options
|
||||
.Select(option => new RescheduleOptionVoteCount(
|
||||
option.OptionId,
|
||||
votes.Count(vote => vote.OptionId == option.OptionId)))
|
||||
.ToList();
|
||||
var decision = RescheduleVoteRules.SelectWinner(voteCounts);
|
||||
var selectedOption = decision.SelectedOptionId is { } selectedOptionId
|
||||
? options.Single(x => x.OptionId == selectedOptionId)
|
||||
: null;
|
||||
|
||||
if (selectedOption is not null)
|
||||
{
|
||||
await connection.ExecuteAsync(
|
||||
"""
|
||||
UPDATE sessions
|
||||
SET scheduled_at = @NewTime,
|
||||
status = @Status,
|
||||
confirmation_message_id = NULL,
|
||||
confirmation_sent_at = NULL,
|
||||
link_message_id = NULL,
|
||||
one_hour_reminder_processed_at = NULL,
|
||||
updated_at = now()
|
||||
WHERE id = @SessionId
|
||||
""",
|
||||
new { NewTime = selectedOption.ProposedAt, proposal.SessionId, Status = SessionStatus.Planned },
|
||||
transaction);
|
||||
|
||||
await connection.ExecuteAsync(
|
||||
"""
|
||||
UPDATE session_participants
|
||||
SET rsvp_status = 'Pending',
|
||||
responded_at = NULL
|
||||
WHERE session_id = @SessionId
|
||||
AND is_gm = false
|
||||
AND registration_status = @Active
|
||||
""",
|
||||
new { proposal.SessionId, Active = ParticipantRegistrationStatus.Active },
|
||||
transaction);
|
||||
|
||||
await connection.ExecuteAsync(
|
||||
"""
|
||||
UPDATE reschedule_proposals
|
||||
SET status = 'Approved',
|
||||
selected_option_id = @SelectedOptionId,
|
||||
proposed_at = @ProposedAt
|
||||
WHERE id = @ProposalId
|
||||
""",
|
||||
new
|
||||
{
|
||||
ProposalId = proposal.ProposalId,
|
||||
SelectedOptionId = selectedOption.OptionId,
|
||||
ProposedAt = selectedOption.ProposedAt
|
||||
},
|
||||
transaction);
|
||||
}
|
||||
else
|
||||
{
|
||||
await connection.ExecuteAsync(
|
||||
"UPDATE reschedule_proposals SET status = 'Rejected' WHERE id = @ProposalId",
|
||||
new { ProposalId = proposal.ProposalId },
|
||||
transaction);
|
||||
}
|
||||
|
||||
await transaction.CommitAsync(ct);
|
||||
|
||||
logger.LogInformation(
|
||||
"Finalized reschedule proposal {ProposalId} for session {SessionId} with outcome {Outcome}",
|
||||
proposal.ProposalId,
|
||||
proposal.SessionId,
|
||||
decision.Outcome);
|
||||
|
||||
return new RescheduleVotingFinalizerResult(
|
||||
proposal.ProposalId,
|
||||
proposal.SessionId,
|
||||
proposal.Title,
|
||||
proposal.CurrentScheduledAt,
|
||||
proposal.BatchId,
|
||||
proposal.NotificationMode,
|
||||
proposal.SourcePlatform,
|
||||
proposal.VotingDeadlineAt,
|
||||
decision,
|
||||
selectedOption,
|
||||
options,
|
||||
votes,
|
||||
participants);
|
||||
}
|
||||
|
||||
private sealed record ProposalRow(
|
||||
Guid ProposalId,
|
||||
Guid SessionId,
|
||||
DateTimeOffset VotingDeadlineAt,
|
||||
string SourcePlatform,
|
||||
string Title,
|
||||
DateTime CurrentScheduledAt,
|
||||
Guid BatchId,
|
||||
string NotificationMode);
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user