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.

229 lines
4.5 KiB

  1. package resolver
  2. import (
  3. "context"
  4. "time"
  5. "git.aiterp.net/rpdata/api/model/change"
  6. "git.aiterp.net/rpdata/api/resolver/types"
  7. "git.aiterp.net/rpdata/api/internal/session"
  8. "git.aiterp.net/rpdata/api/model/log"
  9. )
  10. // LogArgs is an arg
  11. type LogArgs struct {
  12. ID *string
  13. }
  14. // Log finds log
  15. func (r *QueryResolver) Log(ctx context.Context, args *LogArgs) (*types.LogResolver, error) {
  16. var l log.Log
  17. var err error
  18. switch {
  19. case args.ID != nil:
  20. l, err = log.FindID(*args.ID)
  21. default:
  22. err = ErrCannotResolve
  23. }
  24. if err != nil {
  25. return nil, err
  26. }
  27. return &types.LogResolver{L: l}, nil
  28. }
  29. // LogQueryArgs is args for the logs query
  30. type LogQueryArgs struct {
  31. Filter *struct {
  32. Search *string
  33. Characters *[]string
  34. Channels *[]string
  35. Events *[]string
  36. Open *bool
  37. Limit *int32
  38. }
  39. }
  40. // Logs resolves the logs query
  41. func (r *QueryResolver) Logs(ctx context.Context, args *LogQueryArgs) ([]*types.LogResolver, error) {
  42. var logs []log.Log
  43. var err error
  44. filter := args.Filter
  45. if filter != nil {
  46. limit := 100
  47. search := ""
  48. if filter.Search != nil {
  49. search = *filter.Search
  50. limit = 0
  51. }
  52. channels := []string(nil)
  53. if filter.Channels != nil {
  54. channels = *filter.Channels
  55. limit = 0
  56. }
  57. characters := []string(nil)
  58. if filter.Characters != nil {
  59. characters = *filter.Characters
  60. limit = 0
  61. }
  62. events := []string(nil)
  63. if filter.Events != nil {
  64. events = *filter.Events
  65. limit = 0
  66. }
  67. if filter.Limit != nil {
  68. limit = int(*filter.Limit)
  69. }
  70. open := filter.Open != nil && *filter.Open == true
  71. logs, err = log.ListSearch(search, channels, characters, events, open, limit)
  72. if err != nil {
  73. return nil, err
  74. }
  75. } else {
  76. logs, err = log.List(100)
  77. if err != nil {
  78. return nil, err
  79. }
  80. }
  81. resolvers := make([]*types.LogResolver, len(logs))
  82. for i := range logs {
  83. resolvers[i] = &types.LogResolver{L: logs[i]}
  84. }
  85. return resolvers, nil
  86. }
  87. // LogAddArgs is args for the addLog mutation
  88. type LogAddArgs struct {
  89. Input *struct {
  90. Date string
  91. Channel string
  92. Title *string
  93. Open *bool
  94. Event *string
  95. Description *string
  96. }
  97. }
  98. // AddLog resolves the addLog mutation
  99. func (r *MutationResolver) AddLog(ctx context.Context, args *LogAddArgs) (*types.LogResolver, error) {
  100. input := args.Input
  101. user := session.FromContext(ctx).User()
  102. if user == nil || !user.Permitted("log.add") {
  103. return nil, ErrUnauthorized
  104. }
  105. date, err := time.Parse(time.RFC3339Nano, args.Input.Date)
  106. if err != nil {
  107. return nil, err
  108. }
  109. title := ""
  110. if input.Title != nil {
  111. title = *input.Title
  112. }
  113. event := ""
  114. if input.Event != nil {
  115. event = *input.Event
  116. }
  117. description := ""
  118. if input.Description != nil {
  119. description = *input.Description
  120. }
  121. open := input.Open != nil && *input.Open == true
  122. log, err := log.New(date, input.Channel, title, event, description, open)
  123. if err != nil {
  124. return nil, err
  125. }
  126. go change.Submit("Log", "add", user.ID, log.ID, map[string]interface{}{
  127. "channel": log.Channel,
  128. "title": log.Title,
  129. "event": log.Event,
  130. "description": log.Description,
  131. "open": log.Open,
  132. })
  133. return &types.LogResolver{L: log}, nil
  134. }
  135. // LogEditArgs is an input
  136. type LogEditArgs struct {
  137. Input *struct {
  138. ID string
  139. Title *string
  140. Event *string
  141. Description *string
  142. Open *bool
  143. }
  144. }
  145. // EditLog resolves the editLog mutation
  146. func (r *MutationResolver) EditLog(ctx context.Context, args *LogEditArgs) (*types.LogResolver, error) {
  147. input := args.Input
  148. user := session.FromContext(ctx).User()
  149. if user == nil || !user.Permitted("log.edit") {
  150. return nil, ErrUnauthorized
  151. }
  152. log, err := log.FindID(input.ID)
  153. if err != nil {
  154. return nil, err
  155. }
  156. err = log.Edit(input.Title, input.Event, input.Description, input.Open)
  157. if err != nil {
  158. return nil, err
  159. }
  160. go change.Submit("Log", "edit", user.ID, log.ID, map[string]interface{}{
  161. "channel": log.Channel,
  162. "title": input.Title,
  163. "event": input.Event,
  164. "description": input.Description,
  165. "open": input.Open,
  166. })
  167. return &types.LogResolver{L: log}, nil
  168. }
  169. // RemoveLog resolves the removeLog mutation
  170. func (r *MutationResolver) RemoveLog(ctx context.Context, args *struct{ ID string }) (*types.LogResolver, error) {
  171. user := session.FromContext(ctx).User()
  172. if user == nil || !user.Permitted("log.remove") {
  173. return nil, ErrUnauthorized
  174. }
  175. log, err := log.FindID(args.ID)
  176. if err != nil {
  177. return nil, err
  178. }
  179. err = log.Remove()
  180. if err != nil {
  181. return nil, err
  182. }
  183. go change.Submit("Log", "remove", user.ID, log.ID, nil)
  184. return &types.LogResolver{L: log}, nil
  185. }