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.

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