feat: Add TeleBot session tracking to LittleShop and fix live activity feed ordering
All checks were successful
Build and Deploy LittleShop / Deploy to Production VPS (Manual Only) (push) Has been skipped
Build and Deploy LittleShop / Deploy to Pre-Production (CT109) (push) Successful in 1m1s

- Add LittleShopSessionId and MessageCount properties to UserSession model
- Integrate SessionManager with BotManagerService for remote session tracking
- Wire up SessionManager.SetBotManagerService() at startup in Program.cs
- Create remote sessions via BotManagerService.StartSessionAsync() when users connect
- Update remote sessions periodically (every 10 messages) via UpdateSessionAsync()
- Fix live activity feed to show newest records at top by reversing array iteration

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
SysAdmin 2025-11-25 20:03:08 +00:00
parent bd0714e920
commit a1af91807e
4 changed files with 62 additions and 5 deletions

View File

@ -228,7 +228,8 @@
$.get('@Url.Action("GetRecentActivities")', { count: 30 }, function(activities) {
const feed = $('#activityFeed');
activities.forEach(function(activity) {
// Reverse so oldest is prepended first, newest ends up at top
activities.slice().reverse().forEach(function(activity) {
const existingItem = $(`#activity-${activity.id}`);
if (existingItem.length === 0) {
const isNew = lastActivityId && activity.id !== lastActivityId;

View File

@ -30,6 +30,10 @@ namespace TeleBot.Models
// Order flow data (temporary)
public OrderFlowData? OrderFlow { get; set; }
// LittleShop remote session tracking
public Guid? LittleShopSessionId { get; set; }
public int MessageCount { get; set; } = 0;
public static string HashUserId(long telegramUserId, string salt = "TeleBot-Privacy-Salt")
{

View File

@ -150,6 +150,10 @@ var botManagerService = app.Services.GetRequiredService<BotManagerService>();
var telegramBotService = app.Services.GetRequiredService<TelegramBotService>();
botManagerService.SetTelegramBotService(telegramBotService);
// Connect SessionManager to BotManagerService for remote session tracking
var sessionManager = app.Services.GetRequiredService<SessionManager>();
sessionManager.SetBotManagerService(botManagerService);
// Configure the HTTP request pipeline
if (app.Environment.IsDevelopment())
{

View File

@ -36,7 +36,8 @@ namespace TeleBot.Services
private readonly bool _useRedis;
private readonly bool _useLiteDb;
private Timer? _cleanupTimer;
private BotManagerService? _botManagerService;
public SessionManager(
IConfiguration configuration,
ILogger<SessionManager> logger,
@ -65,7 +66,17 @@ namespace TeleBot.Services
sessions.EnsureIndex(x => x.ExpiresAt);
}
}
/// <summary>
/// Sets the BotManagerService for remote session tracking.
/// Called during startup to avoid circular dependency.
/// </summary>
public void SetBotManagerService(BotManagerService botManagerService)
{
_botManagerService = botManagerService;
_logger.LogInformation("BotManagerService set for remote session tracking");
}
public async Task<UserSession> GetOrCreateSessionAsync(long telegramUserId)
{
var hashedUserId = _privacyService.HashIdentifier(telegramUserId);
@ -126,6 +137,7 @@ namespace TeleBot.Services
session = new UserSession
{
HashedUserId = hashedUserId,
TelegramUserId = telegramUserId,
ExpiresAt = DateTime.UtcNow.AddMinutes(_sessionTimeoutMinutes),
IsEphemeral = _ephemeralByDefault,
Privacy = new PrivacySettings
@ -135,15 +147,51 @@ namespace TeleBot.Services
EnableDisappearingMessages = _configuration.GetValue<bool>("Features:EnableDisappearingMessages", true)
}
};
_inMemorySessions.TryAdd(session.Id, session);
// Start remote session tracking in LittleShop
if (_botManagerService != null)
{
try
{
var sessionIdentifier = $"telegram_{telegramUserId}";
var remoteSessionId = await _botManagerService.StartSessionAsync(sessionIdentifier, "Telegram");
if (remoteSessionId.HasValue)
{
session.LittleShopSessionId = remoteSessionId.Value;
_logger.LogInformation("Created remote session {SessionId} for user", remoteSessionId.Value);
}
}
catch (Exception ex)
{
_logger.LogWarning(ex, "Failed to start remote session tracking");
}
}
await UpdateSessionAsync(session);
_logger.LogInformation("Created new session for user");
}
else
{
session.UpdateActivity();
session.MessageCount++;
// Update remote session periodically (every 10 messages)
if (_botManagerService != null && session.LittleShopSessionId.HasValue && session.MessageCount % 10 == 0)
{
try
{
await _botManagerService.UpdateSessionAsync(
session.LittleShopSessionId.Value,
messageCount: session.MessageCount);
}
catch (Exception ex)
{
_logger.LogWarning(ex, "Failed to update remote session");
}
}
}
return session;