mirror of
https://github.com/Govor-team/Govor.git
synced 2026-11-18 05:15:06 +00:00
Add primitive groups and required channel
Adds server-side group and channel management, private invitations, member moderation, avatar handling, and required-channel configuration. This change also introduces SignalR group profile/member notifications, updates registration to atomically join required channels, and adds EF migration and test coverage for group membership, permissions, and invitation expiry rules.
This commit is contained in:
@@ -2,6 +2,7 @@ using Govor.Application.Messages.Parameters;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Govor.Domain;
|
||||
using Govor.Domain.Models.Messages;
|
||||
using Govor.Application.Groups;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
@@ -24,6 +25,25 @@ public class MessageEditingService : IMessageEditingService
|
||||
}
|
||||
|
||||
public async Task<EditMessageResult> EditMessageAsync(EditMessage editParams)
|
||||
{
|
||||
var target = await _dbContext.Messages.AsNoTracking().FirstOrDefaultAsync(m => m.Id == editParams.MessageId);
|
||||
if (target?.RecipientType != RecipientType.Group) return await EditCoreAsync(editParams);
|
||||
return await _dbContext.Database.CreateExecutionStrategy().ExecuteAsync(async () =>
|
||||
{
|
||||
await using var transaction = await _dbContext.Database.BeginTransactionAsync();
|
||||
try
|
||||
{
|
||||
await _dbContext.ChatGroups.Where(g => g.Id == target.RecipientId)
|
||||
.ExecuteUpdateAsync(s => s.SetProperty(g => g.Name, g => g.Name));
|
||||
var result = await EditCoreAsync(editParams);
|
||||
if (result.IsSuccess) await transaction.CommitAsync();
|
||||
return result;
|
||||
}
|
||||
catch { _dbContext.ChangeTracker.Clear(); throw; }
|
||||
});
|
||||
}
|
||||
|
||||
private async Task<EditMessageResult> EditCoreAsync(EditMessage editParams)
|
||||
{
|
||||
var message = await _dbContext.Messages
|
||||
.Include(m => m.MediaAttachments)
|
||||
@@ -38,7 +58,10 @@ public class MessageEditingService : IMessageEditingService
|
||||
}
|
||||
|
||||
if (message.SenderId != editParams.EditorId || !await _dbContext.HasChatAccessAsync(
|
||||
editParams.EditorId, message.RecipientId, message.RecipientType))
|
||||
editParams.EditorId, message.RecipientId, message.RecipientType) ||
|
||||
(message.RecipientType == RecipientType.Group &&
|
||||
await _dbContext.ChatGroups.AnyAsync(g => g.Id == message.RecipientId && g.IsChannel) &&
|
||||
!await _dbContext.IsGroupAdministratorAsync(message.RecipientId, editParams.EditorId)))
|
||||
{
|
||||
_logger.LogWarning(
|
||||
"User {EditorId} unauthorized to edit message {MessageId}",
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
using DeleteMessage = Govor.Application.Messages.Parameters.DeleteMessage;
|
||||
using Govor.Domain;
|
||||
using Govor.Domain.Common;
|
||||
using Govor.Application.Groups;
|
||||
using Govor.Domain.Models.Messages;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Logging;
|
||||
@@ -20,6 +21,25 @@ public class MessageRemovingService : IMessageRemovingService
|
||||
}
|
||||
|
||||
public async Task<Result<Message, Error>> DeleteMessageAsync(DeleteMessage deleteParams)
|
||||
{
|
||||
var target = await _govorDbContext.Messages.AsNoTracking().FirstOrDefaultAsync(m => m.Id == deleteParams.MessageId);
|
||||
if (target?.RecipientType != RecipientType.Group) return await DeleteCoreAsync(deleteParams);
|
||||
return await _govorDbContext.Database.CreateExecutionStrategy().ExecuteAsync(async () =>
|
||||
{
|
||||
await using var transaction = await _govorDbContext.Database.BeginTransactionAsync();
|
||||
try
|
||||
{
|
||||
await _govorDbContext.ChatGroups.Where(g => g.Id == target.RecipientId)
|
||||
.ExecuteUpdateAsync(s => s.SetProperty(g => g.Name, g => g.Name));
|
||||
var result = await DeleteCoreAsync(deleteParams);
|
||||
if (result.IsSuccess) await transaction.CommitAsync();
|
||||
return result;
|
||||
}
|
||||
catch { _govorDbContext.ChangeTracker.Clear(); throw; }
|
||||
});
|
||||
}
|
||||
|
||||
private async Task<Result<Message, Error>> DeleteCoreAsync(DeleteMessage deleteParams)
|
||||
{
|
||||
var message = await _govorDbContext.Messages.FirstOrDefaultAsync(m => m.Id == deleteParams.MessageId);
|
||||
if (message is null)
|
||||
@@ -36,8 +56,7 @@ public class MessageRemovingService : IMessageRemovingService
|
||||
|
||||
var canDelete = message.SenderId == deleteParams.DeleterId ||
|
||||
(message.RecipientType == RecipientType.Group &&
|
||||
await _govorDbContext.GroupAdmins.AnyAsync(a =>
|
||||
a.GroupId == message.RecipientId && a.UserId == deleteParams.DeleterId));
|
||||
await _govorDbContext.IsGroupAdministratorAsync(message.RecipientId, deleteParams.DeleterId));
|
||||
if (!canDelete)
|
||||
return Result.Failure<Message>(Error.Forbidden("Message.Remove.AccessDenied",
|
||||
"Only the author or a group administrator can delete this message."));
|
||||
|
||||
@@ -3,6 +3,7 @@ using Microsoft.EntityFrameworkCore;
|
||||
using Govor.Domain;
|
||||
using Govor.Domain.Models.Messages;
|
||||
using Govor.Domain.Models;
|
||||
using Govor.Application.Groups;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace Govor.Application.Messages;
|
||||
@@ -19,6 +20,30 @@ public class MessageSendingService : IMessageSendingService
|
||||
}
|
||||
|
||||
public async Task<SendMessageResult> SendMessageAsync(SendMessage sendParams)
|
||||
{
|
||||
var operationId = Guid.NewGuid();
|
||||
if (sendParams.RecipientType != RecipientType.Group)
|
||||
return await SendCoreAsync(sendParams, operationId);
|
||||
return await _dbContext.Database.CreateExecutionStrategy().ExecuteAsync(async () =>
|
||||
{
|
||||
await using var transaction = await _dbContext.Database.BeginTransactionAsync();
|
||||
try
|
||||
{
|
||||
// Same group lock as moderation: bans/demotion cannot race a channel post.
|
||||
await _dbContext.ChatGroups.Where(g => g.Id == sendParams.RecipientId)
|
||||
.ExecuteUpdateAsync(s => s.SetProperty(g => g.Name, g => g.Name));
|
||||
var completed = await _dbContext.Messages.AsNoTracking().Include(m => m.MediaAttachments)
|
||||
.FirstOrDefaultAsync(m => m.Id == operationId);
|
||||
if (completed is not null) return new SendMessageResult(true, null, completed);
|
||||
var result = await SendCoreAsync(sendParams, operationId);
|
||||
if (result.IsSuccess) await transaction.CommitAsync();
|
||||
return result;
|
||||
}
|
||||
catch { _dbContext.ChangeTracker.Clear(); throw; }
|
||||
});
|
||||
}
|
||||
|
||||
private async Task<SendMessageResult> SendCoreAsync(SendMessage sendParams, Guid operationId)
|
||||
{
|
||||
var media = sendParams.Media?.ToArray() ?? [];
|
||||
if ((string.IsNullOrWhiteSpace(sendParams.EncryptContent) && media.Length == 0) ||
|
||||
@@ -49,7 +74,7 @@ public class MessageSendingService : IMessageSendingService
|
||||
m.RecipientType == sendParams.RecipientType))
|
||||
return new SendMessageResult(false, new ArgumentException("Reply must belong to the same chat."), default);
|
||||
|
||||
var messageId = Guid.NewGuid();
|
||||
var messageId = operationId;
|
||||
var message = new Message
|
||||
{
|
||||
Id = messageId,
|
||||
@@ -98,8 +123,7 @@ public class MessageSendingService : IMessageSendingService
|
||||
|
||||
var isMember = await _dbContext.GroupMemberships.AnyAsync(gm => gm.UserId == userId && gm.GroupId == groupId && !gm.IsBanned);
|
||||
if (!isMember) return (false, "Sender is not a member of the group.");
|
||||
if (group.IsChannel && !await _dbContext.GroupAdmins.AnyAsync(a =>
|
||||
a.GroupId == groupId && a.UserId == userId))
|
||||
if (group.IsChannel && !await _dbContext.IsGroupAdministratorAsync(groupId, userId))
|
||||
return (false, "Only channel administrators can send messages.");
|
||||
|
||||
return (true, null);
|
||||
|
||||
Reference in New Issue
Block a user