aboutsummaryrefslogtreecommitdiff
path: root/internal/route/api/v1/user/follower.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/route/api/v1/user/follower.go')
-rw-r--r--internal/route/api/v1/user/follower.go114
1 files changed, 114 insertions, 0 deletions
diff --git a/internal/route/api/v1/user/follower.go b/internal/route/api/v1/user/follower.go
new file mode 100644
index 00000000..3a3d0298
--- /dev/null
+++ b/internal/route/api/v1/user/follower.go
@@ -0,0 +1,114 @@
+// 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 (
+ api "github.com/gogs/go-gogs-client"
+
+ "gogs.io/gogs/internal/context"
+ "gogs.io/gogs/internal/db"
+)
+
+func responseApiUsers(c *context.APIContext, users []*db.User) {
+ apiUsers := make([]*api.User, len(users))
+ for i := range users {
+ apiUsers[i] = users[i].APIFormat()
+ }
+ c.JSONSuccess(&apiUsers)
+}
+
+func listUserFollowers(c *context.APIContext, u *db.User) {
+ users, err := u.GetFollowers(c.QueryInt("page"))
+ if err != nil {
+ c.ServerError("GetUserFollowers", err)
+ return
+ }
+ responseApiUsers(c, users)
+}
+
+func ListMyFollowers(c *context.APIContext) {
+ listUserFollowers(c, c.User)
+}
+
+func ListFollowers(c *context.APIContext) {
+ u := GetUserByParams(c)
+ if c.Written() {
+ return
+ }
+ listUserFollowers(c, u)
+}
+
+func listUserFollowing(c *context.APIContext, u *db.User) {
+ users, err := u.GetFollowing(c.QueryInt("page"))
+ if err != nil {
+ c.ServerError("GetFollowing", err)
+ return
+ }
+ responseApiUsers(c, users)
+}
+
+func ListMyFollowing(c *context.APIContext) {
+ listUserFollowing(c, c.User)
+}
+
+func ListFollowing(c *context.APIContext) {
+ u := GetUserByParams(c)
+ if c.Written() {
+ return
+ }
+ listUserFollowing(c, u)
+}
+
+func checkUserFollowing(c *context.APIContext, u *db.User, followID int64) {
+ if u.IsFollowing(followID) {
+ c.NoContent()
+ } else {
+ c.NotFound()
+ }
+}
+
+func CheckMyFollowing(c *context.APIContext) {
+ target := GetUserByParams(c)
+ if c.Written() {
+ return
+ }
+ checkUserFollowing(c, c.User, target.ID)
+}
+
+func CheckFollowing(c *context.APIContext) {
+ u := GetUserByParams(c)
+ if c.Written() {
+ return
+ }
+ target := GetUserByParamsName(c, ":target")
+ if c.Written() {
+ return
+ }
+ checkUserFollowing(c, u, target.ID)
+}
+
+func Follow(c *context.APIContext) {
+ target := GetUserByParams(c)
+ if c.Written() {
+ return
+ }
+ if err := db.FollowUser(c.User.ID, target.ID); err != nil {
+ c.ServerError("FollowUser", err)
+ return
+ }
+ c.NoContent()
+}
+
+func Unfollow(c *context.APIContext) {
+ target := GetUserByParams(c)
+ if c.Written() {
+ return
+ }
+ if err := db.UnfollowUser(c.User.ID, target.ID); err != nil {
+ c.ServerError("UnfollowUser", err)
+ return
+ }
+ c.NoContent()
+}