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/log" ) // PostResolver for the Post graphql type type PostResolver struct{ P log.Post } // PostArgs is an arg type PostArgs struct { ID string } // Post implements the post query func (r *QueryResolver) Post(ctx context.Context, args *PostArgs) (*PostResolver, error) { post, err := log.FindPostID(args.ID) if err != nil { return nil, err } return &PostResolver{P: post}, nil } // PostsArgs is an arg type PostsArgs struct { IDs []string } // Posts implements the posts query func (r *QueryResolver) Posts(ctx context.Context, args *PostsArgs) ([]*PostResolver, error) { posts, err := log.ListPostIDs(args.IDs...) if err != nil { return nil, err } resolvers := make([]*PostResolver, len(posts)) for i := range resolvers { resolvers[i] = &PostResolver{P: posts[i]} } return resolvers, nil } // PostAddInput is an input type PostAddInput struct { LogID string Time string Kind string Nick string Text string } // AddPost resolves the addPost mutation func (r *MutationResolver) AddPost(ctx context.Context, args struct{ Input *PostAddInput }) (*PostResolver, error) { user := session.FromContext(ctx).User() if user == nil || !user.Permitted("post.add") { return nil, ErrUnauthorized } postTime, err := time.Parse(time.RFC3339Nano, args.Input.Time) if err != nil { return nil, err } log, err := log.FindID(args.Input.LogID) if err != nil { return nil, err } post, err := log.NewPost(postTime, args.Input.Kind, args.Input.Nick, args.Input.Text) if err != nil { return nil, err } change.Submit("Post", "add", user.ID, post.ID, map[string]interface{}{ "logId": post.LogID, "time": post.Time, "kind": post.Kind, "nick": post.Nick, "text": post.Text, "index": post.Index, }) go log.UpdateCharacters() return &PostResolver{P: post}, nil } // PostEditInput is an input type PostEditInput struct { ID string Time *string Kind *string Nick *string Text *string } // EditPost resolves the editPost mutation func (r *MutationResolver) EditPost(ctx context.Context, args struct{ Input *PostEditInput }) (*PostResolver, error) { user := session.FromContext(ctx).User() if user == nil || !user.Permitted("post.edit") { return nil, ErrUnauthorized } postTime := (*time.Time)(nil) if args.Input.Time != nil { t, err := time.Parse(time.RFC3339Nano, *args.Input.Time) if err != nil { return nil, err } postTime = &t } post, err := log.FindPostID(args.Input.ID) if err != nil { return nil, err } err = post.Edit(postTime, args.Input.Kind, args.Input.Nick, args.Input.Text) if err != nil { return nil, err } change.Submit("Post", "edit", user.ID, post.ID, map[string]interface{}{ "time": postTime, "kind": args.Input.Kind, "nick": args.Input.Nick, "text": args.Input.Text, }) go log.UpdateCharacters(post.LogID) return &PostResolver{P: post}, nil } // PostMoveInput is an input type PostMoveInput struct { ID string TargetIndex int32 } // MovePost resolves the movePost mutation func (r *MutationResolver) MovePost(ctx context.Context, args struct{ Input *PostMoveInput }) (*PostResolver, error) { user := session.FromContext(ctx).User() if user == nil || !user.Permitted("post.move") { return nil, ErrUnauthorized } post, err := log.FindPostID(args.Input.ID) if err != nil { return nil, err } err = post.Move(int(args.Input.TargetIndex)) if err != nil { return nil, err } change.Submit("Post", "move", user.ID, post.ID, map[string]interface{}{ "logId": post.LogID, "targetIndex": args.Input.TargetIndex, }) return &PostResolver{P: post}, nil } // PostRemoveArgs is an arg type PostRemoveArgs struct { ID string } // RemovePost resolves the removePost mutation func (r *MutationResolver) RemovePost(ctx context.Context, args PostRemoveArgs) (*PostResolver, error) { user := session.FromContext(ctx).User() if user == nil || !user.Permitted("post.remove") { return nil, ErrUnauthorized } post, err := log.RemovePost(args.ID) if err != nil { return nil, err } change.Submit("Post", "remove", user.ID, post.ID, map[string]interface{}{ "logId": post.LogID, }) go log.UpdateCharacters(post.LogID) return &PostResolver{P: post}, nil } // ID resolves Post.id func (r *PostResolver) ID() string { return r.P.ID } // LogID resolves Post.logId func (r *PostResolver) LogID() string { return r.P.LogID } // Time resolves Post.time func (r *PostResolver) Time() string { return r.P.Time.Format(time.RFC3339Nano) } // Kind resolves Post.logId func (r *PostResolver) Kind() string { return r.P.Kind } // Nick resolves Post.nick func (r *PostResolver) Nick() string { return r.P.Nick } // Text resolves Post.text func (r *PostResolver) Text() string { return r.P.Text } // Index resolves Post.text func (r *PostResolver) Index() int32 { return int32(r.P.Index) }