package resolver import ( "context" "time" "git.aiterp.net/rpdata/api/internal/session" "git.aiterp.net/rpdata/api/model/change" "git.aiterp.net/rpdata/api/model/story" "git.aiterp.net/rpdata/api/resolver/types" ) // StoryArgs is args for story query type StoryArgs struct { ID string } // Story implements the story query func (r *QueryResolver) Story(ctx context.Context, args *StoryArgs) (*types.StoryResolver, error) { story, err := story.FindID(args.ID) if err != nil { return nil, err } return &types.StoryResolver{S: story}, nil } // StoriesArg is args for stories query type StoriesArg struct { Filter *struct { Author *string Tags *[]TagInput EarliestFictionalDate *string LatestFictionalDate *string Limit *int32 } } // Stories implements the stories query func (r *QueryResolver) Stories(ctx context.Context, args *StoriesArg) ([]*types.StoryResolver, error) { filter := args.Filter author := "" if filter != nil && filter.Author != nil { author = *filter.Author } tags := make([]story.Tag, 0, 8) if filter != nil && filter.Tags != nil { for _, tagInput := range *filter.Tags { tags = append(tags, story.Tag{ Kind: tagInput.Kind, Name: tagInput.Name, }) } } earliest := time.Time{} err := error(nil) if filter != nil && filter.EarliestFictionalDate != nil { earliest, err = time.Parse(time.RFC3339Nano, *filter.EarliestFictionalDate) if err != nil { return nil, err } } latest := time.Time{} if filter != nil && filter.LatestFictionalDate != nil { latest, err = time.Parse(time.RFC3339Nano, *filter.LatestFictionalDate) if err != nil { return nil, err } } limit := 30 if filter != nil && filter.Limit != nil { limit = int(*filter.Limit) } stories, err := story.List(author, tags, earliest, latest, limit) if err != nil { return nil, err } resolvers := make([]*types.StoryResolver, len(stories)) for i, story := range stories { resolvers[i] = &types.StoryResolver{S: story} } return resolvers, nil } // StoryAddArgs is args for the addStory mutation type StoryAddArgs struct { Input *struct { Name string Category string Author *string Open *bool Listed *bool Tags *[]TagInput FictionalDate *string } } // AddStory implements the addStory mutation func (r *MutationResolver) AddStory(ctx context.Context, args *StoryAddArgs) (*types.StoryResolver, error) { input := args.Input user := session.FromContext(ctx).User() if user == nil || !user.Permitted("member", "story.add") { return nil, ErrUnauthorized } author := user.ID if input.Author != nil { author = *input.Author if user.ID != author && !user.Permitted("story.add") { return nil, ErrPermissionDenied } } listed := (input.Listed != nil && *input.Listed == true) open := (input.Open != nil && *input.Open == true) tags := make([]story.Tag, 0, 8) if input.Tags != nil { for _, tagInput := range *input.Tags { tags = append(tags, story.Tag{ Kind: tagInput.Kind, Name: tagInput.Name, }) } } fictionalDate := time.Time{} if input.FictionalDate != nil { date, err := time.Parse(time.RFC3339Nano, *input.FictionalDate) if err != nil { return nil, err } fictionalDate = date } story, err := story.New(input.Name, author, input.Category, listed, open, tags, time.Now(), fictionalDate) if err != nil { return nil, err } go change.Submit("Story", "add", user.ID, story.ID, map[string]interface{}{ "name": input.Name, "category": input.Category, "author": input.Author, }) return &types.StoryResolver{S: story}, nil } // StoryTagAddArgs is args for the addStoryTag mutation type StoryTagAddArgs struct { Input *struct { ID string Tag TagInput } } // AddStoryTag implements the addStoryTag mutation func (r *MutationResolver) AddStoryTag(ctx context.Context, args *StoryTagAddArgs) (*types.StoryResolver, error) { input := args.Input tag := story.Tag{Kind: input.Tag.Kind, Name: input.Tag.Name} user := session.FromContext(ctx).User() if user == nil || !user.Permitted("member", "story.edit") { return nil, ErrUnauthorized } story, err := story.FindID(input.ID) if err != nil { return nil, err } if story.Author != user.ID && !user.Permitted("story.edit") { return nil, ErrPermissionDenied } err = story.AddTag(tag) if err != nil { return nil, err } go change.Submit("Story", "add.tag", user.ID, story.ID, map[string]interface{}{ "kind": tag.Kind, "name": tag.Name, }) return &types.StoryResolver{S: story}, nil } // StoryTagRemoveArgs is args for the removeStoryTag mutation type StoryTagRemoveArgs struct { Input *struct { ID string Tag TagInput } } // RemoveStoryTag implements the removeStoryTag mutation func (r *MutationResolver) RemoveStoryTag(ctx context.Context, args *StoryTagRemoveArgs) (*types.StoryResolver, error) { input := args.Input tag := story.Tag{Kind: input.Tag.Kind, Name: input.Tag.Name} user := session.FromContext(ctx).User() if user == nil || !user.Permitted("member", "story.edit") { return nil, ErrUnauthorized } story, err := story.FindID(input.ID) if err != nil { return nil, err } if story.Author != user.ID && !user.Permitted("story.edit") { return nil, ErrPermissionDenied } err = story.RemoveTag(tag) if err != nil { return nil, err } go change.Submit("Story", "remove.tag", user.ID, story.ID, map[string]interface{}{ "kind": tag.Kind, "name": tag.Name, }) return &types.StoryResolver{S: story}, nil } // StoryEditArgs is args for the addStory mutation type StoryEditArgs struct { Input *struct { ID string Name *string Category *string Author *string Open *bool Listed *bool FictionalDate *string } } // EditStory implements the editStory mutation func (r *MutationResolver) EditStory(ctx context.Context, args *StoryEditArgs) (*types.StoryResolver, error) { input := args.Input user := session.FromContext(ctx).User() if user == nil || !user.Permitted("member", "story.edit") { return nil, ErrUnauthorized } story, err := story.FindID(input.ID) if err != nil { return nil, err } if story.Author != user.ID && !user.Permitted("story.edit") { return nil, ErrPermissionDenied } var fictionalDate *time.Time if input.FictionalDate != nil { date, err := time.Parse(time.RFC3339Nano, *input.FictionalDate) if err != nil { return nil, err } fictionalDate = &date } err = story.Edit(input.Name, input.Category, input.Listed, input.Open, fictionalDate) if err != nil { return nil, err } go change.Submit("Story", "edit", user.ID, story.ID, map[string]interface{}{ "name": input.Name, "category": input.Category, "author": input.Author, "open": input.Open, "listed": input.Listed, "fictionalDate": input.FictionalDate, }) return &types.StoryResolver{S: story}, nil } // StoryRemoveArgs is args for the removeStory mutation type StoryRemoveArgs struct { ID string } // RemoveStory implements the removeStory mutation func (r *MutationResolver) RemoveStory(ctx context.Context, args *StoryRemoveArgs) (*types.StoryResolver, error) { user := session.FromContext(ctx).User() if user == nil || !user.Permitted("member", "story.edit") { return nil, ErrUnauthorized } story, err := story.FindID(args.ID) if err != nil { return nil, err } if story.Author != user.ID && !user.Permitted("story.remove") { return nil, ErrPermissionDenied } err = story.Remove() if err != nil { return nil, err } go change.Submit("Story", "remove", user.ID, story.ID, nil) return &types.StoryResolver{S: story}, nil }