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.
98 lines
2.7 KiB
98 lines
2.7 KiB
package queries
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
|
|
"git.aiterp.net/rpdata/api/models/logs"
|
|
|
|
"git.aiterp.net/rpdata/api/graph2/input"
|
|
"git.aiterp.net/rpdata/api/internal/auth"
|
|
"git.aiterp.net/rpdata/api/models"
|
|
"git.aiterp.net/rpdata/api/models/posts"
|
|
)
|
|
|
|
// Queries
|
|
|
|
func (r *resolver) Post(ctx context.Context, id string) (models.Post, error) {
|
|
return posts.FindID(id)
|
|
}
|
|
|
|
func (r *resolver) Posts(ctx context.Context, filter *posts.Filter) ([]models.Post, error) {
|
|
// Some sanity checks to avoid querying an insame amount of posts.
|
|
if filter == nil {
|
|
filter = &posts.Filter{Limit: 256}
|
|
} else {
|
|
if (filter.Limit <= 0 || filter.Limit > 256) && filter.LogID == nil {
|
|
return nil, errors.New("a limit of 0 (no limit) or >256 without a logId is not allowed")
|
|
}
|
|
|
|
if len(filter.Kind) > 32 {
|
|
return nil, errors.New("You cannot specify more than 32 kinds")
|
|
}
|
|
|
|
if len(filter.ID) > 32 {
|
|
return nil, errors.New("You cannot specify more than 32 IDs")
|
|
}
|
|
}
|
|
|
|
return posts.List(filter)
|
|
}
|
|
|
|
// Mutation
|
|
|
|
func (r *mutationResolver) AddPost(ctx context.Context, input input.PostAddInput) (models.Post, error) {
|
|
token := auth.TokenFromContext(ctx)
|
|
if !token.Authenticated() || !token.Permitted("post.add") {
|
|
return models.Post{}, errors.New("You are not permitted to edit logs")
|
|
}
|
|
|
|
log, err := logs.FindID(input.LogID)
|
|
if err != nil {
|
|
return models.Post{}, err
|
|
}
|
|
|
|
return posts.Add(log, input.Time, input.Kind, input.Nick, input.Text)
|
|
}
|
|
|
|
func (r *mutationResolver) EditPost(ctx context.Context, input input.PostEditInput) (models.Post, error) {
|
|
token := auth.TokenFromContext(ctx)
|
|
if !token.Authenticated() || !token.Permitted("post.edit") {
|
|
return models.Post{}, errors.New("You are not permitted to edit logs")
|
|
}
|
|
|
|
post, err := posts.FindID(input.ID)
|
|
if err != nil {
|
|
return models.Post{}, errors.New("Post not found")
|
|
}
|
|
|
|
return posts.Edit(post, input.Time, input.Kind, input.Nick, input.Text)
|
|
}
|
|
|
|
func (r *mutationResolver) MovePost(ctx context.Context, input input.PostMoveInput) ([]models.Post, error) {
|
|
token := auth.TokenFromContext(ctx)
|
|
if !token.Authenticated() || !token.Permitted("post.move") {
|
|
return nil, errors.New("You are not permitted to edit logs")
|
|
}
|
|
|
|
post, err := posts.FindID(input.ID)
|
|
if err != nil {
|
|
return nil, errors.New("Post not found")
|
|
}
|
|
|
|
return posts.Move(post, input.ToPosition)
|
|
}
|
|
|
|
func (r *mutationResolver) RemovePost(ctx context.Context, input input.PostRemoveInput) (models.Post, error) {
|
|
token := auth.TokenFromContext(ctx)
|
|
if !token.Authenticated() || !token.Permitted("post.remove") {
|
|
return models.Post{}, errors.New("You are not permitted to edit logs")
|
|
}
|
|
|
|
post, err := posts.FindID(input.ID)
|
|
if err != nil {
|
|
return models.Post{}, errors.New("Post not found (before removing, of course)")
|
|
}
|
|
|
|
return posts.Remove(post)
|
|
}
|