115 lines
4.7 KiB
C#
115 lines
4.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Oqtane.Enums;
|
|
using Oqtane.Infrastructure;
|
|
using Oqtane.Models;
|
|
using Oqtane.Repository;
|
|
using Oqtane.Security;
|
|
using Oqtane.Shared;
|
|
using SZUAbsolventenverein.Module.PremiumArea.Models;
|
|
|
|
namespace SZUAbsolventenverein.Module.PremiumArea.Services
|
|
{
|
|
public class ServerUserContactService : IUserContactService
|
|
{
|
|
private readonly IUserRepository _userRepository;
|
|
private readonly INotificationRepository _notificationRepository;
|
|
private readonly IUserPermissions _userPermissions;
|
|
private readonly ILogManager _logger;
|
|
private readonly IHttpContextAccessor _accessor;
|
|
private readonly ITenantManager _tenantManager;
|
|
private readonly Alias _alias;
|
|
|
|
public ServerUserContactService(IUserRepository userRepository, INotificationRepository notificationRepository, IUserPermissions userPermissions, ITenantManager tenantManager, ILogManager logger, IHttpContextAccessor accessor)
|
|
{
|
|
_userRepository = userRepository;
|
|
_notificationRepository = notificationRepository;
|
|
_userPermissions = userPermissions;
|
|
_logger = logger;
|
|
_accessor = accessor;
|
|
_tenantManager = tenantManager;
|
|
_alias = tenantManager.GetAlias();
|
|
}
|
|
|
|
public Task<List<User>> SearchUsersAsync(string query, int moduleId)
|
|
{
|
|
// Note: moduleId param added to match Interface if it requires it, or just ignore if interface doesn't have it.
|
|
// Client interface: Task<List<User>> SearchUsersAsync(string query, int moduleId);
|
|
// My previous server impl: SearchUsersAsync(string query) -> Mismatch!
|
|
// I must match the signature of the Interface defined in Client.
|
|
|
|
if (string.IsNullOrWhiteSpace(query) || query.Length < 3)
|
|
{
|
|
return Task.FromResult(new List<User>());
|
|
}
|
|
|
|
if (!_accessor.HttpContext.User.Identity.IsAuthenticated)
|
|
{
|
|
return Task.FromResult(new List<User>());
|
|
}
|
|
|
|
// Try GetUsers() without params first
|
|
var users = _userRepository.GetUsers();
|
|
var results = users.Where(u =>
|
|
(u.DisplayName != null && u.DisplayName.Contains(query, StringComparison.OrdinalIgnoreCase)) ||
|
|
(u.Username != null && u.Username.Contains(query, StringComparison.OrdinalIgnoreCase))
|
|
).Take(20).ToList();
|
|
|
|
var sanitized = results.Select(u => new User
|
|
{
|
|
UserId = u.UserId,
|
|
Username = u.Username,
|
|
DisplayName = u.DisplayName,
|
|
PhotoFileId = u.PhotoFileId
|
|
}).ToList();
|
|
|
|
return Task.FromResult(sanitized);
|
|
}
|
|
|
|
public Task SendMessageAsync(int recipientUserId, string message, int moduleId)
|
|
{
|
|
var sender = _accessor.HttpContext.User;
|
|
if (!sender.Identity.IsAuthenticated) return Task.CompletedTask;
|
|
|
|
int senderId = _accessor.HttpContext.GetUserId();
|
|
var recipient = _userRepository.GetUser(recipientUserId);
|
|
if (recipient == null) return Task.CompletedTask;
|
|
|
|
var notification = new Notification
|
|
{
|
|
SiteId = _alias.SiteId,
|
|
FromUserId = senderId,
|
|
ToUserId = recipientUserId,
|
|
ToEmail = "",
|
|
Subject = "New Message from " + sender.Identity.Name,
|
|
Body = message,
|
|
ParentId = null,
|
|
CreatedOn = DateTime.UtcNow,
|
|
IsDelivered = false,
|
|
DeliveredOn = null
|
|
};
|
|
_notificationRepository.AddNotification(notification);
|
|
|
|
var emailNotification = new Notification
|
|
{
|
|
SiteId = _alias.SiteId,
|
|
FromUserId = senderId,
|
|
ToUserId = recipientUserId,
|
|
ToEmail = recipient.Email,
|
|
Subject = $"New Connection Request from {sender.Identity.Name}",
|
|
Body = $"Hello {recipient.DisplayName},<br><br>{sender.Identity.Name} sent you a message:<br><blockquote>{message}</blockquote><br><br>Login to reply.",
|
|
ParentId = null,
|
|
CreatedOn = DateTime.UtcNow,
|
|
IsDelivered = false
|
|
};
|
|
_notificationRepository.AddNotification(emailNotification);
|
|
|
|
_logger.Log(LogLevel.Information, this, LogFunction.Create, "Message sent from {SenderId} to {RecipientId}", senderId, recipientUserId);
|
|
return Task.CompletedTask;
|
|
}
|
|
}
|
|
}
|