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 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. "time"
  7. api "code.gitea.io/sdk/gitea"
  8. "code.gitea.io/gitea/models"
  9. "code.gitea.io/gitea/modules/context"
  10. "code.gitea.io/gitea/modules/setting"
  11. )
  12. // WatchInfo contains information about a watched repository
  13. type WatchInfo struct {
  14. Subscribed bool `json:"subscribed"`
  15. Ignored bool `json:"ignored"`
  16. Reason interface{} `json:"reason"`
  17. CreatedAt time.Time `json:"created_at"`
  18. URL string `json:"url"`
  19. RepositoryURL string `json:"repository_url"`
  20. }
  21. // getWatchedRepos returns the repos that the user with the specified userID is
  22. // watching
  23. func getWatchedRepos(userID int64, private bool) ([]*api.Repository, error) {
  24. watchedRepos, err := models.GetWatchedRepos(userID, private)
  25. if err != nil {
  26. return nil, err
  27. }
  28. user, err := models.GetUserByID(userID)
  29. if err != nil {
  30. return nil, err
  31. }
  32. repos := make([]*api.Repository, len(watchedRepos))
  33. for i, watched := range watchedRepos {
  34. access, err := models.AccessLevel(user, watched)
  35. if err != nil {
  36. return nil, err
  37. }
  38. repos[i] = watched.APIFormat(access)
  39. }
  40. return repos, nil
  41. }
  42. // GetWatchedRepos returns the repos that the user specified in ctx is watching
  43. func GetWatchedRepos(ctx *context.APIContext) {
  44. user := GetUserByParams(ctx)
  45. private := user.ID == ctx.User.ID
  46. repos, err := getWatchedRepos(user.ID, 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. repos, err := getWatchedRepos(ctx.User.ID, true)
  55. if err != nil {
  56. ctx.Error(500, "getWatchedRepos", err)
  57. }
  58. ctx.JSON(200, &repos)
  59. }
  60. // IsWatching returns whether the authenticated user is watching the repo
  61. // specified in ctx
  62. func IsWatching(ctx *context.APIContext) {
  63. if models.IsWatching(ctx.User.ID, ctx.Repo.Repository.ID) {
  64. ctx.JSON(200, WatchInfo{
  65. Subscribed: true,
  66. Ignored: false,
  67. Reason: nil,
  68. CreatedAt: ctx.Repo.Repository.Created,
  69. URL: subscriptionURL(ctx.Repo.Repository),
  70. RepositoryURL: repositoryURL(ctx.Repo.Repository),
  71. })
  72. } else {
  73. ctx.Status(404)
  74. }
  75. }
  76. // Watch the repo specified in ctx, as the authenticated user
  77. func Watch(ctx *context.APIContext) {
  78. err := models.WatchRepo(ctx.User.ID, ctx.Repo.Repository.ID, true)
  79. if err != nil {
  80. ctx.Error(500, "WatchRepo", err)
  81. return
  82. }
  83. ctx.JSON(200, WatchInfo{
  84. Subscribed: true,
  85. Ignored: false,
  86. Reason: nil,
  87. CreatedAt: ctx.Repo.Repository.Created,
  88. URL: subscriptionURL(ctx.Repo.Repository),
  89. RepositoryURL: repositoryURL(ctx.Repo.Repository),
  90. })
  91. }
  92. // Unwatch the repo specified in ctx, as the authenticated user
  93. func Unwatch(ctx *context.APIContext) {
  94. err := models.WatchRepo(ctx.User.ID, ctx.Repo.Repository.ID, false)
  95. if err != nil {
  96. ctx.Error(500, "UnwatchRepo", err)
  97. return
  98. }
  99. ctx.Status(204)
  100. }
  101. // subscriptionURL returns the URL of the subscription API endpoint of a repo
  102. func subscriptionURL(repo *models.Repository) string {
  103. return repositoryURL(repo) + "/subscription"
  104. }
  105. // repositoryURL returns the URL of the API endpoint of a repo
  106. func repositoryURL(repo *models.Repository) string {
  107. return setting.AppURL + "api/v1/" + repo.FullName()
  108. }