2024-08-16 19:43:27 +00:00
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/danielmiessler/fabric/common"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Sessions struct {
|
|
|
|
*Storage
|
|
|
|
}
|
|
|
|
|
2024-08-16 22:01:55 +00:00
|
|
|
func (o *Sessions) GetOrCreateSession(name string) (session *Session, err error) {
|
|
|
|
session = &Session{Name: name}
|
2024-08-16 19:43:27 +00:00
|
|
|
|
2024-08-16 22:01:55 +00:00
|
|
|
if o.Exists(name) {
|
|
|
|
err = o.LoadAsJson(name, &session.Messages)
|
2024-08-16 19:43:27 +00:00
|
|
|
} else {
|
2024-08-16 22:01:55 +00:00
|
|
|
fmt.Printf("Creating new session: %s\n", name)
|
2024-08-16 19:43:27 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-08-16 22:01:55 +00:00
|
|
|
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 22:01:55 +00:00
|
|
|
}
|
2024-08-16 19:43:27 +00:00
|
|
|
|
2024-08-16 22:01:55 +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...)
|
|
|
|
}
|