Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

issue_subscription.go 4.9KB

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