Files
Gogs/routes/api/v1/user/follower.go

115 lines
2.3 KiB
Go
Raw Normal View History

// Copyright 2015 The Gogs 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 user
import (
2018-05-27 08:53:48 +08:00
api "github.com/gogs/go-gogs-client"
2018-05-27 08:53:48 +08:00
"github.com/gogs/gogs/models"
"github.com/gogs/gogs/pkg/context"
)
2017-06-03 07:26:09 -04:00
func responseApiUsers(c *context.APIContext, users []*models.User) {
apiUsers := make([]*api.User, len(users))
for i := range users {
apiUsers[i] = users[i].APIFormat()
}
2019-08-08 23:53:43 -07:00
c.JSONSuccess(&apiUsers)
}
2017-06-03 07:26:09 -04:00
func listUserFollowers(c *context.APIContext, u *models.User) {
users, err := u.GetFollowers(c.QueryInt("page"))
if err != nil {
2019-08-08 23:53:43 -07:00
c.ServerError("GetUserFollowers", err)
return
}
2017-06-03 07:26:09 -04:00
responseApiUsers(c, users)
}
2017-06-03 07:26:09 -04:00
func ListMyFollowers(c *context.APIContext) {
listUserFollowers(c, c.User)
}
2017-06-03 07:26:09 -04:00
func ListFollowers(c *context.APIContext) {
u := GetUserByParams(c)
if c.Written() {
return
}
2017-06-03 07:26:09 -04:00
listUserFollowers(c, u)
}
2017-06-03 07:26:09 -04:00
func listUserFollowing(c *context.APIContext, u *models.User) {
users, err := u.GetFollowing(c.QueryInt("page"))
if err != nil {
2019-08-08 23:53:43 -07:00
c.ServerError("GetFollowing", err)
return
}
2017-06-03 07:26:09 -04:00
responseApiUsers(c, users)
}
2017-06-03 07:26:09 -04:00
func ListMyFollowing(c *context.APIContext) {
listUserFollowing(c, c.User)
}
2017-06-03 07:26:09 -04:00
func ListFollowing(c *context.APIContext) {
u := GetUserByParams(c)
if c.Written() {
return
}
2017-06-03 07:26:09 -04:00
listUserFollowing(c, u)
}
2017-06-03 07:26:09 -04:00
func checkUserFollowing(c *context.APIContext, u *models.User, followID int64) {
if u.IsFollowing(followID) {
c.NoContent()
} else {
2019-08-08 23:53:43 -07:00
c.NotFound()
}
}
2017-06-03 07:26:09 -04:00
func CheckMyFollowing(c *context.APIContext) {
target := GetUserByParams(c)
if c.Written() {
return
}
2017-06-03 07:26:09 -04:00
checkUserFollowing(c, c.User, target.ID)
}
2017-06-03 07:26:09 -04:00
func CheckFollowing(c *context.APIContext) {
u := GetUserByParams(c)
if c.Written() {
return
}
2017-06-03 07:26:09 -04:00
target := GetUserByParamsName(c, ":target")
if c.Written() {
return
}
2017-06-03 07:26:09 -04:00
checkUserFollowing(c, u, target.ID)
}
2017-06-03 07:26:09 -04:00
func Follow(c *context.APIContext) {
target := GetUserByParams(c)
if c.Written() {
return
}
2017-06-03 07:26:09 -04:00
if err := models.FollowUser(c.User.ID, target.ID); err != nil {
2019-08-08 23:53:43 -07:00
c.ServerError("FollowUser", err)
return
}
2019-08-08 23:53:43 -07:00
c.NoContent()
}
2017-06-03 07:26:09 -04:00
func Unfollow(c *context.APIContext) {
target := GetUserByParams(c)
if c.Written() {
return
}
2017-06-03 07:26:09 -04:00
if err := models.UnfollowUser(c.User.ID, target.ID); err != nil {
2019-08-08 23:53:43 -07:00
c.ServerError("UnfollowUser", err)
return
}
2019-08-08 23:53:43 -07:00
c.NoContent()
}