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.

issue_subscription.go 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. "code.gitea.io/gitea/models"
  7. "code.gitea.io/gitea/modules/context"
  8. api "code.gitea.io/gitea/modules/structs"
  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. // description: Issue not found
  48. issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
  49. if err != nil {
  50. if models.IsErrIssueNotExist(err) {
  51. ctx.NotFound()
  52. } else {
  53. ctx.Error(500, "GetIssueByIndex", err)
  54. }
  55. return
  56. }
  57. user, err := models.GetUserByName(ctx.Params(":user"))
  58. if err != nil {
  59. if models.IsErrUserNotExist(err) {
  60. ctx.NotFound()
  61. } else {
  62. ctx.Error(500, "GetUserByName", err)
  63. }
  64. return
  65. }
  66. //only admin and user for itself can change subscription
  67. if user.ID != ctx.User.ID && !ctx.User.IsAdmin {
  68. ctx.Error(403, "User", nil)
  69. return
  70. }
  71. if err := models.CreateOrUpdateIssueWatch(user.ID, issue.ID, true); err != nil {
  72. ctx.Error(500, "CreateOrUpdateIssueWatch", err)
  73. return
  74. }
  75. ctx.Status(201)
  76. }
  77. // DelIssueSubscription Unsubscribe user from issue
  78. func DelIssueSubscription(ctx *context.APIContext) {
  79. // swagger:operation DELETE /repos/{owner}/{repo}/issues/{index}/subscriptions/{user} issue issueDeleteSubscription
  80. // ---
  81. // summary: Unsubscribe user from issue
  82. // consumes:
  83. // - application/json
  84. // produces:
  85. // - application/json
  86. // parameters:
  87. // - name: owner
  88. // in: path
  89. // description: owner of the repo
  90. // type: string
  91. // required: true
  92. // - name: repo
  93. // in: path
  94. // description: name of the repo
  95. // type: string
  96. // required: true
  97. // - name: index
  98. // in: path
  99. // description: index of the issue
  100. // type: integer
  101. // format: int64
  102. // required: true
  103. // - name: user
  104. // in: path
  105. // description: user witch unsubscribe
  106. // type: string
  107. // required: true
  108. // responses:
  109. // "201":
  110. // "$ref": "#/responses/empty"
  111. // "304":
  112. // description: User can only subscribe itself if he is no admin
  113. // "404":
  114. // description: Issue not found
  115. issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
  116. if err != nil {
  117. if models.IsErrIssueNotExist(err) {
  118. ctx.NotFound()
  119. } else {
  120. ctx.Error(500, "GetIssueByIndex", err)
  121. }
  122. return
  123. }
  124. user, err := models.GetUserByName(ctx.Params(":user"))
  125. if err != nil {
  126. if models.IsErrUserNotExist(err) {
  127. ctx.NotFound()
  128. } else {
  129. ctx.Error(500, "GetUserByName", err)
  130. }
  131. return
  132. }
  133. //only admin and user for itself can change subscription
  134. if user.ID != ctx.User.ID && !ctx.User.IsAdmin {
  135. ctx.Error(403, "User", nil)
  136. return
  137. }
  138. if err := models.CreateOrUpdateIssueWatch(user.ID, issue.ID, false); err != nil {
  139. ctx.Error(500, "CreateOrUpdateIssueWatch", err)
  140. return
  141. }
  142. ctx.Status(201)
  143. }
  144. // GetIssueSubscribers return subscribers of an issue
  145. func GetIssueSubscribers(ctx *context.APIContext, form api.User) {
  146. // swagger:operation GET /repos/{owner}/{repo}/issues/{index}/subscriptions issue issueSubscriptions
  147. // ---
  148. // summary: Get users who subscribed on an issue.
  149. // consumes:
  150. // - application/json
  151. // produces:
  152. // - application/json
  153. // parameters:
  154. // - name: owner
  155. // in: path
  156. // description: owner of the repo
  157. // type: string
  158. // required: true
  159. // - name: repo
  160. // in: path
  161. // description: name of the repo
  162. // type: string
  163. // required: true
  164. // - name: index
  165. // in: path
  166. // description: index of the issue
  167. // type: integer
  168. // format: int64
  169. // required: true
  170. // responses:
  171. // "201":
  172. // "$ref": "#/responses/empty"
  173. // "404":
  174. // description: Issue not found
  175. issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
  176. if err != nil {
  177. if models.IsErrIssueNotExist(err) {
  178. ctx.NotFound()
  179. } else {
  180. ctx.Error(500, "GetIssueByIndex", err)
  181. }
  182. return
  183. }
  184. iwl, err := models.GetIssueWatchers(issue.ID)
  185. if err != nil {
  186. ctx.Error(500, "GetIssueWatchers", err)
  187. return
  188. }
  189. users, err := iwl.LoadWatchUsers()
  190. if err != nil {
  191. ctx.Error(500, "LoadWatchUsers", err)
  192. return
  193. }
  194. ctx.JSON(200, users.APIFormat())
  195. }