Browse Source

graph2: Added change logging for story and chapter, fixed channel ops not being logged.

1.1
Gisle Aune 6 years ago
parent
commit
a8cd595876
  1. 67
      graph2/queries/chapter.go
  2. 15
      graph2/queries/log.go
  3. 41
      graph2/queries/story.go
  4. 18
      models/change.go
  5. 16
      models/changes/submit.go

67
graph2/queries/chapter.go

@ -5,6 +5,9 @@ import (
"errors"
"time"
"git.aiterp.net/rpdata/api/models/changekeys"
"git.aiterp.net/rpdata/api/models/changes"
"git.aiterp.net/rpdata/api/internal/auth"
"git.aiterp.net/rpdata/api/models/stories"
@ -45,7 +48,14 @@ func (r *mutationResolver) AddChapter(ctx context.Context, input input.ChapterAd
return models.Chapter{}, errors.New("Story is not open")
}
return chapters.Add(story, input.Title, author, input.Source, time.Now(), input.FictionalDate)
chapter, err := chapters.Add(story, input.Title, author, input.Source, time.Now(), input.FictionalDate)
if err != nil {
return models.Chapter{}, errors.New("Failed to create chapter: " + err.Error())
}
go changes.Submit("Chapter", "add", token.UserID, story.Listed, changekeys.Listed(story, chapter), story, chapter)
return chapter, nil
}
func (r *mutationResolver) MoveChapter(ctx context.Context, input input.ChapterMoveInput) (models.Chapter, error) {
@ -68,7 +78,28 @@ func (r *mutationResolver) MoveChapter(ctx context.Context, input input.ChapterM
return models.Chapter{}, errors.New("You are not permitted to move chapters to this story")
}
return chapters.Move(chapter, target)
oldStoryID := chapter.StoryID
chapter, err = chapters.Move(chapter, target)
if err != nil {
return models.Chapter{}, errors.New("Failed to move chapter: " + err.Error())
}
go func() {
story, err := stories.FindID(chapter.StoryID)
if err != nil {
story.ID = chapter.StoryID
}
oldStory, err := stories.FindID(oldStoryID)
if err != nil {
oldStory.ID = oldStoryID
}
changes.Submit("Chapter", "move-out", chapter.Author, oldStory.Listed, changekeys.Many(oldStory, chapter), chapter)
changes.Submit("Chapter", "move-in", token.UserID, story.Listed, changekeys.Listed(story, chapter), story, chapter)
}()
return chapter, nil
}
func (r *mutationResolver) EditChapter(ctx context.Context, input input.ChapterEditInput) (models.Chapter, error) {
@ -86,7 +117,21 @@ func (r *mutationResolver) EditChapter(ctx context.Context, input input.ChapterE
input.FictionalDate = &time.Time{}
}
return chapters.Edit(chapter, input.Title, input.Source, input.FictionalDate)
chapter, err = chapters.Edit(chapter, input.Title, input.Source, input.FictionalDate)
if err != nil {
return models.Chapter{}, errors.New("Failed to edit chapter: " + err.Error())
}
go func() {
story, err := stories.FindID(chapter.StoryID)
if err != nil {
story.ID = chapter.StoryID
}
changes.Submit("Chapter", "edit", token.UserID, story.Listed, changekeys.Many(story, chapter), chapter)
}()
return chapter, nil
}
func (r *mutationResolver) RemoveChapter(ctx context.Context, input input.ChapterRemoveInput) (models.Chapter, error) {
@ -100,5 +145,19 @@ func (r *mutationResolver) RemoveChapter(ctx context.Context, input input.Chapte
return models.Chapter{}, errors.New("Unauthorized")
}
return chapters.Remove(chapter)
chapter, err = chapters.Remove(chapter)
if err != nil {
return models.Chapter{}, errors.New("Failed to remove chapter: " + err.Error())
}
go func() {
story, err := stories.FindID(chapter.StoryID)
if err != nil {
story.ID = chapter.StoryID
}
changes.Submit("Chapter", "remove", token.UserID, story.Listed, changekeys.Many(story, chapter), chapter)
}()
return chapter, nil
}

15
graph2/queries/log.go

@ -5,6 +5,8 @@ import (
"errors"
"strings"
"git.aiterp.net/rpdata/api/models/channels"
"git.aiterp.net/rpdata/api/graph2/input"
"git.aiterp.net/rpdata/api/internal/auth"
"git.aiterp.net/rpdata/api/internal/loader"
@ -73,12 +75,23 @@ func (r *mutationResolver) AddLog(ctx context.Context, input input.LogAddInput)
description = *input.Description
}
existingChannel, _ := channels.FindName(input.Channel)
log, err := logs.Add(input.Date, input.Channel, title, event, description, open)
if !token.Authenticated() || !token.Permitted("log.add") {
return models.Log{}, errors.New("Failed to create log: " + err.Error())
}
go changes.Submit("Log", "add", token.UserID, true, changekeys.Listed(log), log)
go func() {
if existingChannel.Name == "" {
channel, err := channels.FindName(input.Channel)
if err == nil {
changes.Submit("Channel", "add", token.UserID, true, changekeys.Listed(channel), channel)
}
}
changes.Submit("Log", "add", token.UserID, true, changekeys.Listed(log), log)
}()
return log, nil
}

41
graph2/queries/story.go

@ -5,6 +5,9 @@ import (
"errors"
"time"
"git.aiterp.net/rpdata/api/models/changekeys"
"git.aiterp.net/rpdata/api/models/changes"
"git.aiterp.net/rpdata/api/graph2/input"
"git.aiterp.net/rpdata/api/internal/auth"
"git.aiterp.net/rpdata/api/models"
@ -58,7 +61,14 @@ func (r *mutationResolver) AddStory(ctx context.Context, input input.StoryAddInp
listed := input.Listed != nil && *input.Listed
open := input.Open != nil && *input.Open
return stories.Add(input.Name, author, input.Category, listed, open, input.Tags, time.Now(), fictionalDate)
story, err := stories.Add(input.Name, author, input.Category, listed, open, input.Tags, time.Now(), fictionalDate)
if err != nil {
return models.Story{}, errors.New("Failed to add story: " + err.Error())
}
go changes.Submit("Story", "add", token.UserID, story.Listed, changekeys.Listed(story), story)
return story, nil
}
func (r *mutationResolver) AddStoryTag(ctx context.Context, input input.StoryTagAddInput) (models.Story, error) {
@ -73,7 +83,14 @@ func (r *mutationResolver) AddStoryTag(ctx context.Context, input input.StoryTag
return models.Story{}, errors.New("You are not permitted to edit this story")
}
return stories.AddTag(story, input.Tag)
story, err = stories.AddTag(story, input.Tag)
if err != nil {
return models.Story{}, errors.New("Failed to add story: " + err.Error())
}
go changes.Submit("Story", "tag", token.UserID, story.Listed, changekeys.Listed(story), story, input.Tag)
return story, nil
}
func (r *mutationResolver) RemoveStoryTag(ctx context.Context, input input.StoryTagRemoveInput) (models.Story, error) {
@ -88,7 +105,14 @@ func (r *mutationResolver) RemoveStoryTag(ctx context.Context, input input.Story
return models.Story{}, errors.New("You are not permitted to edit this story")
}
return stories.RemoveTag(story, input.Tag)
story, err = stories.RemoveTag(story, input.Tag)
if err != nil {
return models.Story{}, errors.New("Failed to add story: " + err.Error())
}
go changes.Submit("Story", "untag", token.UserID, story.Listed, changekeys.Listed(story), story, input.Tag)
return story, nil
}
func (r *mutationResolver) EditStory(ctx context.Context, input input.StoryEditInput) (models.Story, error) {
@ -107,7 +131,14 @@ func (r *mutationResolver) EditStory(ctx context.Context, input input.StoryEditI
input.FictionalDate = &time.Time{}
}
return stories.Edit(story, input.Name, input.Category, input.Listed, input.Open, input.FictionalDate)
story, err = stories.Edit(story, input.Name, input.Category, input.Listed, input.Open, input.FictionalDate)
if err != nil {
return models.Story{}, errors.New("Failed to add story: " + err.Error())
}
go changes.Submit("Story", "edit", token.UserID, story.Listed, changekeys.Listed(story), story)
return story, nil
}
func (r *mutationResolver) RemoveStory(ctx context.Context, input input.StoryRemoveInput) (models.Story, error) {
@ -132,5 +163,7 @@ func (r *mutationResolver) RemoveStory(ctx context.Context, input input.StoryRem
return models.Story{}, errors.New("Failed to remove chapters, but story is removed: " + err.Error())
}
go changes.Submit("Story", "remove", token.UserID, story.Listed, changekeys.Listed(story), story)
return story, nil
}

18
models/change.go

@ -14,7 +14,11 @@ type Change struct {
Logs []Log `bson:"logs"`
Characters []Character `bson:"characters"`
Channels []Channel `bson:"channels"`
Posts []Post `bson:"posts"`
Stories []Story `bson:"stories"`
Tags []Tag `bson:"tags"`
Chapters []Chapter `bson:"chapters"`
}
// ChangeKey is a key for a change that can be used when subscribing to them.
@ -25,17 +29,29 @@ type ChangeKey struct {
// Data makes a combined, mixed array of all the models stored in this change.
func (change *Change) Data() []interface{} {
data := make([]interface{}, 0, len(change.Logs)+len(change.Characters)+len(change.Posts))
data := make([]interface{}, 0, len(change.Logs)+len(change.Characters)+len(change.Channels)+len(change.Posts)+len(change.Stories)+len(change.Tags)+len(change.Chapters))
for _, log := range change.Logs {
data = append(data, log)
}
for _, channel := range change.Channels {
data = append(data, channel)
}
for _, character := range change.Characters {
data = append(data, character)
}
for _, post := range change.Posts {
data = append(data, post)
}
for _, story := range change.Stories {
data = append(data, story)
}
for _, tag := range change.Tags {
data = append(data, tag)
}
for _, chapter := range change.Chapters {
data = append(data, chapter)
}
return data
}

16
models/changes/submit.go

@ -38,10 +38,26 @@ func Submit(model, op, author string, listed bool, keys []models.ChangeKey, obje
change.Characters = append(change.Characters, object)
case []models.Character:
change.Characters = append(change.Characters, object...)
case models.Channel:
change.Channels = append(change.Channels, object)
case []models.Channel:
change.Channels = append(change.Channels, object...)
case models.Post:
change.Posts = append(change.Posts, object)
case []models.Post:
change.Posts = append(change.Posts, object...)
case models.Story:
change.Stories = append(change.Stories, object)
case []models.Story:
change.Stories = append(change.Stories, object...)
case models.Tag:
change.Tags = append(change.Tags, object)
case []models.Tag:
change.Tags = append(change.Tags, object...)
case models.Chapter:
change.Chapters = append(change.Chapters, object)
case []models.Chapter:
change.Chapters = append(change.Chapters, object...)
}
}

Loading…
Cancel
Save