Browse Source

API endpoint for subscribers (#598)

tags/v1.2.0-rc1
Ethan Koenig Lunny Xiao 8 years ago
parent
commit
f74f56ed26
3 changed files with 30 additions and 7 deletions
  1. +4
    -7
      models/repo.go
  2. +1
    -0
      routers/api/v1/api.go
  3. +25
    -0
      routers/api/v1/repo/subscriber.go

+ 4
- 7
models/repo.go View File

@@ -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)
}


+ 1
- 0
routers/api/v1/api.go View File

@@ -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)


+ 25
- 0
routers/api/v1/repo/subscriber.go 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)
}

Loading…
Cancel
Save