You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

124 lines
2.7 KiB

4 years ago
package api
import (
"net/http"
"github.com/gin-gonic/gin"
"github.com/imdotdev/im.dev/server/internal/interaction"
"github.com/imdotdev/im.dev/server/internal/user"
"github.com/imdotdev/im.dev/server/pkg/common"
"github.com/imdotdev/im.dev/server/pkg/e"
4 years ago
"github.com/imdotdev/im.dev/server/pkg/models"
4 years ago
)
func Follow(c *gin.Context) {
user := user.CurrentUser(c)
id := c.Param("id")
if id == "" {
c.JSON(http.StatusBadRequest, common.RespError(e.ParamInvalid))
return
}
4 years ago
if id == user.ID {
c.JSON(http.StatusBadRequest, common.RespError("无法关注自己"))
return
}
4 years ago
err := interaction.Follow(id, user.ID)
if err != nil {
c.JSON(err.Status, common.RespError(err.Message))
return
}
c.JSON(http.StatusOK, common.RespSuccess(nil))
}
func Followed(c *gin.Context) {
user := user.CurrentUser(c)
id := c.Param("id")
if id == "" {
c.JSON(http.StatusBadRequest, common.RespError(e.ParamInvalid))
return
}
followed := false
if user != nil {
followed = interaction.GetFollowed(id, user.ID)
}
c.JSON(http.StatusOK, common.RespSuccess(followed))
}
func Like(c *gin.Context) {
user := user.CurrentUser(c)
id := c.Param("id")
if id == "" {
c.JSON(http.StatusBadRequest, common.RespError(e.ParamInvalid))
return
}
err := interaction.Like(id, user.ID)
if err != nil {
c.JSON(err.Status, common.RespError(err.Message))
return
}
c.JSON(http.StatusOK, common.RespSuccess(nil))
}
4 years ago
func GetFollowing(c *gin.Context) {
userID := c.Param("userID")
targetType := c.Query("type")
if userID == "" || !models.ValidFollowIDType(targetType) {
c.JSON(http.StatusBadRequest, common.RespError(e.ParamInvalid))
return
}
if userID == "0" {
u := user.CurrentUser(c)
userID = u.ID
}
following, err := interaction.GetFollowing(userID, targetType)
4 years ago
if err != nil {
c.JSON(err.Status, common.RespError(err.Message))
return
}
c.JSON(http.StatusOK, common.RespSuccess(following))
}
func GetFollowers(c *gin.Context) {
userID := c.Param("userID")
targetType := c.Query("type")
if userID == "" || !models.ValidFollowIDType(targetType) {
c.JSON(http.StatusBadRequest, common.RespError(e.ParamInvalid))
return
}
if userID == "0" {
u := user.CurrentUser(c)
userID = u.ID
}
followers, err := interaction.GetFollowers(userID, targetType)
if err != nil {
c.JSON(err.Status, common.RespError(err.Message))
return
}
c.JSON(http.StatusOK, common.RespSuccess(followers))
4 years ago
}
func SetFollowingWeight(c *gin.Context) {
f := &models.Following{}
c.Bind(&f)
u := user.CurrentUser(c)
err := interaction.SetFolloingWeight(u.ID, f)
if err != nil {
c.JSON(err.Status, common.RespError(err.Message))
return
}
c.JSON(http.StatusOK, common.RespSuccess(nil))
}