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.0 kB

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