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 7.1KB

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