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.
175 lines
4.8 KiB
175 lines
4.8 KiB
package resolvers
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"log"
|
|
"time"
|
|
|
|
"git.aiterp.net/rpdata/api/graph2/graphcore"
|
|
"git.aiterp.net/rpdata/api/internal/auth"
|
|
"git.aiterp.net/rpdata/api/models"
|
|
"git.aiterp.net/rpdata/api/models/changekeys"
|
|
"git.aiterp.net/rpdata/api/models/changes"
|
|
"git.aiterp.net/rpdata/api/models/chapters"
|
|
"git.aiterp.net/rpdata/api/models/characters"
|
|
"git.aiterp.net/rpdata/api/models/comments"
|
|
"git.aiterp.net/rpdata/api/models/stories"
|
|
)
|
|
|
|
// Queries
|
|
|
|
func (r *queryResolver) Comment(ctx context.Context, id string) (*models.Comment, error) {
|
|
comment, err := comments.Find(id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &comment, nil
|
|
}
|
|
|
|
// Mutations
|
|
|
|
func (r *mutationResolver) AddComment(ctx context.Context, input graphcore.CommentAddInput) (*models.Comment, error) {
|
|
chapter, err := chapters.FindID(input.ChapterID)
|
|
if err != nil {
|
|
return nil, errors.New("Chapter not found")
|
|
}
|
|
|
|
token := auth.TokenFromContext(ctx)
|
|
if !token.Permitted("member", "story.edit") {
|
|
return nil, errors.New("Unauthorized")
|
|
}
|
|
|
|
if !chapter.CanComment() {
|
|
return nil, errors.New("Comments are disabled or locked")
|
|
}
|
|
|
|
var characterPtr *models.Character
|
|
if input.CharacterID != nil {
|
|
character, err := characters.FindID(*input.CharacterID)
|
|
if err != nil {
|
|
return nil, errors.New("Character not found")
|
|
} else if character.Author != token.UserID {
|
|
return nil, 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 nil, errors.New("Failed to add comment: " + err.Error())
|
|
}
|
|
|
|
go func() {
|
|
story, err := stories.FindID(chapter.StoryID)
|
|
if err != nil {
|
|
log.Println("WARNING: Couldn't log comment change:", err)
|
|
return
|
|
}
|
|
|
|
changes.Submit("Comment", "add", token.UserID, true, changekeys.Many(comment, chapter, models.Story{ID: chapter.StoryID}), comment, chapter, story)
|
|
}()
|
|
|
|
return &comment, nil
|
|
}
|
|
|
|
func (r *mutationResolver) EditComment(ctx context.Context, input graphcore.CommentEditInput) (*models.Comment, error) {
|
|
comment, err := comments.Find(input.CommentID)
|
|
if err != nil {
|
|
return nil, errors.New("Comment not found")
|
|
}
|
|
|
|
token := auth.TokenFromContext(ctx)
|
|
if !token.PermittedUser(comment.Author, "member", "story.edit") {
|
|
return nil, errors.New("You cannot edit this comment")
|
|
}
|
|
|
|
chapter, err := chapters.FindID(comment.ChapterID)
|
|
if err != nil {
|
|
return nil, errors.New("Comment's chapter not found")
|
|
}
|
|
|
|
if !chapter.CanComment() {
|
|
return nil, errors.New("Comments are disabled or locked")
|
|
}
|
|
|
|
if input.ClearFictionalDate != nil && *input.ClearFictionalDate == true {
|
|
input.FictionalDate = &time.Time{}
|
|
}
|
|
|
|
if input.CharacterID != nil && *input.CharacterID != "" {
|
|
character, err := characters.FindID(*input.CharacterID)
|
|
if err != nil {
|
|
return nil, errors.New("Character not found")
|
|
} else if character.Author != token.UserID {
|
|
return nil, errors.New("That is not your character")
|
|
}
|
|
}
|
|
|
|
comment, err = comments.Edit(comment, input.Source, input.CharacterName, input.CharacterID, input.Subject, input.FictionalDate)
|
|
if err != nil {
|
|
return nil, errors.New("Could not post comment: " + err.Error())
|
|
}
|
|
|
|
go func() {
|
|
story, err := stories.FindID(chapter.StoryID)
|
|
if err != nil {
|
|
log.Println("WARNING: Couldn't log comment change:", err)
|
|
return
|
|
}
|
|
|
|
changes.Submit("Comment", "edit", token.UserID, true, changekeys.Many(comment, chapter, models.Story{ID: chapter.StoryID}), comment, chapter, story)
|
|
}()
|
|
|
|
return &comment, nil
|
|
}
|
|
|
|
func (r *mutationResolver) RemoveComment(ctx context.Context, input graphcore.CommentRemoveInput) (*models.Comment, error) {
|
|
comment, err := comments.Find(input.CommentID)
|
|
if err != nil {
|
|
return nil, errors.New("Comment not found")
|
|
}
|
|
|
|
token := auth.TokenFromContext(ctx)
|
|
if !token.PermittedUser(comment.Author, "member", "story.edit") {
|
|
return nil, errors.New("You cannot remove this comment")
|
|
}
|
|
|
|
chapter, err := chapters.FindID(comment.ChapterID)
|
|
if err != nil {
|
|
return nil, errors.New("Comment's chapter not found")
|
|
}
|
|
|
|
if !chapter.CanComment() {
|
|
return nil, errors.New("Comments are disabled or locked")
|
|
}
|
|
|
|
err = comments.Remove(comment)
|
|
if err != nil {
|
|
return nil, errors.New("Failed to remove comment: " + err.Error())
|
|
}
|
|
|
|
go func() {
|
|
story, err := stories.FindID(chapter.StoryID)
|
|
if err != nil {
|
|
log.Println("WARNING: Couldn't log comment change:", err)
|
|
return
|
|
}
|
|
|
|
changes.Submit("Comment", "remove", token.UserID, true, changekeys.Many(comment, chapter, models.Story{ID: chapter.StoryID}), comment, chapter, story)
|
|
}()
|
|
|
|
return &comment, nil
|
|
}
|