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.

143 lines
3.8 KiB

  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. api "code.gitea.io/sdk/gitea"
  7. "code.gitea.io/gitea/models"
  8. "code.gitea.io/gitea/modules/context"
  9. "code.gitea.io/gitea/modules/setting"
  10. )
  11. // getWatchedRepos returns the repos that the user with the specified userID is
  12. // watching
  13. func getWatchedRepos(userID int64, private bool) ([]*api.Repository, error) {
  14. watchedRepos, err := models.GetWatchedRepos(userID, 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(userID, 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:route GET /users/{username}/subscriptions userListSubscriptions
  31. //
  32. // Produces:
  33. // - application/json
  34. //
  35. // Responses:
  36. // 200: RepositoryList
  37. // 500: error
  38. user := GetUserByParams(ctx)
  39. private := user.ID == ctx.User.ID
  40. repos, err := getWatchedRepos(user.ID, private)
  41. if err != nil {
  42. ctx.Error(500, "getWatchedRepos", err)
  43. }
  44. ctx.JSON(200, &repos)
  45. }
  46. // GetMyWatchedRepos returns the repos that the authenticated user is watching
  47. func GetMyWatchedRepos(ctx *context.APIContext) {
  48. // swagger:route GET /user/subscriptions userCurrentListSubscriptions
  49. //
  50. // Produces:
  51. // - application/json
  52. //
  53. // Responses:
  54. // 200: RepositoryList
  55. // 500: error
  56. repos, err := getWatchedRepos(ctx.User.ID, true)
  57. if err != nil {
  58. ctx.Error(500, "getWatchedRepos", err)
  59. }
  60. ctx.JSON(200, &repos)
  61. }
  62. // IsWatching returns whether the authenticated user is watching the repo
  63. // specified in ctx
  64. func IsWatching(ctx *context.APIContext) {
  65. // swagger:route GET /repos/{username}/{reponame}/subscription userCurrentCheckSubscription
  66. //
  67. // Responses:
  68. // 200: WatchInfo
  69. // 404: notFound
  70. if models.IsWatching(ctx.User.ID, ctx.Repo.Repository.ID) {
  71. ctx.JSON(200, api.WatchInfo{
  72. Subscribed: true,
  73. Ignored: false,
  74. Reason: nil,
  75. CreatedAt: ctx.Repo.Repository.Created,
  76. URL: subscriptionURL(ctx.Repo.Repository),
  77. RepositoryURL: repositoryURL(ctx.Repo.Repository),
  78. })
  79. } else {
  80. ctx.Status(404)
  81. }
  82. }
  83. // Watch the repo specified in ctx, as the authenticated user
  84. func Watch(ctx *context.APIContext) {
  85. // swagger:route PUT /repos/{username}/{reponame}/subscription userCurrentPutSubscription
  86. //
  87. // Responses:
  88. // 200: WatchInfo
  89. // 500: error
  90. err := models.WatchRepo(ctx.User.ID, ctx.Repo.Repository.ID, true)
  91. if err != nil {
  92. ctx.Error(500, "WatchRepo", err)
  93. return
  94. }
  95. ctx.JSON(200, api.WatchInfo{
  96. Subscribed: true,
  97. Ignored: false,
  98. Reason: nil,
  99. CreatedAt: ctx.Repo.Repository.Created,
  100. URL: subscriptionURL(ctx.Repo.Repository),
  101. RepositoryURL: repositoryURL(ctx.Repo.Repository),
  102. })
  103. }
  104. // Unwatch the repo specified in ctx, as the authenticated user
  105. func Unwatch(ctx *context.APIContext) {
  106. // swagger:route DELETE /repos/{username}/{reponame}/subscription userCurrentDeleteSubscription
  107. //
  108. // Responses:
  109. // 204: empty
  110. // 500: error
  111. err := models.WatchRepo(ctx.User.ID, ctx.Repo.Repository.ID, false)
  112. if err != nil {
  113. ctx.Error(500, "UnwatchRepo", err)
  114. return
  115. }
  116. ctx.Status(204)
  117. }
  118. // subscriptionURL returns the URL of the subscription API endpoint of a repo
  119. func subscriptionURL(repo *models.Repository) string {
  120. return repositoryURL(repo) + "/subscription"
  121. }
  122. // repositoryURL returns the URL of the API endpoint of a repo
  123. func repositoryURL(repo *models.Repository) string {
  124. return setting.AppURL + "api/v1/" + repo.FullName()
  125. }