Compare commits

...

2 Commits

Author SHA1 Message Date
Toutsu 64216f5a26 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
2026-05-27 14:05:38 +03:00
Toutsu 383e2c1d8d fix: create Telegram topics for template batches
PR Checks / test-and-build (pull_request) Successful in 12m56s
Create a Telegram forum topic when Web creates a batch from a campaign template, persist thread ownership on the generated sessions, and send the batch schedule into that topic.

Bump version -> 3.1.1
2026-05-27 13:50:18 +03:00
7 changed files with 65 additions and 15 deletions
+1 -1
View File
@@ -6,7 +6,7 @@ on:
- main
env:
VERSION: 3.1.0
VERSION: 3.1.1
jobs:
# ЧАСТЬ 1: Собираем образы и кладем в Gitea (чтобы делиться с ребятами)
+1 -1
View File
@@ -1,6 +1,6 @@
<Project>
<PropertyGroup>
<Version>3.1.0</Version>
<Version>3.1.1</Version>
<TargetFramework>net10.0</TargetFramework>
<LangVersion>preview</LangVersion>
<Nullable>enable</Nullable>
+3 -3
View File
@@ -49,7 +49,7 @@ services:
crond -f
bot:
image: git.codeanddice.ru/toutsu/gmrelay-bot:3.1.0
image: git.codeanddice.ru/toutsu/gmrelay-bot:3.1.1
restart: always
depends_on:
db:
@@ -67,7 +67,7 @@ services:
retries: 3
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
depends_on:
db:
@@ -84,7 +84,7 @@ services:
retries: 3
web:
image: git.codeanddice.ru/toutsu/gmrelay-web:3.1.0
image: git.codeanddice.ru/toutsu/gmrelay-web:3.1.1
restart: always
depends_on:
db:
@@ -73,7 +73,7 @@
</button>
</form>
<div class="nav-version">v3.1.0</div>
<div class="nav-version">v3.1.1</div>
</div>
</Authorized>
<NotAuthorized>
+39 -2
View File
@@ -3,6 +3,7 @@ using GmRelay.Shared.Domain;
using GmRelay.Shared.Rendering;
using Npgsql;
using Telegram.Bot;
using Telegram.Bot.Exceptions;
using GmRelay.Web.Services;
namespace GmRelay.Web.Services;
@@ -95,6 +96,7 @@ internal sealed record WebBatchSessionRow(
string NotificationMode,
bool TopicCreatedByBot = false);
internal sealed record WebTemplateGroupDto(long TelegramChatId);
internal sealed record WebTemplateTopicDestination(int? MessageThreadId, bool TopicCreatedByBot);
public sealed class SessionService(
NpgsqlDataSource dataSource,
@@ -1186,6 +1188,10 @@ public sealed class SessionService(
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(
firstScheduledAt,
template.SessionCount,
@@ -1197,8 +1203,8 @@ public sealed class SessionService(
{
var sessionId = await conn.ExecuteScalarAsync<Guid>(
"""
INSERT INTO sessions (batch_id, group_id, title, join_link, scheduled_at, status, max_players, notification_mode)
VALUES (@BatchId, @GroupId, @Title, @JoinLink, @ScheduledAt, @Status, @MaxPlayers, @NotificationMode)
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, @ThreadId, @TopicCreatedByBot, @MaxPlayers, @NotificationMode)
RETURNING id
""",
new
@@ -1209,6 +1215,8 @@ public sealed class SessionService(
template.JoinLink,
ScheduledAt = scheduledAt,
Status = SessionStatus.Planned,
ThreadId = messageThreadId,
TopicCreatedByBot = topicCreatedByBot,
template.MaxPlayers,
template.NotificationMode
},
@@ -1223,6 +1231,7 @@ public sealed class SessionService(
var renderResult = TelegramSessionBatchRenderer.Render(view);
var batchMessage = await bot.SendMessage(
chatId: group.TelegramChatId,
messageThreadId: messageThreadId,
text: renderResult.Text,
parseMode: Telegram.Bot.Types.Enums.ParseMode.Html,
replyMarkup: renderResult.Markup);
@@ -1242,6 +1251,34 @@ public sealed class SessionService(
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(
Npgsql.NpgsqlConnection conn,
Guid sessionId)
@@ -62,7 +62,7 @@ public sealed class DiscordProjectStructureTests
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: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("src/GmRelay.DiscordBot/Dockerfile", deploy);
Assert.Contains("DISCORD_BOT_TOKEN", deploy);
@@ -76,13 +76,13 @@ public sealed class DiscordProjectStructureTests
{
var repoRoot = GetRepoRoot();
Assert.Contains("<Version>3.1.0</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("gmrelay-bot:3.1.0", File.ReadAllText(Path.Combine(repoRoot, "compose.yaml")));
Assert.Contains("gmrelay-web:3.1.0", File.ReadAllText(Path.Combine(repoRoot, "compose.yaml")));
Assert.Contains("gmrelay-discord-bot:3.1.0", File.ReadAllText(Path.Combine(repoRoot, "compose.yaml")));
Assert.Contains("<Version>3.1.1</Version>", File.ReadAllText(Path.Combine(repoRoot, "Directory.Build.props")));
Assert.Contains("VERSION: 3.1.1", File.ReadAllText(Path.Combine(repoRoot, ".gitea", "workflows", "deploy.yml")));
Assert.Contains("gmrelay-bot:3.1.1", 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.1", File.ReadAllText(Path.Combine(repoRoot, "compose.yaml")));
Assert.Contains(
"v3.1.0",
"v3.1.1",
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);
}
[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)
{
var directory = new DirectoryInfo(AppContext.BaseDirectory);