package change import ( "log" "sync" "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", } var submitMutex sync.Mutex // Submit submits a change to the history. func Submit(model, op, author, objectID string, data interface{}) (Change, error) { submitMutex.Lock() defer submitMutex.Unlock() 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, } Timeline.push(change) err = collection.Insert(&change) if err != nil { return Change{}, err } return change, err } // FindID gets a change by ID func FindID(id int) (Change, error) { change := Change{} err := collection.FindId(id).One(&change) if err != nil { return Change{}, err } return change, nil } // List gets all changes in ascending order func List() ([]Change, error) { changes := make([]Change, 0, 64) err := collection.Find(nil).Sort("_id").All(&changes) if err != nil { return nil, err } return changes, nil } 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) } }) }