Skip to content

Commit

Permalink
修复关注bug
Browse files Browse the repository at this point in the history
  • Loading branch information
palp1tate committed Aug 16, 2023
1 parent deb6701 commit ae03523
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 27 deletions.
18 changes: 12 additions & 6 deletions controllers/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func (c *baseController) UploadJPG(imgPath string, videoUrl string) string {
return fmt.Sprintf("%s/%s", domain, ret.Key)
}

func (c *baseController) GetUserInfo(uid int) object.UserInfo {
func (c *baseController) GetUserInfo(uid int, token string) object.UserInfo {
var (
user models.User
videos []*models.Video
Expand All @@ -114,18 +114,24 @@ func (c *baseController) GetUserInfo(uid int) object.UserInfo {
//获取用户信息
c.o.QueryTable(new(models.User)).Filter("id", uid).One(&user)
followerCount, _ := c.o.QueryTable(new(models.Follow)).Filter("user_id", uid).Count()
if followerCount > 0 {
isFollow = true
} else {
isFollow = false
}

followCount, _ := c.o.QueryTable(new(models.Follow)).Filter("followed_user_id", uid).Count()

favoriteCount, _ := c.o.QueryTable(new(models.Favorite)).Filter("user_id", uid).Count()

workCount, _ := c.o.QueryTable(new(models.Video)).Filter("author_id", uid).All(&videos)

//判断当前用户是否关注该用户
if baseId, err := utils.GetUserIdFromToken(token); err == nil {
if exist := c.o.QueryTable(new(models.Follow)).Filter("user_id", user.Id).Filter("followed_user_id", baseId).Exist(); exist {
isFollow = true
} else {
isFollow = false
}
} else {
isFollow = false
}

//获取视频获赞数量
for _, video := range videos {
count, _ := c.o.QueryTable(new(models.Favorite)).Filter("video_id", video.Id).Count()
Expand Down
2 changes: 1 addition & 1 deletion controllers/comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func (c *CommentController) CommentList() {
var commentInfos []object.CommentInfo
c.o.QueryTable(new(models.Comment)).Filter("video_id", videoId).OrderBy("-create_time").All(&comments)
for _, comm := range comments {
userInfo := c.GetUserInfo(user.Id)
userInfo := c.GetUserInfo(user.Id, tokenString)
commentInfo := object.CommentInfo{
Content: comm.Content,
CreateDate: comm.CreateTime.Format("2006-01-02 15:04"),
Expand Down
2 changes: 1 addition & 1 deletion controllers/favorite.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func (c *FavoriteController) FavoriteList() {
favs []models.Favorite
videos []object.VideoInfo
)
userInfo := c.GetUserInfo(userId)
userInfo := c.GetUserInfo(userId, tokenString)
c.o.QueryTable(new(models.Favorite)).Filter("user_id", user.Id).All(&favs)
for _, fav := range favs {
var video models.Video
Expand Down
10 changes: 5 additions & 5 deletions controllers/follow.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func (c *FollowController) ListFollowRelation() {
return
}
userId, _ := c.GetInt("user_id")
followList, err := GetAllFollowByUserId(c, userId)
followList, err := GetAllFollowByUserId(c, userId, token)
if err != nil {

c.Data["json"] = map[string]interface{}{
Expand Down Expand Up @@ -155,7 +155,7 @@ func (c *FollowController) ListFollowerRelation() {
// 获取所有粉丝的被关注用户ID
var userInfos []object.UserInfo
for _, follow := range follows {
userInfo := c.GetUserInfo(follow.FollowedUserId.Id)
userInfo := c.GetUserInfo(follow.FollowedUserId.Id, token)
userInfos = append(userInfos, userInfo)
}

Expand Down Expand Up @@ -209,7 +209,7 @@ func (c *FollowController) ListFriendRelation() {
for _, follow := range follows {
count, _ := c.o.QueryTable(new(models.Follow)).Filter("user_id", follow.FollowedUserId.Id).Filter("followed_user_id", userId).Count()
if count == 1 {
userInfo := c.GetUserInfo(follow.FollowedUserId.Id)
userInfo := c.GetUserInfo(follow.FollowedUserId.Id, token)
userInfos = append(userInfos, userInfo)
} else {
continue
Expand Down Expand Up @@ -292,7 +292,7 @@ func CancelFollow(c *FollowController, fromUseId int, toUseId int) (err error) {
}

// GetAllFollowByUserId 获取关注列表
func GetAllFollowByUserId(c *FollowController, userId int) (followList []object.UserInfo, err error) {
func GetAllFollowByUserId(c *FollowController, userId int, token string) (followList []object.UserInfo, err error) {
// 查询出被关注者的id集合
var list []orm.ParamsList

Expand All @@ -302,7 +302,7 @@ func GetAllFollowByUserId(c *FollowController, userId int) (followList []object.
id := list[i][0]
id_int, _ := strconv.Atoi(id.(string))

user := c.GetUserInfo(id_int)
user := c.GetUserInfo(id_int, token)
if err == nil {
followList = append(followList, user)

Expand Down
2 changes: 1 addition & 1 deletion controllers/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func (c *UserController) Info() {
c.ServeJSON()
return
}
userInfo := c.GetUserInfo(uid)
userInfo := c.GetUserInfo(uid, token)
c.Data["json"] = map[string]interface{}{
"status_code": 0,
"status_msg": "获取用户信息成功",
Expand Down
18 changes: 5 additions & 13 deletions controllers/video.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,7 @@ func (c *VideoController) Feed() {
latestTimeStamp := int(time.Now().Unix())
latestTime := time.Unix(int64(latestTimeStamp), 0)
//不需要验证token,不登录也能看视频流
//token := c.GetString("token")
//if token != "" {
// c.o.QueryTable(new(models.Video)).Filter("create_time__lte", latestTime).OrderBy("-create_time").Limit(30, 0).All(&videos)
//} else {
//
// c.o.QueryTable(new(models.Video)).OrderBy("-create_time").Limit(30, 0).All(&videos)
//}
token := c.GetString("token")

c.o.QueryTable(new(models.Video)).Filter("create_time__lte", latestTime).OrderBy("-create_time").Limit(30, 0).All(&videos)
if len(videos) == 0 {
Expand All @@ -55,7 +49,7 @@ func (c *VideoController) Feed() {
} else {
isFavorite = true
}
userInfo := c.GetUserInfo(video.AuthorId.Id)
userInfo := c.GetUserInfo(video.AuthorId.Id, token)
videoList = append(videoList, &object.VideoInfo{
ID: video.Id,
Title: video.Title,
Expand Down Expand Up @@ -87,10 +81,8 @@ func (c *VideoController) Publish() {
c.PublishFail("token验证失败")
return
}
var user models.User

username, _ := utils.GetUsernameFromToken(token)
c.o.QueryTable(new(models.User)).Filter("username", username).One(&user)
user, _ := utils.GetUserFromToken(token)
if url := c.UploadMP4(c.GetFile("data")); url == "" {
c.PublishFail("发布失败")
return
Expand All @@ -109,7 +101,7 @@ func (c *VideoController) Publish() {
os.Remove(imgPath)

video := models.Video{
AuthorId: &user,
AuthorId: user,
PlayUrl: url,
Title: title,
CoverUrl: coverUrl,
Expand Down Expand Up @@ -154,7 +146,7 @@ func (c *VideoController) List() {
var (
videos []*models.Video
videoList []*object.VideoInfo
userInfo = c.GetUserInfo(uid)
userInfo = c.GetUserInfo(uid, token)
)
c.o.QueryTable(new(models.Video)).Filter("author_id", uid).All(&videos)
for _, video := range videos {
Expand Down

0 comments on commit ae03523

Please sign in to comment.