fabric/db/sessions.go

39 lines
726 B
Go
Raw Normal View History

2024-08-16 19:43:27 +00:00
package db
import (
"fmt"
"github.com/danielmiessler/fabric/common"
)
type Sessions struct {
*Storage
}
func (o *Sessions) GetOrCreateSession(name string) (session *Session, err error) {
session = &Session{Name: name}
2024-08-16 19:43:27 +00:00
if o.Exists(name) {
err = o.LoadAsJson(name, &session.Messages)
2024-08-16 19:43:27 +00:00
} else {
fmt.Printf("Creating new session: %s\n", name)
2024-08-16 19:43:27 +00:00
}
return
}
func (o *Sessions) SaveSession(session *Session) (err error) {
return o.SaveAsJson(session.Name, session.Messages)
2024-08-16 19:43:27 +00:00
}
type Session struct {
Name string
Messages []*common.Message
}
2024-08-16 19:43:27 +00:00
func (o *Session) IsEmpty() bool {
return len(o.Messages) == 0
2024-08-16 19:43:27 +00:00
}
func (o *Session) Append(messages ...*common.Message) {
o.Messages = append(o.Messages, messages...)
}