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.

53 lines
1.2 KiB

  1. package types
  2. import (
  3. "context"
  4. "errors"
  5. "git.aiterp.net/rpdata/api/services"
  6. "git.aiterp.net/rpdata/api/internal/loader"
  7. "git.aiterp.net/rpdata/api/models"
  8. "git.aiterp.net/rpdata/api/models/posts"
  9. )
  10. type logResolver struct {
  11. characters *services.CharacterService
  12. }
  13. func (r *logResolver) Channel(ctx context.Context, log *models.Log) (*models.Channel, error) {
  14. loader := loader.FromContext(ctx)
  15. if loader == nil {
  16. return nil, errors.New("no loader")
  17. }
  18. return loader.Channel("name", log.ChannelName)
  19. }
  20. func (r *logResolver) Characters(ctx context.Context, log *models.Log) ([]*models.Character, error) {
  21. if len(log.CharacterIDs) == 0 {
  22. return []*models.Character{}, nil
  23. }
  24. return r.characters.List(ctx, models.CharacterFilter{
  25. IDs: log.CharacterIDs,
  26. })
  27. }
  28. func (r *logResolver) Posts(ctx context.Context, log *models.Log, kinds []string) ([]*models.Post, error) {
  29. posts, err := posts.List(&posts.Filter{LogID: &log.ShortID, Kind: kinds, Limit: 0})
  30. if err != nil {
  31. return nil, err
  32. }
  33. posts2 := make([]*models.Post, len(posts))
  34. for i := range posts {
  35. posts2[i] = &posts[i]
  36. }
  37. return posts2, nil
  38. }
  39. // LogResolver is a resolver
  40. func LogResolver(s *services.Bundle) *logResolver {
  41. return &logResolver{characters: s.Characters}
  42. }