From 8422ab542c69592b4629188d4460ca560307465b Mon Sep 17 00:00:00 2001 From: Ethan Koenig Date: Fri, 6 Jan 2017 22:13:02 -0500 Subject: [PATCH] API endpoint for subscribers (#598) --- models/repo.go | 11 ++++------- routers/api/v1/api.go | 1 + routers/api/v1/repo/subscriber.go | 25 +++++++++++++++++++++++++ 3 files changed, 30 insertions(+), 7 deletions(-) create mode 100644 routers/api/v1/repo/subscriber.go diff --git a/models/repo.go b/models/repo.go index 5f1cef3029..5868b8b705 100644 --- a/models/repo.go +++ b/models/repo.go @@ -2107,13 +2107,10 @@ func GetWatchers(repoID int64) ([]*Watch, error) { // GetWatchers returns range of users watching given repository. func (repo *Repository) GetWatchers(page int) ([]*User, error) { users := make([]*User, 0, ItemsPerPage) - sess := x. - Limit(ItemsPerPage, (page-1)*ItemsPerPage). - Where("watch.repo_id=?", repo.ID) - if setting.UsePostgreSQL { - sess = sess.Join("LEFT", "watch", `"user".id=watch.user_id`) - } else { - sess = sess.Join("LEFT", "watch", "user.id=watch.user_id") + sess := x.Where("watch.repo_id=?", repo.ID). + Join("LEFT", "watch", "`user`.id=`watch`.user_id") + if page > 0 { + sess = sess.Limit(ItemsPerPage, (page-1)*ItemsPerPage) } return users, sess.Find(&users) } diff --git a/routers/api/v1/api.go b/routers/api/v1/api.go index cc773150e6..69b9a272e0 100644 --- a/routers/api/v1/api.go +++ b/routers/api/v1/api.go @@ -327,6 +327,7 @@ func RegisterRoutes(m *macaron.Macaron) { Delete(reqRepoWriter(), repo.DeleteMilestone) }) m.Get("/stargazers", repo.ListStargazers) + m.Get("/subscribers", repo.ListSubscribers) m.Group("/subscription", func() { m.Get("", user.IsWatching) m.Put("", user.Watch) diff --git a/routers/api/v1/repo/subscriber.go b/routers/api/v1/repo/subscriber.go new file mode 100644 index 0000000000..d9d9a6c96d --- /dev/null +++ b/routers/api/v1/repo/subscriber.go @@ -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) +}