mirror of
https://github.com/Govor-team/Govor.git
synced 2026-11-16 20:56:03 +00:00
29cc51ee0c
Implements message reactions and reaction packs (domain models, services, admin HTTP API, media processing via ImageSharp and limits) and adds reliable push delivery (ChatPushNotification, ChatPushDispatcher, ChatPushWorker, improved ChatNotificationService). Enhances message reading (ReadChat API, unread counts, concurrency-safe receipts) and tightens session security (access-token session validation in JWT, hub filter, SessionAuthorization). Improves SignalR behavior (session groups, reaction/channel events, hub/error mappings). Adds EF migrations, many controller fixes (Forbid handling, password API), tests (Govor.Server.Tests), docs, CI and tooling updates (Dockerfile → .NET 10, GH Actions matrix for sqlite/postgres, Govor.Server.slnx, global.json), and small infra changes (AutoMapper bump, NullPushProvider, storage/token handling).
107 lines
3.7 KiB
C#
107 lines
3.7 KiB
C#
using Govor.Application.Messages.Parameters;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Govor.Domain;
|
|
using Govor.Domain.Models.Messages;
|
|
using Microsoft.Extensions.Logging;
|
|
using Microsoft.Extensions.Options;
|
|
|
|
namespace Govor.Application.Messages;
|
|
|
|
public class MessageEditingService : IMessageEditingService
|
|
{
|
|
private readonly GovorDbContext _dbContext;
|
|
private readonly ILogger<MessageEditingService> _logger;
|
|
private readonly MessageEditingOptions _options;
|
|
|
|
public MessageEditingService(
|
|
GovorDbContext dbContext,
|
|
ILogger<MessageEditingService> logger,
|
|
IOptions<MessageEditingOptions> options)
|
|
{
|
|
_dbContext = dbContext;
|
|
_logger = logger;
|
|
_options = options.Value;
|
|
}
|
|
|
|
public async Task<EditMessageResult> EditMessageAsync(EditMessage editParams)
|
|
{
|
|
var message = await _dbContext.Messages
|
|
.Include(m => m.MediaAttachments)
|
|
.FirstOrDefaultAsync(m => m.Id == editParams.MessageId);
|
|
|
|
if (message == null)
|
|
{
|
|
return new EditMessageResult(
|
|
false,
|
|
new KeyNotFoundException("Message not found."),
|
|
null);
|
|
}
|
|
|
|
if (message.SenderId != editParams.EditorId || !await _dbContext.HasChatAccessAsync(
|
|
editParams.EditorId, message.RecipientId, message.RecipientType))
|
|
{
|
|
_logger.LogWarning(
|
|
"User {EditorId} unauthorized to edit message {MessageId}",
|
|
editParams.EditorId,
|
|
editParams.MessageId);
|
|
|
|
return new EditMessageResult(
|
|
false,
|
|
new UnauthorizedAccessException(
|
|
"User is not authorized to edit this message."),
|
|
null);
|
|
}
|
|
|
|
// Проверяем время, прошедшее с момента отправки
|
|
var now = editParams.EditedAt;
|
|
var editDeadline = message.SentAt.AddMinutes(
|
|
_options.MaxEditTimeMinutes);
|
|
|
|
if (now > editDeadline && _options.Enabled)
|
|
{
|
|
_logger.LogWarning(
|
|
"Message {MessageId} cannot be edited. " +
|
|
"Edit time limit of {MaxEditTimeMinutes} minutes has expired.",
|
|
message.Id,
|
|
_options.MaxEditTimeMinutes);
|
|
|
|
return new EditMessageResult(
|
|
false,
|
|
new InvalidOperationException(
|
|
$"Message can only be edited within " +
|
|
$"{_options.MaxEditTimeMinutes} minutes after sending."),
|
|
null);
|
|
}
|
|
|
|
var originalMessageSnapshot = new Message
|
|
{
|
|
Id = message.Id,
|
|
SenderId = message.SenderId,
|
|
RecipientId = message.RecipientId,
|
|
RecipientType = message.RecipientType,
|
|
SentAt = message.SentAt,
|
|
ReplyToMessageId = message.ReplyToMessageId,
|
|
MediaAttachments = message.MediaAttachments?.ToList() ?? []
|
|
};
|
|
|
|
if (editParams.NewContent is null || editParams.NewContent.Length > 50_000 ||
|
|
(string.IsNullOrWhiteSpace(editParams.NewContent) && message.MediaAttachments.Count == 0))
|
|
return new EditMessageResult(false, new ArgumentException("Invalid message content."), null);
|
|
|
|
message.EncryptedContent = editParams.NewContent;
|
|
message.IsEdited = true;
|
|
message.EditedAt = editParams.EditedAt;
|
|
|
|
await _dbContext.SaveChangesAsync();
|
|
|
|
_logger.LogInformation(
|
|
"Message {MessageId} edited successfully.",
|
|
editParams.MessageId);
|
|
|
|
return new EditMessageResult(
|
|
true,
|
|
null,
|
|
originalMessageSnapshot);
|
|
}
|
|
}
|