You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

issue_subscription.go 4.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package repo
  5. import (
  6. "net/http"
  7. "code.gitea.io/gitea/models"
  8. "code.gitea.io/gitea/modules/context"
  9. )
  10. // AddIssueSubscription Subscribe user to issue
  11. func AddIssueSubscription(ctx *context.APIContext) {
  12. // swagger:operation PUT /repos/{owner}/{repo}/issues/{index}/subscriptions/{user} issue issueAddSubscription
  13. // ---
  14. // summary: Subscribe user to issue
  15. // consumes:
  16. // - application/json
  17. // produces:
  18. // - application/json
  19. // parameters:
  20. // - name: owner
  21. // in: path
  22. // description: owner of the repo
  23. // type: string
  24. // required: true
  25. // - name: repo
  26. // in: path
  27. // description: name of the repo
  28. // type: string
  29. // required: true
  30. // - name: index
  31. // in: path
  32. // description: index of the issue
  33. // type: integer
  34. // format: int64
  35. // required: true
  36. // - name: user
  37. // in: path
  38. // description: user to subscribe
  39. // type: string
  40. // required: true
  41. // responses:
  42. // "201":
  43. // "$ref": "#/responses/empty"
  44. // "304":
  45. // description: User can only subscribe itself if he is no admin
  46. // "404":
  47. // "$ref": "#/responses/notFound"
  48. setIssueSubscription(ctx, true)
  49. }
  50. // DelIssueSubscription Unsubscribe user from issue
  51. func DelIssueSubscription(ctx *context.APIContext) {
  52. // swagger:operation DELETE /repos/{owner}/{repo}/issues/{index}/subscriptions/{user} issue issueDeleteSubscription
  53. // ---
  54. // summary: Unsubscribe user from issue
  55. // consumes:
  56. // - application/json
  57. // produces:
  58. // - application/json
  59. // parameters:
  60. // - name: owner
  61. // in: path
  62. // description: owner of the repo
  63. // type: string
  64. // required: true
  65. // - name: repo
  66. // in: path
  67. // description: name of the repo
  68. // type: string
  69. // required: true
  70. // - name: index
  71. // in: path
  72. // description: index of the issue
  73. // type: integer
  74. // format: int64
  75. // required: true
  76. // - name: user
  77. // in: path
  78. // description: user witch unsubscribe
  79. // type: string
  80. // required: true
  81. // responses:
  82. // "201":
  83. // "$ref": "#/responses/empty"
  84. // "304":
  85. // description: User can only subscribe itself if he is no admin
  86. // "404":
  87. // "$ref": "#/responses/notFound"
  88. setIssueSubscription(ctx, false)
  89. }
  90. func setIssueSubscription(ctx *context.APIContext, watch bool) {
  91. issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
  92. if err != nil {
  93. if models.IsErrIssueNotExist(err) {
  94. ctx.NotFound()
  95. } else {
  96. ctx.Error(http.StatusInternalServerError, "GetIssueByIndex", err)
  97. }
  98. return
  99. }
  100. user, err := models.GetUserByName(ctx.Params(":user"))
  101. if err != nil {
  102. if models.IsErrUserNotExist(err) {
  103. ctx.NotFound()
  104. } else {
  105. ctx.Error(http.StatusInternalServerError, "GetUserByName", err)
  106. }
  107. return
  108. }
  109. //only admin and user for itself can change subscription
  110. if user.ID != ctx.User.ID && !ctx.User.IsAdmin {
  111. ctx.Error(http.StatusForbidden, "User", nil)
  112. return
  113. }
  114. if err := models.CreateOrUpdateIssueWatch(user.ID, issue.ID, watch); err != nil {
  115. ctx.Error(http.StatusInternalServerError, "CreateOrUpdateIssueWatch", err)
  116. return
  117. }
  118. ctx.Status(http.StatusCreated)
  119. }
  120. // GetIssueSubscribers return subscribers of an issue
  121. func GetIssueSubscribers(ctx *context.APIContext) {
  122. // swagger:operation GET /repos/{owner}/{repo}/issues/{index}/subscriptions issue issueSubscriptions
  123. // ---
  124. // summary: Get users who subscribed on an issue.
  125. // consumes:
  126. // - application/json
  127. // produces:
  128. // - application/json
  129. // parameters:
  130. // - name: owner
  131. // in: path
  132. // description: owner of the repo
  133. // type: string
  134. // required: true
  135. // - name: repo
  136. // in: path
  137. // description: name of the repo
  138. // type: string
  139. // required: true
  140. // - name: index
  141. // in: path
  142. // description: index of the issue
  143. // type: integer
  144. // format: int64
  145. // required: true
  146. // responses:
  147. // "200":
  148. // "$ref": "#/responses/UserList"
  149. // "404":
  150. // "$ref": "#/responses/notFound"
  151. issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
  152. if err != nil {
  153. if models.IsErrIssueNotExist(err) {
  154. ctx.NotFound()
  155. } else {
  156. ctx.Error(http.StatusInternalServerError, "GetIssueByIndex", err)
  157. }
  158. return
  159. }
  160. iwl, err := models.GetIssueWatchers(issue.ID)
  161. if err != nil {
  162. ctx.Error(http.StatusInternalServerError, "GetIssueWatchers", err)
  163. return
  164. }
  165. users, err := iwl.LoadWatchUsers()
  166. if err != nil {
  167. ctx.Error(http.StatusInternalServerError, "LoadWatchUsers", err)
  168. return
  169. }
  170. ctx.JSON(http.StatusOK, users.APIFormat())
  171. }