fix: align backend APIs and upload flow

This commit is contained in:
2026-09-15 14:10:55 +08:00
parent 32177a7293
commit 53e6195938
149 changed files with 4791 additions and 435 deletions
@@ -0,0 +1,18 @@
using GroupService.Domain.Events;
using IM.Commons.IntegrationEvents;
using MassTransit;
using MediatR;
namespace GroupService.WebApi.Application.EventHandler
{
public class GroupMemberLeftHandler(IPublishEndpoint endpoint)
: INotificationHandler<GroupMemberLeftDomainEvent>
{
public Task Handle(GroupMemberLeftDomainEvent notification, CancellationToken cancellationToken)
{
return endpoint.Publish(
new GroupMemberLeftEvent(notification.Member.UserId, notification.Member.GroupId),
cancellationToken);
}
}
}
@@ -1,7 +1,8 @@
using AutoMapper;
using AutoMapper;
using GroupService.Domain.IReposities;
using GroupService.WebApi.Application.Dtos;
using IM.Commons;
using Microsoft.EntityFrameworkCore;
namespace GroupService.WebApi.Application.Group
{
@@ -9,29 +10,33 @@ namespace GroupService.WebApi.Application.Group
{
private readonly IGroupReposity reposity;
private readonly IGroupMemberReposity memberReposity;
private readonly IMapper mapper;
private readonly IMapper mapper; private readonly IM.InitCommon.Management.RuntimePolicy runtime; private readonly global::GroupService.Infrastructure.GroupDbContext db;
public GroupService(IGroupReposity reposity, IGroupMemberReposity memberReposity, IMapper mapper)
public GroupService(IGroupReposity reposity, IGroupMemberReposity memberReposity, IMapper mapper, IM.InitCommon.Management.RuntimePolicy runtime, global::GroupService.Infrastructure.GroupDbContext db)
{
this.reposity = reposity;
this.memberReposity = memberReposity;
this.mapper = mapper;
this.mapper = mapper; this.runtime = runtime; this.db = db;
}
public async Task<Result<GroupResponse>> CreateAsync(GroupCreateCommand command)
{
var policy = runtime.Current;
if (policy.CreatedGroupLimit > 0 && await db.Groups.CountAsync(x => x.GroupMaster == command.GroupMasterId) >= policy.CreatedGroupLimit)
return Result.Fail<GroupResponse>(ResultCode.PERMISSION_DENIED, "创建群组数量已达到平台上限");
var group = new Domain.Entities.Group(command.GroupMasterId, command.Name);
group.Update(null, (Domain.Enums.GroupAuthorityType)policy.DefaultJoinAuthority, null, null);
reposity.Create(group);
return Result<GroupResponse>.Success(mapper.Map<GroupResponse>(group));
}
public async Task<Result<List<GroupResponse>>> GetAllAsync(Guid userId)
{
var groups = await reposity.FindByMasterIdAsync(userId);
var groups = await reposity.FindByMemberIdAsync(userId);
return Result<List<GroupResponse>>.Success(mapper.Map<List<GroupResponse>>(groups));
}
public async Task<Result<GroupResponse>> GetByIdAsync(Guid groupId)
public async Task<Result<GroupResponse>> GetByIdAsync(Guid groupId, Guid userId)
{
var group = await reposity.FindByIdAsync(groupId);
if (group is null)
@@ -39,9 +44,36 @@ namespace GroupService.WebApi.Application.Group
return Result<GroupResponse>.Fail(ResultCode.GROUP_NOT_FOUND);
}
if (!await memberReposity.CheckMemberExistAsync(groupId, userId))
{
return Result<GroupResponse>.Fail(ResultCode.PERMISSION_DENIED);
}
return Result<GroupResponse>.Success(mapper.Map<GroupResponse>(group));
}
public async Task<Result<object>> DissolveAsync(Guid groupId, Guid userId)
{
var group = await reposity.FindByIdAsync(groupId);
if (group is null)
{
return Result.Fail(ResultCode.GROUP_NOT_FOUND);
}
if (group.GroupMaster != userId)
{
return Result.Fail(ResultCode.PERMISSION_DENIED);
}
var members = await memberReposity.FindByGroupIdAsync(groupId);
foreach (var member in members)
{
member.Leave();
}
group.SoftDelete();
return Result.Success();
}
public async Task<Result<GroupResponse>> UpdateAsync(GroupUpdateCommand command)
{
var group = await reposity.FindByIdAsync(command.GroupId);
@@ -1,4 +1,4 @@
using AutoMapper;
using AutoMapper;
using GroupService.Domain.IReposities;
using GroupService.Domain.ValueObjects;
using GroupService.WebApi.Application.Dtos;
@@ -46,7 +46,7 @@ namespace GroupService.WebApi.Application.GroupInvitation
return Result.Success(mapper.Map<GroupInvitationResponse>(existing));
}
var group = await groupReposity.FindByIdAsync(groupId);
var group = await groupReposity.FindByIdAsync(groupId); if (group?.Status != Domain.Enums.GroupState.Normal) throw new IM.DomainCommons.DomainException("群组不可用或已被封禁");
var userProfile = new UserProfile()
{
@@ -75,7 +75,7 @@ namespace GroupService.WebApi.Application.GroupInvitation
public async Task<Result<object>> CreateBatchAsync(Guid operatorId, List<Guid> userIds, Guid groupId)
{
var group = await groupReposity.FindByIdAsync(groupId);
var group = await groupReposity.FindByIdAsync(groupId); if (group?.Status != Domain.Enums.GroupState.Normal) throw new IM.DomainCommons.DomainException("群组不可用或已被封禁");
if (group is null)
return Result.Fail(ResultCode.GROUP_NOT_FOUND);
@@ -1,4 +1,4 @@
using AutoMapper;
using AutoMapper;
using GroupService.Domain;
using GroupService.Domain.IReposities;
using GroupService.WebApi.Application.Dtos;
@@ -13,24 +13,28 @@ namespace GroupService.WebApi.Application.GroupMember
private readonly IGroupReposity groupReposity;
private readonly GroupMemberDomainService service;
private readonly IIdentityIntegrationService idService;
private IMapper mapper;
private IMapper mapper; private readonly IM.InitCommon.Management.RuntimePolicy runtime;
public GroupMemberService(IGroupMemberReposity reposity, IGroupReposity groupReposity, GroupMemberDomainService service, IIdentityIntegrationService idService, IMapper mapper)
public GroupMemberService(IGroupMemberReposity reposity, IGroupReposity groupReposity, GroupMemberDomainService service, IIdentityIntegrationService idService, IMapper mapper, IM.InitCommon.Management.RuntimePolicy runtime)
{
this.reposity = reposity;
this.groupReposity = groupReposity;
this.service = service;
this.idService = idService;
this.mapper = mapper;
this.mapper = mapper; this.runtime = runtime;
}
public async Task<Result<List<GroupMemberResponse>>> GetByGroupIdAsync(Guid groupId)
public async Task<Result<List<GroupMemberResponse>>> GetByGroupIdAsync(Guid groupId, Guid userId)
{
var group = await groupReposity.FindByIdAsync(groupId);
if (group is null)
{
return Result<List<GroupMemberResponse>>.Fail(ResultCode.GROUP_NOT_FOUND);
}
if (!await reposity.CheckMemberExistAsync(groupId, userId))
{
return Result<List<GroupMemberResponse>>.Fail(ResultCode.PERMISSION_DENIED);
}
var members = await reposity.FindByGroupIdAsync(groupId);
return Result<List<GroupMemberResponse>>.Success(mapper.Map<List<GroupMemberResponse>>(members.ToList()));
@@ -45,6 +49,9 @@ namespace GroupService.WebApi.Application.GroupMember
return Result<GroupMemberResponse>.Fail(ResultCode.GROUP_NOT_FOUND);
}
if (group.Status != Domain.Enums.GroupState.Normal) throw new IM.DomainCommons.DomainException("群组已被封禁,不能加入");
var limit = runtime.Current.GroupMemberLimit;
if (limit > 0 && (await reposity.FindByGroupIdAsync(groupId)).Count() >= limit) throw new IM.DomainCommons.DomainException("群成员数量已达到平台上限");
var userRes = await idService.FindUserByIdAsync(userId);
if (!userRes.Succeeded)
{
@@ -64,7 +71,7 @@ namespace GroupService.WebApi.Application.GroupMember
public async Task<Result<bool>> CheckMemberAsync(Guid groupId, Guid userId)
{
var exist = await reposity.CheckMemberExistAsync(groupId, userId);
var group = await groupReposity.FindByIdAsync(groupId); var exist = group?.Status == Domain.Enums.GroupState.Normal && await reposity.CheckMemberExistAsync(groupId, userId);
return Result.Success(exist);
}
@@ -78,14 +85,32 @@ namespace GroupService.WebApi.Application.GroupMember
}
var operatorMember = await reposity.FindOneByGroupIdAndUserIdAsync(member.GroupId, operatorId);
if (operatorMember is null || operatorMember.Role == Domain.Enums.GroupMemberRole.Normal)
if (operatorMember is null || operatorMember.Id == member.Id ||
member.Role == Domain.Enums.GroupMemberRole.Master ||
operatorMember.Role <= member.Role)
{
return Result.Fail(ResultCode.PERMISSION_DENIED);
}
member.SoftDelete();
member.Leave();
return Result.Success();
}
public async Task<Result<object>> LeaveAsync(Guid groupId, Guid userId)
{
var member = await reposity.FindOneByGroupIdAndUserIdAsync(groupId, userId);
if (member is null)
{
return Result.Fail(ResultCode.GROUP_MEMBER_NOT_FOUNT);
}
if (member.Role == Domain.Enums.GroupMemberRole.Master)
{
return Result.Fail<object>(ResultCode.PERMISSION_DENIED, "群主不能直接退群,请使用解散群接口");
}
member.Leave();
return Result.Success();
}
}
}
@@ -1,4 +1,4 @@
using AutoMapper;
using AutoMapper;
using GroupService.Domain.Entities;
using GroupService.Domain.IReposities;
using GroupService.Domain.ValueObjects;
@@ -33,6 +33,8 @@ namespace GroupService.WebApi.Application.GroupRequest
return Result.Fail<GroupRequestResponse>(ResultCode.GROUP_NOT_FOUND);
}
if (group.Status != Domain.Enums.GroupState.Normal || group.Authority == Domain.Enums.GroupAuthorityType.NOT_ALLOWED_TO_JOIN)
return Result.Fail<GroupRequestResponse>(ResultCode.PERMISSION_DENIED, "群组当前不允许加入");
var user = await idService.FindUserByIdAsync(userId);
var groupProfile = new GroupProfile()
@@ -98,7 +100,7 @@ namespace GroupService.WebApi.Application.GroupRequest
}
public async Task<Result<List<GroupRequestResponse>>> GetListAsync(Guid userId)
{
var list = await reposity.FindByUserIdAsync(userId);
var list = await reposity.FindVisibleToUserAsync(userId);
return Result.Success(mapper.Map<List<GroupRequestResponse>>(list.ToList()));
}