Skip to content

Commit fac2260

Browse files
committed
API Endpoint for watching
1 parent 026ad4a commit fac2260

File tree

3 files changed

+151
-0
lines changed

3 files changed

+151
-0
lines changed

models/user.go

+15
Original file line numberDiff line numberDiff line change
@@ -1236,3 +1236,18 @@ func GetStarredRepos(userID int64, private bool) ([]*Repository, error) {
12361236
}
12371237
return repos, nil
12381238
}
1239+
1240+
// GetWatchedRepos returns the repos watched by a particular user
1241+
func GetWatchedRepos(userID int64, private bool) ([]*Repository, error) {
1242+
sess := x.Where("watch.user_id=?", userID).
1243+
Join("LEFT", "watch", "`repository`.id=`watch`.repo_id")
1244+
if !private {
1245+
sess = sess.And("is_private=?", false)
1246+
}
1247+
repos := make([]*Repository, 0, 10)
1248+
err := sess.Find(&repos)
1249+
if err != nil {
1250+
return nil, err
1251+
}
1252+
return repos, nil
1253+
}

routers/api/v1/api.go

+9
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,8 @@ func RegisterRoutes(m *macaron.Macaron) {
209209
})
210210

211211
m.Get("/starred", user.GetStarredRepos)
212+
213+
m.Get("/subscriptions", user.GetWatchedRepos)
212214
})
213215
}, reqToken())
214216

@@ -239,6 +241,8 @@ func RegisterRoutes(m *macaron.Macaron) {
239241
m.Delete("", user.Unstar)
240242
}, context.ExtractOwnerAndRepo())
241243
})
244+
245+
m.Get("/subscriptions", user.GetMyWatchedRepos)
242246
}, reqToken())
243247

244248
// Repositories
@@ -311,6 +315,11 @@ func RegisterRoutes(m *macaron.Macaron) {
311315
Patch(reqRepoWriter(), bind(api.EditMilestoneOption{}), repo.EditMilestone).
312316
Delete(reqRepoWriter(), repo.DeleteMilestone)
313317
})
318+
m.Group("/subscription", func() {
319+
m.Get("", user.IsWatching)
320+
m.Put("", user.Watch)
321+
m.Delete("", user.Unwatch)
322+
}, context.ExtractOwnerAndRepo())
314323
m.Get("/editorconfig/:filename", context.RepoRef(), repo.GetEditorconfig)
315324
m.Group("/pulls", func() {
316325
m.Combo("").Get(bind(api.ListPullRequestsOptions{}), repo.ListPullRequests).Post(reqRepoWriter(), bind(api.CreatePullRequestOption{}), repo.CreatePullRequest)

routers/api/v1/user/watch.go

+127
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
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(&api.Permission{
46+
Admin: access >= models.AccessModeAdmin,
47+
Push: access >= models.AccessModeWrite,
48+
Pull: access >= models.AccessModeRead,
49+
})
50+
}
51+
return repos, nil
52+
}
53+
54+
// GetWatchedRepos returns the repos that the user specified in ctx is watching
55+
func GetWatchedRepos(ctx *context.APIContext) {
56+
user := GetUserByParams(ctx)
57+
private := user.ID == ctx.User.ID
58+
repos, err := getWatchedRepos(user.ID, private)
59+
if err != nil {
60+
ctx.Error(500, "getWatchedRepos", err)
61+
}
62+
ctx.JSON(200, &repos)
63+
}
64+
65+
// GetMyWatchedRepos returns the repos that the authenticated user is watching
66+
func GetMyWatchedRepos(ctx *context.APIContext) {
67+
repos, err := getWatchedRepos(ctx.User.ID, true)
68+
if err != nil {
69+
ctx.Error(500, "getWatchedRepos", err)
70+
}
71+
ctx.JSON(200, &repos)
72+
}
73+
74+
// IsWatching returns whether the authenticated user is watching the repo
75+
// specified in ctx
76+
func IsWatching(ctx *context.APIContext) {
77+
if models.IsWatching(ctx.User.ID, ctx.Repo.Repository.ID) {
78+
ctx.JSON(200, WatchInfo{
79+
Subscribed: true,
80+
Ignored: false,
81+
Reason: nil,
82+
CreatedAt: ctx.Repo.Repository.Created,
83+
URL: subscriptionURL(ctx.Repo.Repository),
84+
RepositoryURL: repositoryURL(ctx.Repo.Repository),
85+
})
86+
} else {
87+
ctx.Status(404)
88+
}
89+
}
90+
91+
// Watch the repo specified in ctx, as the authenticated user
92+
func Watch(ctx *context.APIContext) {
93+
err := models.WatchRepo(ctx.User.ID, ctx.Repo.Repository.ID, true)
94+
if err != nil {
95+
ctx.Error(500, "WatchRepo", err)
96+
return
97+
}
98+
ctx.JSON(200, WatchInfo{
99+
Subscribed: true,
100+
Ignored: false,
101+
Reason: nil,
102+
CreatedAt: ctx.Repo.Repository.Created,
103+
URL: subscriptionURL(ctx.Repo.Repository),
104+
RepositoryURL: repositoryURL(ctx.Repo.Repository),
105+
})
106+
107+
}
108+
109+
// Unwatch the repo specified in ctx, as the authenticated user
110+
func Unwatch(ctx *context.APIContext) {
111+
err := models.WatchRepo(ctx.User.ID, ctx.Repo.Repository.ID, false)
112+
if err != nil {
113+
ctx.Error(500, "UnwatchRepo", err)
114+
return
115+
}
116+
ctx.Status(204)
117+
}
118+
119+
// subscriptionURL returns the URL of the subscription API endpoint of a repo
120+
func subscriptionURL(repo *models.Repository) string {
121+
return repositoryURL(repo) + "/subscription"
122+
}
123+
124+
// repositoryURL returns the URL of the API endpoint of a repo
125+
func repositoryURL(repo *models.Repository) string {
126+
return setting.AppURL + "api/v1/" + repo.FullName()
127+
}

0 commit comments

Comments
 (0)