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.

107 lines
3.0 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. user := GetUserByParams(ctx)
  31. private := user.ID == ctx.User.ID
  32. repos, err := getWatchedRepos(user.ID, private)
  33. if err != nil {
  34. ctx.Error(500, "getWatchedRepos", err)
  35. }
  36. ctx.JSON(200, &repos)
  37. }
  38. // GetMyWatchedRepos returns the repos that the authenticated user is watching
  39. func GetMyWatchedRepos(ctx *context.APIContext) {
  40. repos, err := getWatchedRepos(ctx.User.ID, true)
  41. if err != nil {
  42. ctx.Error(500, "getWatchedRepos", err)
  43. }
  44. ctx.JSON(200, &repos)
  45. }
  46. // IsWatching returns whether the authenticated user is watching the repo
  47. // specified in ctx
  48. func IsWatching(ctx *context.APIContext) {
  49. if models.IsWatching(ctx.User.ID, ctx.Repo.Repository.ID) {
  50. ctx.JSON(200, api.WatchInfo{
  51. Subscribed: true,
  52. Ignored: false,
  53. Reason: nil,
  54. CreatedAt: ctx.Repo.Repository.Created,
  55. URL: subscriptionURL(ctx.Repo.Repository),
  56. RepositoryURL: repositoryURL(ctx.Repo.Repository),
  57. })
  58. } else {
  59. ctx.Status(404)
  60. }
  61. }
  62. // Watch the repo specified in ctx, as the authenticated user
  63. func Watch(ctx *context.APIContext) {
  64. err := models.WatchRepo(ctx.User.ID, ctx.Repo.Repository.ID, true)
  65. if err != nil {
  66. ctx.Error(500, "WatchRepo", err)
  67. return
  68. }
  69. ctx.JSON(200, api.WatchInfo{
  70. Subscribed: true,
  71. Ignored: false,
  72. Reason: nil,
  73. CreatedAt: ctx.Repo.Repository.Created,
  74. URL: subscriptionURL(ctx.Repo.Repository),
  75. RepositoryURL: repositoryURL(ctx.Repo.Repository),
  76. })
  77. }
  78. // Unwatch the repo specified in ctx, as the authenticated user
  79. func Unwatch(ctx *context.APIContext) {
  80. err := models.WatchRepo(ctx.User.ID, ctx.Repo.Repository.ID, false)
  81. if err != nil {
  82. ctx.Error(500, "UnwatchRepo", err)
  83. return
  84. }
  85. ctx.Status(204)
  86. }
  87. // subscriptionURL returns the URL of the subscription API endpoint of a repo
  88. func subscriptionURL(repo *models.Repository) string {
  89. return repositoryURL(repo) + "/subscription"
  90. }
  91. // repositoryURL returns the URL of the API endpoint of a repo
  92. func repositoryURL(repo *models.Repository) string {
  93. return setting.AppURL + "api/v1/" + repo.FullName()
  94. }