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

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