feat: Add TeleBot session tracking to LittleShop and fix live activity feed ordering
- 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:
parent
bd0714e920
commit
a1af91807e
@ -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;
|
||||
|
||||
@ -31,6 +31,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")
|
||||
{
|
||||
using var sha256 = SHA256.Create();
|
||||
|
||||
@ -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())
|
||||
{
|
||||
|
||||
@ -36,6 +36,7 @@ namespace TeleBot.Services
|
||||
private readonly bool _useRedis;
|
||||
private readonly bool _useLiteDb;
|
||||
private Timer? _cleanupTimer;
|
||||
private BotManagerService? _botManagerService;
|
||||
|
||||
public SessionManager(
|
||||
IConfiguration configuration,
|
||||
@ -66,6 +67,16 @@ namespace TeleBot.Services
|
||||
}
|
||||
}
|
||||
|
||||
/// <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
|
||||
@ -137,6 +149,26 @@ namespace TeleBot.Services
|
||||
};
|
||||
|
||||
_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");
|
||||
@ -144,6 +176,22 @@ namespace TeleBot.Services
|
||||
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;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user