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.
63 lines
1.3 KiB
63 lines
1.3 KiB
package mutations
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"git.aiterp.net/rpdata/api/graphql/resolver/types"
|
|
"git.aiterp.net/rpdata/api/internal/auth"
|
|
"git.aiterp.net/rpdata/api/model/change"
|
|
"git.aiterp.net/rpdata/api/model/log"
|
|
)
|
|
|
|
// PostEditArgs is args for the editPost mutation
|
|
type PostEditArgs struct {
|
|
Input *struct {
|
|
ID string
|
|
Time *string
|
|
Kind *string
|
|
Nick *string
|
|
Text *string
|
|
}
|
|
}
|
|
|
|
// EditPost resolves the editPost mutation
|
|
func (r *MutationResolver) EditPost(ctx context.Context, args *PostEditArgs) (*types.PostResolver, error) {
|
|
input := args.Input
|
|
|
|
token := auth.TokenFromContext(ctx)
|
|
if !token.Permitted("post.edit") {
|
|
return nil, ErrUnauthorized
|
|
}
|
|
|
|
postTime := (*time.Time)(nil)
|
|
if args.Input.Time != nil {
|
|
t, err := time.Parse(time.RFC3339Nano, *input.Time)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
postTime = &t
|
|
}
|
|
|
|
post, err := log.FindPostID(input.ID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
err = post.Edit(postTime, input.Kind, input.Nick, input.Text)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
go change.Submit("Post", "edit", token.UserID, post.ID, map[string]interface{}{
|
|
"time": postTime,
|
|
"kind": input.Kind,
|
|
"nick": input.Nick,
|
|
"text": input.Text,
|
|
})
|
|
|
|
go log.UpdateCharacters(post.LogID)
|
|
|
|
return &types.PostResolver{P: post}, nil
|
|
}
|