Long overdue initial commit
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
package change
|
||||
|
||||
import (
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"git.aiterp.net/rpdata/api/model/counter"
|
||||
|
||||
"git.aiterp.net/rpdata/api/internal/store"
|
||||
"github.com/globalsign/mgo"
|
||||
)
|
||||
|
||||
var collection *mgo.Collection
|
||||
|
||||
// A Change represents a change in any other model
|
||||
type Change struct {
|
||||
ID int `bson:"_id" json:"id"`
|
||||
Time time.Time `bson:"time" json:"time"`
|
||||
Model string `bson:"model" json:"model"`
|
||||
Op string `bson:"op" json:"op"`
|
||||
Author string `bson:"author,omitempty" json:"author,omitempty"`
|
||||
ObjectID string `bson:"objectId,omitempty" json:"objectId,omitempty"`
|
||||
Data interface{} `bson:"data,omitempty" json:"data,omitempty"`
|
||||
}
|
||||
|
||||
// PublicModels lists which models can be listed in bulk by anyone.
|
||||
var PublicModels = []string{
|
||||
"Character",
|
||||
"Log",
|
||||
"Post",
|
||||
}
|
||||
|
||||
// Submit submits a change to the history.
|
||||
func Submit(model, op, author, objectID string, data interface{}) (Change, error) {
|
||||
index, err := counter.Next("auto_increment", "Change")
|
||||
if err != nil {
|
||||
return Change{}, err
|
||||
}
|
||||
|
||||
change := Change{
|
||||
ID: index,
|
||||
Time: time.Now(),
|
||||
Model: model,
|
||||
Op: op,
|
||||
Author: author,
|
||||
ObjectID: objectID,
|
||||
Data: data,
|
||||
}
|
||||
|
||||
err = collection.Insert(&change)
|
||||
if err != nil {
|
||||
return Change{}, err
|
||||
}
|
||||
|
||||
return change, err
|
||||
}
|
||||
|
||||
func init() {
|
||||
store.HandleInit(func(db *mgo.Database) {
|
||||
collection = db.C("common.history")
|
||||
|
||||
collection.EnsureIndexKey("model")
|
||||
collection.EnsureIndexKey("author")
|
||||
collection.EnsureIndexKey("objectId")
|
||||
|
||||
err := collection.EnsureIndex(mgo.Index{
|
||||
Name: "expiry",
|
||||
Key: []string{"time"},
|
||||
ExpireAfter: time.Hour * 336,
|
||||
})
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user