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.

watch.go 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. // Copyright 2016 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 user
  5. import (
  6. "net/http"
  7. "code.gitea.io/gitea/models"
  8. "code.gitea.io/gitea/modules/context"
  9. "code.gitea.io/gitea/modules/convert"
  10. api "code.gitea.io/gitea/modules/structs"
  11. "code.gitea.io/gitea/routers/api/v1/utils"
  12. )
  13. // getWatchedRepos returns the repos that the user with the specified userID is watching
  14. func getWatchedRepos(user *models.User, private bool, listOptions models.ListOptions) ([]*api.Repository, int64, error) {
  15. watchedRepos, total, err := models.GetWatchedRepos(user.ID, private, listOptions)
  16. if err != nil {
  17. return nil, 0, err
  18. }
  19. repos := make([]*api.Repository, len(watchedRepos))
  20. for i, watched := range watchedRepos {
  21. access, err := models.AccessLevel(user, watched)
  22. if err != nil {
  23. return nil, 0, err
  24. }
  25. repos[i] = convert.ToRepo(watched, access)
  26. }
  27. return repos, total, nil
  28. }
  29. // GetWatchedRepos returns the repos that the user specified in ctx is watching
  30. func GetWatchedRepos(ctx *context.APIContext) {
  31. // swagger:operation GET /users/{username}/subscriptions user userListSubscriptions
  32. // ---
  33. // summary: List the repositories watched by a user
  34. // produces:
  35. // - application/json
  36. // parameters:
  37. // - name: username
  38. // type: string
  39. // in: path
  40. // description: username of the user
  41. // required: true
  42. // - name: page
  43. // in: query
  44. // description: page number of results to return (1-based)
  45. // type: integer
  46. // - name: limit
  47. // in: query
  48. // description: page size of results
  49. // type: integer
  50. // responses:
  51. // "200":
  52. // "$ref": "#/responses/RepositoryList"
  53. user := GetUserByParams(ctx)
  54. private := user.ID == ctx.User.ID
  55. repos, total, err := getWatchedRepos(user, private, utils.GetListOptions(ctx))
  56. if err != nil {
  57. ctx.Error(http.StatusInternalServerError, "getWatchedRepos", err)
  58. }
  59. ctx.SetTotalCountHeader(total)
  60. ctx.JSON(http.StatusOK, &repos)
  61. }
  62. // GetMyWatchedRepos returns the repos that the authenticated user is watching
  63. func GetMyWatchedRepos(ctx *context.APIContext) {
  64. // swagger:operation GET /user/subscriptions user userCurrentListSubscriptions
  65. // ---
  66. // summary: List repositories watched by the authenticated user
  67. // produces:
  68. // - application/json
  69. // parameters:
  70. // - name: page
  71. // in: query
  72. // description: page number of results to return (1-based)
  73. // type: integer
  74. // - name: limit
  75. // in: query
  76. // description: page size of results
  77. // type: integer
  78. // responses:
  79. // "200":
  80. // "$ref": "#/responses/RepositoryList"
  81. repos, total, err := getWatchedRepos(ctx.User, true, utils.GetListOptions(ctx))
  82. if err != nil {
  83. ctx.Error(http.StatusInternalServerError, "getWatchedRepos", err)
  84. }
  85. ctx.SetTotalCountHeader(total)
  86. ctx.JSON(http.StatusOK, &repos)
  87. }
  88. // IsWatching returns whether the authenticated user is watching the repo
  89. // specified in ctx
  90. func IsWatching(ctx *context.APIContext) {
  91. // swagger:operation GET /repos/{owner}/{repo}/subscription repository userCurrentCheckSubscription
  92. // ---
  93. // summary: Check if the current user is watching a repo
  94. // parameters:
  95. // - name: owner
  96. // in: path
  97. // description: owner of the repo
  98. // type: string
  99. // required: true
  100. // - name: repo
  101. // in: path
  102. // description: name of the repo
  103. // type: string
  104. // required: true
  105. // responses:
  106. // "200":
  107. // "$ref": "#/responses/WatchInfo"
  108. // "404":
  109. // description: User is not watching this repo or repo do not exist
  110. if models.IsWatching(ctx.User.ID, ctx.Repo.Repository.ID) {
  111. ctx.JSON(http.StatusOK, api.WatchInfo{
  112. Subscribed: true,
  113. Ignored: false,
  114. Reason: nil,
  115. CreatedAt: ctx.Repo.Repository.CreatedUnix.AsTime(),
  116. URL: subscriptionURL(ctx.Repo.Repository),
  117. RepositoryURL: ctx.Repo.Repository.APIURL(),
  118. })
  119. } else {
  120. ctx.NotFound()
  121. }
  122. }
  123. // Watch the repo specified in ctx, as the authenticated user
  124. func Watch(ctx *context.APIContext) {
  125. // swagger:operation PUT /repos/{owner}/{repo}/subscription repository userCurrentPutSubscription
  126. // ---
  127. // summary: Watch a repo
  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. // responses:
  140. // "200":
  141. // "$ref": "#/responses/WatchInfo"
  142. err := models.WatchRepo(ctx.User.ID, ctx.Repo.Repository.ID, true)
  143. if err != nil {
  144. ctx.Error(http.StatusInternalServerError, "WatchRepo", err)
  145. return
  146. }
  147. ctx.JSON(http.StatusOK, api.WatchInfo{
  148. Subscribed: true,
  149. Ignored: false,
  150. Reason: nil,
  151. CreatedAt: ctx.Repo.Repository.CreatedUnix.AsTime(),
  152. URL: subscriptionURL(ctx.Repo.Repository),
  153. RepositoryURL: ctx.Repo.Repository.APIURL(),
  154. })
  155. }
  156. // Unwatch the repo specified in ctx, as the authenticated user
  157. func Unwatch(ctx *context.APIContext) {
  158. // swagger:operation DELETE /repos/{owner}/{repo}/subscription repository userCurrentDeleteSubscription
  159. // ---
  160. // summary: Unwatch a repo
  161. // parameters:
  162. // - name: owner
  163. // in: path
  164. // description: owner of the repo
  165. // type: string
  166. // required: true
  167. // - name: repo
  168. // in: path
  169. // description: name of the repo
  170. // type: string
  171. // required: true
  172. // responses:
  173. // "204":
  174. // "$ref": "#/responses/empty"
  175. err := models.WatchRepo(ctx.User.ID, ctx.Repo.Repository.ID, false)
  176. if err != nil {
  177. ctx.Error(http.StatusInternalServerError, "UnwatchRepo", err)
  178. return
  179. }
  180. ctx.Status(http.StatusNoContent)
  181. }
  182. // subscriptionURL returns the URL of the subscription API endpoint of a repo
  183. func subscriptionURL(repo *models.Repository) string {
  184. return repo.APIURL() + "/subscription"
  185. }