feat: state is now scoped by both user id and chat id

This commit is contained in:
abelith committed 2026-10-07 15:01:18 +03:00
1 parent 4b94fcb920
commit ec12f29fdb
3 files changed
+31 -23

No files matched your search

+7 -13
View File
@@ -3,8 +3,7 @@ package state
import (
"encoding/json"
"fmt"
"github.com/rs/zerolog/log"
"github.com/abelith/gomax/v1/isolation"
)
var (
@@ -32,13 +31,13 @@ func (JSONCodec) Unmarshal(d []byte, v any) error {
}
type Snapshot struct {
ID int `json:"id"`
Scope isolation.Scope `json:"id"`
State State `json:"state"`
Data map[string][]byte `json:"data"`
}
type Context struct {
id int
scope isolation.Scope
state State
data map[string][]byte
codec Codec
@@ -48,9 +47,9 @@ type Option func(*Context)
func WithCodec(c Codec) Option { return func(sc *Context) { sc.codec = c } }
func New(id int, opts ...Option) *Context {
func New(scope isolation.Scope, opts ...Option) *Context {
sc := &Context{
id: id,
scope: scope,
data: make(map[string][]byte),
codec: JSONCodec{},
}
@@ -61,7 +60,7 @@ func New(id int, opts ...Option) *Context {
}
func Restore(s Snapshot, opts ...Option) *Context {
sc := New(s.ID, opts...)
sc := New(s.Scope, opts...)
sc.state = s.State
for k, v := range s.Data {
sc.data[k] = v
@@ -74,15 +73,10 @@ func (sc *Context) Snapshot() Snapshot {
for k, v := range sc.data {
data[k] = v
}
return Snapshot{ID: sc.id, State: sc.state, Data: data}
return Snapshot{Scope: sc.scope, State: sc.state, Data: data}
}
func (sc *Context) Set(state State) {
log.Debug().
Int("user_id", sc.id).
Str("from", string(sc.state)).
Str("to", string(state)).
Msg("fsm state transition")
sc.state = state
}