/
relation_service.go
155 lines (129 loc) · 3.6 KB
/
relation_service.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package service
import (
"context"
"github.com/pkg/errors"
"github.com/1024casts/snake/internal/model"
"github.com/1024casts/snake/pkg/log"
)
const (
// FollowStatusNormal 关注状态-正常
FollowStatusNormal int = 1 // 正常
// FollowStatusDelete 关注状态-删除
FollowStatusDelete = 0 // 删除
)
// IsFollowing 是否正在关注某用户
func (s *Service) IsFollowing(ctx context.Context, userID uint64, followedUID uint64) bool {
userFollowModel := &model.UserFollowModel{}
result := model.GetDB().
Where("user_id=? AND followed_uid=? ", userID, followedUID).
Find(userFollowModel)
if err := result.Error; err != nil {
log.Warnf("[user_service] get user follow err, %v", err)
return false
}
if userFollowModel.ID > 0 && userFollowModel.Status == FollowStatusNormal {
return true
}
return false
}
// Follow 关注目标用户
func (s *Service) Follow(ctx context.Context, userID uint64, followedUID uint64) error {
db := model.GetDB()
tx := db.Begin()
defer func() {
if r := recover(); r != nil {
tx.Rollback()
}
}()
// 添加到关注表
err := s.dao.CreateUserFollow(ctx, tx, userID, followedUID)
if err != nil {
tx.Rollback()
return errors.Wrap(err, "insert into user follow err")
}
// 添加到粉丝表
err = s.dao.CreateUserFans(ctx, tx, followedUID, userID)
if err != nil {
tx.Rollback()
return errors.Wrap(err, "insert into user fans err")
}
// 添加关注数
err = s.dao.IncrFollowCount(ctx, tx, userID, 1)
if err != nil {
tx.Rollback()
return errors.Wrap(err, "update user follow count err")
}
// 添加粉丝数
err = s.dao.IncrFollowerCount(ctx, tx, followedUID, 1)
if err != nil {
return errors.Wrap(err, "update user fans count err")
}
err = tx.Commit().Error
if err != nil {
tx.Rollback()
return errors.Wrap(err, "tx commit err")
}
return nil
}
// Unfollow 取消用户关注
func (s *Service) Unfollow(ctx context.Context, userID uint64, followedUID uint64) error {
db := model.GetDB()
tx := db.Begin()
defer func() {
if r := recover(); r != nil {
tx.Rollback()
}
}()
// 删除关注
err := s.dao.UpdateUserFollowStatus(ctx, tx, userID, followedUID, FollowStatusDelete)
if err != nil {
tx.Rollback()
return errors.Wrap(err, "update user follow err")
}
// 删除粉丝
err = s.dao.UpdateUserFansStatus(ctx, tx, followedUID, userID, FollowStatusDelete)
if err != nil {
tx.Rollback()
return errors.Wrap(err, "update user follow err")
}
// 减少关注数
err = s.dao.IncrFollowCount(ctx, tx, userID, -1)
if err != nil {
tx.Rollback()
return errors.Wrap(err, "update user follow count err")
}
// 减少粉丝数
err = s.dao.IncrFollowerCount(ctx, tx, followedUID, -1)
if err != nil {
tx.Rollback()
return errors.Wrap(err, "update user fans count err")
}
err = tx.Commit().Error
if err != nil {
tx.Rollback()
return errors.Wrap(err, "tx commit err")
}
return nil
}
// GetFollowingUserList 获取正在关注的用户列表
func (s *Service) GetFollowingUserList(ctx context.Context, userID uint64, lastID uint64, limit int) ([]*model.UserFollowModel, error) {
if lastID == 0 {
lastID = MaxID
}
userFollowList, err := s.dao.GetFollowingUserList(ctx, userID, lastID, limit)
if err != nil {
return nil, err
}
return userFollowList, nil
}
// GetFollowerUserList 获取粉丝用户列表
func (s *Service) GetFollowerUserList(ctx context.Context, userID uint64, lastID uint64, limit int) ([]*model.UserFansModel, error) {
if lastID == 0 {
lastID = MaxID
}
userFollowerList, err := s.dao.GetFollowerUserList(ctx, userID, lastID, limit)
if err != nil {
return nil, err
}
return userFollowerList, nil
}