GraphQL API and utilities for the rpdata project
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.
 
 

325 lines
6.7 KiB

package resolver
import (
"context"
"errors"
"time"
"git.aiterp.net/rpdata/api/loader"
"git.aiterp.net/rpdata/api/model/change"
"git.aiterp.net/rpdata/api/internal/session"
"git.aiterp.net/rpdata/api/model/log"
)
// LogResolver for the Log graphql type
type LogResolver struct{ L log.Log }
// LogArgs is an arg
type LogArgs struct {
ID *string
}
// LogPostArgs is an arg
type LogPostArgs struct {
Kinds *[]string
}
// Log finds log
func (r *QueryResolver) Log(ctx context.Context, args *LogArgs) (*LogResolver, error) {
var l log.Log
var err error
switch {
case args.ID != nil:
l, err = log.FindID(*args.ID)
default:
err = ErrCannotResolve
}
if err != nil {
return nil, err
}
return &LogResolver{L: l}, nil
}
// LogQueryInput is an input
type LogQueryInput struct {
Search *string
Characters *[]string
Channels *[]string
Events *[]string
Open *bool
Limit *int32
}
// Logs lists logs
func (r *QueryResolver) Logs(ctx context.Context, args *struct{ Input *LogQueryInput }) ([]*LogResolver, error) {
var logs []log.Log
var err error
input := args.Input
if input != nil {
// Parse input
limit := 100
search := ""
if input.Search != nil {
search = *input.Search
limit = 0
}
channels := []string(nil)
if input.Channels != nil {
channels = *input.Channels
limit = 0
}
characters := []string(nil)
if input.Characters != nil {
characters = *input.Characters
limit = 0
}
events := []string(nil)
if input.Events != nil {
events = *input.Events
limit = 0
}
if input.Limit != nil {
limit = int(*input.Limit)
}
open := input.Open != nil && *input.Open == true
logs, err = log.ListSearch(search, channels, characters, events, open, limit)
if err != nil {
return nil, err
}
} else {
logs, err = log.List(100)
if err != nil {
return nil, err
}
}
resolvers := make([]*LogResolver, len(logs))
for i := range logs {
resolvers[i] = &LogResolver{L: logs[i]}
}
return resolvers, nil
}
// LogAddInput is an input
type LogAddInput struct {
Date string
Channel string
Title *string
Open *bool
Event *string
Description *string
}
// AddLog resolves the addLog mutation
func (r *MutationResolver) AddLog(ctx context.Context, args *struct{ Input LogAddInput }) (*LogResolver, error) {
user := session.FromContext(ctx).User()
if user == nil || !user.Permitted("log.add") {
return nil, ErrUnauthorized
}
date, err := time.Parse(time.RFC3339Nano, args.Input.Date)
if err != nil {
return nil, err
}
title := ""
if args.Input.Title != nil {
title = *args.Input.Title
}
event := ""
if args.Input.Event != nil {
event = *args.Input.Event
}
description := ""
if args.Input.Description != nil {
description = *args.Input.Description
}
open := args.Input.Open != nil && *args.Input.Open == true
log, err := log.New(date, args.Input.Channel, title, event, description, open)
if err != nil {
return nil, err
}
change.Submit("Log", "add", user.ID, log.ID, map[string]interface{}{
"channel": log.Channel,
"title": log.Title,
"event": log.Event,
"description": log.Description,
"open": log.Open,
})
return &LogResolver{L: log}, nil
}
// LogEditInput is an input
type LogEditInput struct {
ID string
Title *string
Event *string
Description *string
Open *bool
}
// EditLog resolves the addLog mutation
func (r *MutationResolver) EditLog(ctx context.Context, args *struct{ Input LogEditInput }) (*LogResolver, error) {
user := session.FromContext(ctx).User()
if user == nil || !user.Permitted("log.edit") {
return nil, ErrUnauthorized
}
l, err := log.FindID(args.Input.ID)
if err != nil {
return nil, err
}
err = l.Edit(args.Input.Title, args.Input.Event, args.Input.Description, args.Input.Open)
if err != nil {
return nil, err
}
change.Submit("Log", "edit", user.ID, l.ID, map[string]interface{}{
"channel": l.Channel,
"title": args.Input.Title,
"event": args.Input.Event,
"description": args.Input.Description,
"open": args.Input.Open,
})
return &LogResolver{L: l}, nil
}
// RemoveLog resolves the removeLog mutation
func (r *MutationResolver) RemoveLog(ctx context.Context, args *struct{ ID string }) (*LogResolver, error) {
user := session.FromContext(ctx).User()
if user == nil || !user.Permitted("log.remove") {
return nil, ErrUnauthorized
}
l, err := log.FindID(args.ID)
if err != nil {
return nil, err
}
err = log.Remove(args.ID)
if err != nil {
return nil, err
}
change.Submit("Log", "add", user.ID, l.ID, map[string]interface{}{
"channel": l.Channel,
"title": l.Title,
"event": l.Event,
"description": l.Description,
"open": l.Open,
})
return &LogResolver{L: l}, nil
}
// ID resolves Log.id
func (r *LogResolver) ID() string {
return r.L.ID
}
// ShortID resolves Log.shortId
func (r *LogResolver) ShortID() string {
return r.L.ShortID
}
// Date resolves Log.date
func (r *LogResolver) Date() string {
return r.L.Date.Format(time.RFC3339Nano)
}
// ChannelName resolves Log.channelName
func (r *LogResolver) ChannelName() string {
return r.L.Channel
}
// Channel resolves Log.channe
func (r *LogResolver) Channel(ctx context.Context) (*ChannelResolver, error) {
loader := loader.FromContext(ctx)
if loader == nil {
return nil, errors.New("no loader")
}
channel, err := loader.Channel("name", r.L.Channel)
if err != nil {
return nil, err
}
return &ChannelResolver{C: channel}, nil
}
// Title resolves Log.title
func (r *LogResolver) Title() string {
return r.L.Title
}
// Event resolves Log.event
func (r *LogResolver) Event() string {
return r.L.Event
}
// Description resolves Log.description
func (r *LogResolver) Description() string {
return r.L.Description
}
// Open resolves Log.open
func (r *LogResolver) Open() bool {
return r.L.Open
}
// Characters resolves Log.characters
func (r *LogResolver) Characters(ctx context.Context) ([]*CharacterResolver, error) {
loader := loader.FromContext(ctx)
if loader == nil {
return nil, errors.New("no loader")
}
chars, err := loader.Characters("id", r.L.CharacterIDs...)
if err != nil {
return nil, err
}
resolvers := make([]*CharacterResolver, 0, len(chars))
for i := range chars {
if chars[i].ID == "" {
continue
}
resolvers = append(resolvers, &CharacterResolver{C: chars[i]})
}
return resolvers, nil
}
// Posts resolves Log.posts
func (r *LogResolver) Posts(ctx context.Context, args *LogPostArgs) ([]*PostResolver, error) {
var kinds []string
if args.Kinds != nil {
kinds = *args.Kinds
}
posts, err := r.L.Posts(kinds...)
if err != nil {
return nil, err
}
resolvers := make([]*PostResolver, len(posts))
for i := range posts {
resolvers[i] = &PostResolver{posts[i]}
}
return resolvers, nil
}