72 lines
1.6 KiB
Go
72 lines
1.6 KiB
Go
package message
|
|
|
|
import (
|
|
lib "git.sr.ht/~cco/go-scopes"
|
|
sql "git.sr.ht/~cco/go-scopes/storage"
|
|
"git.sr.ht/~cco/go-scopes/storage/tracking"
|
|
)
|
|
|
|
type message struct {
|
|
*tracking.BaseTrack
|
|
}
|
|
|
|
func MakeMessage(c *tracking.Container, h ...string) tracking.Track {
|
|
h = append(h, "") // make sure at least domain is there
|
|
if h[0] == "" {
|
|
h[0] = "scopes"
|
|
}
|
|
return &message{tracking.MakeBaseTrack(c, h...)}
|
|
}
|
|
|
|
func New(cont *tracking.Container, h ...string) *message {
|
|
return tracking.New(cont, h...).(*message)
|
|
}
|
|
|
|
func (msg *message) Base() *tracking.BaseTrack {
|
|
return msg.BaseTrack
|
|
}
|
|
|
|
func (msg *message) Domain() string {
|
|
return msg.Head()["domain"]
|
|
}
|
|
|
|
func Messages(db *sql.Storage) *tracking.Container {
|
|
return &tracking.Container{container_definition, db}
|
|
}
|
|
|
|
// message store action handler
|
|
|
|
func Store(act lib.Action) bool {
|
|
db := lib.GetState[*sql.Storage](act.Context())
|
|
StoreDB(db, act.Message())
|
|
return true
|
|
}
|
|
|
|
func StoreDB(db *sql.Storage, msg lib.Message) {
|
|
cont := Messages(db)
|
|
// cont.Save(PrepareMessage(cont, msg))
|
|
cont.Save(cont.ItemFactory(cont))
|
|
}
|
|
|
|
// container definition
|
|
|
|
var container_definition *tracking.ContDef
|
|
|
|
const type_prefix = "msg"
|
|
|
|
func init() {
|
|
hf := lib.StrSlice{"domain", "action", "class", "item"}
|
|
ixs := []lib.StrSlice{hf}
|
|
ixs = append(ixs, lib.StrSlice{"domain", "class", "item"})
|
|
container_definition = &tracking.ContDef{
|
|
Prefix: type_prefix,
|
|
ContFactory: Messages,
|
|
ItemFactory: MakeMessage,
|
|
TableName: "messages",
|
|
HeadFields: hf,
|
|
IdFields: hf,
|
|
Indexes: ixs,
|
|
InsertOnChange: true,
|
|
}
|
|
tracking.RegisterContainerDef(container_definition)
|
|
}
|