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.

325 lines
6.7 KiB

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