feat: state is now scoped by both user id and chat id
This commit is contained in:
1 parent
4b94fcb920
commit
ec12f29fdb
3 files changed
+31
-23
No files matched your search
@@ -27,9 +27,9 @@ type EventIsolation interface {
|
||||
}
|
||||
|
||||
type FSMStorage interface {
|
||||
Get(ctx context.Context, userID int) (state.Snapshot, error)
|
||||
Get(ctx context.Context, scope isolation.Scope) (state.Snapshot, error)
|
||||
Put(ctx context.Context, state state.Snapshot) error
|
||||
Delete(ctx context.Context, userID int) error
|
||||
Delete(ctx context.Context, scope isolation.Scope) error
|
||||
}
|
||||
|
||||
type UpdateHandler interface {
|
||||
@@ -157,13 +157,13 @@ func (d *Dispatcher) handleUpdates(ch <-chan model.Update) {
|
||||
func (d *Dispatcher) handleUpdate(ctx context.Context, upd model.Update) {
|
||||
var st *state.Context
|
||||
|
||||
snapshot, err := d.fsmStorage.Get(ctx, int(upd.UserID))
|
||||
snapshot, err := d.fsmStorage.Get(ctx, isolation.Scope{UserID: upd.UserID, ChatID: upd.ChatID})
|
||||
if err != nil && !errors.Is(err, state.ErrStateNotFound) {
|
||||
log.Error().Err(err).Send()
|
||||
return
|
||||
}
|
||||
if errors.Is(err, state.ErrStateNotFound) {
|
||||
st = state.New(int(upd.UserID))
|
||||
st = state.New(isolation.Scope{UserID: upd.UserID, ChatID: upd.ChatID})
|
||||
} else {
|
||||
st = state.Restore(snapshot)
|
||||
}
|
||||
@@ -177,7 +177,7 @@ func (d *Dispatcher) handleUpdate(ctx context.Context, upd model.Update) {
|
||||
|
||||
st = c.State()
|
||||
if st.State() == state.ClearState {
|
||||
if err := d.fsmStorage.Delete(ctx, int(upd.UserID)); err != nil {
|
||||
if err := d.fsmStorage.Delete(ctx, isolation.Scope{UserID: upd.UserID, ChatID: upd.ChatID}); err != nil {
|
||||
log.Error().Err(err).Send()
|
||||
return
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user