Доработан менеджер состояний.
This commit is contained in:
@@ -8,7 +8,7 @@
|
||||
/// <summary>
|
||||
/// Получает состояние пользователя.
|
||||
/// </summary>
|
||||
Task<UserState> GetAsync(long userId, CancellationToken ct);
|
||||
Task<UserState> GetAsync(string transportId, long userId, CancellationToken ct);
|
||||
|
||||
/// <summary>
|
||||
/// Сохраняет состояние пользователя.
|
||||
|
||||
@@ -6,17 +6,21 @@
|
||||
/// </summary>
|
||||
public sealed class InMemoryStateStore : IStateStore
|
||||
{
|
||||
private readonly Dictionary<long, UserState> _store = new();
|
||||
private readonly Dictionary<(string chatClientId, long userId), UserState> _store = new();
|
||||
|
||||
/// <summary>
|
||||
/// Получает состояние пользователя, создавая новое при отсутствии.
|
||||
/// </summary>
|
||||
public Task<UserState> GetAsync(long userId, CancellationToken ct)
|
||||
public Task<UserState> GetAsync(string chatClientId, long userId, CancellationToken ct)
|
||||
{
|
||||
if (!_store.TryGetValue(userId, out var st))
|
||||
if (!_store.TryGetValue((chatClientId, userId), out var st))
|
||||
{
|
||||
st = new UserState { UserId = userId };
|
||||
_store[userId] = st;
|
||||
st = new UserState
|
||||
{
|
||||
UserId = userId,
|
||||
ChatClientId = chatClientId,
|
||||
};
|
||||
_store[(chatClientId, userId)] = st;
|
||||
}
|
||||
return Task.FromResult(st);
|
||||
}
|
||||
@@ -26,7 +30,7 @@
|
||||
/// </summary>
|
||||
public Task SaveAsync(UserState state, CancellationToken ct)
|
||||
{
|
||||
_store[state.UserId] = state;
|
||||
_store[(state.ChatClientId, state.UserId)] = state;
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,7 +22,7 @@
|
||||
/// </summary>
|
||||
public async Task PushAsync(string pageId, object? args, UpdateContext ctx, CancellationToken ct)
|
||||
{
|
||||
var state = await _store.GetAsync(ctx.User.Id, ct);
|
||||
var state = await _store.GetAsync(ctx.Client.Id, ctx.User.Id, ct);
|
||||
state.Stack.Add(new NavEntry(pageId, args));
|
||||
await _store.SaveAsync(state, ct);
|
||||
|
||||
@@ -35,7 +35,7 @@
|
||||
/// </summary>
|
||||
public async Task ReplaceAsync(string pageId, object? args, UpdateContext ctx, CancellationToken ct)
|
||||
{
|
||||
var state = await _store.GetAsync(ctx.User.Id, ct);
|
||||
var state = await _store.GetAsync(ctx.Client.Id, ctx.User.Id, ct);
|
||||
if (state.Stack.Count > 0) state.Stack[^1] = new NavEntry(pageId, args);
|
||||
else state.Stack.Add(new NavEntry(pageId, args));
|
||||
await _store.SaveAsync(state, ct);
|
||||
@@ -49,7 +49,7 @@
|
||||
/// </summary>
|
||||
public async Task PopAsync(UpdateContext ctx, CancellationToken ct)
|
||||
{
|
||||
var state = await _store.GetAsync(ctx.User.Id, ct);
|
||||
var state = await _store.GetAsync(ctx.Client.Id, ctx.User.Id, ct);
|
||||
if (state.Stack.Count == 0) return;
|
||||
|
||||
var currentId = state.Stack[^1].PageId;
|
||||
@@ -92,7 +92,7 @@
|
||||
/// </summary>
|
||||
public async Task<NavEntry?> CurrentAsync(UpdateContext ctx, CancellationToken ct)
|
||||
{
|
||||
var state = await _store.GetAsync(ctx.User.Id, ct);
|
||||
var state = await _store.GetAsync(ctx.Client.Id, ctx.User.Id, ct);
|
||||
return state.Stack.Count == 0 ? null : state.Stack[^1];
|
||||
}
|
||||
|
||||
@@ -101,7 +101,7 @@
|
||||
/// </summary>
|
||||
public async Task<IReadOnlyList<NavEntry>> StackAsync(UpdateContext ctx, CancellationToken ct)
|
||||
{
|
||||
var state = await _store.GetAsync(ctx.User.Id, ct);
|
||||
var state = await _store.GetAsync(ctx.Client.Id, ctx.User.Id, ct);
|
||||
return state.Stack.AsReadOnly();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,7 +8,12 @@
|
||||
/// <summary>
|
||||
/// Идентификатор пользователя.
|
||||
/// </summary>
|
||||
public long UserId { get; init; }
|
||||
public required long UserId { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// Идентификатор клиента чата.
|
||||
/// </summary>
|
||||
public required string ChatClientId { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// Навигационный стек страниц.
|
||||
|
||||
20
BotPages.Core/Pages/ActionAttribute.cs
Normal file
20
BotPages.Core/Pages/ActionAttribute.cs
Normal file
@@ -0,0 +1,20 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace BotPages.Core
|
||||
{
|
||||
public class ActionAttribute : Attribute
|
||||
{
|
||||
public ActionAttribute(string label)
|
||||
{
|
||||
Label = label;
|
||||
}
|
||||
|
||||
public string Label { get; }
|
||||
}
|
||||
}
|
||||
48
BotPages.Core/Pages/ActionExtensions.cs
Normal file
48
BotPages.Core/Pages/ActionExtensions.cs
Normal file
@@ -0,0 +1,48 @@
|
||||
using System.Reflection;
|
||||
|
||||
namespace BotPages.Core
|
||||
{
|
||||
public static class ActionExtensions
|
||||
{
|
||||
private static readonly Dictionary<Type, Dictionary<string, object>> _cache = new();
|
||||
|
||||
public static string GetActionLabel<T>(this T value)
|
||||
where T : Enum
|
||||
{
|
||||
var fieldName = value.ToString();
|
||||
var field = typeof(T).GetField(fieldName, BindingFlags.Public | BindingFlags.Static);
|
||||
return field?.GetCustomAttribute<ActionAttribute>()?.Label ?? fieldName;
|
||||
}
|
||||
|
||||
public static T? FromActionLabel<T>(string? value) where T : struct, Enum
|
||||
{
|
||||
if (value == null) return null;
|
||||
|
||||
var type = typeof(T);
|
||||
if (!_cache.TryGetValue(type, out var map))
|
||||
{
|
||||
map = new Dictionary<string, object>();
|
||||
|
||||
var fields = type.GetFields(BindingFlags.Public | BindingFlags.Static);
|
||||
|
||||
foreach (var field in fields)
|
||||
{
|
||||
var fieldValue = field.GetValue(null)!;
|
||||
var fieldName = field.Name;
|
||||
|
||||
var attr = field.GetCustomAttribute<ActionAttribute>();
|
||||
|
||||
if (attr != null)
|
||||
{
|
||||
fieldName = attr.Label;
|
||||
}
|
||||
|
||||
map[fieldName] = fieldValue;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return map.TryGetValue(value, out var result) ? (T)result : null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -18,11 +18,23 @@
|
||||
/// <summary>
|
||||
/// Тип кнопки: inline или reply.
|
||||
/// </summary>
|
||||
public ActionPlacement Placement { get; init; } = ActionPlacement.Inline;
|
||||
public ActionPlacement Placement { get; init; } = ActionPlacement.Reply;
|
||||
|
||||
/// <summary>
|
||||
/// Номер ряда для макета (0 — первая строка).
|
||||
/// </summary>
|
||||
public int Row { get; init; } = 0;
|
||||
|
||||
public PageAction()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
public PageAction(Enum en)
|
||||
{
|
||||
Label = en.GetActionLabel();
|
||||
Value = en.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,10 +2,15 @@
|
||||
{
|
||||
/// <summary>
|
||||
/// Универсальный клиент для отправки сообщений и файлов в чат.
|
||||
/// Адаптеры (Telegram, MAX и др.) реализуют этот интерфейс.
|
||||
/// Адаптеры реализуют этот интерфейс.
|
||||
/// </summary>
|
||||
public interface IChatClient
|
||||
{
|
||||
/// <summary>
|
||||
/// Идентификатор клиента.
|
||||
/// </summary>
|
||||
string Id { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// Отправляет текстовое сообщение.
|
||||
/// Может сопровождаться клавиатурой (inline или reply).
|
||||
|
||||
Reference in New Issue
Block a user