|
| 1 | +// Copyright 2016 The Gogs 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 | + |
| 5 | +package user |
| 6 | + |
| 7 | +import ( |
| 8 | + api "code.gitea.io/sdk/gitea" |
| 9 | + |
| 10 | + "code.gitea.io/gitea/modules/context" |
| 11 | + "code.gitea.io/gitea/models" |
| 12 | +) |
| 13 | + |
| 14 | +// getWatchedRepos returns the repos that the user with the specified userID is |
| 15 | +// watching |
| 16 | +func getWatchedRepos(userID int64, private bool) ([]*api.Repository, error) { |
| 17 | + watchedRepos, err := models.GetWatchedRepos(userID, private) |
| 18 | + if err != nil { |
| 19 | + return nil, err |
| 20 | + } |
| 21 | + repos := make([]*api.Repository, len(watchedRepos)) |
| 22 | + for i, watched := range watchedRepos { |
| 23 | + repos[i] = watched.APIFormat(&api.Permission{true, true, true}) |
| 24 | + } |
| 25 | + return repos, nil |
| 26 | +} |
| 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 | + |
| 39 | +// GetMyWatchedRepos returns the repos that the authenticated user is watching |
| 40 | +func GetMyWatchedRepos(ctx *context.APIContext) { |
| 41 | + repos, err := getWatchedRepos(ctx.User.ID, true) |
| 42 | + if err != nil { |
| 43 | + ctx.Error(500, "getWatchedRepos", err) |
| 44 | + } |
| 45 | + ctx.JSON(200, &repos) |
| 46 | +} |
| 47 | + |
| 48 | +// IsWatching returns whether the authenticated user is watching the repo |
| 49 | +// specified in ctx |
| 50 | +func IsWatching(ctx *context.APIContext) { |
| 51 | + if models.IsWatching(ctx.User.ID, ctx.Repo.Repository.ID) { |
| 52 | + ctx.Status(204) |
| 53 | + } else { |
| 54 | + ctx.Status(404) |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +// Watch the repo specified in ctx, as the authenticated user |
| 59 | +func Watch(ctx *context.APIContext) { |
| 60 | + err := models.WatchRepo(ctx.User.ID, ctx.Repo.Repository.ID, true) |
| 61 | + if err != nil { |
| 62 | + ctx.Status(500) |
| 63 | + return |
| 64 | + } |
| 65 | + ctx.Status(204) |
| 66 | +} |
| 67 | + |
| 68 | +// Unwatch the repo specified in ctx, as the authenticated user |
| 69 | +func Unwatch(ctx *context.APIContext) { |
| 70 | + err := models.WatchRepo(ctx.User.ID, ctx.Repo.Repository.ID, false) |
| 71 | + if err != nil { |
| 72 | + ctx.Status(500) |
| 73 | + return |
| 74 | + } |
| 75 | + ctx.Status(204) |
| 76 | +} |
0 commit comments