Skip to content

Commit

Permalink
optimize Folloship feature allow follow/unfollow user in follow page
Browse files Browse the repository at this point in the history
  • Loading branch information
alimy committed Aug 18, 2023
1 parent b6d9522 commit fd5e54b
Show file tree
Hide file tree
Showing 35 changed files with 260 additions and 84 deletions.
13 changes: 7 additions & 6 deletions internal/core/cs/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,13 @@ type (

// UserInfo 用户基本信息
type UserInfo struct {
ID int64 `json:"id"`
Nickname string `json:"nickname"`
Username string `json:"username"`
Status int `json:"status"`
Avatar string `json:"avatar"`
IsAdmin bool `json:"is_admin"`
ID int64 `json:"id"`
Nickname string `json:"nickname"`
Username string `json:"username"`
Status int `json:"status"`
Avatar string `json:"avatar"`
IsAdmin bool `json:"is_admin"`
CreatedOn int64 `json:"created_on"`
}

func (t RelationTyp) String() string {
Expand Down
13 changes: 7 additions & 6 deletions internal/core/ms/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ package ms

type (
ContactItem struct {
UserId int64 `json:"user_id"`
Username string `db:"username" json:"username"`
Nickname string `json:"nickname"`
Avatar string `json:"avatar"`
Phone string `json:"phone,omitempty"`
IsFollow bool `json:"is_follow,omitempty"`
UserId int64 `json:"user_id"`
Username string `db:"username" json:"username"`
Nickname string `json:"nickname"`
Avatar string `json:"avatar"`
Phone string `json:"phone,omitempty"`
IsFollowing bool `json:"is_following"`
CreatedOn int64 `json:"created_on"`
}

ContactList struct {
Expand Down
11 changes: 6 additions & 5 deletions internal/dao/jinzhu/contacts.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,11 +248,12 @@ func (s *contactManageSrv) GetContacts(userId int64, offset int, limit int) (*ms
for _, c := range contacts {
if c.User != nil {
resp.Contacts = append(resp.Contacts, ms.ContactItem{
UserId: c.FriendId,
Username: c.User.Username,
Nickname: c.User.Nickname,
Avatar: c.User.Avatar,
Phone: c.User.Phone,
UserId: c.FriendId,
Username: c.User.Username,
Nickname: c.User.Nickname,
Avatar: c.User.Avatar,
Phone: c.User.Phone,
CreatedOn: c.User.CreatedOn,
})
}
}
Expand Down
20 changes: 10 additions & 10 deletions internal/dao/jinzhu/following.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ func (s *followingManageSrv) ListFollows(userId int64, limit, offset int) (*ms.C
}
for _, f := range follows {
res.Contacts = append(res.Contacts, ms.ContactItem{
UserId: f.User.ID,
Username: f.User.Username,
Nickname: f.User.Nickname,
Avatar: f.User.Avatar,
IsFollow: true,
UserId: f.User.ID,
Username: f.User.Username,
Nickname: f.User.Nickname,
Avatar: f.User.Avatar,
CreatedOn: f.User.CreatedOn,
})
}
return res, nil
Expand All @@ -82,11 +82,11 @@ func (s *followingManageSrv) ListFollowings(userId int64, limit, offset int) (*m
}
for _, user := range followings {
res.Contacts = append(res.Contacts, ms.ContactItem{
UserId: user.ID,
Username: user.Username,
Nickname: user.Nickname,
Avatar: user.Avatar,
IsFollow: s.IsFollow(userId, user.ID),
UserId: user.ID,
Username: user.Username,
Nickname: user.Nickname,
Avatar: user.Avatar,
CreatedOn: user.CreatedOn,
})
}
return res, nil
Expand Down
2 changes: 2 additions & 0 deletions internal/model/web/xerror.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ var (
ErrListFollowsFailed = xerror.NewError(80102, "获取关注列表失败")
ErrListFollowingsFailed = xerror.NewError(80103, "获取粉丝列表列表失败")
ErrGetFollowCountFailed = xerror.NewError(80104, "获取关注计数信息失败")
ErrNotAllowFollowSelf = xerror.NewError(80105, "不能关注自己")
ErrNotAllowUnfollowSelf = xerror.NewError(80106, "不能取消关注自己")

ErrFollowTopicFailed = xerror.NewError(90001, "关注话题失败")
ErrUnfollowTopicFailed = xerror.NewError(90002, "取消关注话题失败")
Expand Down
29 changes: 28 additions & 1 deletion internal/servants/web/followship.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/rocboss/paopao-ce/internal/model/web"
"github.com/rocboss/paopao-ce/internal/servants/base"
"github.com/rocboss/paopao-ce/internal/servants/chain"
"github.com/rocboss/paopao-ce/pkg/xerror"
"github.com/sirupsen/logrus"
)

Expand All @@ -24,7 +25,7 @@ type followshipSrv struct {
}

func (s *followshipSrv) Chain() gin.HandlersChain {
return gin.HandlersChain{chain.JWT()}
return gin.HandlersChain{chain.JwtLoose()}
}

func (s *followshipSrv) ListFollowings(r *web.ListFollowingsReq) (*web.ListFollowingsResp, mir.Error) {
Expand All @@ -38,6 +39,11 @@ func (s *followshipSrv) ListFollowings(r *web.ListFollowingsReq) (*web.ListFollo
logrus.Errorf("Ds.ListFollowings err: %s", err)
return nil, web.ErrListFollowingsFailed
}
if r.BaseInfo.User != nil {
for i, contact := range res.Contacts {
res.Contacts[i].IsFollowing = s.Ds.IsFollow(r.User.ID, contact.UserId)
}
}
resp := base.PageRespFrom(res.Contacts, r.Page, r.PageSize, res.Total)
return (*web.ListFollowingsResp)(resp), nil
}
Expand All @@ -53,11 +59,27 @@ func (s *followshipSrv) ListFollows(r *web.ListFollowsReq) (*web.ListFollowsResp
logrus.Errorf("Ds.ListFollows err: %s", err)
return nil, web.ErrListFollowsFailed
}
if r.BaseInfo.User != nil {
if r.User.Username == r.Username {
for i := range res.Contacts {
res.Contacts[i].IsFollowing = true
}
} else {
for i, contact := range res.Contacts {
res.Contacts[i].IsFollowing = s.Ds.IsFollow(r.User.ID, contact.UserId)
}
}
}
resp := base.PageRespFrom(res.Contacts, r.Page, r.PageSize, res.Total)
return (*web.ListFollowsResp)(resp), nil
}

func (s *followshipSrv) UnfollowUser(r *web.UnfollowUserReq) mir.Error {
if r.User == nil {
return xerror.UnauthorizedTokenError
} else if r.User.ID == r.UserId {
return web.ErrNotAllowUnfollowSelf
}
if err := s.Ds.UnfollowUser(r.User.ID, r.UserId); err != nil {
logrus.Errorf("Ds.UnfollowUser err: %s userId: %d followId: %d", err, r.User.ID, r.UserId)
return web.ErrUnfollowUserFailed
Expand All @@ -66,6 +88,11 @@ func (s *followshipSrv) UnfollowUser(r *web.UnfollowUserReq) mir.Error {
}

func (s *followshipSrv) FollowUser(r *web.FollowUserReq) mir.Error {
if r.User == nil {
return xerror.UnauthorizedTokenError
} else if r.User.ID == r.UserId {
return web.ErrNotAllowFollowSelf
}
if err := s.Ds.FollowUser(r.User.ID, r.UserId); err != nil {
logrus.Errorf("Ds.FollowUser err: %s userId: %d followId: %d", err, r.User.ID, r.UserId)
return web.ErrUnfollowUserFailed
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit fd5e54b

Please sign in to comment.