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.

59 lines
1.3 KiB

  1. package mutations
  2. import (
  3. "context"
  4. "git.aiterp.net/rpdata/api/graphql/resolver/types"
  5. "git.aiterp.net/rpdata/api/internal/auth"
  6. "git.aiterp.net/rpdata/api/model/change"
  7. "git.aiterp.net/rpdata/api/model/channel"
  8. )
  9. // ChannelAddArgs is input for the addChannel mutation
  10. type ChannelAddArgs struct {
  11. Input *struct {
  12. Name string
  13. Logged *bool
  14. Hub *bool
  15. EventName *string
  16. LocationName *string
  17. }
  18. }
  19. // AddChannel resolves the addChannel mutation
  20. func (r *MutationResolver) AddChannel(ctx context.Context, args *ChannelAddArgs) (*types.ChannelResolver, error) {
  21. input := args.Input
  22. token := auth.TokenFromContext(ctx)
  23. if !token.Permitted("channel.add") {
  24. return nil, ErrUnauthorized
  25. }
  26. logged := input.Logged != nil && *input.Logged
  27. hub := input.Hub != nil && *input.Hub
  28. eventName := ""
  29. if input.EventName != nil {
  30. eventName = *input.EventName
  31. }
  32. locationName := ""
  33. if input.LocationName != nil {
  34. locationName = *input.LocationName
  35. }
  36. channel, err := channel.New(input.Name, logged, hub, eventName, locationName)
  37. if err != nil {
  38. return nil, err
  39. }
  40. go change.Submit("Channel", "add", token.UserID, channel.Name, map[string]interface{}{
  41. "logged": channel.Logged,
  42. "hub": channel.Hub,
  43. "location": input.LocationName,
  44. "event": input.EventName,
  45. })
  46. return &types.ChannelResolver{C: channel}, nil
  47. }