|
|
package queries
import ( "context" "errors" "strings" "time"
"git.aiterp.net/rpdata/api/models/channels"
"git.aiterp.net/rpdata/api/graph2/input" "git.aiterp.net/rpdata/api/internal/auth" "git.aiterp.net/rpdata/api/internal/loader" "git.aiterp.net/rpdata/api/models" "git.aiterp.net/rpdata/api/models/changekeys" "git.aiterp.net/rpdata/api/models/changes" "git.aiterp.net/rpdata/api/models/logs" "github.com/99designs/gqlgen/graphql" )
// Queries
func (r *resolver) Log(ctx context.Context, id string) (models.Log, error) { return logs.FindID(id) }
func (r *resolver) Logs(ctx context.Context, filter *logs.Filter) ([]models.Log, error) { logs, err := logs.List(filter) if err != nil { return nil, err }
reqCtx := graphql.GetRequestContext(ctx) maybeCharacters := strings.Contains(reqCtx.RawQuery, "characters") maybeChannels := strings.Contains(reqCtx.RawQuery, "channels")
if len(logs) >= 100 && (maybeCharacters || maybeChannels) { loader := loader.FromContext(ctx) if loader == nil { return nil, errors.New("no loader") }
for _, log := range logs { if maybeChannels { loader.PrimeChannels("name", log.ChannelName) }
if maybeCharacters { loader.PrimeCharacters("id", log.CharacterIDs...) } } }
return logs, nil }
// Mutations
func (r *mutationResolver) AddLog(ctx context.Context, input input.LogAddInput) (models.Log, error) { token := auth.TokenFromContext(ctx) if !token.Authenticated() || !token.Permitted("log.add") { return models.Log{}, errors.New("You are not permitted to add logs") }
open := input.Open != nil && *input.Open == true title := "" if input.Title != nil { title = *input.Title } event := "" if input.Event != nil { event = *input.Event } description := "" if input.Description != nil { description = *input.Description }
existingChannel, _ := channels.FindName(input.Channel)
log, err := logs.Add(input.Date, input.Channel, title, event, description, open) if !token.Authenticated() || !token.Permitted("log.add") { return models.Log{}, errors.New("Failed to create log: " + err.Error()) }
go func() { if existingChannel.Name == "" { channel, err := channels.FindName(input.Channel) if err == nil { changes.Submit("Channel", "add", token.UserID, true, changekeys.Listed(channel), channel) } }
changes.Submit("Log", "add", token.UserID, true, changekeys.Listed(log), log) }()
return log, nil }
func (r *mutationResolver) ImportLog(ctx context.Context, input input.LogImportInput) ([]models.Log, error) { token := auth.TokenFromContext(ctx) if !token.Authenticated() || !token.Permitted("log.add") { return nil, errors.New("You are not permitted to add logs") }
date := time.Time{} if input.Date != nil { date = *input.Date }
tz := time.UTC if input.Timezone != nil { parsedTZ, err := time.LoadLocation(*input.Timezone) if err != nil { return nil, errors.New("Unknown timezone: " + *input.Timezone) }
tz = parsedTZ }
results, err := logs.Import(input.Importer, date, tz, input.ChannelName, input.Data) if err != nil { return nil, err }
newLogs := make([]models.Log, 0, len(results)) for _, result := range results { go func() { changes.Submit("Log", "add", token.UserID, true, changekeys.Many(result.Log), result.Log) changes.Submit("Post", "add", token.UserID, true, changekeys.Many(result.Log, result.Posts), result.Posts) }()
log, err := logs.UpdateCharacters(result.Log) if err != nil { log = result.Log }
newLogs = append(newLogs, log) }
return newLogs, nil }
func (r *mutationResolver) EditLog(ctx context.Context, input input.LogEditInput) (models.Log, error) { token := auth.TokenFromContext(ctx) if !token.Authenticated() || !token.Permitted("log.edit") { return models.Log{}, errors.New("You are not permitted to edit logs") }
log, err := logs.FindID(input.ID) if err != nil { return models.Log{}, errors.New("Log not found") }
log, err = logs.Edit(log, input.Title, input.Event, input.Description, input.Open) if err != nil { return models.Log{}, errors.New("Failed to edit log: " + err.Error()) }
go changes.Submit("Log", "edit", token.UserID, true, changekeys.Listed(log), log)
return log, nil }
func (r *mutationResolver) RemoveLog(ctx context.Context, input input.LogRemoveInput) (models.Log, error) { token := auth.TokenFromContext(ctx) if !token.Authenticated() || !token.Permitted("log.remove") { return models.Log{}, errors.New("You are not permitted to remove logs") }
log, err := logs.FindID(input.ID) if err != nil { return models.Log{}, errors.New("Log not found") }
log, err = logs.Remove(log) if err != nil { return models.Log{}, errors.New("Failed to remove log: " + err.Error()) }
go changes.Submit("Log", "remove", token.UserID, true, changekeys.Listed(log), log)
return log, nil }
|