選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

issue_subscription.go 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. api "code.gitea.io/gitea/modules/structs"
  10. "code.gitea.io/gitea/routers/api/v1/utils"
  11. )
  12. // AddIssueSubscription Subscribe user to issue
  13. func AddIssueSubscription(ctx *context.APIContext) {
  14. // swagger:operation PUT /repos/{owner}/{repo}/issues/{index}/subscriptions/{user} issue issueAddSubscription
  15. // ---
  16. // summary: Subscribe user to issue
  17. // consumes:
  18. // - application/json
  19. // produces:
  20. // - application/json
  21. // parameters:
  22. // - name: owner
  23. // in: path
  24. // description: owner of the repo
  25. // type: string
  26. // required: true
  27. // - name: repo
  28. // in: path
  29. // description: name of the repo
  30. // type: string
  31. // required: true
  32. // - name: index
  33. // in: path
  34. // description: index of the issue
  35. // type: integer
  36. // format: int64
  37. // required: true
  38. // - name: user
  39. // in: path
  40. // description: user to subscribe
  41. // type: string
  42. // required: true
  43. // responses:
  44. // "200":
  45. // description: Already subscribed
  46. // "201":
  47. // description: Successfully Subscribed
  48. // "304":
  49. // description: User can only subscribe itself if he is no admin
  50. // "404":
  51. // "$ref": "#/responses/notFound"
  52. setIssueSubscription(ctx, true)
  53. }
  54. // DelIssueSubscription Unsubscribe user from issue
  55. func DelIssueSubscription(ctx *context.APIContext) {
  56. // swagger:operation DELETE /repos/{owner}/{repo}/issues/{index}/subscriptions/{user} issue issueDeleteSubscription
  57. // ---
  58. // summary: Unsubscribe user from issue
  59. // consumes:
  60. // - application/json
  61. // produces:
  62. // - application/json
  63. // parameters:
  64. // - name: owner
  65. // in: path
  66. // description: owner of the repo
  67. // type: string
  68. // required: true
  69. // - name: repo
  70. // in: path
  71. // description: name of the repo
  72. // type: string
  73. // required: true
  74. // - name: index
  75. // in: path
  76. // description: index of the issue
  77. // type: integer
  78. // format: int64
  79. // required: true
  80. // - name: user
  81. // in: path
  82. // description: user witch unsubscribe
  83. // type: string
  84. // required: true
  85. // responses:
  86. // "200":
  87. // description: Already unsubscribed
  88. // "201":
  89. // description: Successfully Unsubscribed
  90. // "304":
  91. // description: User can only subscribe itself if he is no admin
  92. // "404":
  93. // "$ref": "#/responses/notFound"
  94. setIssueSubscription(ctx, false)
  95. }
  96. func setIssueSubscription(ctx *context.APIContext, watch bool) {
  97. issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
  98. if err != nil {
  99. if models.IsErrIssueNotExist(err) {
  100. ctx.NotFound()
  101. } else {
  102. ctx.Error(http.StatusInternalServerError, "GetIssueByIndex", err)
  103. }
  104. return
  105. }
  106. user, err := models.GetUserByName(ctx.Params(":user"))
  107. if err != nil {
  108. if models.IsErrUserNotExist(err) {
  109. ctx.NotFound()
  110. } else {
  111. ctx.Error(http.StatusInternalServerError, "GetUserByName", err)
  112. }
  113. return
  114. }
  115. //only admin and user for itself can change subscription
  116. if user.ID != ctx.User.ID && !ctx.User.IsAdmin {
  117. ctx.Error(http.StatusForbidden, "User", nil)
  118. return
  119. }
  120. current, err := models.CheckIssueWatch(user, issue)
  121. if err != nil {
  122. ctx.Error(http.StatusInternalServerError, "CheckIssueWatch", err)
  123. return
  124. }
  125. // If watch state wont change
  126. if current == watch {
  127. ctx.Status(http.StatusOK)
  128. return
  129. }
  130. // Update watch state
  131. if err := models.CreateOrUpdateIssueWatch(user.ID, issue.ID, watch); err != nil {
  132. ctx.Error(http.StatusInternalServerError, "CreateOrUpdateIssueWatch", err)
  133. return
  134. }
  135. ctx.Status(http.StatusCreated)
  136. }
  137. // CheckIssueSubscription check if user is subscribed to an issue
  138. func CheckIssueSubscription(ctx *context.APIContext) {
  139. // swagger:operation GET /repos/{owner}/{repo}/issues/{index}/subscriptions/check issue issueCheckSubscription
  140. // ---
  141. // summary: Check if user is subscribed to an issue
  142. // consumes:
  143. // - application/json
  144. // produces:
  145. // - application/json
  146. // parameters:
  147. // - name: owner
  148. // in: path
  149. // description: owner of the repo
  150. // type: string
  151. // required: true
  152. // - name: repo
  153. // in: path
  154. // description: name of the repo
  155. // type: string
  156. // required: true
  157. // - name: index
  158. // in: path
  159. // description: index of the issue
  160. // type: integer
  161. // format: int64
  162. // required: true
  163. // responses:
  164. // "200":
  165. // "$ref": "#/responses/WatchInfo"
  166. // "404":
  167. // "$ref": "#/responses/notFound"
  168. issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
  169. if err != nil {
  170. if models.IsErrIssueNotExist(err) {
  171. ctx.NotFound()
  172. } else {
  173. ctx.Error(http.StatusInternalServerError, "GetIssueByIndex", err)
  174. }
  175. return
  176. }
  177. watching, err := models.CheckIssueWatch(ctx.User, issue)
  178. if err != nil {
  179. ctx.InternalServerError(err)
  180. return
  181. }
  182. ctx.JSON(http.StatusOK, api.WatchInfo{
  183. Subscribed: watching,
  184. Ignored: !watching,
  185. Reason: nil,
  186. CreatedAt: issue.CreatedUnix.AsTime(),
  187. URL: issue.APIURL() + "/subscriptions",
  188. RepositoryURL: ctx.Repo.Repository.APIURL(),
  189. })
  190. }
  191. // GetIssueSubscribers return subscribers of an issue
  192. func GetIssueSubscribers(ctx *context.APIContext) {
  193. // swagger:operation GET /repos/{owner}/{repo}/issues/{index}/subscriptions issue issueSubscriptions
  194. // ---
  195. // summary: Get users who subscribed on an issue.
  196. // consumes:
  197. // - application/json
  198. // produces:
  199. // - application/json
  200. // parameters:
  201. // - name: owner
  202. // in: path
  203. // description: owner of the repo
  204. // type: string
  205. // required: true
  206. // - name: repo
  207. // in: path
  208. // description: name of the repo
  209. // type: string
  210. // required: true
  211. // - name: index
  212. // in: path
  213. // description: index of the issue
  214. // type: integer
  215. // format: int64
  216. // required: true
  217. // - name: page
  218. // in: query
  219. // description: page number of results to return (1-based)
  220. // type: integer
  221. // - name: limit
  222. // in: query
  223. // description: page size of results
  224. // type: integer
  225. // responses:
  226. // "200":
  227. // "$ref": "#/responses/UserList"
  228. // "404":
  229. // "$ref": "#/responses/notFound"
  230. issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
  231. if err != nil {
  232. if models.IsErrIssueNotExist(err) {
  233. ctx.NotFound()
  234. } else {
  235. ctx.Error(http.StatusInternalServerError, "GetIssueByIndex", err)
  236. }
  237. return
  238. }
  239. iwl, err := models.GetIssueWatchers(issue.ID, utils.GetListOptions(ctx))
  240. if err != nil {
  241. ctx.Error(http.StatusInternalServerError, "GetIssueWatchers", err)
  242. return
  243. }
  244. var userIDs = make([]int64, 0, len(iwl))
  245. for _, iw := range iwl {
  246. userIDs = append(userIDs, iw.UserID)
  247. }
  248. users, err := models.GetUsersByIDs(userIDs)
  249. if err != nil {
  250. ctx.Error(http.StatusInternalServerError, "GetUsersByIDs", err)
  251. return
  252. }
  253. ctx.JSON(http.StatusOK, users.APIFormat())
  254. }