tg/session.go

51 lines
1 KiB
Go
Raw Normal View History

2023-08-19 09:12:26 +03:00
package tg
2023-07-09 01:28:59 +03:00
2023-09-26 17:13:31 +03:00
// The way to determine where the context is
// related to.
type SessionScope uint8
const (
NoSessionScope SessionScope = iota
2023-09-26 17:13:31 +03:00
PrivateSessionScope
GroupSessionScope
ChannelSessionScope
)
2023-07-09 01:28:59 +03:00
// Represents unique value to identify chats.
// In fact is simply ID of the chat.
type SessionId int64
2023-07-09 01:28:59 +03:00
2023-08-13 15:37:36 +03:00
// Convert the SessionId to Telegram API's type.
func (si SessionId) ToApi() int64 {
2023-08-13 15:37:36 +03:00
return int64(si)
}
2023-07-09 01:28:59 +03:00
// The type represents current state of
// user interaction per each of them.
type Session struct {
2023-08-15 16:02:14 +03:00
// Id of the chat of the user.
2023-07-09 01:28:59 +03:00
Id SessionId
2023-09-26 17:13:31 +03:00
Scope SessionScope
2023-08-15 16:02:14 +03:00
// Custom value for each user.
Data any
2023-07-09 01:28:59 +03:00
}
2023-08-13 15:37:36 +03:00
// Return new empty session with specified user ID.
func NewSession(id SessionId, scope SessionScope) *Session {
2023-08-13 15:37:36 +03:00
return &Session{
Id: id,
Scope: scope,
2023-08-13 15:37:36 +03:00
}
}
2023-07-09 01:28:59 +03:00
// The type represents map of sessions using
// as key.
type SessionMap map[SessionId]*Session
2023-07-09 01:28:59 +03:00
2023-08-13 15:37:36 +03:00
// Add new empty session by it's ID.
func (sm SessionMap) Add(sid SessionId, scope SessionScope) *Session {
ret := NewSession(sid, scope)
sm[sid] = ret
2023-09-26 17:13:31 +03:00
return ret
2023-08-13 15:37:36 +03:00
}