package queries import ( "context" "errors" "time" "git.aiterp.net/rpdata/api/graph2/input" "git.aiterp.net/rpdata/api/internal/auth" "git.aiterp.net/rpdata/api/models" "git.aiterp.net/rpdata/api/models/chapters" "git.aiterp.net/rpdata/api/models/stories" ) func (r *resolver) Story(ctx context.Context, id string) (models.Story, error) { return stories.FindID(id) } func (r *resolver) Stories(ctx context.Context, filter *stories.Filter) ([]models.Story, error) { if filter != nil { if filter.Unlisted != nil && *filter.Unlisted == true { token := auth.TokenFromContext(ctx) if !token.Authenticated() { return nil, errors.New("You are not permitted to view unlisted stories") } if !token.Permitted("story.unlisted") { filter.Author = &token.UserID } } } return stories.List(filter) } // Mutations func (r *mutationResolver) AddStory(ctx context.Context, input input.StoryAddInput) (models.Story, error) { token := auth.TokenFromContext(ctx) if token == nil || !token.Permitted("member", "story.add") { return models.Story{}, errors.New("Permission denied") } author := token.UserID if input.Author != nil && *input.Author != author { if !token.Permitted("story.add") { return models.Story{}, errors.New("You are not permitted to add a story in another author's name") } author = *input.Author } fictionalDate := time.Time{} if input.FictionalDate != nil { fictionalDate = *input.FictionalDate } 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) } func (r *mutationResolver) AddStoryTag(ctx context.Context, input input.StoryTagAddInput) (models.Story, error) { token := auth.TokenFromContext(ctx) story, err := stories.FindID(input.ID) if err != nil { return models.Story{}, errors.New("Story not found") } if !token.PermittedUser(story.Author, "member", "story.edit") { return models.Story{}, errors.New("You are not permitted to edit this story") } return stories.AddTag(story, input.Tag) } func (r *mutationResolver) RemoveStoryTag(ctx context.Context, input input.StoryTagRemoveInput) (models.Story, error) { token := auth.TokenFromContext(ctx) story, err := stories.FindID(input.ID) if err != nil { return models.Story{}, errors.New("Story not found") } if !token.PermittedUser(story.Author, "member", "story.edit") { return models.Story{}, errors.New("You are not permitted to edit this story") } return stories.RemoveTag(story, input.Tag) } func (r *mutationResolver) EditStory(ctx context.Context, input input.StoryEditInput) (models.Story, error) { token := auth.TokenFromContext(ctx) story, err := stories.FindID(input.ID) if err != nil { return models.Story{}, errors.New("Story not found") } if !token.PermittedUser(story.Author, "member", "story.edit") { return models.Story{}, errors.New("You are not permitted to remove this story") } if input.ClearFictionalDate != nil && *input.ClearFictionalDate { input.FictionalDate = &time.Time{} } return stories.Edit(story, input.Name, input.Category, input.Listed, input.Open, input.FictionalDate) } func (r *mutationResolver) RemoveStory(ctx context.Context, input input.StoryRemoveInput) (models.Story, error) { token := auth.TokenFromContext(ctx) story, err := stories.FindID(input.ID) if err != nil { return models.Story{}, errors.New("Story not found") } if !token.PermittedUser(story.Author, "member", "story.remove") { return models.Story{}, errors.New("You are not permitted to remove this story") } story, err = stories.Remove(story) if err != nil { return models.Story{}, err } err = chapters.RemoveStory(story) if err != nil { return models.Story{}, errors.New("Failed to remove chapters, but story is removed: " + err.Error()) } return story, nil }