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 6.7 kB

API add/generalize pagination (#9452) * paginate results * fixed deadlock * prevented breaking change * updated swagger * go fmt * fixed find topic * go mod tidy * go mod vendor with go1.13.5 * fixed repo find topics * fixed unit test * added Limit method to Engine struct; use engine variable when provided; fixed gitignore * use ItemsPerPage for default pagesize; fix GetWatchers, getOrgUsersByOrgID and GetStargazers; fix GetAllCommits headers; reverted some changed behaviors * set Page value on Home route * improved memory allocations * fixed response headers * removed logfiles * fixed import order * import order * improved swagger * added function to get models.ListOptions from context * removed pagesize diff on unit test * fixed imports * removed unnecessary struct field * fixed go fmt * scoped PR * code improvements * code improvements * go mod tidy * fixed import order * fixed commit statuses session * fixed files headers * fixed headers; added pagination for notifications * go mod tidy * go fmt * removed Private from user search options; added setting.UI.IssuePagingNum as default valeu on repo's issues list * Apply suggestions from code review Co-Authored-By: 6543 <6543@obermui.de> Co-Authored-By: zeripath <art27@cantab.net> * fixed build error * CI.restart() * fixed merge conflicts resolve * fixed conflicts resolve * improved FindTrackedTimesOptions.ToOptions() method * added backwards compatibility on ListReleases request; fixed issue tracked time ToSession * fixed build error; fixed swagger template * fixed swagger template * fixed ListReleases backwards compatibility * added page to user search route Co-authored-by: techknowlogick <matti@mdranta.net> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: zeripath <art27@cantab.net>
5 years ago
API add/generalize pagination (#9452) * paginate results * fixed deadlock * prevented breaking change * updated swagger * go fmt * fixed find topic * go mod tidy * go mod vendor with go1.13.5 * fixed repo find topics * fixed unit test * added Limit method to Engine struct; use engine variable when provided; fixed gitignore * use ItemsPerPage for default pagesize; fix GetWatchers, getOrgUsersByOrgID and GetStargazers; fix GetAllCommits headers; reverted some changed behaviors * set Page value on Home route * improved memory allocations * fixed response headers * removed logfiles * fixed import order * import order * improved swagger * added function to get models.ListOptions from context * removed pagesize diff on unit test * fixed imports * removed unnecessary struct field * fixed go fmt * scoped PR * code improvements * code improvements * go mod tidy * fixed import order * fixed commit statuses session * fixed files headers * fixed headers; added pagination for notifications * go mod tidy * go fmt * removed Private from user search options; added setting.UI.IssuePagingNum as default valeu on repo's issues list * Apply suggestions from code review Co-Authored-By: 6543 <6543@obermui.de> Co-Authored-By: zeripath <art27@cantab.net> * fixed build error * CI.restart() * fixed merge conflicts resolve * fixed conflicts resolve * improved FindTrackedTimesOptions.ToOptions() method * added backwards compatibility on ListReleases request; fixed issue tracked time ToSession * fixed build error; fixed swagger template * fixed swagger template * fixed ListReleases backwards compatibility * added page to user search route Co-authored-by: techknowlogick <matti@mdranta.net> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: zeripath <art27@cantab.net>
5 years ago
API add/generalize pagination (#9452) * paginate results * fixed deadlock * prevented breaking change * updated swagger * go fmt * fixed find topic * go mod tidy * go mod vendor with go1.13.5 * fixed repo find topics * fixed unit test * added Limit method to Engine struct; use engine variable when provided; fixed gitignore * use ItemsPerPage for default pagesize; fix GetWatchers, getOrgUsersByOrgID and GetStargazers; fix GetAllCommits headers; reverted some changed behaviors * set Page value on Home route * improved memory allocations * fixed response headers * removed logfiles * fixed import order * import order * improved swagger * added function to get models.ListOptions from context * removed pagesize diff on unit test * fixed imports * removed unnecessary struct field * fixed go fmt * scoped PR * code improvements * code improvements * go mod tidy * fixed import order * fixed commit statuses session * fixed files headers * fixed headers; added pagination for notifications * go mod tidy * go fmt * removed Private from user search options; added setting.UI.IssuePagingNum as default valeu on repo's issues list * Apply suggestions from code review Co-Authored-By: 6543 <6543@obermui.de> Co-Authored-By: zeripath <art27@cantab.net> * fixed build error * CI.restart() * fixed merge conflicts resolve * fixed conflicts resolve * improved FindTrackedTimesOptions.ToOptions() method * added backwards compatibility on ListReleases request; fixed issue tracked time ToSession * fixed build error; fixed swagger template * fixed swagger template * fixed ListReleases backwards compatibility * added page to user search route Co-authored-by: techknowlogick <matti@mdranta.net> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: zeripath <art27@cantab.net>
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. // "201":
  45. // "$ref": "#/responses/empty"
  46. // "304":
  47. // description: User can only subscribe itself if he is no admin
  48. // "404":
  49. // "$ref": "#/responses/notFound"
  50. setIssueSubscription(ctx, true)
  51. }
  52. // DelIssueSubscription Unsubscribe user from issue
  53. func DelIssueSubscription(ctx *context.APIContext) {
  54. // swagger:operation DELETE /repos/{owner}/{repo}/issues/{index}/subscriptions/{user} issue issueDeleteSubscription
  55. // ---
  56. // summary: Unsubscribe user from issue
  57. // consumes:
  58. // - application/json
  59. // produces:
  60. // - application/json
  61. // parameters:
  62. // - name: owner
  63. // in: path
  64. // description: owner of the repo
  65. // type: string
  66. // required: true
  67. // - name: repo
  68. // in: path
  69. // description: name of the repo
  70. // type: string
  71. // required: true
  72. // - name: index
  73. // in: path
  74. // description: index of the issue
  75. // type: integer
  76. // format: int64
  77. // required: true
  78. // - name: user
  79. // in: path
  80. // description: user witch unsubscribe
  81. // type: string
  82. // required: true
  83. // responses:
  84. // "201":
  85. // "$ref": "#/responses/empty"
  86. // "304":
  87. // description: User can only subscribe itself if he is no admin
  88. // "404":
  89. // "$ref": "#/responses/notFound"
  90. setIssueSubscription(ctx, false)
  91. }
  92. func setIssueSubscription(ctx *context.APIContext, watch bool) {
  93. issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
  94. if err != nil {
  95. if models.IsErrIssueNotExist(err) {
  96. ctx.NotFound()
  97. } else {
  98. ctx.Error(http.StatusInternalServerError, "GetIssueByIndex", err)
  99. }
  100. return
  101. }
  102. user, err := models.GetUserByName(ctx.Params(":user"))
  103. if err != nil {
  104. if models.IsErrUserNotExist(err) {
  105. ctx.NotFound()
  106. } else {
  107. ctx.Error(http.StatusInternalServerError, "GetUserByName", err)
  108. }
  109. return
  110. }
  111. //only admin and user for itself can change subscription
  112. if user.ID != ctx.User.ID && !ctx.User.IsAdmin {
  113. ctx.Error(http.StatusForbidden, "User", nil)
  114. return
  115. }
  116. if err := models.CreateOrUpdateIssueWatch(user.ID, issue.ID, watch); err != nil {
  117. ctx.Error(http.StatusInternalServerError, "CreateOrUpdateIssueWatch", err)
  118. return
  119. }
  120. ctx.Status(http.StatusCreated)
  121. }
  122. // CheckIssueSubscription check if user is subscribed to an issue
  123. func CheckIssueSubscription(ctx *context.APIContext) {
  124. // swagger:operation GET /repos/{owner}/{repo}/issues/{index}/subscriptions/check issue issueCheckSubscription
  125. // ---
  126. // summary: Check if user is subscribed to an issue
  127. // consumes:
  128. // - application/json
  129. // produces:
  130. // - application/json
  131. // parameters:
  132. // - name: owner
  133. // in: path
  134. // description: owner of the repo
  135. // type: string
  136. // required: true
  137. // - name: repo
  138. // in: path
  139. // description: name of the repo
  140. // type: string
  141. // required: true
  142. // - name: index
  143. // in: path
  144. // description: index of the issue
  145. // type: integer
  146. // format: int64
  147. // required: true
  148. // responses:
  149. // "200":
  150. // "$ref": "#/responses/WatchInfo"
  151. // "404":
  152. // "$ref": "#/responses/notFound"
  153. issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
  154. if err != nil {
  155. if models.IsErrIssueNotExist(err) {
  156. ctx.NotFound()
  157. } else {
  158. ctx.Error(http.StatusInternalServerError, "GetIssueByIndex", err)
  159. }
  160. return
  161. }
  162. watching, err := models.CheckIssueWatch(ctx.User, issue)
  163. if err != nil {
  164. ctx.InternalServerError(err)
  165. return
  166. }
  167. ctx.JSON(http.StatusOK, api.WatchInfo{
  168. Subscribed: watching,
  169. Ignored: !watching,
  170. Reason: nil,
  171. CreatedAt: issue.CreatedUnix.AsTime(),
  172. URL: issue.APIURL() + "/subscriptions",
  173. RepositoryURL: ctx.Repo.Repository.APIURL(),
  174. })
  175. }
  176. // GetIssueSubscribers return subscribers of an issue
  177. func GetIssueSubscribers(ctx *context.APIContext) {
  178. // swagger:operation GET /repos/{owner}/{repo}/issues/{index}/subscriptions issue issueSubscriptions
  179. // ---
  180. // summary: Get users who subscribed on an issue.
  181. // consumes:
  182. // - application/json
  183. // produces:
  184. // - application/json
  185. // parameters:
  186. // - name: owner
  187. // in: path
  188. // description: owner of the repo
  189. // type: string
  190. // required: true
  191. // - name: repo
  192. // in: path
  193. // description: name of the repo
  194. // type: string
  195. // required: true
  196. // - name: index
  197. // in: path
  198. // description: index of the issue
  199. // type: integer
  200. // format: int64
  201. // required: true
  202. // - name: page
  203. // in: query
  204. // description: page number of results to return (1-based)
  205. // type: integer
  206. // - name: limit
  207. // in: query
  208. // description: page size of results, maximum page size is 50
  209. // type: integer
  210. // responses:
  211. // "200":
  212. // "$ref": "#/responses/UserList"
  213. // "404":
  214. // "$ref": "#/responses/notFound"
  215. issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
  216. if err != nil {
  217. if models.IsErrIssueNotExist(err) {
  218. ctx.NotFound()
  219. } else {
  220. ctx.Error(http.StatusInternalServerError, "GetIssueByIndex", err)
  221. }
  222. return
  223. }
  224. iwl, err := models.GetIssueWatchers(issue.ID, utils.GetListOptions(ctx))
  225. if err != nil {
  226. ctx.Error(http.StatusInternalServerError, "GetIssueWatchers", err)
  227. return
  228. }
  229. var userIDs = make([]int64, 0, len(iwl))
  230. for _, iw := range iwl {
  231. userIDs = append(userIDs, iw.UserID)
  232. }
  233. users, err := models.GetUsersByIDs(userIDs)
  234. if err != nil {
  235. ctx.Error(http.StatusInternalServerError, "GetUsersByIDs", err)
  236. return
  237. }
  238. ctx.JSON(http.StatusOK, users.APIFormat())
  239. }