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