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.

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