前端:
1、会话列表、消息界面展示与后端打通 后端: 1、修复会话和消息服务现存问题 2、会话对象不再返回Message对象,而是使用MessageBaseDto替代 3、修改查询会话列表和会话信息的逻辑 4、新增消息列表查询 文档: 后端代码规范文档新增从数据库同步到模型的命令
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using IM_API.Dtos;
|
||||
using AutoMapper;
|
||||
using IM_API.Dtos;
|
||||
using IM_API.Exceptions;
|
||||
using IM_API.Interface.Services;
|
||||
using IM_API.Models;
|
||||
@@ -10,9 +11,11 @@ namespace IM_API.Services
|
||||
public class ConversationService : IConversationService
|
||||
{
|
||||
private readonly ImContext _context;
|
||||
public ConversationService(ImContext context)
|
||||
private readonly IMapper _mapper;
|
||||
public ConversationService(ImContext context, IMapper mapper)
|
||||
{
|
||||
_context = context;
|
||||
_mapper = mapper;
|
||||
}
|
||||
#region 删除用户会话
|
||||
public async Task<bool> ClearConversationsAsync(int userId)
|
||||
@@ -26,47 +29,41 @@ namespace IM_API.Services
|
||||
#region 获取用户会话列表
|
||||
public async Task<List<ConversationDto>> GetConversationsAsync(int userId)
|
||||
{
|
||||
var privateQuery = from c in _context.Conversations
|
||||
join f in _context.Friends on new { c.UserId, c.TargetId}
|
||||
equals new { UserId = f.UserId, TargetId = f.FriendId}
|
||||
where c.UserId == userId && c.ChatType == (int)ChatType.PRIVATE
|
||||
select new ConversationDto
|
||||
{
|
||||
Id = c.Id,
|
||||
UserId = c.UserId,
|
||||
TargetId = c.TargetId,
|
||||
LastReadMessageId = c.LastReadMessageId,
|
||||
LastReadMessage = c.LastReadMessage,
|
||||
UnreadCount = c.UnreadCount,
|
||||
ChatType = c.ChatType,
|
||||
LastMessage = c.LastMessage,
|
||||
TargetAvatar = f.Avatar,
|
||||
TargetName = f.RemarkName,
|
||||
DateTime = c.LastMessageTime
|
||||
// 1. 获取私聊会话
|
||||
var privateList = await (from c in _context.Conversations
|
||||
join f in _context.Friends on new { c.UserId, c.TargetId }
|
||||
equals new { UserId = f.UserId, TargetId = f.FriendId }
|
||||
where c.UserId == userId && c.ChatType == (int)ChatType.PRIVATE
|
||||
select new { c, f.Avatar, f.RemarkName })
|
||||
.ToListAsync();
|
||||
|
||||
};
|
||||
// 2. 获取群聊会话
|
||||
var groupList = await (from c in _context.Conversations
|
||||
join g in _context.Groups on c.TargetId equals g.Id
|
||||
where c.UserId == userId && c.ChatType == (int)ChatType.GROUP
|
||||
select new { c, g.Avatar, g.Name })
|
||||
.ToListAsync();
|
||||
|
||||
var groupQuery = from c in _context.Conversations
|
||||
join g in _context.Groups on c.TargetId equals g.Id
|
||||
where c.UserId == userId && c.ChatType == (int)ChatType.GROUP
|
||||
select new ConversationDto
|
||||
{
|
||||
Id = c.Id,
|
||||
UserId = c.UserId,
|
||||
TargetId = c.TargetId,
|
||||
LastReadMessageId = c.LastReadMessageId,
|
||||
LastReadMessage = c.LastReadMessage,
|
||||
UnreadCount = c.UnreadCount,
|
||||
ChatType = c.ChatType,
|
||||
LastMessage = c.LastMessage,
|
||||
TargetAvatar = g.Avatar,
|
||||
TargetName = g.Name,
|
||||
DateTime = c.LastMessageTime
|
||||
};
|
||||
return await privateQuery
|
||||
.Concat(groupQuery)
|
||||
.OrderByDescending(x => x.DateTime)
|
||||
.ToListAsync();
|
||||
var privateDtos = privateList.Select(x =>
|
||||
{
|
||||
var dto = _mapper.Map<ConversationDto>(x.c);
|
||||
dto.TargetAvatar = x.Avatar;
|
||||
dto.TargetName = x.RemarkName;
|
||||
return dto;
|
||||
});
|
||||
|
||||
var groupDtos = groupList.Select(x =>
|
||||
{
|
||||
var dto = _mapper.Map<ConversationDto>(x.c);
|
||||
dto.TargetAvatar = x.Avatar;
|
||||
dto.TargetName = x.Name;
|
||||
return dto;
|
||||
});
|
||||
|
||||
// 4. 合并并排序
|
||||
return privateDtos.Concat(groupDtos)
|
||||
.OrderByDescending(x => x.DateTime)
|
||||
.ToList();
|
||||
}
|
||||
#endregion
|
||||
#region 删除单个会话
|
||||
@@ -81,6 +78,7 @@ namespace IM_API.Services
|
||||
|
||||
|
||||
#endregion
|
||||
#region 获取用户所有统一聊天凭证
|
||||
public async Task<List<string>> GetUserAllStreamKeyAsync(int userId)
|
||||
{
|
||||
return await _context.Conversations.Where(x => x.UserId == userId)
|
||||
@@ -88,5 +86,37 @@ namespace IM_API.Services
|
||||
.Distinct()
|
||||
.ToListAsync();
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 获取单个会话信息
|
||||
public async Task<ConversationDto> GetConversationByIdAsync(int userId, int conversationId)
|
||||
{
|
||||
var conversation = await _context.Conversations
|
||||
.Include(x => x.LastReadMessage)
|
||||
.FirstOrDefaultAsync(
|
||||
x => x.UserId == userId && x.Id == conversationId
|
||||
);
|
||||
if (conversation is null) throw new BaseException(CodeDefine.CONVERSATION_NOT_FOUND);
|
||||
var dto = _mapper.Map<ConversationDto>(conversation);
|
||||
//dto.LastReadMessage = _mapper.Map<MessageBaseDto>(conversation);
|
||||
if(conversation.ChatType == (int)ChatType.PRIVATE)
|
||||
{
|
||||
var friendInfo = await _context.Friends.FirstOrDefaultAsync(
|
||||
x => x.UserId == userId && x.FriendId == conversation.TargetId
|
||||
);
|
||||
if (friendInfo is null) throw new BaseException(CodeDefine.FRIEND_RELATION_NOT_FOUND);
|
||||
_mapper.Map(friendInfo,dto);
|
||||
}
|
||||
if(conversation.ChatType == (int)ChatType.GROUP)
|
||||
{
|
||||
var groupInfo = await _context.Groups.FirstOrDefaultAsync(
|
||||
x => x.Id == conversation.TargetId
|
||||
);
|
||||
if (groupInfo is null) throw new BaseException(CodeDefine.GROUP_NOT_FOUND);
|
||||
_mapper.Map(groupInfo, dto);
|
||||
}
|
||||
return dto;
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -20,14 +20,38 @@ namespace IM_API.Services
|
||||
_mapper = mapper;
|
||||
}
|
||||
|
||||
public Task<List<MessageBaseDto>> GetGroupMessagesAsync(int groupId, int page, int pageSize, bool desc)
|
||||
public async Task<List<MessageBaseDto>> GetMessagesAsync(int userId, int conversationId, int? msgId, int? pageSize, bool desc)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public Task<List<MessageBaseDto>> GetPrivateMessagesAsync(int userAId, int userBId, int page, int pageSize, bool desc)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
//获取会话信息,用于获取双方聊天的唯一标识streamkey
|
||||
Conversation? conversation = await _context.Conversations.FirstOrDefaultAsync(
|
||||
x => x.Id == conversationId && x.UserId == userId
|
||||
);
|
||||
if (conversation is null) throw new BaseException(CodeDefine.CONVERSATION_NOT_FOUND);
|
||||
var query = _context.Messages.AsQueryable();
|
||||
if(msgId != null)
|
||||
{
|
||||
query = query.Where(
|
||||
x => x.StreamKey == conversation.StreamKey && x.Id < msgId.Value
|
||||
)
|
||||
.OrderByDescending(x => x.Id);
|
||||
}
|
||||
else
|
||||
{
|
||||
query = query.Where(
|
||||
x => x.StreamKey == conversation.StreamKey && x.Id > conversation.LastReadMessageId
|
||||
);
|
||||
}
|
||||
if(pageSize != null)
|
||||
{
|
||||
query = query.Take(pageSize.Value);
|
||||
}
|
||||
var msgList = await query
|
||||
.ToListAsync();
|
||||
msgList = msgList
|
||||
.OrderBy(x => x.Created)
|
||||
.ThenBy(t => t.Id)
|
||||
.ToList();
|
||||
return _mapper.Map<List<MessageBaseDto>>(msgList);
|
||||
}
|
||||
|
||||
public Task<int> GetUnreadCountAsync(int userId)
|
||||
|
||||
Reference in New Issue
Block a user