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.

193 lines
4.9 KiB

  1. package resolvers
  2. import (
  3. "context"
  4. "errors"
  5. "strings"
  6. "time"
  7. "git.aiterp.net/rpdata/api/graph2/graphcore"
  8. "git.aiterp.net/rpdata/api/internal/auth"
  9. "git.aiterp.net/rpdata/api/internal/loader"
  10. "git.aiterp.net/rpdata/api/models"
  11. "git.aiterp.net/rpdata/api/models/changekeys"
  12. "git.aiterp.net/rpdata/api/models/changes"
  13. "git.aiterp.net/rpdata/api/models/channels"
  14. "git.aiterp.net/rpdata/api/models/logs"
  15. "github.com/99designs/gqlgen/graphql"
  16. )
  17. // Queries
  18. func (r *queryResolver) Log(ctx context.Context, id string) (*models.Log, error) {
  19. log, err := logs.FindID(id)
  20. if err != nil {
  21. return nil, err
  22. }
  23. return &log, nil
  24. }
  25. func (r *queryResolver) Logs(ctx context.Context, filter *logs.Filter) ([]*models.Log, error) {
  26. logs, err := logs.List(filter)
  27. if err != nil {
  28. return nil, err
  29. }
  30. reqCtx := graphql.GetRequestContext(ctx)
  31. maybeCharacters := strings.Contains(reqCtx.RawQuery, "characters")
  32. maybeChannels := strings.Contains(reqCtx.RawQuery, "channels")
  33. if len(logs) >= 100 && (maybeCharacters || maybeChannels) {
  34. loader := loader.FromContext(ctx)
  35. if loader == nil {
  36. return nil, errors.New("no loader")
  37. }
  38. for _, log := range logs {
  39. if maybeChannels {
  40. loader.PrimeChannels("name", log.ChannelName)
  41. }
  42. if maybeCharacters {
  43. loader.PrimeCharacters("id", log.CharacterIDs...)
  44. }
  45. }
  46. }
  47. logs2 := make([]*models.Log, len(logs))
  48. for i := range logs {
  49. logs2[i] = &logs[i]
  50. }
  51. return logs2, nil
  52. }
  53. // Mutations
  54. func (r *mutationResolver) AddLog(ctx context.Context, input graphcore.LogAddInput) (*models.Log, error) {
  55. token := auth.TokenFromContext(ctx)
  56. if !token.Authenticated() || !token.Permitted("log.add") {
  57. return nil, errors.New("You are not permitted to add logs")
  58. }
  59. open := input.Open != nil && *input.Open == true
  60. title := ""
  61. if input.Title != nil {
  62. title = *input.Title
  63. }
  64. event := ""
  65. if input.Event != nil {
  66. event = *input.Event
  67. }
  68. description := ""
  69. if input.Description != nil {
  70. description = *input.Description
  71. }
  72. existingChannel, _ := channels.FindName(input.Channel)
  73. log, err := logs.Add(input.Date, input.Channel, title, event, description, open)
  74. if !token.Authenticated() || !token.Permitted("log.add") {
  75. return nil, errors.New("Failed to create log: " + err.Error())
  76. }
  77. go func() {
  78. if existingChannel.Name == "" {
  79. channel, err := channels.FindName(input.Channel)
  80. if err == nil {
  81. changes.Submit("Channel", "add", token.UserID, true, changekeys.Listed(channel), channel)
  82. }
  83. }
  84. changes.Submit("Log", "add", token.UserID, true, changekeys.Listed(log), log)
  85. }()
  86. return &log, nil
  87. }
  88. func (r *mutationResolver) ImportLog(ctx context.Context, input graphcore.LogImportInput) ([]*models.Log, error) {
  89. token := auth.TokenFromContext(ctx)
  90. if !token.Authenticated() || !token.Permitted("log.add") {
  91. return nil, errors.New("You are not permitted to add logs")
  92. }
  93. date := time.Time{}
  94. if input.Date != nil {
  95. date = *input.Date
  96. }
  97. tz := time.UTC
  98. if input.Timezone != nil {
  99. parsedTZ, err := time.LoadLocation(*input.Timezone)
  100. if err != nil {
  101. return nil, errors.New("Unknown timezone: " + *input.Timezone)
  102. }
  103. tz = parsedTZ
  104. }
  105. results, err := logs.Import(input.Importer, date, tz, input.ChannelName, input.Data)
  106. if err != nil {
  107. return nil, err
  108. }
  109. newLogs := make([]*models.Log, 0, len(results))
  110. for _, result := range results {
  111. go func(result logs.ImportedLog) {
  112. changes.Submit("Log", "add", token.UserID, true, changekeys.Many(result.Log), result.Log)
  113. changes.Submit("Post", "add", token.UserID, true, changekeys.Many(result.Log, result.Posts), result.Posts)
  114. }(result)
  115. log, err := logs.UpdateCharacters(result.Log, nil)
  116. if err != nil {
  117. log = result.Log
  118. }
  119. newLogs = append(newLogs, &log)
  120. }
  121. return newLogs, nil
  122. }
  123. func (r *mutationResolver) EditLog(ctx context.Context, input graphcore.LogEditInput) (*models.Log, error) {
  124. token := auth.TokenFromContext(ctx)
  125. if !token.Authenticated() || !token.Permitted("log.edit") {
  126. return nil, errors.New("You are not permitted to edit logs")
  127. }
  128. log, err := logs.FindID(input.ID)
  129. if err != nil {
  130. return nil, errors.New("Log not found")
  131. }
  132. log, err = logs.Edit(log, input.Title, input.Event, input.Description, input.Open)
  133. if err != nil {
  134. return nil, errors.New("Failed to edit log: " + err.Error())
  135. }
  136. go changes.Submit("Log", "edit", token.UserID, true, changekeys.Listed(log), log)
  137. return &log, nil
  138. }
  139. func (r *mutationResolver) RemoveLog(ctx context.Context, input graphcore.LogRemoveInput) (*models.Log, error) {
  140. token := auth.TokenFromContext(ctx)
  141. if !token.Authenticated() || !token.Permitted("log.remove") {
  142. return nil, errors.New("You are not permitted to remove logs")
  143. }
  144. log, err := logs.FindID(input.ID)
  145. if err != nil {
  146. return nil, errors.New("Log not found")
  147. }
  148. log, err = logs.Remove(log)
  149. if err != nil {
  150. return nil, errors.New("Failed to remove log: " + err.Error())
  151. }
  152. go changes.Submit("Log", "remove", token.UserID, true, changekeys.Listed(log), log)
  153. return &log, nil
  154. }