Merge pull request #105: fix template batch topics
Deploy Telegram Bot / build-and-push (push) Successful in 6m3s
Deploy Telegram Bot / scan-images (push) Successful in 3m25s
Deploy Telegram Bot / deploy (push) Successful in 29s

This commit is contained in:
2026-05-27 14:05:38 +03:00
7 changed files with 65 additions and 15 deletions
+1 -1
View File
@@ -6,7 +6,7 @@ on:
- main - main
env: env:
VERSION: 3.1.0 VERSION: 3.1.1
jobs: jobs:
# ЧАСТЬ 1: Собираем образы и кладем в Gitea (чтобы делиться с ребятами) # ЧАСТЬ 1: Собираем образы и кладем в Gitea (чтобы делиться с ребятами)
+1 -1
View File
@@ -1,6 +1,6 @@
<Project> <Project>
<PropertyGroup> <PropertyGroup>
<Version>3.1.0</Version> <Version>3.1.1</Version>
<TargetFramework>net10.0</TargetFramework> <TargetFramework>net10.0</TargetFramework>
<LangVersion>preview</LangVersion> <LangVersion>preview</LangVersion>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
+3 -3
View File
@@ -49,7 +49,7 @@ services:
crond -f crond -f
bot: bot:
image: git.codeanddice.ru/toutsu/gmrelay-bot:3.1.0 image: git.codeanddice.ru/toutsu/gmrelay-bot:3.1.1
restart: always restart: always
depends_on: depends_on:
db: db:
@@ -67,7 +67,7 @@ services:
retries: 3 retries: 3
discord: discord:
image: git.codeanddice.ru/toutsu/gmrelay-discord-bot:3.1.0 image: git.codeanddice.ru/toutsu/gmrelay-discord-bot:3.1.1
restart: always restart: always
depends_on: depends_on:
db: db:
@@ -84,7 +84,7 @@ services:
retries: 3 retries: 3
web: web:
image: git.codeanddice.ru/toutsu/gmrelay-web:3.1.0 image: git.codeanddice.ru/toutsu/gmrelay-web:3.1.1
restart: always restart: always
depends_on: depends_on:
db: db:
@@ -73,7 +73,7 @@
</button> </button>
</form> </form>
<div class="nav-version">v3.1.0</div> <div class="nav-version">v3.1.1</div>
</div> </div>
</Authorized> </Authorized>
<NotAuthorized> <NotAuthorized>
+39 -2
View File
@@ -3,6 +3,7 @@ using GmRelay.Shared.Domain;
using GmRelay.Shared.Rendering; using GmRelay.Shared.Rendering;
using Npgsql; using Npgsql;
using Telegram.Bot; using Telegram.Bot;
using Telegram.Bot.Exceptions;
using GmRelay.Web.Services; using GmRelay.Web.Services;
namespace GmRelay.Web.Services; namespace GmRelay.Web.Services;
@@ -95,6 +96,7 @@ internal sealed record WebBatchSessionRow(
string NotificationMode, string NotificationMode,
bool TopicCreatedByBot = false); bool TopicCreatedByBot = false);
internal sealed record WebTemplateGroupDto(long TelegramChatId); internal sealed record WebTemplateGroupDto(long TelegramChatId);
internal sealed record WebTemplateTopicDestination(int? MessageThreadId, bool TopicCreatedByBot);
public sealed class SessionService( public sealed class SessionService(
NpgsqlDataSource dataSource, NpgsqlDataSource dataSource,
@@ -1186,6 +1188,10 @@ public sealed class SessionService(
throw new SessionAccessDeniedException(groupId, "0"); throw new SessionAccessDeniedException(groupId, "0");
} }
var topicDestination = await ResolveTemplateBatchTopicAsync(group.TelegramChatId, template.Title);
var messageThreadId = topicDestination.MessageThreadId;
var topicCreatedByBot = topicDestination.TopicCreatedByBot;
var schedule = BatchSchedulePlanner.BuildRecurringSchedule( var schedule = BatchSchedulePlanner.BuildRecurringSchedule(
firstScheduledAt, firstScheduledAt,
template.SessionCount, template.SessionCount,
@@ -1197,8 +1203,8 @@ public sealed class SessionService(
{ {
var sessionId = await conn.ExecuteScalarAsync<Guid>( var sessionId = await conn.ExecuteScalarAsync<Guid>(
""" """
INSERT INTO sessions (batch_id, group_id, title, join_link, scheduled_at, status, max_players, notification_mode) INSERT INTO sessions (batch_id, group_id, title, join_link, scheduled_at, status, thread_id, topic_created_by_bot, max_players, notification_mode)
VALUES (@BatchId, @GroupId, @Title, @JoinLink, @ScheduledAt, @Status, @MaxPlayers, @NotificationMode) VALUES (@BatchId, @GroupId, @Title, @JoinLink, @ScheduledAt, @Status, @ThreadId, @TopicCreatedByBot, @MaxPlayers, @NotificationMode)
RETURNING id RETURNING id
""", """,
new new
@@ -1209,6 +1215,8 @@ public sealed class SessionService(
template.JoinLink, template.JoinLink,
ScheduledAt = scheduledAt, ScheduledAt = scheduledAt,
Status = SessionStatus.Planned, Status = SessionStatus.Planned,
ThreadId = messageThreadId,
TopicCreatedByBot = topicCreatedByBot,
template.MaxPlayers, template.MaxPlayers,
template.NotificationMode template.NotificationMode
}, },
@@ -1223,6 +1231,7 @@ public sealed class SessionService(
var renderResult = TelegramSessionBatchRenderer.Render(view); var renderResult = TelegramSessionBatchRenderer.Render(view);
var batchMessage = await bot.SendMessage( var batchMessage = await bot.SendMessage(
chatId: group.TelegramChatId, chatId: group.TelegramChatId,
messageThreadId: messageThreadId,
text: renderResult.Text, text: renderResult.Text,
parseMode: Telegram.Bot.Types.Enums.ParseMode.Html, parseMode: Telegram.Bot.Types.Enums.ParseMode.Html,
replyMarkup: renderResult.Markup); replyMarkup: renderResult.Markup);
@@ -1242,6 +1251,34 @@ public sealed class SessionService(
template.NotificationMode); template.NotificationMode);
} }
private async Task<WebTemplateTopicDestination> ResolveTemplateBatchTopicAsync(long telegramChatId, string title)
{
var chat = await bot.GetChat(chatId: telegramChatId);
if (!chat.IsForum)
{
return new WebTemplateTopicDestination(null, TopicCreatedByBot: false);
}
try
{
var topic = await bot.CreateForumTopic(
chatId: telegramChatId,
name: $"🎲 Игры: {title}");
return new WebTemplateTopicDestination(topic.MessageThreadId, TopicCreatedByBot: true);
}
catch (ApiRequestException ex) when (IsMissingForumTopicRightsError(ex.Message))
{
throw new InvalidOperationException(
"Не удалось создать Telegram topic. Сделайте бота admin и включите право Manage Topics, затем повторите действие.",
ex);
}
}
private static bool IsMissingForumTopicRightsError(string apiError) =>
apiError.Contains("not enough rights", StringComparison.OrdinalIgnoreCase) ||
apiError.Contains("CHAT_ADMIN_REQUIRED", StringComparison.OrdinalIgnoreCase) ||
apiError.Contains("not an administrator", StringComparison.OrdinalIgnoreCase);
private async Task<List<WebDirectNotificationRecipient>> LoadSessionDirectRecipientsAsync( private async Task<List<WebDirectNotificationRecipient>> LoadSessionDirectRecipientsAsync(
Npgsql.NpgsqlConnection conn, Npgsql.NpgsqlConnection conn,
Guid sessionId) Guid sessionId)
@@ -62,7 +62,7 @@ public sealed class DiscordProjectStructureTests
var prChecks = File.ReadAllText(Path.Combine(repoRoot, ".gitea", "workflows", "pr-checks.yml")); var prChecks = File.ReadAllText(Path.Combine(repoRoot, ".gitea", "workflows", "pr-checks.yml"));
var deploy = File.ReadAllText(Path.Combine(repoRoot, ".gitea", "workflows", "deploy.yml")); var deploy = File.ReadAllText(Path.Combine(repoRoot, ".gitea", "workflows", "deploy.yml"));
Assert.Contains("gmrelay-discord-bot:3.1.0", compose); Assert.Contains("gmrelay-discord-bot:3.1.1", compose);
Assert.Contains("Discord__Token=${DISCORD_BOT_TOKEN:?Set DISCORD_BOT_TOKEN in .env}", compose); Assert.Contains("Discord__Token=${DISCORD_BOT_TOKEN:?Set DISCORD_BOT_TOKEN in .env}", compose);
Assert.Contains("src/GmRelay.DiscordBot/Dockerfile", deploy); Assert.Contains("src/GmRelay.DiscordBot/Dockerfile", deploy);
Assert.Contains("DISCORD_BOT_TOKEN", deploy); Assert.Contains("DISCORD_BOT_TOKEN", deploy);
@@ -76,13 +76,13 @@ public sealed class DiscordProjectStructureTests
{ {
var repoRoot = GetRepoRoot(); var repoRoot = GetRepoRoot();
Assert.Contains("<Version>3.1.0</Version>", File.ReadAllText(Path.Combine(repoRoot, "Directory.Build.props"))); Assert.Contains("<Version>3.1.1</Version>", File.ReadAllText(Path.Combine(repoRoot, "Directory.Build.props")));
Assert.Contains("VERSION: 3.1.0", File.ReadAllText(Path.Combine(repoRoot, ".gitea", "workflows", "deploy.yml"))); Assert.Contains("VERSION: 3.1.1", File.ReadAllText(Path.Combine(repoRoot, ".gitea", "workflows", "deploy.yml")));
Assert.Contains("gmrelay-bot:3.1.0", File.ReadAllText(Path.Combine(repoRoot, "compose.yaml"))); Assert.Contains("gmrelay-bot:3.1.1", File.ReadAllText(Path.Combine(repoRoot, "compose.yaml")));
Assert.Contains("gmrelay-web:3.1.0", File.ReadAllText(Path.Combine(repoRoot, "compose.yaml"))); Assert.Contains("gmrelay-web:3.1.1", File.ReadAllText(Path.Combine(repoRoot, "compose.yaml")));
Assert.Contains("gmrelay-discord-bot:3.1.0", File.ReadAllText(Path.Combine(repoRoot, "compose.yaml"))); Assert.Contains("gmrelay-discord-bot:3.1.1", File.ReadAllText(Path.Combine(repoRoot, "compose.yaml")));
Assert.Contains( Assert.Contains(
"v3.1.0", "v3.1.1",
File.ReadAllText(Path.Combine(repoRoot, "src", "GmRelay.Web", "Components", "Layout", "NavMenu.razor"))); File.ReadAllText(Path.Combine(repoRoot, "src", "GmRelay.Web", "Components", "Layout", "NavMenu.razor")));
} }
@@ -60,6 +60,19 @@ public sealed class TelegramTopicIntegrationSmokeTests
Assert.Contains("ExternalThreadId", telegramMessenger, StringComparison.Ordinal); Assert.Contains("ExternalThreadId", telegramMessenger, StringComparison.Ordinal);
} }
[Fact]
public async Task WebTemplateBatches_ShouldCreateAndPersistForumTopic()
{
var sessionService = await ReadRepositoryFileAsync("src/GmRelay.Web/Services/SessionService.cs");
Assert.Contains("GetChat", sessionService, StringComparison.Ordinal);
Assert.Contains("CreateForumTopic", sessionService, StringComparison.Ordinal);
Assert.Contains("thread_id, topic_created_by_bot", sessionService, StringComparison.Ordinal);
Assert.Contains("ThreadId = messageThreadId", sessionService, StringComparison.Ordinal);
Assert.Contains("TopicCreatedByBot = topicCreatedByBot", sessionService, StringComparison.Ordinal);
Assert.Contains("messageThreadId: messageThreadId", sessionService, StringComparison.Ordinal);
}
private static async Task<string> ReadRepositoryFileAsync(string relativePath) private static async Task<string> ReadRepositoryFileAsync(string relativePath)
{ {
var directory = new DirectoryInfo(AppContext.BaseDirectory); var directory = new DirectoryInfo(AppContext.BaseDirectory);