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.
102 lines
2.2 KiB
102 lines
2.2 KiB
package queries
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"git.aiterp.net/rpdata/api/graphql/resolver/types"
|
|
"git.aiterp.net/rpdata/api/internal/session"
|
|
"git.aiterp.net/rpdata/api/model/story"
|
|
)
|
|
|
|
// StoriesArg is args for stories query
|
|
type StoriesArg struct {
|
|
Filter *struct {
|
|
Author *string
|
|
Tags *[]struct {
|
|
Kind string
|
|
Name string
|
|
}
|
|
EarliestFictionalDate *string
|
|
LatestFictionalDate *string
|
|
Category *string
|
|
Limit *int32
|
|
Open *bool
|
|
Unlisted *bool
|
|
}
|
|
}
|
|
|
|
// Stories resolves 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
|
|
}
|
|
|
|
category := ""
|
|
if filter != nil && filter.Category != nil {
|
|
category = *filter.Category
|
|
}
|
|
|
|
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
|
|
}
|
|
}
|
|
|
|
unlisted := filter.Unlisted != nil && *filter.Unlisted == true
|
|
if unlisted {
|
|
user := session.FromContext(ctx).User()
|
|
if user == nil {
|
|
return nil, ErrUnauthorized
|
|
}
|
|
|
|
if author != "" && author != user.ID && !user.Permitted("story.unlisted") {
|
|
return nil, ErrPermissionDenied
|
|
}
|
|
|
|
author = user.ID
|
|
}
|
|
|
|
open := filter.Open
|
|
|
|
limit := 0
|
|
if filter != nil && filter.Limit != nil {
|
|
limit = int(*filter.Limit)
|
|
}
|
|
|
|
stories, err := story.List(author, category, tags, earliest, latest, unlisted, open, 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
|
|
}
|