|
@ -2,7 +2,18 @@ package queries |
|
|
|
|
|
|
|
|
import ( |
|
|
import ( |
|
|
"context" |
|
|
"context" |
|
|
|
|
|
"errors" |
|
|
|
|
|
"time" |
|
|
|
|
|
|
|
|
|
|
|
"git.aiterp.net/rpdata/api/models/characters" |
|
|
|
|
|
|
|
|
|
|
|
"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/chapters" |
|
|
"git.aiterp.net/rpdata/api/models/comments" |
|
|
"git.aiterp.net/rpdata/api/models/comments" |
|
|
|
|
|
|
|
|
"git.aiterp.net/rpdata/api/models" |
|
|
"git.aiterp.net/rpdata/api/models" |
|
@ -13,3 +24,52 @@ import ( |
|
|
func (r *resolver) Comment(ctx context.Context, id string) (models.Comment, error) { |
|
|
func (r *resolver) Comment(ctx context.Context, id string) (models.Comment, error) { |
|
|
return comments.Find(id) |
|
|
return comments.Find(id) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Mutations
|
|
|
|
|
|
|
|
|
|
|
|
func (r *mutationResolver) AddComment(ctx context.Context, input input.CommentAddInput) (models.Comment, error) { |
|
|
|
|
|
chapter, err := chapters.FindID(input.ChapterID) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
return models.Comment{}, errors.New("Chapter not found") |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
token := auth.TokenFromContext(ctx) |
|
|
|
|
|
if !token.Permitted("member", "story.edit") { |
|
|
|
|
|
return models.Comment{}, errors.New("Unauthorized") |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if !chapter.CanComment() { |
|
|
|
|
|
return models.Comment{}, errors.New("You cannot comment on this chapter") |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
var characterPtr *models.Character |
|
|
|
|
|
if input.CharacterID != nil { |
|
|
|
|
|
character, err := characters.FindID(*input.CharacterID) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
return models.Comment{}, errors.New("Character not found") |
|
|
|
|
|
} else if character.Author != token.UserID { |
|
|
|
|
|
return models.Comment{}, errors.New("That is not your character") |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
characterPtr = &character |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fictionalDate := time.Time{} |
|
|
|
|
|
if input.FictionalDate != nil { |
|
|
|
|
|
fictionalDate = *input.FictionalDate |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
subject := "" |
|
|
|
|
|
if input.Subject != nil { |
|
|
|
|
|
subject = *input.Subject |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
comment, err := comments.Add(chapter, subject, token.UserID, input.Source, input.CharacterName, characterPtr, time.Now(), fictionalDate) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
return models.Comment{}, errors.New("Failed to add comment: " + err.Error()) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
go changes.Submit("Comment", "add", token.UserID, true, changekeys.Many(comment, chapter, models.Story{ID: chapter.StoryID}), comment) |
|
|
|
|
|
|
|
|
|
|
|
return comment, nil |
|
|
|
|
|
} |