修复机器人列表统计与删除:仅统计频道并忽略并发删除

This commit is contained in:
meoacgx committed 2025-12-19 18:45:02 +08:00
1 parent e71ce7cf55
commit 02090e5ae2
3 files changed
+22 -8

No files matched your search

@@ -1,3 +1,4 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using TelegramPanel.Data.Entities;
using TelegramPanel.Data.Repositories;
@@ -75,7 +76,14 @@ public class BotManagementService
if (bot == null)
return;
await _botRepository.DeleteAsync(bot);
try
{
await _botRepository.DeleteAsync(bot);
}
catch (DbUpdateConcurrencyException)
{
// 视为已被删除(幂等)
}
}
public async Task<IEnumerable<BotChannelCategory>> GetCategoriesAsync(int botId)
@@ -119,7 +127,9 @@ public class BotManagementService
public async Task<IEnumerable<BotChannel>> GetChannelsAsync(int botId, int? categoryId = null)
{
return await _botChannelRepository.GetForBotAsync(botId, categoryId);
// Bot 频道列表仅展示“频道”(不展示群组/超级群组)
var list = await _botChannelRepository.GetForBotAsync(botId, categoryId);
return list.Where(x => x.IsBroadcast);
}
public async Task<BotChannel> UpsertChannelAsync(BotChannel channel)
@@ -52,7 +52,7 @@ public class BotTelegramService
if (result.ValueKind != JsonValueKind.Array)
return 0;
// Telegram 会针对同一个频道连续产生多条 my_chat_member(例如 member -> administrator),
// Telegram 会针对同一个 chat 连续产生多条 my_chat_member(例如 member -> administrator),
// 这里按 chat_id 去重并只应用“最后一次状态”,避免“新增两个”的错觉/重复写库。
var changesByChatId = new Dictionary<long, BotChatMemberChange>();
long? maxUpdateId = null;
@@ -76,10 +76,6 @@ public class BotTelegramService
if (!TryParseChatMemberUpdate(myChatMember, out var chat, out var status))
continue;
// 仅同步“频道”(不包含群组/超级群组),避免把讨论群等误当成频道条目
if (chat.Type is not "channel")
continue;
changesByChatId[chat.Id] = new BotChatMemberChange(chat, status);
}
@@ -91,6 +87,14 @@ public class BotTelegramService
var chatId = kv.Key;
var change = kv.Value;
// 本系统 Bot 只管理频道(channel);如果收到群组/超级群组更新,则仅用于“清理旧数据”,避免列表统计被污染。
if (change.Chat.Type is not "channel")
{
await _botManagement.DeleteChannelByTelegramIdAsync(bot.Id, chatId);
affected++;
continue;
}
// 只把“具备管理员/创建者权限”的频道纳入 Bot 列表;
// left/kicked 或降权为 member 等,直接从列表移除(否则导出邀请会失败且列表不准)。
if (change.Status is "administrator" or "creator")
@@ -49,7 +49,7 @@
<RowTemplate>
<MudTd DataLabel="名称">@context.Name</MudTd>
<MudTd DataLabel="用户名">@(!string.IsNullOrWhiteSpace(context.Username) ? $"@{context.Username}" : "-")</MudTd>
<MudTd DataLabel="频道数">@context.Channels.Count</MudTd>
<MudTd DataLabel="频道数">@context.Channels.Count(x => x.IsBroadcast)</MudTd>
<MudTd DataLabel="分类数">@context.Categories.Count</MudTd>
<MudTd DataLabel="最后同步">@((context.LastSyncAt.HasValue ? context.LastSyncAt.Value.ToString("yyyy-MM-dd HH:mm") : "-"))</MudTd>
<MudTd DataLabel="状态">