Skip to content

Commit fbfd089

Browse files
committed
API Endpoint for watching
1 parent 0e13925 commit fbfd089

File tree

3 files changed

+147
-0
lines changed

3 files changed

+147
-0
lines changed

models/user.go

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

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
@@ -316,6 +320,11 @@ func RegisterRoutes(m *macaron.Macaron) {
316320
Patch(reqRepoWriter(), bind(api.EditMilestoneOption{}), repo.EditMilestone).
317321
Delete(reqRepoWriter(), repo.DeleteMilestone)
318322
})
323+
m.Group("/subscription", func() {
324+
m.Get("", user.IsWatching)
325+
m.Put("", user.Watch)
326+
m.Delete("", user.Unwatch)
327+
}, context.ExtractOwnerAndRepo())
319328
m.Get("/editorconfig/:filename", context.RepoRef(), repo.GetEditorconfig)
320329
m.Group("/pulls", func() {
321330
m.Combo("").Get(bind(api.ListPullRequestsOptions{}), repo.ListPullRequests).Post(reqRepoWriter(), bind(api.CreatePullRequestOption{}), repo.CreatePullRequest)

routers/api/v1/user/watch.go

+123
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
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

Comments
 (0)