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.

233 lines
4.9 KiB

  1. package resolver
  2. import (
  3. "context"
  4. "time"
  5. "git.aiterp.net/rpdata/api/internal/session"
  6. "git.aiterp.net/rpdata/api/model/change"
  7. "git.aiterp.net/rpdata/api/model/log"
  8. )
  9. // PostResolver for the Post graphql type
  10. type PostResolver struct{ P log.Post }
  11. // PostArgs is an arg
  12. type PostArgs struct {
  13. ID string
  14. }
  15. // Post implements the post query
  16. func (r *QueryResolver) Post(ctx context.Context, args *PostArgs) (*PostResolver, error) {
  17. post, err := log.FindPostID(args.ID)
  18. if err != nil {
  19. return nil, err
  20. }
  21. return &PostResolver{P: post}, nil
  22. }
  23. // PostsArgs is an arg
  24. type PostsArgs struct {
  25. IDs []string
  26. }
  27. // Posts implements the posts query
  28. func (r *QueryResolver) Posts(ctx context.Context, args *PostsArgs) ([]*PostResolver, error) {
  29. posts, err := log.ListPostIDs(args.IDs...)
  30. if err != nil {
  31. return nil, err
  32. }
  33. resolvers := make([]*PostResolver, len(posts))
  34. for i := range resolvers {
  35. resolvers[i] = &PostResolver{P: posts[i]}
  36. }
  37. return resolvers, nil
  38. }
  39. // PostAddInput is an input
  40. type PostAddInput struct {
  41. LogID string
  42. Time string
  43. Kind string
  44. Nick string
  45. Text string
  46. }
  47. // AddPost resolves the addPost mutation
  48. func (r *MutationResolver) AddPost(ctx context.Context, args struct{ Input *PostAddInput }) (*PostResolver, error) {
  49. user := session.FromContext(ctx).User()
  50. if user == nil || !user.Permitted("post.add") {
  51. return nil, ErrUnauthorized
  52. }
  53. postTime, err := time.Parse(time.RFC3339Nano, args.Input.Time)
  54. if err != nil {
  55. return nil, err
  56. }
  57. log, err := log.FindID(args.Input.LogID)
  58. if err != nil {
  59. return nil, err
  60. }
  61. post, err := log.NewPost(postTime, args.Input.Kind, args.Input.Nick, args.Input.Text)
  62. if err != nil {
  63. return nil, err
  64. }
  65. change.Submit("Post", "add", user.ID, post.ID, map[string]interface{}{
  66. "logId": post.LogID,
  67. "time": post.Time,
  68. "kind": post.Kind,
  69. "nick": post.Nick,
  70. "text": post.Text,
  71. "position": post.Position,
  72. })
  73. go log.UpdateCharacters()
  74. return &PostResolver{P: post}, nil
  75. }
  76. // PostEditInput is an input
  77. type PostEditInput struct {
  78. ID string
  79. Time *string
  80. Kind *string
  81. Nick *string
  82. Text *string
  83. }
  84. // EditPost resolves the editPost mutation
  85. func (r *MutationResolver) EditPost(ctx context.Context, args struct{ Input *PostEditInput }) (*PostResolver, error) {
  86. user := session.FromContext(ctx).User()
  87. if user == nil || !user.Permitted("post.edit") {
  88. return nil, ErrUnauthorized
  89. }
  90. postTime := (*time.Time)(nil)
  91. if args.Input.Time != nil {
  92. t, err := time.Parse(time.RFC3339Nano, *args.Input.Time)
  93. if err != nil {
  94. return nil, err
  95. }
  96. postTime = &t
  97. }
  98. post, err := log.FindPostID(args.Input.ID)
  99. if err != nil {
  100. return nil, err
  101. }
  102. err = post.Edit(postTime, args.Input.Kind, args.Input.Nick, args.Input.Text)
  103. if err != nil {
  104. return nil, err
  105. }
  106. change.Submit("Post", "edit", user.ID, post.ID, map[string]interface{}{
  107. "time": postTime,
  108. "kind": args.Input.Kind,
  109. "nick": args.Input.Nick,
  110. "text": args.Input.Text,
  111. })
  112. go log.UpdateCharacters(post.LogID)
  113. return &PostResolver{P: post}, nil
  114. }
  115. // PostMoveInput is an input
  116. type PostMoveInput struct {
  117. ID string
  118. ToPosition int32
  119. }
  120. // MovePost resolves the movePost mutation
  121. func (r *MutationResolver) MovePost(ctx context.Context, args struct{ Input *PostMoveInput }) (*PostResolver, error) {
  122. user := session.FromContext(ctx).User()
  123. if user == nil || !user.Permitted("post.move") {
  124. return nil, ErrUnauthorized
  125. }
  126. post, err := log.FindPostID(args.Input.ID)
  127. if err != nil {
  128. return nil, err
  129. }
  130. err = post.Move(int(args.Input.ToPosition))
  131. if err != nil {
  132. return nil, err
  133. }
  134. change.Submit("Post", "move", user.ID, post.ID, map[string]interface{}{
  135. "logId": post.LogID,
  136. "targetIndex": args.Input.ToPosition,
  137. })
  138. return &PostResolver{P: post}, nil
  139. }
  140. // PostRemoveArgs is an arg
  141. type PostRemoveArgs struct {
  142. ID string
  143. }
  144. // RemovePost resolves the removePost mutation
  145. func (r *MutationResolver) RemovePost(ctx context.Context, args PostRemoveArgs) (*PostResolver, error) {
  146. user := session.FromContext(ctx).User()
  147. if user == nil || !user.Permitted("post.remove") {
  148. return nil, ErrUnauthorized
  149. }
  150. post, err := log.RemovePost(args.ID)
  151. if err != nil {
  152. return nil, err
  153. }
  154. change.Submit("Post", "remove", user.ID, post.ID, map[string]interface{}{
  155. "logId": post.LogID,
  156. })
  157. go log.UpdateCharacters(post.LogID)
  158. return &PostResolver{P: post}, nil
  159. }
  160. // ID resolves Post.id
  161. func (r *PostResolver) ID() string {
  162. return r.P.ID
  163. }
  164. // LogID resolves Post.logId
  165. func (r *PostResolver) LogID() string {
  166. return r.P.LogID
  167. }
  168. // Time resolves Post.time
  169. func (r *PostResolver) Time() string {
  170. return r.P.Time.Format(time.RFC3339Nano)
  171. }
  172. // Kind resolves Post.logId
  173. func (r *PostResolver) Kind() string {
  174. return r.P.Kind
  175. }
  176. // Nick resolves Post.nick
  177. func (r *PostResolver) Nick() string {
  178. return r.P.Nick
  179. }
  180. // Text resolves Post.text
  181. func (r *PostResolver) Text() string {
  182. return r.P.Text
  183. }
  184. // Position resolves Post.text
  185. func (r *PostResolver) Position() int32 {
  186. return int32(r.P.Position)
  187. }