120 lines
3.7 KiB
C#
120 lines
3.7 KiB
C#
|
|
#nullable enable
|
|||
|
|
|
|||
|
|
using System;
|
|||
|
|
using System.Linq;
|
|||
|
|
using System.Threading.Tasks;
|
|||
|
|
using Microsoft.AspNetCore.Authorization;
|
|||
|
|
using Microsoft.AspNetCore.SignalR;
|
|||
|
|
using Volo.Abp.Domain.Repositories;
|
|||
|
|
using Volo.Abp.Guids;
|
|||
|
|
using Volo.Abp.Identity;
|
|||
|
|
using Volo.Abp.MultiTenancy;
|
|||
|
|
using Volo.Abp.Users;
|
|||
|
|
|
|||
|
|
namespace Sozsoft.Platform.Messenger;
|
|||
|
|
|
|||
|
|
[Authorize]
|
|||
|
|
public class MessengerHub : Hub
|
|||
|
|
{
|
|||
|
|
private readonly ICurrentTenant _currentTenant;
|
|||
|
|
private readonly ICurrentUser _currentUser;
|
|||
|
|
private readonly IGuidGenerator _guidGenerator;
|
|||
|
|
private readonly IRepository<IdentityUser, Guid> _userRepository;
|
|||
|
|
|
|||
|
|
public MessengerHub(
|
|||
|
|
ICurrentTenant currentTenant,
|
|||
|
|
ICurrentUser currentUser,
|
|||
|
|
IGuidGenerator guidGenerator,
|
|||
|
|
IRepository<IdentityUser, Guid> userRepository)
|
|||
|
|
{
|
|||
|
|
_currentTenant = currentTenant;
|
|||
|
|
_currentUser = currentUser;
|
|||
|
|
_guidGenerator = guidGenerator;
|
|||
|
|
_userRepository = userRepository;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public override async Task OnConnectedAsync()
|
|||
|
|
{
|
|||
|
|
if (_currentUser.Id.HasValue)
|
|||
|
|
{
|
|||
|
|
await Groups.AddToGroupAsync(Context.ConnectionId, UserGroupName(_currentUser.Id.Value));
|
|||
|
|
await Groups.AddToGroupAsync(Context.ConnectionId, TenantGroupName());
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
await base.OnConnectedAsync();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public override async Task OnDisconnectedAsync(Exception? exception)
|
|||
|
|
{
|
|||
|
|
if (_currentUser.Id.HasValue)
|
|||
|
|
{
|
|||
|
|
await Groups.RemoveFromGroupAsync(Context.ConnectionId, UserGroupName(_currentUser.Id.Value));
|
|||
|
|
await Groups.RemoveFromGroupAsync(Context.ConnectionId, TenantGroupName());
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
await base.OnDisconnectedAsync(exception);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public async Task SendMessage(MessengerSendMessageDto input)
|
|||
|
|
{
|
|||
|
|
if (!_currentUser.Id.HasValue)
|
|||
|
|
{
|
|||
|
|
throw new HubException("Kullanıcı oturumu bulunamadı.");
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
var recipientIds = input.RecipientIds
|
|||
|
|
.Where(id => id != Guid.Empty)
|
|||
|
|
.Distinct()
|
|||
|
|
.ToList();
|
|||
|
|
|
|||
|
|
if (recipientIds.Count == 0)
|
|||
|
|
{
|
|||
|
|
throw new HubException("En az bir alıcı seçilmelidir.");
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (input.Text.IsNullOrWhiteSpace() && input.Attachments.Count == 0)
|
|||
|
|
{
|
|||
|
|
throw new HubException("Mesaj veya dosya gönderilmelidir.");
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
var tenantId = _currentTenant.Id;
|
|||
|
|
var validRecipients = await _userRepository.GetListAsync(user =>
|
|||
|
|
recipientIds.Contains(user.Id) &&
|
|||
|
|
user.TenantId == tenantId &&
|
|||
|
|
user.IsActive);
|
|||
|
|
|
|||
|
|
if (validRecipients.Count == 0)
|
|||
|
|
{
|
|||
|
|
throw new HubException("Bu tenant içinde geçerli alıcı bulunamadı.");
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
var senderName = _currentUser.Name ?? _currentUser.UserName ?? "Kullanıcı";
|
|||
|
|
var message = new MessengerMessageDto
|
|||
|
|
{
|
|||
|
|
Id = _guidGenerator.Create(),
|
|||
|
|
TenantId = tenantId,
|
|||
|
|
SenderId = _currentUser.Id.Value,
|
|||
|
|
SenderUserName = _currentUser.UserName ?? string.Empty,
|
|||
|
|
SenderName = senderName,
|
|||
|
|
RecipientIds = validRecipients.Select(user => user.Id).ToList(),
|
|||
|
|
Text = input.Text?.Trim(),
|
|||
|
|
Attachments = input.Attachments,
|
|||
|
|
SentAt = DateTime.UtcNow
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
var targetGroups = message.RecipientIds
|
|||
|
|
.Append(message.SenderId)
|
|||
|
|
.Distinct()
|
|||
|
|
.Select(UserGroupName)
|
|||
|
|
.ToList();
|
|||
|
|
|
|||
|
|
await Clients.Groups(targetGroups).SendAsync("MessengerMessageReceived", message);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private string TenantKey => _currentTenant.Id?.ToString("N") ?? "host";
|
|||
|
|
|
|||
|
|
private string TenantGroupName() => $"messenger:tenant:{TenantKey}";
|
|||
|
|
|
|||
|
|
private string UserGroupName(Guid userId) => $"messenger:user:{TenantKey}:{userId:N}";
|
|||
|
|
}
|