Merge branch 'feature-nxdev' of https://gitea.nxsir.cn/code/IM into feature-nxdev
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
using AutoMapper;
|
||||
using IM_API.Dtos;
|
||||
using IM_API.Dtos.Auth;
|
||||
using IM_API.Dtos.User;
|
||||
using IM_API.Exceptions;
|
||||
using IM_API.Interface.Services;
|
||||
using IM_API.Models;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
using AutoMapper;
|
||||
using IM_API.Dtos;
|
||||
using IM_API.Dtos.Conversation;
|
||||
using IM_API.Exceptions;
|
||||
using IM_API.Interface.Services;
|
||||
using IM_API.Models;
|
||||
@@ -134,5 +134,27 @@ namespace IM_API.Services
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
public async Task MakeConversationAsync(int userAId, int userBId, ChatType chatType)
|
||||
{
|
||||
var userAcExist = await _context.Conversations.AnyAsync(x => x.UserId == userAId && x.TargetId == userBId);
|
||||
if (userAcExist) return;
|
||||
var streamKey = chatType == ChatType.PRIVATE ?
|
||||
StreamKeyBuilder.Private(userAId, userBId) : StreamKeyBuilder.Group(userBId);
|
||||
var conversation = new Conversation()
|
||||
{
|
||||
ChatType = (int)chatType,
|
||||
LastMessage = "",
|
||||
LastMessageTime = DateTime.UtcNow,
|
||||
LastReadMessageId = -1,
|
||||
StreamKey = streamKey,
|
||||
TargetId = userBId,
|
||||
UnreadCount = 0,
|
||||
UserId = userAId
|
||||
|
||||
};
|
||||
_context.Conversations.Add(conversation);
|
||||
await _context.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
using AutoMapper;
|
||||
using IM_API.Domain.Events;
|
||||
using IM_API.Dtos;
|
||||
using IM_API.Dtos.Friend;
|
||||
using IM_API.Exceptions;
|
||||
using IM_API.Interface.Services;
|
||||
using IM_API.Models;
|
||||
@@ -117,15 +117,12 @@ namespace IM_API.Services
|
||||
var friend = await _context.Friends.FirstOrDefaultAsync(
|
||||
x => x.UserId == friendRequest.RequestUser && x.FriendId == friendRequest.ResponseUser
|
||||
);
|
||||
if (friend is null) throw new BaseException(CodeDefine.FRIEND_RELATION_NOT_FOUND);
|
||||
|
||||
if (friend.StatusEnum != FriendStatus.Pending) throw new BaseException(CodeDefine.FRIEND_REQUEST_EXISTS);
|
||||
if (friend != null) throw new BaseException(CodeDefine.ALREADY_FRIENDS);
|
||||
//处理好友请求操作
|
||||
switch (requestDto.Action)
|
||||
{
|
||||
//拒绝后标记
|
||||
case HandleFriendRequestAction.Reject:
|
||||
friend.StatusEnum = FriendStatus.Declined;
|
||||
friendRequest.StateEnum = FriendRequestState.Declined;
|
||||
break;
|
||||
|
||||
@@ -141,6 +138,8 @@ namespace IM_API.Services
|
||||
OperatorId = friendRequest.ResponseUser,
|
||||
RequestInfo = _mapper.Map<FriendRequestDto>(friendRequest),
|
||||
requestUserRemarkname = requestDto.RemarkName,
|
||||
RequestUserId = friendRequest.RequestUser,
|
||||
ResponseUserId = friendRequest.ResponseUser
|
||||
|
||||
});
|
||||
break;
|
||||
|
||||
@@ -0,0 +1,110 @@
|
||||
using AutoMapper;
|
||||
using IM_API.Domain.Events;
|
||||
using IM_API.Dtos.Group;
|
||||
using IM_API.Exceptions;
|
||||
using IM_API.Interface.Services;
|
||||
using IM_API.Models;
|
||||
using IM_API.Tools;
|
||||
using MassTransit;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
|
||||
namespace IM_API.Services
|
||||
{
|
||||
public class GroupService : IGroupService
|
||||
{
|
||||
private readonly ImContext _context;
|
||||
private readonly IMapper _mapper;
|
||||
private readonly ILogger<GroupService> _logger;
|
||||
private readonly IPublishEndpoint _endPoint;
|
||||
public GroupService(ImContext context, IMapper mapper, ILogger<GroupService> logger, IPublishEndpoint publishEndpoint)
|
||||
{
|
||||
_context = context;
|
||||
_mapper = mapper;
|
||||
_logger = logger;
|
||||
_endPoint = publishEndpoint;
|
||||
}
|
||||
|
||||
private async Task<List<GroupInvite>> GetGroupInvites(int userId, int groupId, List<int> ids)
|
||||
{
|
||||
DateTime dateTime = DateTime.UtcNow;
|
||||
//验证被邀请用户是否为好友
|
||||
var validFriendIds = await _context.Friends
|
||||
.Where(f => f.UserId == userId && ids.Contains(f.FriendId))
|
||||
.Select(f => f.FriendId)
|
||||
.ToListAsync();
|
||||
//创建群成员对象
|
||||
return validFriendIds.Select(fid => new GroupInvite
|
||||
{
|
||||
Created = dateTime,
|
||||
GroupId = groupId,
|
||||
InvitedUser = fid,
|
||||
StateEnum = GroupInviteState.Pending,
|
||||
InviteUser = userId
|
||||
}).ToList();
|
||||
}
|
||||
|
||||
public async Task<GroupInfoDto> CreateGroup(int userId, GroupCreateDto groupCreateDto, List<int> userIds)
|
||||
{
|
||||
using var transaction = await _context.Database.BeginTransactionAsync();
|
||||
try
|
||||
{
|
||||
//先创建群
|
||||
DateTime dateTime = DateTime.UtcNow;
|
||||
Group group = _mapper.Map<Group>(groupCreateDto);
|
||||
group.GroupMaster = userId;
|
||||
_context.Groups.Add(group);
|
||||
await _context.SaveChangesAsync();
|
||||
var groupInvites = new List<GroupInvite>();
|
||||
if (userIds.Count > 0)
|
||||
{
|
||||
groupInvites = await GetGroupInvites(userId,group.Id, userIds);
|
||||
_context.GroupInvites.AddRange(groupInvites);
|
||||
}
|
||||
var groupMember = new GroupMember
|
||||
{
|
||||
UserId = userId,
|
||||
Created = dateTime,
|
||||
RoleEnum = GroupMemberRole.Master,
|
||||
GroupId = group.Id
|
||||
};
|
||||
_context.GroupMembers.Add(groupMember);
|
||||
await _context.SaveChangesAsync();
|
||||
await transaction.CommitAsync();
|
||||
await _endPoint.Publish(new GroupInviteEvent
|
||||
{
|
||||
AggregateId = userId.ToString(),
|
||||
GroupId = group.Id,
|
||||
EventId = Guid.NewGuid(),
|
||||
OccurredAt = dateTime,
|
||||
Ids = groupInvites.Select(x => x.Id).ToList(),
|
||||
OperatorId = userId,
|
||||
UserId = userId
|
||||
});
|
||||
return _mapper.Map<GroupInfoDto>(group);
|
||||
}
|
||||
catch
|
||||
{
|
||||
await transaction.RollbackAsync();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public Task DeleteGroup(int userId, int groupId)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public async Task InviteUsers(int userId, int groupId, List<int> userIds)
|
||||
{
|
||||
var group = await _context.Groups.FirstOrDefaultAsync(
|
||||
x => x.Id == groupId) ?? throw new BaseException(CodeDefine.GROUP_NOT_FOUND);
|
||||
|
||||
}
|
||||
|
||||
public Task JoinGroup(int userId, int groupId)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
using AutoMapper;
|
||||
using IM_API.Dtos;
|
||||
using IM_API.Dtos.User;
|
||||
using IM_API.Exceptions;
|
||||
using IM_API.Interface.Services;
|
||||
using IM_API.Models;
|
||||
|
||||
Reference in New Issue
Block a user