2016-12-24 07:23:11 +05:30
|
|
|
// Copyright 2016 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package user
|
|
|
|
|
|
|
|
import (
|
2019-12-20 22:37:12 +05:30
|
|
|
"net/http"
|
|
|
|
|
2021-09-24 17:02:56 +05:30
|
|
|
"code.gitea.io/gitea/models/db"
|
2022-05-11 15:39:36 +05:30
|
|
|
access_model "code.gitea.io/gitea/models/perm/access"
|
2021-12-10 06:57:50 +05:30
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-24 15:19:20 +05:30
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2016-12-24 07:23:11 +05:30
|
|
|
"code.gitea.io/gitea/modules/context"
|
2020-12-03 03:08:30 +05:30
|
|
|
"code.gitea.io/gitea/modules/convert"
|
2019-08-23 22:10:30 +05:30
|
|
|
api "code.gitea.io/gitea/modules/structs"
|
2020-01-25 00:30:29 +05:30
|
|
|
"code.gitea.io/gitea/routers/api/v1/utils"
|
2016-12-24 07:23:11 +05:30
|
|
|
)
|
|
|
|
|
2021-08-12 18:13:08 +05:30
|
|
|
// getWatchedRepos returns the repos that the user with the specified userID is watching
|
2021-11-24 15:19:20 +05:30
|
|
|
func getWatchedRepos(user *user_model.User, private bool, listOptions db.ListOptions) ([]*api.Repository, int64, error) {
|
2022-03-29 11:59:02 +05:30
|
|
|
watchedRepos, total, err := repo_model.GetWatchedRepos(user.ID, private, listOptions)
|
2016-12-24 07:23:11 +05:30
|
|
|
if err != nil {
|
2021-08-12 18:13:08 +05:30
|
|
|
return nil, 0, err
|
2016-12-24 07:23:11 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
repos := make([]*api.Repository, len(watchedRepos))
|
|
|
|
for i, watched := range watchedRepos {
|
2022-05-11 15:39:36 +05:30
|
|
|
access, err := access_model.AccessLevel(user, watched)
|
2016-12-24 07:23:11 +05:30
|
|
|
if err != nil {
|
2021-08-12 18:13:08 +05:30
|
|
|
return nil, 0, err
|
2016-12-24 07:23:11 +05:30
|
|
|
}
|
2020-12-03 03:08:30 +05:30
|
|
|
repos[i] = convert.ToRepo(watched, access)
|
2016-12-24 07:23:11 +05:30
|
|
|
}
|
2021-08-12 18:13:08 +05:30
|
|
|
return repos, total, nil
|
2016-12-24 07:23:11 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// GetWatchedRepos returns the repos that the user specified in ctx is watching
|
|
|
|
func GetWatchedRepos(ctx *context.APIContext) {
|
2017-11-13 12:32:25 +05:30
|
|
|
// swagger:operation GET /users/{username}/subscriptions user userListSubscriptions
|
|
|
|
// ---
|
|
|
|
// summary: List the repositories watched by a user
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// parameters:
|
|
|
|
// - name: username
|
|
|
|
// type: string
|
|
|
|
// in: path
|
|
|
|
// description: username of the user
|
2018-06-02 20:50:28 +05:30
|
|
|
// required: true
|
2020-01-25 00:30:29 +05:30
|
|
|
// - name: page
|
|
|
|
// in: query
|
|
|
|
// description: page number of results to return (1-based)
|
|
|
|
// type: integer
|
|
|
|
// - name: limit
|
|
|
|
// in: query
|
2020-06-09 10:27:38 +05:30
|
|
|
// description: page size of results
|
2020-01-25 00:30:29 +05:30
|
|
|
// type: integer
|
2017-11-13 12:32:25 +05:30
|
|
|
// responses:
|
|
|
|
// "200":
|
|
|
|
// "$ref": "#/responses/RepositoryList"
|
2019-12-20 22:37:12 +05:30
|
|
|
|
2022-03-26 14:34:22 +05:30
|
|
|
private := ctx.ContextUser.ID == ctx.Doer.ID
|
|
|
|
repos, total, err := getWatchedRepos(ctx.ContextUser, private, utils.GetListOptions(ctx))
|
2016-12-24 07:23:11 +05:30
|
|
|
if err != nil {
|
2019-12-20 22:37:12 +05:30
|
|
|
ctx.Error(http.StatusInternalServerError, "getWatchedRepos", err)
|
2016-12-24 07:23:11 +05:30
|
|
|
}
|
2021-08-12 18:13:08 +05:30
|
|
|
|
|
|
|
ctx.SetTotalCountHeader(total)
|
2019-12-20 22:37:12 +05:30
|
|
|
ctx.JSON(http.StatusOK, &repos)
|
2016-12-24 07:23:11 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// GetMyWatchedRepos returns the repos that the authenticated user is watching
|
|
|
|
func GetMyWatchedRepos(ctx *context.APIContext) {
|
2017-11-13 12:32:25 +05:30
|
|
|
// swagger:operation GET /user/subscriptions user userCurrentListSubscriptions
|
|
|
|
// ---
|
|
|
|
// summary: List repositories watched by the authenticated user
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
2020-01-25 00:30:29 +05:30
|
|
|
// parameters:
|
|
|
|
// - name: page
|
|
|
|
// in: query
|
|
|
|
// description: page number of results to return (1-based)
|
|
|
|
// type: integer
|
|
|
|
// - name: limit
|
|
|
|
// in: query
|
2020-06-09 10:27:38 +05:30
|
|
|
// description: page size of results
|
2020-01-25 00:30:29 +05:30
|
|
|
// type: integer
|
2017-11-13 12:32:25 +05:30
|
|
|
// responses:
|
|
|
|
// "200":
|
|
|
|
// "$ref": "#/responses/RepositoryList"
|
2019-12-20 22:37:12 +05:30
|
|
|
|
2022-03-22 12:33:22 +05:30
|
|
|
repos, total, err := getWatchedRepos(ctx.Doer, true, utils.GetListOptions(ctx))
|
2016-12-24 07:23:11 +05:30
|
|
|
if err != nil {
|
2019-12-20 22:37:12 +05:30
|
|
|
ctx.Error(http.StatusInternalServerError, "getWatchedRepos", err)
|
2016-12-24 07:23:11 +05:30
|
|
|
}
|
2021-08-12 18:13:08 +05:30
|
|
|
|
|
|
|
ctx.SetTotalCountHeader(total)
|
2019-12-20 22:37:12 +05:30
|
|
|
ctx.JSON(http.StatusOK, &repos)
|
2016-12-24 07:23:11 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// IsWatching returns whether the authenticated user is watching the repo
|
|
|
|
// specified in ctx
|
|
|
|
func IsWatching(ctx *context.APIContext) {
|
2017-11-13 12:32:25 +05:30
|
|
|
// swagger:operation GET /repos/{owner}/{repo}/subscription repository userCurrentCheckSubscription
|
|
|
|
// ---
|
|
|
|
// summary: Check if the current user is watching a repo
|
|
|
|
// parameters:
|
|
|
|
// - name: owner
|
|
|
|
// in: path
|
|
|
|
// description: owner of the repo
|
|
|
|
// type: string
|
|
|
|
// required: true
|
|
|
|
// - name: repo
|
|
|
|
// in: path
|
|
|
|
// description: name of the repo
|
|
|
|
// type: string
|
|
|
|
// required: true
|
|
|
|
// responses:
|
|
|
|
// "200":
|
|
|
|
// "$ref": "#/responses/WatchInfo"
|
2020-04-15 18:33:05 +05:30
|
|
|
// "404":
|
|
|
|
// description: User is not watching this repo or repo do not exist
|
2019-12-20 22:37:12 +05:30
|
|
|
|
2022-03-22 12:33:22 +05:30
|
|
|
if repo_model.IsWatching(ctx.Doer.ID, ctx.Repo.Repository.ID) {
|
2019-12-20 22:37:12 +05:30
|
|
|
ctx.JSON(http.StatusOK, api.WatchInfo{
|
2016-12-24 07:23:11 +05:30
|
|
|
Subscribed: true,
|
|
|
|
Ignored: false,
|
|
|
|
Reason: nil,
|
2017-12-11 10:07:04 +05:30
|
|
|
CreatedAt: ctx.Repo.Repository.CreatedUnix.AsTime(),
|
2016-12-24 07:23:11 +05:30
|
|
|
URL: subscriptionURL(ctx.Repo.Repository),
|
2020-04-21 19:18:53 +05:30
|
|
|
RepositoryURL: ctx.Repo.Repository.APIURL(),
|
2016-12-24 07:23:11 +05:30
|
|
|
})
|
|
|
|
} else {
|
2019-03-19 07:59:43 +05:30
|
|
|
ctx.NotFound()
|
2016-12-24 07:23:11 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Watch the repo specified in ctx, as the authenticated user
|
|
|
|
func Watch(ctx *context.APIContext) {
|
2017-11-13 12:32:25 +05:30
|
|
|
// swagger:operation PUT /repos/{owner}/{repo}/subscription repository userCurrentPutSubscription
|
|
|
|
// ---
|
|
|
|
// summary: Watch a repo
|
|
|
|
// parameters:
|
|
|
|
// - name: owner
|
|
|
|
// in: path
|
|
|
|
// description: owner of the repo
|
|
|
|
// type: string
|
|
|
|
// required: true
|
|
|
|
// - name: repo
|
|
|
|
// in: path
|
|
|
|
// description: name of the repo
|
|
|
|
// type: string
|
|
|
|
// required: true
|
|
|
|
// responses:
|
|
|
|
// "200":
|
|
|
|
// "$ref": "#/responses/WatchInfo"
|
2019-12-20 22:37:12 +05:30
|
|
|
|
2022-05-20 19:38:52 +05:30
|
|
|
err := repo_model.WatchRepo(ctx, ctx.Doer.ID, ctx.Repo.Repository.ID, true)
|
2016-12-24 07:23:11 +05:30
|
|
|
if err != nil {
|
2019-12-20 22:37:12 +05:30
|
|
|
ctx.Error(http.StatusInternalServerError, "WatchRepo", err)
|
2016-12-24 07:23:11 +05:30
|
|
|
return
|
|
|
|
}
|
2019-12-20 22:37:12 +05:30
|
|
|
ctx.JSON(http.StatusOK, api.WatchInfo{
|
2016-12-24 07:23:11 +05:30
|
|
|
Subscribed: true,
|
|
|
|
Ignored: false,
|
|
|
|
Reason: nil,
|
2017-12-11 10:07:04 +05:30
|
|
|
CreatedAt: ctx.Repo.Repository.CreatedUnix.AsTime(),
|
2016-12-24 07:23:11 +05:30
|
|
|
URL: subscriptionURL(ctx.Repo.Repository),
|
2020-04-21 19:18:53 +05:30
|
|
|
RepositoryURL: ctx.Repo.Repository.APIURL(),
|
2016-12-24 07:23:11 +05:30
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unwatch the repo specified in ctx, as the authenticated user
|
|
|
|
func Unwatch(ctx *context.APIContext) {
|
2017-11-13 12:32:25 +05:30
|
|
|
// swagger:operation DELETE /repos/{owner}/{repo}/subscription repository userCurrentDeleteSubscription
|
|
|
|
// ---
|
|
|
|
// summary: Unwatch a repo
|
|
|
|
// parameters:
|
|
|
|
// - name: owner
|
|
|
|
// in: path
|
|
|
|
// description: owner of the repo
|
|
|
|
// type: string
|
|
|
|
// required: true
|
|
|
|
// - name: repo
|
|
|
|
// in: path
|
|
|
|
// description: name of the repo
|
|
|
|
// type: string
|
|
|
|
// required: true
|
|
|
|
// responses:
|
|
|
|
// "204":
|
|
|
|
// "$ref": "#/responses/empty"
|
2019-12-20 22:37:12 +05:30
|
|
|
|
2022-05-20 19:38:52 +05:30
|
|
|
err := repo_model.WatchRepo(ctx, ctx.Doer.ID, ctx.Repo.Repository.ID, false)
|
2016-12-24 07:23:11 +05:30
|
|
|
if err != nil {
|
2019-12-20 22:37:12 +05:30
|
|
|
ctx.Error(http.StatusInternalServerError, "UnwatchRepo", err)
|
2016-12-24 07:23:11 +05:30
|
|
|
return
|
|
|
|
}
|
2019-12-20 22:37:12 +05:30
|
|
|
ctx.Status(http.StatusNoContent)
|
2016-12-24 07:23:11 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// subscriptionURL returns the URL of the subscription API endpoint of a repo
|
2021-12-10 06:57:50 +05:30
|
|
|
func subscriptionURL(repo *repo_model.Repository) string {
|
2020-04-21 19:18:53 +05:30
|
|
|
return repo.APIURL() + "/subscription"
|
2016-12-24 07:23:11 +05:30
|
|
|
}
|