2015-12-21 04:24:11 -08:00
|
|
|
package user
|
|
|
|
|
|
|
|
|
|
import (
|
2018-05-27 08:53:48 +08:00
|
|
|
api "github.com/gogs/go-gogs-client"
|
2015-12-21 04:24:11 -08:00
|
|
|
|
2019-10-24 01:51:46 -07:00
|
|
|
"gogs.io/gogs/internal/context"
|
2024-02-18 19:39:41 -05:00
|
|
|
"gogs.io/gogs/internal/database"
|
2015-12-21 04:24:11 -08:00
|
|
|
)
|
|
|
|
|
|
2025-04-14 18:00:07 -04:00
|
|
|
func responseAPIUsers(c *context.APIContext, users []*database.User) {
|
2015-12-21 04:24:11 -08:00
|
|
|
apiUsers := make([]*api.User, len(users))
|
|
|
|
|
for i := range users {
|
2016-08-14 04:17:26 -07:00
|
|
|
apiUsers[i] = users[i].APIFormat()
|
2015-12-21 04:24:11 -08:00
|
|
|
}
|
2019-08-08 23:53:43 -07:00
|
|
|
c.JSONSuccess(&apiUsers)
|
2015-12-21 04:24:11 -08:00
|
|
|
}
|
|
|
|
|
|
2024-02-18 19:39:41 -05:00
|
|
|
func listUserFollowers(c *context.APIContext, u *database.User) {
|
2024-03-27 23:18:59 -04:00
|
|
|
users, err := database.Handle.Users().ListFollowers(c.Req.Context(), u.ID, c.QueryInt("page"), database.ItemsPerPage)
|
2015-12-21 04:24:11 -08:00
|
|
|
if err != nil {
|
2022-10-23 16:17:53 +08:00
|
|
|
c.Error(err, "list followers")
|
2015-12-21 04:24:11 -08:00
|
|
|
return
|
|
|
|
|
}
|
2025-04-14 18:00:07 -04:00
|
|
|
responseAPIUsers(c, users)
|
2015-12-21 04:24:11 -08:00
|
|
|
}
|
|
|
|
|
|
2017-06-03 07:26:09 -04:00
|
|
|
func ListMyFollowers(c *context.APIContext) {
|
|
|
|
|
listUserFollowers(c, c.User)
|
2015-12-21 04:24:11 -08:00
|
|
|
}
|
|
|
|
|
|
2017-06-03 07:26:09 -04:00
|
|
|
func ListFollowers(c *context.APIContext) {
|
|
|
|
|
u := GetUserByParams(c)
|
|
|
|
|
if c.Written() {
|
2015-12-21 04:24:11 -08:00
|
|
|
return
|
|
|
|
|
}
|
2017-06-03 07:26:09 -04:00
|
|
|
listUserFollowers(c, u)
|
2015-12-21 04:24:11 -08:00
|
|
|
}
|
|
|
|
|
|
2024-02-18 19:39:41 -05:00
|
|
|
func listUserFollowing(c *context.APIContext, u *database.User) {
|
2024-03-27 23:18:59 -04:00
|
|
|
users, err := database.Handle.Users().ListFollowings(c.Req.Context(), u.ID, c.QueryInt("page"), database.ItemsPerPage)
|
2015-12-21 04:24:11 -08:00
|
|
|
if err != nil {
|
2022-10-23 16:17:53 +08:00
|
|
|
c.Error(err, "list followings")
|
2015-12-21 04:24:11 -08:00
|
|
|
return
|
|
|
|
|
}
|
2025-04-14 18:00:07 -04:00
|
|
|
responseAPIUsers(c, users)
|
2015-12-21 04:24:11 -08:00
|
|
|
}
|
|
|
|
|
|
2017-06-03 07:26:09 -04:00
|
|
|
func ListMyFollowing(c *context.APIContext) {
|
|
|
|
|
listUserFollowing(c, c.User)
|
2015-12-21 04:24:11 -08:00
|
|
|
}
|
|
|
|
|
|
2017-06-03 07:26:09 -04:00
|
|
|
func ListFollowing(c *context.APIContext) {
|
|
|
|
|
u := GetUserByParams(c)
|
|
|
|
|
if c.Written() {
|
2015-12-21 04:24:11 -08:00
|
|
|
return
|
|
|
|
|
}
|
2017-06-03 07:26:09 -04:00
|
|
|
listUserFollowing(c, u)
|
2015-12-21 04:24:11 -08:00
|
|
|
}
|
|
|
|
|
|
2024-02-18 19:39:41 -05:00
|
|
|
func checkUserFollowing(c *context.APIContext, u *database.User, followID int64) {
|
2024-03-27 23:18:59 -04:00
|
|
|
if database.Handle.Users().IsFollowing(c.Req.Context(), u.ID, followID) {
|
2019-10-03 02:00:26 +00:00
|
|
|
c.NoContent()
|
2015-12-21 04:24:11 -08:00
|
|
|
} else {
|
2019-08-08 23:53:43 -07:00
|
|
|
c.NotFound()
|
2015-12-21 04:24:11 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-03 07:26:09 -04:00
|
|
|
func CheckMyFollowing(c *context.APIContext) {
|
|
|
|
|
target := GetUserByParams(c)
|
|
|
|
|
if c.Written() {
|
2015-12-21 04:24:11 -08:00
|
|
|
return
|
|
|
|
|
}
|
2017-06-03 07:26:09 -04:00
|
|
|
checkUserFollowing(c, c.User, target.ID)
|
2015-12-21 04:24:11 -08:00
|
|
|
}
|
|
|
|
|
|
2017-06-03 07:26:09 -04:00
|
|
|
func CheckFollowing(c *context.APIContext) {
|
|
|
|
|
u := GetUserByParams(c)
|
|
|
|
|
if c.Written() {
|
2015-12-21 04:24:11 -08:00
|
|
|
return
|
|
|
|
|
}
|
2017-06-03 07:26:09 -04:00
|
|
|
target := GetUserByParamsName(c, ":target")
|
|
|
|
|
if c.Written() {
|
2015-12-21 04:24:11 -08:00
|
|
|
return
|
|
|
|
|
}
|
2017-06-03 07:26:09 -04:00
|
|
|
checkUserFollowing(c, u, target.ID)
|
2015-12-21 04:24:11 -08:00
|
|
|
}
|
|
|
|
|
|
2017-06-03 07:26:09 -04:00
|
|
|
func Follow(c *context.APIContext) {
|
|
|
|
|
target := GetUserByParams(c)
|
|
|
|
|
if c.Written() {
|
2015-12-21 04:24:11 -08:00
|
|
|
return
|
|
|
|
|
}
|
2024-03-27 23:18:59 -04:00
|
|
|
if err := database.Handle.Users().Follow(c.Req.Context(), c.User.ID, target.ID); err != nil {
|
2020-03-16 01:22:27 +08:00
|
|
|
c.Error(err, "follow user")
|
2015-12-21 04:24:11 -08:00
|
|
|
return
|
|
|
|
|
}
|
2019-08-08 23:53:43 -07:00
|
|
|
c.NoContent()
|
2015-12-21 04:24:11 -08:00
|
|
|
}
|
|
|
|
|
|
2017-06-03 07:26:09 -04:00
|
|
|
func Unfollow(c *context.APIContext) {
|
|
|
|
|
target := GetUserByParams(c)
|
|
|
|
|
if c.Written() {
|
2015-12-21 04:24:11 -08:00
|
|
|
return
|
|
|
|
|
}
|
2024-03-27 23:18:59 -04:00
|
|
|
if err := database.Handle.Users().Unfollow(c.Req.Context(), c.User.ID, target.ID); err != nil {
|
2020-03-16 01:22:27 +08:00
|
|
|
c.Error(err, "unfollow user")
|
2015-12-21 04:24:11 -08:00
|
|
|
return
|
|
|
|
|
}
|
2019-08-08 23:53:43 -07:00
|
|
|
c.NoContent()
|
2015-12-21 04:24:11 -08:00
|
|
|
}
|