You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
125 lines
3.2 KiB
125 lines
3.2 KiB
package changes
|
|
|
|
import (
|
|
"log"
|
|
"strconv"
|
|
"time"
|
|
|
|
"git.aiterp.net/rpdata/api/internal/counter"
|
|
"git.aiterp.net/rpdata/api/models"
|
|
)
|
|
|
|
// Submit a change to the database. The objects may be any supported model, or arrays.
|
|
func Submit(model, op, author string, listed bool, keys []models.ChangeKey, objects ...interface{}) (models.Change, error) {
|
|
submitMutex.Lock()
|
|
defer submitMutex.Unlock()
|
|
|
|
id, err := counter.Next("auto_increment", "Change")
|
|
if err != nil {
|
|
return models.Change{}, err
|
|
}
|
|
|
|
if !models.ChangeModel(model).IsValid() {
|
|
panic("Invalid model")
|
|
}
|
|
|
|
// Silently discard * keys on unlisted changes.
|
|
if !listed {
|
|
keysCopy := make([]models.ChangeKey, len(keys))
|
|
copy(keysCopy, keys)
|
|
keys = keysCopy
|
|
|
|
deletes := make([]int, 0, 1)
|
|
for i, key := range keys {
|
|
if key.ID == "*" {
|
|
deletes = append(deletes, i-len(deletes))
|
|
}
|
|
}
|
|
|
|
for _, index := range deletes {
|
|
keys = append(keys[:index], keys[index+1:]...)
|
|
}
|
|
}
|
|
|
|
change := models.Change{
|
|
ID: "Change_" + strconv.Itoa(id),
|
|
Model: models.ChangeModel(model),
|
|
Date: time.Now(),
|
|
Op: op,
|
|
Author: author,
|
|
Keys: keys,
|
|
Listed: listed,
|
|
}
|
|
|
|
for _, object := range objects {
|
|
switch object := object.(type) {
|
|
case models.Log:
|
|
change.Logs = append(change.Logs, &object)
|
|
case *models.Log:
|
|
change.Logs = append(change.Logs, object)
|
|
case []models.Log:
|
|
for _, obj := range object {
|
|
change.Logs = append(change.Logs, &obj)
|
|
}
|
|
case models.Character:
|
|
change.Characters = append(change.Characters, &object)
|
|
case *models.Character:
|
|
change.Characters = append(change.Characters, object)
|
|
case []models.Character:
|
|
for _, obj := range object {
|
|
change.Characters = append(change.Characters, &obj)
|
|
}
|
|
case models.Channel:
|
|
change.Channels = append(change.Channels, &object)
|
|
case *models.Channel:
|
|
change.Channels = append(change.Channels, object)
|
|
case []models.Channel:
|
|
for _, obj := range object {
|
|
change.Channels = append(change.Channels, &obj)
|
|
}
|
|
case models.Post:
|
|
change.Posts = append(change.Posts, &object)
|
|
case *models.Post:
|
|
change.Posts = append(change.Posts, object)
|
|
case []models.Post:
|
|
for _, obj := range object {
|
|
change.Posts = append(change.Posts, &obj)
|
|
}
|
|
case models.Story:
|
|
change.Stories = append(change.Stories, &object)
|
|
case *models.Story:
|
|
change.Stories = append(change.Stories, object)
|
|
case []models.Story:
|
|
for _, obj := range object {
|
|
change.Stories = append(change.Stories, &obj)
|
|
}
|
|
case models.Chapter:
|
|
change.Chapters = append(change.Chapters, &object)
|
|
case *models.Chapter:
|
|
change.Chapters = append(change.Chapters, object)
|
|
case []models.Chapter:
|
|
for _, obj := range object {
|
|
change.Chapters = append(change.Chapters, &obj)
|
|
}
|
|
case models.Comment:
|
|
change.Comments = append(change.Comments, &object)
|
|
case *models.Comment:
|
|
change.Comments = append(change.Comments, object)
|
|
case []models.Comment:
|
|
for _, obj := range object {
|
|
change.Comments = append(change.Comments, &obj)
|
|
}
|
|
default:
|
|
log.Printf("Warning: unrecognized object in change: %#+v", object)
|
|
}
|
|
}
|
|
|
|
pushToSubscribers(change)
|
|
|
|
err = collection.Insert(&change)
|
|
if err != nil {
|
|
return models.Change{}, err
|
|
}
|
|
|
|
return change, nil
|
|
}
|