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