package mutations import ( "context" "time" "git.aiterp.net/rpdata/api/graphql/resolver/types" "git.aiterp.net/rpdata/api/internal/session" "git.aiterp.net/rpdata/api/model/change" "git.aiterp.net/rpdata/api/model/log" ) // PostAddArgs is args for addPost mutation type PostAddArgs struct { Input *struct { LogID string Time string Kind string Nick string Text string } } // AddPost resolves the addPost mutation func (r *MutationResolver) AddPost(ctx context.Context, args *PostAddArgs) (*types.PostResolver, error) { input := args.Input user := session.FromContext(ctx).User() if user == nil || !user.Permitted("post.add") { return nil, ErrUnauthorized } postTime, err := time.Parse(time.RFC3339Nano, input.Time) if err != nil { return nil, err } log, err := log.FindID(input.LogID) if err != nil { return nil, err } post, err := log.NewPost(postTime, input.Kind, input.Nick, input.Text) if err != nil { return nil, err } go 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, "position": post.Position, }) go log.UpdateCharacters() return &types.PostResolver{P: post}, nil }