feat:单聊页面bug修复+后台管理端好友页面bug修复+项目进度、记忆文件更新
This commit is contained in:
@@ -42,7 +42,7 @@ func (d *FriendshipDAO) CreateRequest(ctx context.Context, userID, friendID int6
|
||||
return f, err
|
||||
}
|
||||
|
||||
// AcceptRequest 接受好友申请(事务内:更新 A→B 为 accepted + 创建 B→A)
|
||||
// AcceptRequest 接受好友申请(事务内:更新 A→B 为 accepted + 创建或更新 B→A 为 accepted)
|
||||
func (d *FriendshipDAO) AcceptRequest(ctx context.Context, requestID, userID int64) error {
|
||||
funcName := "dao.friendship_dao.AcceptRequest"
|
||||
logs.Info(ctx, funcName, "接受好友申请",
|
||||
@@ -65,6 +65,15 @@ func (d *FriendshipDAO) AcceptRequest(ctx context.Context, requestID, userID int
|
||||
return err
|
||||
}
|
||||
|
||||
var existing model.Friendship
|
||||
err := tx.Where("user_id = ? AND friend_id = ?", userID, req.UserID).First(&existing).Error
|
||||
if err == nil {
|
||||
return tx.Model(&existing).Updates(map[string]interface{}{
|
||||
"status": constants.FriendshipStatusAccepted,
|
||||
"updated_at": now,
|
||||
}).Error
|
||||
}
|
||||
|
||||
reverse := &model.Friendship{
|
||||
UserID: userID,
|
||||
FriendID: req.UserID,
|
||||
@@ -268,6 +277,28 @@ func (d *FriendshipDAO) HasPendingRequest(ctx context.Context, userID, friendID
|
||||
return count > 0, err
|
||||
}
|
||||
|
||||
// ReactivateRejectedRequest 将已拒绝的申请重新激活为待处理状态
|
||||
// 返回是否找到并更新了已拒绝的记录
|
||||
func (d *FriendshipDAO) ReactivateRejectedRequest(ctx context.Context, userID, friendID int64, message string) (bool, error) {
|
||||
funcName := "dao.friendship_dao.ReactivateRejectedRequest"
|
||||
logs.Info(ctx, funcName, "重新激活已拒绝的好友申请",
|
||||
zap.Int64("user_id", userID), zap.Int64("friend_id", friendID))
|
||||
|
||||
result := d.db.WithContext(ctx).
|
||||
Model(&model.Friendship{}).
|
||||
Where("user_id = ? AND friend_id = ? AND status = ?", userID, friendID, constants.FriendshipStatusRejected).
|
||||
Updates(map[string]interface{}{
|
||||
"status": constants.FriendshipStatusPending,
|
||||
"message": message,
|
||||
})
|
||||
|
||||
if result.Error != nil {
|
||||
logs.Error(ctx, funcName, "重新激活好友申请失败", zap.Error(result.Error))
|
||||
return false, result.Error
|
||||
}
|
||||
return result.RowsAffected > 0, nil
|
||||
}
|
||||
|
||||
// GetRequestByID 根据 ID 获取好友申请
|
||||
func (d *FriendshipDAO) GetRequestByID(ctx context.Context, id int64) (*model.Friendship, error) {
|
||||
var f model.Friendship
|
||||
|
||||
@@ -25,11 +25,17 @@ var (
|
||||
ErrUserNotFound = errors.New("用户不存在")
|
||||
)
|
||||
|
||||
// OnlineChecker 在线状态查询接口(避免直接依赖 ws 模块的 OnlineService)
|
||||
type OnlineChecker interface {
|
||||
BatchCheckOnline(ctx context.Context, userIDs []int64) map[int64]bool
|
||||
}
|
||||
|
||||
// ContactService 联系人业务服务
|
||||
type ContactService struct {
|
||||
friendshipDAO *dao.FriendshipDAO
|
||||
friendGroupDAO *dao.FriendGroupDAO
|
||||
pubsub *ws.PubSub
|
||||
onlineChecker OnlineChecker
|
||||
}
|
||||
|
||||
// NewContactService 创建 ContactService 实例
|
||||
@@ -37,11 +43,13 @@ func NewContactService(
|
||||
friendshipDAO *dao.FriendshipDAO,
|
||||
friendGroupDAO *dao.FriendGroupDAO,
|
||||
pubsub *ws.PubSub,
|
||||
onlineChecker OnlineChecker,
|
||||
) *ContactService {
|
||||
return &ContactService{
|
||||
friendshipDAO: friendshipDAO,
|
||||
friendGroupDAO: friendGroupDAO,
|
||||
pubsub: pubsub,
|
||||
onlineChecker: onlineChecker,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -80,10 +88,16 @@ func (s *ContactService) SendFriendRequest(ctx context.Context, userID, targetID
|
||||
return ErrPendingExists
|
||||
}
|
||||
|
||||
_, err = s.friendshipDAO.CreateRequest(ctx, userID, targetID, message)
|
||||
reactivated, err := s.friendshipDAO.ReactivateRejectedRequest(ctx, userID, targetID, message)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !reactivated {
|
||||
_, err = s.friendshipDAO.CreateRequest(ctx, userID, targetID, message)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
push := ws.NewPushMessage("notify.friend.request", map[string]interface{}{
|
||||
"from_user_id": userID,
|
||||
@@ -141,7 +155,7 @@ func (s *ContactService) RejectFriendRequest(ctx context.Context, requestID, use
|
||||
return err
|
||||
}
|
||||
|
||||
// GetFriendList 获取好友列表
|
||||
// GetFriendList 获取好友列表(包含在线状态)
|
||||
func (s *ContactService) GetFriendList(ctx context.Context, userID int64, groupID *int64) ([]dto.FriendInfo, error) {
|
||||
funcName := "service.contact_service.GetFriendList"
|
||||
logs.Debug(ctx, funcName, "获取好友列表", zap.Int64("user_id", userID))
|
||||
@@ -151,6 +165,15 @@ func (s *ContactService) GetFriendList(ctx context.Context, userID int64, groupI
|
||||
return nil, err
|
||||
}
|
||||
|
||||
friendIDs := make([]int64, 0, len(friends))
|
||||
for _, f := range friends {
|
||||
friendIDs = append(friendIDs, f.UserID)
|
||||
}
|
||||
onlineMap := make(map[int64]bool)
|
||||
if s.onlineChecker != nil && len(friendIDs) > 0 {
|
||||
onlineMap = s.onlineChecker.BatchCheckOnline(ctx, friendIDs)
|
||||
}
|
||||
|
||||
result := make([]dto.FriendInfo, 0, len(friends))
|
||||
for _, f := range friends {
|
||||
result = append(result, dto.FriendInfo{
|
||||
@@ -161,6 +184,7 @@ func (s *ContactService) GetFriendList(ctx context.Context, userID int64, groupI
|
||||
Avatar: f.Avatar,
|
||||
Remark: f.Remark,
|
||||
GroupID: f.GroupID,
|
||||
IsOnline: onlineMap[f.UserID],
|
||||
CreatedAt: f.CreatedAt.Format("2006-01-02 15:04:05"),
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user