Skip to content

Commit a3e85f5

Browse files
committed
API Endpoint for watching
1 parent cf045b0 commit a3e85f5

File tree

3 files changed

+102
-0
lines changed

3 files changed

+102
-0
lines changed

models/user.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1194,3 +1194,18 @@ func GetStarredRepos(userID int64, private bool) ([]*Repository, error) {
11941194
}
11951195
return repos, nil
11961196
}
1197+
1198+
// GetWatchedRepos returns the repos watched by a particular user
1199+
func GetWatchedRepos(userID int64, private bool) ([]*Repository, error) {
1200+
sess := x.Where("watch.user_id=?", userID).
1201+
Join("LEFT", "watch", "`repository`.id=`watch`.repo_id")
1202+
if !private {
1203+
sess = sess.And("is_private=?", false)
1204+
}
1205+
repos := make([]*Repository, 0, 10)
1206+
err := sess.Find(&repos)
1207+
if err != nil {
1208+
return nil, err
1209+
}
1210+
return repos, nil
1211+
}

routers/api/v1/api.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,8 @@ func RegisterRoutes(m *macaron.Macaron) {
202202
})
203203

204204
m.Get("/starred", user.GetStarredRepos)
205+
206+
m.Get("/subscriptions", user.GetWatchedRepos)
205207
})
206208
}, reqToken())
207209

@@ -232,6 +234,15 @@ func RegisterRoutes(m *macaron.Macaron) {
232234
m.Delete("", user.Unstar)
233235
}, context.ExtractOwnerAndRepo())
234236
})
237+
238+
m.Group("/subscriptions", func() {
239+
m.Get("", user.GetMyWatchedRepos)
240+
m.Group("/:username/:reponame", func() {
241+
m.Get("", user.IsWatching)
242+
m.Put("", user.Watch)
243+
m.Delete("", user.Unwatch)
244+
}, context.ExtractOwnerAndRepo())
245+
})
235246
}, reqToken())
236247

237248
// Repositories

routers/api/v1/user/watch.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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

Comments
 (0)