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.
60 lines
1.3 KiB
60 lines
1.3 KiB
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
|
|
}
|