后端:
完善注册页面
This commit is contained in:
@@ -79,6 +79,7 @@ namespace IM_API.Services
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public Task DeleteGroupAsync(int userId, int groupId)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
@@ -90,15 +91,15 @@ namespace IM_API.Services
|
||||
x => x.Id == groupId) ?? throw new BaseException(CodeDefine.GROUP_NOT_FOUND);
|
||||
//过滤非好友
|
||||
var groupInviteIds = await validFriendshipAsync(userId, userIds);
|
||||
var inviteList = groupInviteIds.Select(id => new GroupInvite
|
||||
var inviteList = groupInviteIds.Select(id => new GroupRequest
|
||||
{
|
||||
Created = DateTime.UtcNow,
|
||||
GroupId = group.Id,
|
||||
InviteUser = userId,
|
||||
InvitedUser = id,
|
||||
StateEnum = GroupInviteState.Pending
|
||||
UserId = id,
|
||||
InviteUserId = userId,
|
||||
StateEnum = GroupRequestState.TargetPending
|
||||
}).ToList();
|
||||
_context.GroupInvites.AddRange(inviteList);
|
||||
_context.GroupRequests.AddRange(inviteList);
|
||||
await _context.SaveChangesAsync();
|
||||
await _endPoint.Publish(new GroupInviteEvent
|
||||
{
|
||||
@@ -132,7 +133,7 @@ namespace IM_API.Services
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public async Task<List<GroupInfoDto>> GetGroupListAsync(int userId, int page, int limit, bool desc)
|
||||
public async Task<List<GroupInfoDto>> GetGroupListAsync(int userId, int page = 1, int limit = 50, bool desc = false)
|
||||
{
|
||||
var query = _context.GroupMembers
|
||||
.Where(x => x.UserId == userId)
|
||||
@@ -159,14 +160,18 @@ namespace IM_API.Services
|
||||
_context.Groups.Update(group);
|
||||
await _context.SaveChangesAsync();
|
||||
}
|
||||
|
||||
public async Task HandleGroupInviteAsync(int userid, HandleGroupInviteDto dto)
|
||||
{
|
||||
var user = _userService.GetUserInfoAsync(userid);
|
||||
var inviteInfo = await _context.GroupInvites.FirstOrDefaultAsync(x => x.Id == dto.InviteId)
|
||||
var inviteInfo = await _context.GroupRequests
|
||||
.FirstOrDefaultAsync(x => x.UserId == userid && x.StateEnum == GroupRequestState.TargetPending)
|
||||
?? throw new BaseException(CodeDefine.INVALID_ACTION);
|
||||
if (inviteInfo.InvitedUser != userid) throw new BaseException(CodeDefine.AUTH_FAILED);
|
||||
if (!(dto.Action == GroupRequestState.TargetPending ||
|
||||
dto.Action == GroupRequestState.TargetDeclined))
|
||||
return;
|
||||
inviteInfo.StateEnum = dto.Action;
|
||||
_context.GroupInvites.Update(inviteInfo);
|
||||
_context.GroupRequests.Update(inviteInfo);
|
||||
await _context.SaveChangesAsync();
|
||||
await _endPoint.Publish(new GroupInviteActionUpdateEvent
|
||||
{
|
||||
@@ -176,12 +181,13 @@ namespace IM_API.Services
|
||||
EventId = Guid.NewGuid(),
|
||||
GroupId = inviteInfo.GroupId,
|
||||
InviteId = inviteInfo.Id,
|
||||
InviteUserId = inviteInfo.InviteUser.Value,
|
||||
InviteUserId = inviteInfo.InviteUserId!.Value,
|
||||
OperatorId = userid,
|
||||
UserId = userid
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
public async Task HandleGroupRequestAsync(int userid, HandleGroupRequestDto dto)
|
||||
{
|
||||
var user = _userService.GetUserInfoAsync(userid);
|
||||
@@ -302,5 +308,68 @@ namespace IM_API.Services
|
||||
|
||||
return _mapper.Map<GroupInfoVo>(groupInfo);
|
||||
}
|
||||
|
||||
public async Task<List<GroupNotificationVo>> GetGroupNotificationAsync(int userId)
|
||||
{
|
||||
// 1. 查询群请求记录
|
||||
var groupList = await _context.GroupMembers
|
||||
.Where(x => x.UserId == userId &&
|
||||
(x.Role == (sbyte)GroupMemberRole.Master || x.Role == (sbyte)GroupMemberRole.Administrator))
|
||||
.Select(s => s.GroupId)
|
||||
.ToListAsync();
|
||||
|
||||
var groupRequest = await _context.GroupRequests
|
||||
.Where(x => groupList.Contains(x.GroupId) || x.UserId == userId || x.InviteUserId == userId)
|
||||
.OrderByDescending(o => o.Id)
|
||||
.ToListAsync();
|
||||
|
||||
if (!groupRequest.Any()) return new List<GroupNotificationVo>();
|
||||
|
||||
// 2. 收集所有需要的 ID 并去重
|
||||
var userIds = groupRequest.Select(s => s.UserId).Distinct().ToList();
|
||||
var inviteUserIds = groupRequest.Where(x => x.InviteUserId != null).Select(s => s.InviteUserId.Value).Distinct().ToList();
|
||||
var groupIds = groupRequest.Select(s => s.GroupId).Distinct().ToList();
|
||||
|
||||
var userList = await _userService.GetUserInfoListAsync(userIds);
|
||||
var inviteUserList = await _userService.GetUserInfoListAsync(inviteUserIds);
|
||||
var groupInfoList = await _context.Groups
|
||||
.Where(x => groupIds.Contains(x.Id))
|
||||
.ToListAsync();
|
||||
|
||||
// 2. 转换为字典
|
||||
var userDict = userList.ToDictionary(u => u.Id);
|
||||
var inviteUserDict = inviteUserList.ToDictionary(u => u.Id);
|
||||
var groupDict = groupInfoList.ToDictionary(g => g.Id);
|
||||
|
||||
// 3. 组装数据 (Select 逻辑不变)
|
||||
return groupRequest.Select(g =>
|
||||
{
|
||||
var gnv = _mapper.Map<GroupNotificationVo>(g);
|
||||
|
||||
// 匹配用户信息
|
||||
if (userDict.TryGetValue(g.UserId, out var u))
|
||||
{
|
||||
gnv.UserAvatar = u.Avatar;
|
||||
gnv.NickName = u.NickName;
|
||||
}
|
||||
|
||||
// 匹配邀请人信息
|
||||
if (g.InviteUserId.HasValue && inviteUserDict.TryGetValue(g.InviteUserId.Value, out var i))
|
||||
{
|
||||
gnv.InviteUserAvatar = i.Avatar;
|
||||
gnv.InviteUserNickname = i.NickName;
|
||||
}
|
||||
|
||||
// 匹配群信息
|
||||
if (groupDict.TryGetValue(g.GroupId, out var gi))
|
||||
{
|
||||
gnv.GroupAvatar = gi.Avatar;
|
||||
gnv.GroupName = gi.Name;
|
||||
}
|
||||
|
||||
return gnv;
|
||||
}).ToList();
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user