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.

304 lines
6.3 KiB

  1. package resolver
  2. import (
  3. "context"
  4. "time"
  5. "git.aiterp.net/rpdata/api/model/change"
  6. "git.aiterp.net/rpdata/api/internal/session"
  7. "git.aiterp.net/rpdata/api/model/character"
  8. "git.aiterp.net/rpdata/api/model/log"
  9. )
  10. // LogResolver for the Log graphql type
  11. type LogResolver struct{ L log.Log }
  12. // LogArgs is an arg
  13. type LogArgs struct {
  14. ID *string
  15. }
  16. // LogPostArgs is an arg
  17. type LogPostArgs struct {
  18. Kinds *[]string
  19. }
  20. // Log finds log
  21. func (r *QueryResolver) Log(ctx context.Context, args *LogArgs) (*LogResolver, error) {
  22. var l log.Log
  23. var err error
  24. switch {
  25. case args.ID != nil:
  26. l, err = log.FindID(*args.ID)
  27. default:
  28. err = ErrCannotResolve
  29. }
  30. if err != nil {
  31. return nil, err
  32. }
  33. return &LogResolver{L: l}, nil
  34. }
  35. // LogQueryInput is an input
  36. type LogQueryInput struct {
  37. Search *string
  38. Characters *[]string
  39. Channels *[]string
  40. Events *[]string
  41. Open *bool
  42. Limit *int32
  43. }
  44. // Logs lists logs
  45. func (r *QueryResolver) Logs(ctx context.Context, args *struct{ Input *LogQueryInput }) ([]*LogResolver, error) {
  46. var logs []log.Log
  47. var err error
  48. input := args.Input
  49. if input != nil {
  50. // Parse input
  51. limit := 100
  52. search := ""
  53. if input.Search != nil {
  54. search = *input.Search
  55. limit = 0
  56. }
  57. channels := []string(nil)
  58. if input.Channels != nil {
  59. channels = *input.Channels
  60. limit = 0
  61. }
  62. characters := []string(nil)
  63. if input.Characters != nil {
  64. characters = *input.Characters
  65. limit = 0
  66. }
  67. events := []string(nil)
  68. if input.Events != nil {
  69. events = *input.Events
  70. limit = 0
  71. }
  72. if input.Limit != nil {
  73. limit = int(*input.Limit)
  74. }
  75. open := input.Open != nil && *input.Open == true
  76. logs, err = log.ListSearch(search, channels, characters, events, open, limit)
  77. if err != nil {
  78. return nil, err
  79. }
  80. } else {
  81. logs, err = log.List(100)
  82. if err != nil {
  83. return nil, err
  84. }
  85. }
  86. resolvers := make([]*LogResolver, len(logs))
  87. for i := range logs {
  88. resolvers[i] = &LogResolver{L: logs[i]}
  89. }
  90. return resolvers, nil
  91. }
  92. // LogAddInput is an input
  93. type LogAddInput struct {
  94. Date string
  95. Channel string
  96. Title *string
  97. Open *bool
  98. Event *string
  99. Description *string
  100. }
  101. // AddLog resolves the addLog mutation
  102. func (r *MutationResolver) AddLog(ctx context.Context, args *struct{ Input LogAddInput }) (*LogResolver, error) {
  103. user := session.FromContext(ctx).User()
  104. if user == nil || !user.Permitted("log.add") {
  105. return nil, ErrUnauthorized
  106. }
  107. date, err := time.Parse(time.RFC3339Nano, args.Input.Date)
  108. if err != nil {
  109. return nil, err
  110. }
  111. title := ""
  112. if args.Input.Title != nil {
  113. title = *args.Input.Title
  114. }
  115. event := ""
  116. if args.Input.Event != nil {
  117. event = *args.Input.Event
  118. }
  119. description := ""
  120. if args.Input.Description != nil {
  121. description = *args.Input.Description
  122. }
  123. open := args.Input.Open != nil && *args.Input.Open == true
  124. log, err := log.New(date, args.Input.Channel, title, event, description, open)
  125. if err != nil {
  126. return nil, err
  127. }
  128. change.Submit("Log", "add", user.ID, log.ID, map[string]interface{}{
  129. "channel": log.Channel,
  130. "title": log.Title,
  131. "event": log.Event,
  132. "description": log.Description,
  133. "open": log.Open,
  134. })
  135. return &LogResolver{L: log}, nil
  136. }
  137. // LogEditInput is an input
  138. type LogEditInput struct {
  139. ID string
  140. Title *string
  141. Event *string
  142. Description *string
  143. Open *bool
  144. }
  145. // EditLog resolves the addLog mutation
  146. func (r *MutationResolver) EditLog(ctx context.Context, args *struct{ Input LogEditInput }) (*LogResolver, error) {
  147. user := session.FromContext(ctx).User()
  148. if user == nil || !user.Permitted("log.edit") {
  149. return nil, ErrUnauthorized
  150. }
  151. l, err := log.FindID(args.Input.ID)
  152. if err != nil {
  153. return nil, err
  154. }
  155. err = l.Edit(args.Input.Title, args.Input.Event, args.Input.Description, args.Input.Open)
  156. if err != nil {
  157. return nil, err
  158. }
  159. change.Submit("Log", "edit", user.ID, l.ID, map[string]interface{}{
  160. "channel": l.Channel,
  161. "title": args.Input.Title,
  162. "event": args.Input.Event,
  163. "description": args.Input.Description,
  164. "open": args.Input.Open,
  165. })
  166. return &LogResolver{L: l}, nil
  167. }
  168. // RemoveLog resolves the removeLog mutation
  169. func (r *MutationResolver) RemoveLog(ctx context.Context, args *struct{ ID string }) (*LogResolver, error) {
  170. user := session.FromContext(ctx).User()
  171. if user == nil || !user.Permitted("log.remove") {
  172. return nil, ErrUnauthorized
  173. }
  174. l, err := log.FindID(args.ID)
  175. if err != nil {
  176. return nil, err
  177. }
  178. err = log.Remove(args.ID)
  179. if err != nil {
  180. return nil, err
  181. }
  182. change.Submit("Log", "add", user.ID, l.ID, map[string]interface{}{
  183. "channel": l.Channel,
  184. "title": l.Title,
  185. "event": l.Event,
  186. "description": l.Description,
  187. "open": l.Open,
  188. })
  189. return &LogResolver{L: l}, nil
  190. }
  191. // ID resolves Log.id
  192. func (r *LogResolver) ID() string {
  193. return r.L.ID
  194. }
  195. // ShortID resolves Log.shortId
  196. func (r *LogResolver) ShortID() string {
  197. return r.L.ShortID
  198. }
  199. // Date resolves Log.date
  200. func (r *LogResolver) Date() string {
  201. return r.L.Date.Format(time.RFC3339Nano)
  202. }
  203. // Channel resolves Log.channel
  204. func (r *LogResolver) Channel() string {
  205. return r.L.Channel
  206. }
  207. // Title resolves Log.title
  208. func (r *LogResolver) Title() string {
  209. return r.L.Title
  210. }
  211. // Event resolves Log.event
  212. func (r *LogResolver) Event() string {
  213. return r.L.Event
  214. }
  215. // Description resolves Log.description
  216. func (r *LogResolver) Description() string {
  217. return r.L.Description
  218. }
  219. // Open resolves Log.open
  220. func (r *LogResolver) Open() bool {
  221. return r.L.Open
  222. }
  223. // Characters resolves Log.characters
  224. func (r *LogResolver) Characters(ctx context.Context) ([]*CharacterResolver, error) {
  225. chars, err := character.ListIDs(r.L.CharacterIDs...)
  226. if err != nil {
  227. return nil, err
  228. }
  229. resolvers := make([]*CharacterResolver, 0, len(chars))
  230. for i := range chars {
  231. if chars[i].ID == "" {
  232. continue
  233. }
  234. resolvers = append(resolvers, &CharacterResolver{C: chars[i]})
  235. }
  236. return resolvers, nil
  237. }
  238. // Posts resolves Log.posts
  239. func (r *LogResolver) Posts(ctx context.Context, args *LogPostArgs) ([]*PostResolver, error) {
  240. var kinds []string
  241. if args.Kinds != nil {
  242. kinds = *args.Kinds
  243. }
  244. posts, err := r.L.Posts(kinds...)
  245. if err != nil {
  246. return nil, err
  247. }
  248. resolvers := make([]*PostResolver, len(posts))
  249. for i := range posts {
  250. resolvers[i] = &PostResolver{posts[i]}
  251. }
  252. return resolvers, nil
  253. }