API endpoint for subscribers (#598)

release/v1.15
Ethan Koenig 2017-01-06 22:13:02 -05:00 committed by Lunny Xiao
parent 03b45284e1
commit 8422ab542c
3 changed files with 30 additions and 7 deletions

View File

@ -2107,13 +2107,10 @@ func GetWatchers(repoID int64) ([]*Watch, error) {
// GetWatchers returns range of users watching given repository. // GetWatchers returns range of users watching given repository.
func (repo *Repository) GetWatchers(page int) ([]*User, error) { func (repo *Repository) GetWatchers(page int) ([]*User, error) {
users := make([]*User, 0, ItemsPerPage) users := make([]*User, 0, ItemsPerPage)
sess := x. sess := x.Where("watch.repo_id=?", repo.ID).
Limit(ItemsPerPage, (page-1)*ItemsPerPage). Join("LEFT", "watch", "`user`.id=`watch`.user_id")
Where("watch.repo_id=?", repo.ID) if page > 0 {
if setting.UsePostgreSQL { sess = sess.Limit(ItemsPerPage, (page-1)*ItemsPerPage)
sess = sess.Join("LEFT", "watch", `"user".id=watch.user_id`)
} else {
sess = sess.Join("LEFT", "watch", "user.id=watch.user_id")
} }
return users, sess.Find(&users) return users, sess.Find(&users)
} }

View File

@ -327,6 +327,7 @@ func RegisterRoutes(m *macaron.Macaron) {
Delete(reqRepoWriter(), repo.DeleteMilestone) Delete(reqRepoWriter(), repo.DeleteMilestone)
}) })
m.Get("/stargazers", repo.ListStargazers) m.Get("/stargazers", repo.ListStargazers)
m.Get("/subscribers", repo.ListSubscribers)
m.Group("/subscription", func() { m.Group("/subscription", func() {
m.Get("", user.IsWatching) m.Get("", user.IsWatching)
m.Put("", user.Watch) m.Put("", user.Watch)

View File

@ -0,0 +1,25 @@
// Copyright 2017 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package repo
import (
api "code.gitea.io/sdk/gitea"
"code.gitea.io/gitea/modules/context"
)
// ListSubscribers list a repo's subscribers (i.e. watchers)
func ListSubscribers(ctx *context.APIContext) {
subscribers, err := ctx.Repo.Repository.GetWatchers(0)
if err != nil {
ctx.Error(500, "GetWatchers", err)
return
}
users := make([]*api.User, len(subscribers))
for i, subscriber := range subscribers {
users[i] = subscriber.APIFormat()
}
ctx.JSON(200, users)
}