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.

245 lines
5.0 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. // PostAddArgs is args for addPost mutation
  40. type PostAddArgs struct {
  41. Input *struct {
  42. LogID string
  43. Time string
  44. Kind string
  45. Nick string
  46. Text string
  47. }
  48. }
  49. // AddPost resolves the addPost mutation
  50. func (r *MutationResolver) AddPost(ctx context.Context, args *PostAddArgs) (*PostResolver, error) {
  51. input := args.Input
  52. user := session.FromContext(ctx).User()
  53. if user == nil || !user.Permitted("post.add") {
  54. return nil, ErrUnauthorized
  55. }
  56. postTime, err := time.Parse(time.RFC3339Nano, input.Time)
  57. if err != nil {
  58. return nil, err
  59. }
  60. log, err := log.FindID(input.LogID)
  61. if err != nil {
  62. return nil, err
  63. }
  64. post, err := log.NewPost(postTime, input.Kind, input.Nick, input.Text)
  65. if err != nil {
  66. return nil, err
  67. }
  68. go change.Submit("Post", "add", user.ID, post.ID, map[string]interface{}{
  69. "logId": post.LogID,
  70. "time": post.Time,
  71. "kind": post.Kind,
  72. "nick": post.Nick,
  73. "text": post.Text,
  74. "position": post.Position,
  75. })
  76. go log.UpdateCharacters()
  77. return &PostResolver{P: post}, nil
  78. }
  79. // PostEditArgs is args for the editPost mutation
  80. type PostEditArgs struct {
  81. Input *struct {
  82. ID string
  83. Time *string
  84. Kind *string
  85. Nick *string
  86. Text *string
  87. }
  88. }
  89. // EditPost resolves the editPost mutation
  90. func (r *MutationResolver) EditPost(ctx context.Context, args *PostEditArgs) (*PostResolver, error) {
  91. input := args.Input
  92. user := session.FromContext(ctx).User()
  93. if user == nil || !user.Permitted("post.edit") {
  94. return nil, ErrUnauthorized
  95. }
  96. postTime := (*time.Time)(nil)
  97. if args.Input.Time != nil {
  98. t, err := time.Parse(time.RFC3339Nano, *input.Time)
  99. if err != nil {
  100. return nil, err
  101. }
  102. postTime = &t
  103. }
  104. post, err := log.FindPostID(input.ID)
  105. if err != nil {
  106. return nil, err
  107. }
  108. err = post.Edit(postTime, input.Kind, input.Nick, input.Text)
  109. if err != nil {
  110. return nil, err
  111. }
  112. go change.Submit("Post", "edit", user.ID, post.ID, map[string]interface{}{
  113. "time": postTime,
  114. "kind": input.Kind,
  115. "nick": input.Nick,
  116. "text": input.Text,
  117. })
  118. go log.UpdateCharacters(post.LogID)
  119. return &PostResolver{P: post}, nil
  120. }
  121. // PostMoveArgs is args for movePost mutation
  122. type PostMoveArgs struct {
  123. Input *struct {
  124. ID string
  125. ToPosition int32
  126. }
  127. }
  128. // MovePost resolves the movePost mutation
  129. func (r *MutationResolver) MovePost(ctx context.Context, args *PostMoveArgs) (*PostResolver, error) {
  130. input := args.Input
  131. user := session.FromContext(ctx).User()
  132. if user == nil || !user.Permitted("post.move") {
  133. return nil, ErrUnauthorized
  134. }
  135. post, err := log.FindPostID(input.ID)
  136. if err != nil {
  137. return nil, err
  138. }
  139. err = post.Move(int(input.ToPosition))
  140. if err != nil {
  141. return nil, err
  142. }
  143. go change.Submit("Post", "move", user.ID, post.ID, map[string]interface{}{
  144. "logId": post.LogID,
  145. "targetIndex": input.ToPosition,
  146. })
  147. return &PostResolver{P: post}, nil
  148. }
  149. // PostRemoveArgs is an arg
  150. type PostRemoveArgs struct {
  151. ID string
  152. }
  153. // RemovePost resolves the removePost mutation
  154. func (r *MutationResolver) RemovePost(ctx context.Context, args PostRemoveArgs) (*PostResolver, error) {
  155. user := session.FromContext(ctx).User()
  156. if user == nil || !user.Permitted("post.remove") {
  157. return nil, ErrUnauthorized
  158. }
  159. post, err := log.RemovePost(args.ID)
  160. if err != nil {
  161. return nil, err
  162. }
  163. go change.Submit("Post", "remove", user.ID, post.ID, map[string]interface{}{
  164. "logId": post.LogID,
  165. })
  166. go log.UpdateCharacters(post.LogID)
  167. return &PostResolver{P: post}, nil
  168. }
  169. // ID resolves Post.id
  170. func (r *PostResolver) ID() string {
  171. return r.P.ID
  172. }
  173. // LogID resolves Post.logId
  174. func (r *PostResolver) LogID() string {
  175. return r.P.LogID
  176. }
  177. // Time resolves Post.time
  178. func (r *PostResolver) Time() string {
  179. return r.P.Time.Format(time.RFC3339Nano)
  180. }
  181. // Kind resolves Post.logId
  182. func (r *PostResolver) Kind() string {
  183. return r.P.Kind
  184. }
  185. // Nick resolves Post.nick
  186. func (r *PostResolver) Nick() string {
  187. return r.P.Nick
  188. }
  189. // Text resolves Post.text
  190. func (r *PostResolver) Text() string {
  191. return r.P.Text
  192. }
  193. // Position resolves Post.text
  194. func (r *PostResolver) Position() int32 {
  195. return int32(r.P.Position)
  196. }