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.

watch.go 5.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. )
  12. // getWatchedRepos returns the repos that the user with the specified userID is
  13. // watching
  14. func getWatchedRepos(user *models.User, private bool) ([]*api.Repository, error) {
  15. watchedRepos, err := models.GetWatchedRepos(user.ID, private)
  16. if err != nil {
  17. return nil, 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, err
  24. }
  25. repos[i] = watched.APIFormat(access)
  26. }
  27. return repos, 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. // responses:
  43. // "200":
  44. // "$ref": "#/responses/RepositoryList"
  45. user := GetUserByParams(ctx)
  46. private := user.ID == ctx.User.ID
  47. repos, err := getWatchedRepos(user, private)
  48. if err != nil {
  49. ctx.Error(http.StatusInternalServerError, "getWatchedRepos", err)
  50. }
  51. ctx.JSON(http.StatusOK, &repos)
  52. }
  53. // GetMyWatchedRepos returns the repos that the authenticated user is watching
  54. func GetMyWatchedRepos(ctx *context.APIContext) {
  55. // swagger:operation GET /user/subscriptions user userCurrentListSubscriptions
  56. // ---
  57. // summary: List repositories watched by the authenticated user
  58. // produces:
  59. // - application/json
  60. // responses:
  61. // "200":
  62. // "$ref": "#/responses/RepositoryList"
  63. repos, err := getWatchedRepos(ctx.User, true)
  64. if err != nil {
  65. ctx.Error(http.StatusInternalServerError, "getWatchedRepos", err)
  66. }
  67. ctx.JSON(http.StatusOK, &repos)
  68. }
  69. // IsWatching returns whether the authenticated user is watching the repo
  70. // specified in ctx
  71. func IsWatching(ctx *context.APIContext) {
  72. // swagger:operation GET /repos/{owner}/{repo}/subscription repository userCurrentCheckSubscription
  73. // ---
  74. // summary: Check if the current user is watching a repo
  75. // parameters:
  76. // - name: owner
  77. // in: path
  78. // description: owner of the repo
  79. // type: string
  80. // required: true
  81. // - name: repo
  82. // in: path
  83. // description: name of the repo
  84. // type: string
  85. // required: true
  86. // responses:
  87. // "200":
  88. // "$ref": "#/responses/WatchInfo"
  89. if models.IsWatching(ctx.User.ID, ctx.Repo.Repository.ID) {
  90. ctx.JSON(http.StatusOK, api.WatchInfo{
  91. Subscribed: true,
  92. Ignored: false,
  93. Reason: nil,
  94. CreatedAt: ctx.Repo.Repository.CreatedUnix.AsTime(),
  95. URL: subscriptionURL(ctx.Repo.Repository),
  96. RepositoryURL: repositoryURL(ctx.Repo.Repository),
  97. })
  98. } else {
  99. ctx.NotFound()
  100. }
  101. }
  102. // Watch the repo specified in ctx, as the authenticated user
  103. func Watch(ctx *context.APIContext) {
  104. // swagger:operation PUT /repos/{owner}/{repo}/subscription repository userCurrentPutSubscription
  105. // ---
  106. // summary: Watch a repo
  107. // parameters:
  108. // - name: owner
  109. // in: path
  110. // description: owner of the repo
  111. // type: string
  112. // required: true
  113. // - name: repo
  114. // in: path
  115. // description: name of the repo
  116. // type: string
  117. // required: true
  118. // responses:
  119. // "200":
  120. // "$ref": "#/responses/WatchInfo"
  121. err := models.WatchRepo(ctx.User.ID, ctx.Repo.Repository.ID, true)
  122. if err != nil {
  123. ctx.Error(http.StatusInternalServerError, "WatchRepo", err)
  124. return
  125. }
  126. ctx.JSON(http.StatusOK, api.WatchInfo{
  127. Subscribed: true,
  128. Ignored: false,
  129. Reason: nil,
  130. CreatedAt: ctx.Repo.Repository.CreatedUnix.AsTime(),
  131. URL: subscriptionURL(ctx.Repo.Repository),
  132. RepositoryURL: repositoryURL(ctx.Repo.Repository),
  133. })
  134. }
  135. // Unwatch the repo specified in ctx, as the authenticated user
  136. func Unwatch(ctx *context.APIContext) {
  137. // swagger:operation DELETE /repos/{owner}/{repo}/subscription repository userCurrentDeleteSubscription
  138. // ---
  139. // summary: Unwatch a repo
  140. // parameters:
  141. // - name: owner
  142. // in: path
  143. // description: owner of the repo
  144. // type: string
  145. // required: true
  146. // - name: repo
  147. // in: path
  148. // description: name of the repo
  149. // type: string
  150. // required: true
  151. // responses:
  152. // "204":
  153. // "$ref": "#/responses/empty"
  154. err := models.WatchRepo(ctx.User.ID, ctx.Repo.Repository.ID, false)
  155. if err != nil {
  156. ctx.Error(http.StatusInternalServerError, "UnwatchRepo", err)
  157. return
  158. }
  159. ctx.Status(http.StatusNoContent)
  160. }
  161. // subscriptionURL returns the URL of the subscription API endpoint of a repo
  162. func subscriptionURL(repo *models.Repository) string {
  163. return repositoryURL(repo) + "/subscription"
  164. }
  165. // repositoryURL returns the URL of the API endpoint of a repo
  166. func repositoryURL(repo *models.Repository) string {
  167. return setting.AppURL + "api/v1/" + repo.FullName()
  168. }