本站源代码
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.

180 lines
5.0KB

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