Browse Source

Eugh.

module-madness-pointers
Gisle Aune 6 years ago
parent
commit
a5410dff2c
  1. 18
      graph2/queries/channel.go
  2. 56
      graph2/queries/chapter.go
  3. 58
      graph2/queries/story.go
  4. 11
      graph2/types/change.go
  5. 5
      models/channel.go
  6. 8
      models/channels/add.go
  7. 8
      models/channels/edit.go
  8. 10
      models/channels/ensure.go
  9. 7
      models/channels/find.go
  10. 5
      models/chapter.go
  11. 6
      models/chapters/add.go
  12. 7
      models/chapters/db.go
  13. 6
      models/chapters/edit.go
  14. 2
      models/chapters/find.go
  15. 6
      models/chapters/move.go
  16. 4
      models/chapters/remove.go
  17. 5
      models/character.go
  18. 5
      models/comment.go
  19. 5
      models/file.go
  20. 5
      models/log.go
  21. 5
      models/post.go
  22. 8
      models/stories/add-tag.go
  23. 6
      models/stories/add.go
  24. 7
      models/stories/db.go
  25. 8
      models/stories/edit.go
  26. 2
      models/stories/find.go
  27. 8
      models/stories/remove-tag.go
  28. 4
      models/stories/remove.go
  29. 5
      models/story.go
  30. 5
      models/tag.go

18
graph2/queries/channel.go

@ -14,7 +14,7 @@ import (
// Queries // Queries
func (r *resolver) Channel(ctx context.Context, name string) (models.Channel, error) {
func (r *resolver) Channel(ctx context.Context, name string) (*models.Channel, error) {
return channels.FindName(name) return channels.FindName(name)
} }
@ -24,10 +24,10 @@ func (r *resolver) Channels(ctx context.Context, filter *channels.Filter) ([]mod
// Mutations // Mutations
func (r *mutationResolver) AddChannel(ctx context.Context, input input.ChannelAddInput) (models.Channel, error) {
func (r *mutationResolver) AddChannel(ctx context.Context, input input.ChannelAddInput) (*models.Channel, error) {
token := auth.TokenFromContext(ctx) token := auth.TokenFromContext(ctx)
if !token.Authenticated() || !token.Permitted("channel.add") { if !token.Authenticated() || !token.Permitted("channel.add") {
return models.Channel{}, errors.New("You are not permitted to add channels")
return nil, errors.New("You are not permitted to add channels")
} }
logged := false logged := false
@ -49,7 +49,7 @@ func (r *mutationResolver) AddChannel(ctx context.Context, input input.ChannelAd
channel, err := channels.Add(input.Name, logged, hub, eventName, locationName) channel, err := channels.Add(input.Name, logged, hub, eventName, locationName)
if err != nil { if err != nil {
return models.Channel{}, errors.New("Failed to add channel: " + err.Error())
return nil, errors.New("Failed to add channel: " + err.Error())
} }
go changes.Submit("Channel", "add", token.UserID, true, changekeys.Listed(channel), channel) go changes.Submit("Channel", "add", token.UserID, true, changekeys.Listed(channel), channel)
@ -57,20 +57,20 @@ func (r *mutationResolver) AddChannel(ctx context.Context, input input.ChannelAd
return channel, nil return channel, nil
} }
func (r *mutationResolver) EditChannel(ctx context.Context, input input.ChannelEditInput) (models.Channel, error) {
func (r *mutationResolver) EditChannel(ctx context.Context, input input.ChannelEditInput) (*models.Channel, error) {
token := auth.TokenFromContext(ctx) token := auth.TokenFromContext(ctx)
if !token.Authenticated() || !token.Permitted("channel.edit") { if !token.Authenticated() || !token.Permitted("channel.edit") {
return models.Channel{}, errors.New("You are not permitted to edit channels")
return nil, errors.New("You are not permitted to edit channels")
} }
channel, err := channels.FindName(input.Name) channel, err := channels.FindName(input.Name)
if err != nil { if err != nil {
return models.Channel{}, errors.New("Channel not found")
return nil, errors.New("Channel not found")
} }
channel, err = channels.Edit(channel, input.Logged, input.Hub, input.EventName, input.LocationName)
channel, err = channels.Edit(*channel, input.Logged, input.Hub, input.EventName, input.LocationName)
if err != nil { if err != nil {
return models.Channel{}, errors.New("Failed to edit channel: " + err.Error())
return nil, errors.New("Failed to edit channel: " + err.Error())
} }
go changes.Submit("Channel", "edit", token.UserID, true, changekeys.Listed(channel), channel) go changes.Submit("Channel", "edit", token.UserID, true, changekeys.Listed(channel), channel)

56
graph2/queries/chapter.go

@ -19,34 +19,34 @@ import (
// Queries // Queries
func (r *resolver) Chapter(ctx context.Context, id string) (models.Chapter, error) {
func (r *resolver) Chapter(ctx context.Context, id string) (*models.Chapter, error) {
return chapters.FindID(id) return chapters.FindID(id)
} }
// Mutations // Mutations
func (r *mutationResolver) AddChapter(ctx context.Context, input input.ChapterAddInput) (models.Chapter, error) {
func (r *mutationResolver) AddChapter(ctx context.Context, input input.ChapterAddInput) (*models.Chapter, error) {
story, err := stories.FindID(input.StoryID) story, err := stories.FindID(input.StoryID)
if err != nil { if err != nil {
return models.Chapter{}, errors.New("Story not found")
return nil, errors.New("Story not found")
} }
token := auth.TokenFromContext(ctx) token := auth.TokenFromContext(ctx)
if !token.Permitted("member", "story.add") { if !token.Permitted("member", "story.add") {
return models.Chapter{}, errors.New("Unauthorized")
return nil, errors.New("Unauthorized")
} }
author := token.UserID author := token.UserID
if input.Author != nil && *input.Author != author { if input.Author != nil && *input.Author != author {
if !token.Permitted("story.add") { if !token.Permitted("story.add") {
return models.Chapter{}, errors.New("False pretender")
return nil, errors.New("False pretender")
} }
author = *input.Author author = *input.Author
} }
if !story.Open && story.Author != author { if !story.Open && story.Author != author {
return models.Chapter{}, errors.New("Story is not open")
return nil, errors.New("Story is not open")
} }
commentMode := models.ChapterCommentModeDisabled commentMode := models.ChapterCommentModeDisabled
@ -54,40 +54,40 @@ func (r *mutationResolver) AddChapter(ctx context.Context, input input.ChapterAd
commentMode = *input.CommentMode commentMode = *input.CommentMode
} }
chapter, err := chapters.Add(story, input.Title, author, input.Source, time.Now(), input.FictionalDate, commentMode)
chapter, err := chapters.Add(*story, input.Title, author, input.Source, time.Now(), input.FictionalDate, commentMode)
if err != nil { if err != nil {
return models.Chapter{}, errors.New("Failed to create chapter: " + err.Error())
return nil, errors.New("Failed to create chapter: " + err.Error())
} }
go changes.Submit("Chapter", "add", token.UserID, story.Listed, changekeys.Listed(story, chapter), story, chapter) go changes.Submit("Chapter", "add", token.UserID, story.Listed, changekeys.Listed(story, chapter), story, chapter)
return chapter, nil
return &chapter, nil
} }
func (r *mutationResolver) MoveChapter(ctx context.Context, input input.ChapterMoveInput) (models.Chapter, error) {
func (r *mutationResolver) MoveChapter(ctx context.Context, input input.ChapterMoveInput) (*models.Chapter, error) {
chapter, err := chapters.FindID(input.ID) chapter, err := chapters.FindID(input.ID)
if err != nil { if err != nil {
return models.Chapter{}, errors.New("Chapter not found")
return nil, errors.New("Chapter not found")
} }
token := auth.TokenFromContext(ctx) token := auth.TokenFromContext(ctx)
if !token.Authenticated() || !token.PermittedUser(chapter.Author, "member", "chapter.move") { if !token.Authenticated() || !token.PermittedUser(chapter.Author, "member", "chapter.move") {
return models.Chapter{}, errors.New("You are not allowed to move this chapter")
return nil, errors.New("You are not allowed to move this chapter")
} }
target, err := stories.FindID(input.StoryID) target, err := stories.FindID(input.StoryID)
if err != nil { if err != nil {
return models.Chapter{}, errors.New("Target story not found")
return nil, errors.New("Target story not found")
} }
if !target.Open && !token.PermittedUser(target.Author, "member", "chapter.move") { if !target.Open && !token.PermittedUser(target.Author, "member", "chapter.move") {
return models.Chapter{}, errors.New("You are not permitted to move chapters to this story")
return nil, errors.New("You are not permitted to move chapters to this story")
} }
oldStoryID := chapter.StoryID oldStoryID := chapter.StoryID
chapter, err = chapters.Move(chapter, target)
chapter, err = chapters.Move(chapter, *target)
if err != nil { if err != nil {
return models.Chapter{}, errors.New("Failed to move chapter: " + err.Error())
return nil, errors.New("Failed to move chapter: " + err.Error())
} }
go func() { go func() {
@ -105,18 +105,18 @@ func (r *mutationResolver) MoveChapter(ctx context.Context, input input.ChapterM
changes.Submit("Chapter", "move-in", token.UserID, story.Listed, changekeys.Listed(story, chapter), story, chapter) changes.Submit("Chapter", "move-in", token.UserID, story.Listed, changekeys.Listed(story, chapter), story, chapter)
}() }()
return chapter, nil
return &chapter, nil
} }
func (r *mutationResolver) EditChapter(ctx context.Context, input input.ChapterEditInput) (models.Chapter, error) {
func (r *mutationResolver) EditChapter(ctx context.Context, input input.ChapterEditInput) (*models.Chapter, error) {
chapter, err := chapters.FindID(input.ID) chapter, err := chapters.FindID(input.ID)
if err != nil { if err != nil {
return models.Chapter{}, errors.New("Chapter not found")
return nil, errors.New("Chapter not found")
} }
token := auth.TokenFromContext(ctx) token := auth.TokenFromContext(ctx)
if !token.Authenticated() || !token.PermittedUser(chapter.Author, "member", "chapter.edit") { if !token.Authenticated() || !token.PermittedUser(chapter.Author, "member", "chapter.edit") {
return models.Chapter{}, errors.New("Unauthorized")
return nil, errors.New("Unauthorized")
} }
if input.ClearFictionalDate != nil && *input.ClearFictionalDate == true { if input.ClearFictionalDate != nil && *input.ClearFictionalDate == true {
@ -125,7 +125,7 @@ func (r *mutationResolver) EditChapter(ctx context.Context, input input.ChapterE
chapter, err = chapters.Edit(chapter, input.Title, input.Source, input.FictionalDate, input.CommentMode, input.CommentsLocked) chapter, err = chapters.Edit(chapter, input.Title, input.Source, input.FictionalDate, input.CommentMode, input.CommentsLocked)
if err != nil { if err != nil {
return models.Chapter{}, errors.New("Failed to edit chapter: " + err.Error())
return nil, errors.New("Failed to edit chapter: " + err.Error())
} }
go func() { go func() {
@ -137,28 +137,28 @@ func (r *mutationResolver) EditChapter(ctx context.Context, input input.ChapterE
changes.Submit("Chapter", "edit", token.UserID, story.Listed, changekeys.Many(story, chapter), chapter) changes.Submit("Chapter", "edit", token.UserID, story.Listed, changekeys.Many(story, chapter), chapter)
}() }()
return chapter, nil
return &chapter, nil
} }
func (r *mutationResolver) RemoveChapter(ctx context.Context, input input.ChapterRemoveInput) (models.Chapter, error) {
func (r *mutationResolver) RemoveChapter(ctx context.Context, input input.ChapterRemoveInput) (*models.Chapter, error) {
chapter, err := chapters.FindID(input.ID) chapter, err := chapters.FindID(input.ID)
if err != nil { if err != nil {
return models.Chapter{}, errors.New("Chapter not found")
return nil, errors.New("Chapter not found")
} }
token := auth.TokenFromContext(ctx) token := auth.TokenFromContext(ctx)
if !token.Authenticated() || !token.PermittedUser(chapter.Author, "member", "chapter.remove") { if !token.Authenticated() || !token.PermittedUser(chapter.Author, "member", "chapter.remove") {
return models.Chapter{}, errors.New("Unauthorized")
return nil, errors.New("Unauthorized")
} }
chapter, err = chapters.Remove(chapter) chapter, err = chapters.Remove(chapter)
if err != nil { if err != nil {
return models.Chapter{}, errors.New("Failed to remove chapter: " + err.Error())
return nil, errors.New("Failed to remove chapter: " + err.Error())
} }
err = comments.RemoveChapter(chapter) err = comments.RemoveChapter(chapter)
if err != nil { if err != nil {
return models.Chapter{}, errors.New("Chapter was removed, but comment removal failed: " + err.Error())
return nil, errors.New("Chapter was removed, but comment removal failed: " + err.Error())
} }
go func() { go func() {
@ -170,5 +170,5 @@ func (r *mutationResolver) RemoveChapter(ctx context.Context, input input.Chapte
changes.Submit("Chapter", "remove", token.UserID, story.Listed, changekeys.Many(story, chapter), chapter) changes.Submit("Chapter", "remove", token.UserID, story.Listed, changekeys.Many(story, chapter), chapter)
}() }()
return chapter, nil
return &chapter, nil
} }

58
graph2/queries/story.go

@ -15,7 +15,7 @@ import (
"git.aiterp.net/rpdata/api/models/stories" "git.aiterp.net/rpdata/api/models/stories"
) )
func (r *resolver) Story(ctx context.Context, id string) (models.Story, error) {
func (r *resolver) Story(ctx context.Context, id string) (*models.Story, error) {
return stories.FindID(id) return stories.FindID(id)
} }
@ -38,16 +38,16 @@ func (r *resolver) Stories(ctx context.Context, filter *stories.Filter) ([]model
// Mutations // Mutations
func (r *mutationResolver) AddStory(ctx context.Context, input input.StoryAddInput) (models.Story, error) {
func (r *mutationResolver) AddStory(ctx context.Context, input input.StoryAddInput) (*models.Story, error) {
token := auth.TokenFromContext(ctx) token := auth.TokenFromContext(ctx)
if token == nil || !token.Permitted("member", "story.add") { if token == nil || !token.Permitted("member", "story.add") {
return models.Story{}, errors.New("Permission denied")
return nil, errors.New("Permission denied")
} }
author := token.UserID author := token.UserID
if input.Author != nil && *input.Author != author { if input.Author != nil && *input.Author != author {
if !token.Permitted("story.add") { if !token.Permitted("story.add") {
return models.Story{}, errors.New("You are not permitted to add a story in another author's name")
return nil, errors.New("You are not permitted to add a story in another author's name")
} }
author = *input.Author author = *input.Author
@ -63,7 +63,7 @@ func (r *mutationResolver) AddStory(ctx context.Context, input input.StoryAddInp
story, err := 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 { if err != nil {
return models.Story{}, errors.New("Failed to add story: " + err.Error())
return nil, errors.New("Failed to add story: " + err.Error())
} }
go changes.Submit("Story", "add", token.UserID, story.Listed, changekeys.Listed(story), story) go changes.Submit("Story", "add", token.UserID, story.Listed, changekeys.Listed(story), story)
@ -71,27 +71,27 @@ func (r *mutationResolver) AddStory(ctx context.Context, input input.StoryAddInp
return story, nil return story, nil
} }
func (r *mutationResolver) AddStoryTag(ctx context.Context, input input.StoryTagAddInput) (models.Story, error) {
func (r *mutationResolver) AddStoryTag(ctx context.Context, input input.StoryTagAddInput) (*models.Story, error) {
token := auth.TokenFromContext(ctx) token := auth.TokenFromContext(ctx)
story, err := stories.FindID(input.ID) story, err := stories.FindID(input.ID)
if err != nil { if err != nil {
return models.Story{}, errors.New("Story not found")
return nil, errors.New("Story not found")
} }
if story.Open { if story.Open {
if !token.Permitted("member") { if !token.Permitted("member") {
return models.Story{}, errors.New("You are not permitted to edit this story")
return nil, errors.New("You are not permitted to edit this story")
} }
} else { } else {
if !token.PermittedUser(story.Author, "member", "story.edit") { if !token.PermittedUser(story.Author, "member", "story.edit") {
return models.Story{}, errors.New("You are not permitted to edit this story")
return nil, errors.New("You are not permitted to edit this story")
} }
} }
story, err = stories.AddTag(story, input.Tag)
story, err = stories.AddTag(*story, input.Tag)
if err != nil { if err != nil {
return models.Story{}, errors.New("Failed to add story: " + err.Error())
return nil, errors.New("Failed to add story: " + err.Error())
} }
go changes.Submit("Story", "tag", token.UserID, story.Listed, changekeys.Listed(story), story, input.Tag) go changes.Submit("Story", "tag", token.UserID, story.Listed, changekeys.Listed(story), story, input.Tag)
@ -99,27 +99,27 @@ func (r *mutationResolver) AddStoryTag(ctx context.Context, input input.StoryTag
return story, nil return story, nil
} }
func (r *mutationResolver) RemoveStoryTag(ctx context.Context, input input.StoryTagRemoveInput) (models.Story, error) {
func (r *mutationResolver) RemoveStoryTag(ctx context.Context, input input.StoryTagRemoveInput) (*models.Story, error) {
token := auth.TokenFromContext(ctx) token := auth.TokenFromContext(ctx)
story, err := stories.FindID(input.ID) story, err := stories.FindID(input.ID)
if err != nil { if err != nil {
return models.Story{}, errors.New("Story not found")
return nil, errors.New("Story not found")
} }
if story.Open { if story.Open {
if !token.Permitted("member") { if !token.Permitted("member") {
return models.Story{}, errors.New("You are not permitted to edit this story")
return nil, errors.New("You are not permitted to edit this story")
} }
} else { } else {
if !token.PermittedUser(story.Author, "member", "story.edit") { if !token.PermittedUser(story.Author, "member", "story.edit") {
return models.Story{}, errors.New("You are not permitted to edit this story")
return nil, errors.New("You are not permitted to edit this story")
} }
} }
story, err = stories.RemoveTag(story, input.Tag)
story, err = stories.RemoveTag(*story, input.Tag)
if err != nil { if err != nil {
return models.Story{}, errors.New("Failed to add story: " + err.Error())
return nil, errors.New("Failed to add story: " + err.Error())
} }
go changes.Submit("Story", "untag", token.UserID, story.Listed, changekeys.Listed(story), story, input.Tag) go changes.Submit("Story", "untag", token.UserID, story.Listed, changekeys.Listed(story), story, input.Tag)
@ -127,25 +127,25 @@ func (r *mutationResolver) RemoveStoryTag(ctx context.Context, input input.Story
return story, nil return story, nil
} }
func (r *mutationResolver) EditStory(ctx context.Context, input input.StoryEditInput) (models.Story, error) {
func (r *mutationResolver) EditStory(ctx context.Context, input input.StoryEditInput) (*models.Story, error) {
token := auth.TokenFromContext(ctx) token := auth.TokenFromContext(ctx)
story, err := stories.FindID(input.ID) story, err := stories.FindID(input.ID)
if err != nil { if err != nil {
return models.Story{}, errors.New("Story not found")
return nil, errors.New("Story not found")
} }
if !token.PermittedUser(story.Author, "member", "story.edit") { if !token.PermittedUser(story.Author, "member", "story.edit") {
return models.Story{}, errors.New("You are not permitted to remove this story")
return nil, errors.New("You are not permitted to remove this story")
} }
if input.ClearFictionalDate != nil && *input.ClearFictionalDate { if input.ClearFictionalDate != nil && *input.ClearFictionalDate {
input.FictionalDate = &time.Time{} input.FictionalDate = &time.Time{}
} }
story, err = 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 { if err != nil {
return models.Story{}, errors.New("Failed to add story: " + err.Error())
return nil, errors.New("Failed to add story: " + err.Error())
} }
go changes.Submit("Story", "edit", token.UserID, story.Listed, changekeys.Listed(story), story) go changes.Submit("Story", "edit", token.UserID, story.Listed, changekeys.Listed(story), story)
@ -153,26 +153,26 @@ func (r *mutationResolver) EditStory(ctx context.Context, input input.StoryEditI
return story, nil return story, nil
} }
func (r *mutationResolver) RemoveStory(ctx context.Context, input input.StoryRemoveInput) (models.Story, error) {
func (r *mutationResolver) RemoveStory(ctx context.Context, input input.StoryRemoveInput) (*models.Story, error) {
token := auth.TokenFromContext(ctx) token := auth.TokenFromContext(ctx)
story, err := stories.FindID(input.ID) story, err := stories.FindID(input.ID)
if err != nil { if err != nil {
return models.Story{}, errors.New("Story not found")
return nil, errors.New("Story not found")
} }
if !token.PermittedUser(story.Author, "member", "story.remove") { if !token.PermittedUser(story.Author, "member", "story.remove") {
return models.Story{}, errors.New("You are not permitted to remove this story")
return nil, errors.New("You are not permitted to remove this story")
} }
story, err = stories.Remove(story)
story, err = stories.Remove(*story)
if err != nil { if err != nil {
return models.Story{}, err
return nil, err
} }
err = chapters.RemoveStory(story)
err = chapters.RemoveStory(*story)
if err != nil { if err != nil {
return models.Story{}, errors.New("Failed to remove chapters, but story is removed: " + err.Error())
return nil, 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) go changes.Submit("Story", "remove", token.UserID, story.Listed, changekeys.Listed(story), story)

11
graph2/types/change.go

@ -2,6 +2,7 @@ package types
import ( import (
"context" "context"
"log"
"git.aiterp.net/rpdata/api/graph2/input" "git.aiterp.net/rpdata/api/graph2/input"
"git.aiterp.net/rpdata/api/models" "git.aiterp.net/rpdata/api/models"
@ -12,9 +13,13 @@ type changeResolver struct{}
func (r *changeResolver) Objects(ctx context.Context, obj *models.Change) ([]input.ChangeObject, error) { func (r *changeResolver) Objects(ctx context.Context, obj *models.Change) ([]input.ChangeObject, error) {
objects := obj.Objects() objects := obj.Objects()
results := make([]input.ChangeObject, len(objects))
for i := range objects {
results[i] = objects[i]
results := make([]input.ChangeObject, 0, len(objects))
for _, object := range objects {
if cObj, ok := object.(input.ChangeObject); ok {
results = append(results, cObj)
} else {
log.Printf("Warning: %T is not a change object", object)
}
} }
return results, nil return results, nil

5
models/channel.go

@ -8,3 +8,8 @@ type Channel struct {
EventName string `bson:"eventName,omitempty"` EventName string `bson:"eventName,omitempty"`
LocationName string `bson:"locationName,omitempty"` LocationName string `bson:"locationName,omitempty"`
} }
// IsChangeObject is a dummy interface implementation
func (*Channel) IsChangeObject() {
// Dummy interface implementation
}

8
models/channels/add.go

@ -11,9 +11,9 @@ import (
var ErrInvalidName = errors.New("Invalid channel name") var ErrInvalidName = errors.New("Invalid channel name")
// Add creates a new channel. // Add creates a new channel.
func Add(name string, logged, hub bool, event, location string) (models.Channel, error) {
func Add(name string, logged, hub bool, event, location string) (*models.Channel, error) {
if len(name) < 3 && !strings.HasPrefix(name, "#") { if len(name) < 3 && !strings.HasPrefix(name, "#") {
return models.Channel{}, ErrInvalidName
return nil, ErrInvalidName
} }
channel := models.Channel{ channel := models.Channel{
@ -26,8 +26,8 @@ func Add(name string, logged, hub bool, event, location string) (models.Channel,
err := collection.Insert(channel) err := collection.Insert(channel)
if err != nil { if err != nil {
return models.Channel{}, err
return nil, err
} }
return channel, nil
return &channel, nil
} }

8
models/channels/edit.go

@ -6,7 +6,7 @@ import (
) )
// Edit edits a channels // Edit edits a channels
func Edit(channel models.Channel, logged, hub *bool, eventName, locationName *string) (models.Channel, error) {
func Edit(channel models.Channel, logged, hub *bool, eventName, locationName *string) (*models.Channel, error) {
mutation := bson.M{} mutation := bson.M{}
if logged != nil && *logged != channel.Logged { if logged != nil && *logged != channel.Logged {
@ -27,13 +27,13 @@ func Edit(channel models.Channel, logged, hub *bool, eventName, locationName *st
} }
if len(mutation) == 0 { if len(mutation) == 0 {
return channel, nil
return &channel, nil
} }
err := collection.UpdateId(channel.Name, bson.M{"$set": mutation}) err := collection.UpdateId(channel.Name, bson.M{"$set": mutation})
if err != nil { if err != nil {
return models.Channel{}, err
return nil, err
} }
return channel, nil
return &channel, nil
} }

10
models/channels/ensure.go

@ -10,22 +10,22 @@ import (
// Ensure adds a channel if it doesn't exist. If logged is set and the found channel isn't logged, // Ensure adds a channel if it doesn't exist. If logged is set and the found channel isn't logged,
// that is changed. // that is changed.
func Ensure(name string, logged bool) (models.Channel, error) {
func Ensure(name string, logged bool) (*models.Channel, error) {
if len(name) < 3 && !strings.HasPrefix(name, "#") { if len(name) < 3 && !strings.HasPrefix(name, "#") {
return models.Channel{}, ErrInvalidName
return nil, ErrInvalidName
} }
channel, err := FindName(name) channel, err := FindName(name)
if err == mgo.ErrNotFound { if err == mgo.ErrNotFound {
return Add(name, logged, false, "", "") return Add(name, logged, false, "", "")
} else if err != nil { } else if err != nil {
return models.Channel{}, err
return nil, err
} }
if logged && !channel.Logged { if logged && !channel.Logged {
channel, err = Edit(channel, &logged, nil, nil, nil)
channel, err = Edit(*channel, &logged, nil, nil, nil)
if err != nil { if err != nil {
return models.Channel{}, err
return nil, err
} }
} }

7
models/channels/find.go

@ -3,9 +3,12 @@ package channels
import "git.aiterp.net/rpdata/api/models" import "git.aiterp.net/rpdata/api/models"
// FindName finds a channel by its id (its name). // FindName finds a channel by its id (its name).
func FindName(name string) (models.Channel, error) {
func FindName(name string) (*models.Channel, error) {
channel := models.Channel{} channel := models.Channel{}
err := collection.FindId(name).One(&channel) err := collection.FindId(name).One(&channel)
if err != nil {
return nil, err
}
return channel, err
return &channel, nil
} }

5
models/chapter.go

@ -20,3 +20,8 @@ type Chapter struct {
func (chapter *Chapter) CanComment() bool { func (chapter *Chapter) CanComment() bool {
return !chapter.CommentsLocked && chapter.CommentMode.IsEnabled() return !chapter.CommentsLocked && chapter.CommentMode.IsEnabled()
} }
// IsChangeObject is a dummy interface implementation
func (*Chapter) IsChangeObject() {
// Dummy interface implementation
}

6
models/chapters/add.go

@ -8,7 +8,7 @@ import (
) )
// Add adds a new chapter. // Add adds a new chapter.
func Add(story models.Story, title, author, source string, createdDate time.Time, finctionalDate *time.Time, commentMode models.ChapterCommentMode) (models.Chapter, error) {
func Add(story models.Story, title, author, source string, createdDate time.Time, finctionalDate *time.Time, commentMode models.ChapterCommentMode) (*models.Chapter, error) {
chapter := models.Chapter{ chapter := models.Chapter{
ID: makeChapterID(), ID: makeChapterID(),
StoryID: story.ID, StoryID: story.ID,
@ -27,7 +27,7 @@ func Add(story models.Story, title, author, source string, createdDate time.Time
err := collection.Insert(chapter) err := collection.Insert(chapter)
if err != nil { if err != nil {
return models.Chapter{}, err
return nil, err
} }
if createdDate.After(story.UpdatedDate) { if createdDate.After(story.UpdatedDate) {
@ -36,5 +36,5 @@ func Add(story models.Story, title, author, source string, createdDate time.Time
} }
} }
return chapter, nil
return &chapter, nil
} }

7
models/chapters/db.go

@ -13,11 +13,14 @@ import (
var collection *mgo.Collection var collection *mgo.Collection
var storyCollection *mgo.Collection var storyCollection *mgo.Collection
func find(query interface{}) (models.Chapter, error) {
func find(query interface{}) (*models.Chapter, error) {
chapter := models.Chapter{} chapter := models.Chapter{}
err := collection.Find(query).One(&chapter) err := collection.Find(query).One(&chapter)
if err != nil {
return nil, err
}
return chapter, err
return &chapter, err
} }
func list(query interface{}) ([]models.Chapter, error) { func list(query interface{}) ([]models.Chapter, error) {

6
models/chapters/edit.go

@ -9,7 +9,7 @@ import (
// Edit edits a chapter, and updates EditedDate. While many Edit functions cheat if there's nothing to // Edit edits a chapter, and updates EditedDate. While many Edit functions cheat if there's nothing to
// change, this functill will due to EditedDate. // change, this functill will due to EditedDate.
func Edit(chapter models.Chapter, title, source *string, fictionalDate *time.Time, commentMode *models.ChapterCommentMode, commentsLocked *bool) (models.Chapter, error) {
func Edit(chapter models.Chapter, title, source *string, fictionalDate *time.Time, commentMode *models.ChapterCommentMode, commentsLocked *bool) (*models.Chapter, error) {
now := time.Now() now := time.Now()
changes := bson.M{"editedDate": now} changes := bson.M{"editedDate": now}
@ -39,8 +39,8 @@ func Edit(chapter models.Chapter, title, source *string, fictionalDate *time.Tim
err := collection.UpdateId(chapter.ID, bson.M{"$set": changes}) err := collection.UpdateId(chapter.ID, bson.M{"$set": changes})
if err != nil { if err != nil {
return chapter, err
return nil, err
} }
return edited, nil
return &edited, nil
} }

2
models/chapters/find.go

@ -6,6 +6,6 @@ import (
) )
// FindID finds a chapter by ID // FindID finds a chapter by ID
func FindID(id string) (models.Chapter, error) {
func FindID(id string) (*models.Chapter, error) {
return find(bson.M{"_id": id}) return find(bson.M{"_id": id})
} }

6
models/chapters/move.go

@ -8,12 +8,12 @@ import (
) )
// Move updates the chapter, moving it to the given story. // Move updates the chapter, moving it to the given story.
func Move(chapter models.Chapter, story models.Story) (models.Chapter, error) {
func Move(chapter models.Chapter, story models.Story) (*models.Chapter, error) {
now := time.Now() now := time.Now()
err := collection.UpdateId(chapter.ID, bson.M{"$set": bson.M{"editedDate": now, "storyId": story.ID}}) err := collection.UpdateId(chapter.ID, bson.M{"$set": bson.M{"editedDate": now, "storyId": story.ID}})
if err != nil { if err != nil {
return models.Chapter{}, err
return nil, err
} }
chapter.EditedDate = now chapter.EditedDate = now
@ -24,5 +24,5 @@ func Move(chapter models.Chapter, story models.Story) (models.Chapter, error) {
} }
} }
return chapter, nil
return &chapter, nil
} }

4
models/chapters/remove.go

@ -6,9 +6,9 @@ import (
) )
// Remove removes a chapter. // Remove removes a chapter.
func Remove(chapter models.Chapter) (models.Chapter, error) {
func Remove(chapter models.Chapter) (*models.Chapter, error) {
if err := collection.RemoveId(chapter.ID); err != nil { if err := collection.RemoveId(chapter.ID); err != nil {
return models.Chapter{}, err
return nil, err
} }
return chapter, nil return chapter, nil

5
models/character.go

@ -29,3 +29,8 @@ func (character *Character) HasNick(nick string) bool {
return false return false
} }
// IsChangeObject is a dummy interface implementation
func (*Character) IsChangeObject() {
// Dummy interface implementation
}

5
models/comment.go

@ -15,3 +15,8 @@ type Comment struct {
EditedDate time.Time `bson:"editeddDate"` EditedDate time.Time `bson:"editeddDate"`
Source string `bson:"source"` Source string `bson:"source"`
} }
// IsChangeObject is a dummy interface implementation
func (*Comment) IsChangeObject() {
// Dummy interface implementation
}

5
models/file.go

@ -14,3 +14,8 @@ type File struct {
Author string `bson:"author" json:"author"` Author string `bson:"author" json:"author"`
URL string `bson:"url,omitempty" json:"url,omitempty"` URL string `bson:"url,omitempty" json:"url,omitempty"`
} }
// IsChangeObject is a dummy interface implementation
func (*File) IsChangeObject() {
// Dummy interface implementation
}

5
models/log.go

@ -14,3 +14,8 @@ type Log struct {
Open bool `bson:"open"` Open bool `bson:"open"`
CharacterIDs []string `bson:"characterIds"` CharacterIDs []string `bson:"characterIds"`
} }
// IsChangeObject is a dummy interface implementation
func (*Log) IsChangeObject() {
// Dummy interface implementation
}

5
models/post.go

@ -12,3 +12,8 @@ type Post struct {
Text string `bson:"text"` Text string `bson:"text"`
Position int `bson:"position"` Position int `bson:"position"`
} }
// IsChangeObject is a dummy interface implementation
func (*Post) IsChangeObject() {
// Dummy interface implementation
}

8
models/stories/add-tag.go

@ -11,19 +11,19 @@ import (
var ErrTagAlreadyExists = errors.New("Tag already exists on story") var ErrTagAlreadyExists = errors.New("Tag already exists on story")
// AddTag adds a tag to the story. It returns ErrTagAlreadyExists if the tag is already there // AddTag adds a tag to the story. It returns ErrTagAlreadyExists if the tag is already there
func AddTag(story models.Story, tag models.Tag) (models.Story, error) {
func AddTag(story models.Story, tag models.Tag) (*models.Story, error) {
for i := range story.Tags { for i := range story.Tags {
if story.Tags[i].Equal(tag) { if story.Tags[i].Equal(tag) {
return models.Story{}, ErrTagAlreadyExists
return nil, ErrTagAlreadyExists
} }
} }
err := collection.UpdateId(story.ID, bson.M{"$push": bson.M{"tags": tag}}) err := collection.UpdateId(story.ID, bson.M{"$push": bson.M{"tags": tag}})
if err != nil { if err != nil {
return models.Story{}, err
return nil, err
} }
story.Tags = append(story.Tags, tag) story.Tags = append(story.Tags, tag)
return story, nil
return &story, nil
} }

6
models/stories/add.go

@ -7,7 +7,7 @@ import (
) )
// Add creates a new story. // Add creates a new story.
func Add(name, author string, category models.StoryCategory, listed, open bool, tags []models.Tag, createdDate, fictionalDate time.Time) (models.Story, error) {
func Add(name, author string, category models.StoryCategory, listed, open bool, tags []models.Tag, createdDate, fictionalDate time.Time) (*models.Story, error) {
story := models.Story{ story := models.Story{
ID: makeStoryID(), ID: makeStoryID(),
Name: name, Name: name,
@ -23,8 +23,8 @@ func Add(name, author string, category models.StoryCategory, listed, open bool,
err := collection.Insert(story) err := collection.Insert(story)
if err != nil { if err != nil {
return models.Story{}, err
return nil, err
} }
return story, nil
return &story, nil
} }

7
models/stories/db.go

@ -12,11 +12,14 @@ import (
var collection *mgo.Collection var collection *mgo.Collection
func find(query interface{}) (models.Story, error) {
func find(query interface{}) (*models.Story, error) {
story := models.Story{} story := models.Story{}
err := collection.Find(query).One(&story) err := collection.Find(query).One(&story)
if err != nil {
return nil, err
}
return story, err
return &story, err
} }
func list(query interface{}, limit int) ([]models.Story, error) { func list(query interface{}, limit int) ([]models.Story, error) {

8
models/stories/edit.go

@ -8,7 +8,7 @@ import (
) )
// Edit edits the story and returns the edited story if it succeeds. // Edit edits the story and returns the edited story if it succeeds.
func Edit(story models.Story, name *string, category *models.StoryCategory, listed, open *bool, fictionalDate *time.Time) (models.Story, error) {
func Edit(story models.Story, name *string, category *models.StoryCategory, listed, open *bool, fictionalDate *time.Time) (*models.Story, error) {
changes := bson.M{} changes := bson.M{}
if name != nil && *name != story.Name { if name != nil && *name != story.Name {
@ -33,13 +33,13 @@ func Edit(story models.Story, name *string, category *models.StoryCategory, list
} }
if len(changes) == 0 { if len(changes) == 0 {
return story, nil
return &story, nil
} }
err := collection.UpdateId(story.ID, bson.M{"$set": changes}) err := collection.UpdateId(story.ID, bson.M{"$set": changes})
if err != nil { if err != nil {
return models.Story{}, err
return nil, err
} }
return story, nil
return &story, nil
} }

2
models/stories/find.go

@ -6,6 +6,6 @@ import (
) )
// FindID finds a story by ID // FindID finds a story by ID
func FindID(id string) (models.Story, error) {
func FindID(id string) (*models.Story, error) {
return find(bson.M{"_id": id}) return find(bson.M{"_id": id})
} }

8
models/stories/remove-tag.go

@ -11,7 +11,7 @@ import (
var ErrTagNotExists = errors.New("Tag does not exist on story") var ErrTagNotExists = errors.New("Tag does not exist on story")
// RemoveTag removes a tag to the story. It returns ErrTagNotExists if the tag does not exist. // RemoveTag removes a tag to the story. It returns ErrTagNotExists if the tag does not exist.
func RemoveTag(story models.Story, tag models.Tag) (models.Story, error) {
func RemoveTag(story models.Story, tag models.Tag) (*models.Story, error) {
index := -1 index := -1
for i := range story.Tags { for i := range story.Tags {
if story.Tags[i].Equal(tag) { if story.Tags[i].Equal(tag) {
@ -20,15 +20,15 @@ func RemoveTag(story models.Story, tag models.Tag) (models.Story, error) {
} }
} }
if index == -1 { if index == -1 {
return models.Story{}, ErrTagNotExists
return nil, ErrTagNotExists
} }
err := collection.UpdateId(story.ID, bson.M{"$pull": bson.M{"tags": tag}}) err := collection.UpdateId(story.ID, bson.M{"$pull": bson.M{"tags": tag}})
if err != nil { if err != nil {
return models.Story{}, err
return nil, err
} }
story.Tags = append(story.Tags[:index], story.Tags[index+1:]...) story.Tags = append(story.Tags[:index], story.Tags[index+1:]...)
return story, nil
return &story, nil
} }

4
models/stories/remove.go

@ -5,6 +5,6 @@ import (
) )
// Remove the story from the database // Remove the story from the database
func Remove(story models.Story) (models.Story, error) {
return story, collection.RemoveId(story.ID)
func Remove(story models.Story) (*models.Story, error) {
return &story, collection.RemoveId(story.ID)
} }

5
models/story.go

@ -16,3 +16,8 @@ type Story struct {
FictionalDate time.Time `bson:"fictionalDate,omitempty"` FictionalDate time.Time `bson:"fictionalDate,omitempty"`
UpdatedDate time.Time `bson:"updatedDate"` UpdatedDate time.Time `bson:"updatedDate"`
} }
// IsChangeObject is a dummy interface implementation
func (*Story) IsChangeObject() {
// Dummy interface implementation
}

5
models/tag.go

@ -10,3 +10,8 @@ type Tag struct {
func (tag *Tag) Equal(other Tag) bool { func (tag *Tag) Equal(other Tag) bool {
return tag.Kind == other.Kind && tag.Name == other.Name return tag.Kind == other.Kind && tag.Name == other.Name
} }
// IsChangeObject is a dummy interface implementation
func (*Tag) IsChangeObject() {
// Dummy interface implementation
}
Loading…
Cancel
Save