diff --git a/LittleShop/Areas/Admin/Views/BotActivity/LiveView.cshtml b/LittleShop/Areas/Admin/Views/BotActivity/LiveView.cshtml index 1b134b6..a85d1e4 100644 --- a/LittleShop/Areas/Admin/Views/BotActivity/LiveView.cshtml +++ b/LittleShop/Areas/Admin/Views/BotActivity/LiveView.cshtml @@ -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; diff --git a/TeleBot/TeleBot/Models/UserSession.cs b/TeleBot/TeleBot/Models/UserSession.cs index fe5efa8..6d57a4b 100644 --- a/TeleBot/TeleBot/Models/UserSession.cs +++ b/TeleBot/TeleBot/Models/UserSession.cs @@ -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") { diff --git a/TeleBot/TeleBot/Program.cs b/TeleBot/TeleBot/Program.cs index a3dac5e..6606c80 100644 --- a/TeleBot/TeleBot/Program.cs +++ b/TeleBot/TeleBot/Program.cs @@ -150,6 +150,10 @@ var botManagerService = app.Services.GetRequiredService(); var telegramBotService = app.Services.GetRequiredService(); botManagerService.SetTelegramBotService(telegramBotService); +// Connect SessionManager to BotManagerService for remote session tracking +var sessionManager = app.Services.GetRequiredService(); +sessionManager.SetBotManagerService(botManagerService); + // Configure the HTTP request pipeline if (app.Environment.IsDevelopment()) { diff --git a/TeleBot/TeleBot/Services/SessionManager.cs b/TeleBot/TeleBot/Services/SessionManager.cs index 01e0887..5a312c3 100644 --- a/TeleBot/TeleBot/Services/SessionManager.cs +++ b/TeleBot/TeleBot/Services/SessionManager.cs @@ -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 logger, @@ -65,7 +66,17 @@ namespace TeleBot.Services sessions.EnsureIndex(x => x.ExpiresAt); } } - + + /// + /// Sets the BotManagerService for remote session tracking. + /// Called during startup to avoid circular dependency. + /// + public void SetBotManagerService(BotManagerService botManagerService) + { + _botManagerService = botManagerService; + _logger.LogInformation("BotManagerService set for remote session tracking"); + } + public async Task 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("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;