Добавлены новые функции для управления контактами и фотографиями сотрудников, улучшена логика обработки избранных контактов, добавлены уведомления о днях рождения сотрудников.

This commit is contained in:
Oleg-Nevsky
2026-04-28 19:07:34 +03:00
parent a205f274b5
commit 34efa36d8b
11 changed files with 527 additions and 135 deletions
+26 -13
View File
@@ -1,27 +1,30 @@
using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace nsszcontactbot;
public class FavoriteContactsManager
{
private readonly IDatabase _redisDb;
private const string KeyPrefix = "user:favorites:";
public FavoriteContactsManager(ConnectionMultiplexer redis)
{
_redisDb = redis.GetDatabase();
}
private static string GetUserKey(long userId) => $"user:favorites:{userId}";
private static string GetContactKey(string contactId) => $"contact:favorites:{contactId}";
/// <summary>
/// Добавить контакт в избранное.
/// </summary>
public async Task AddFavoriteAsync(long userId, string contactId)
{
string key = GetRedisKey(userId);
await _redisDb.SetAddAsync(key, contactId);
string userKey = GetUserKey(userId);
string contactKey = GetContactKey(contactId);
await Task.WhenAll(
_redisDb.SetAddAsync(userKey, contactId),
_redisDb.SetAddAsync(contactKey, userId.ToString())
);
}
/// <summary>
@@ -29,8 +32,12 @@ public class FavoriteContactsManager
/// </summary>
public async Task RemoveFavoriteAsync(long userId, string contactId)
{
string key = GetRedisKey(userId);
await _redisDb.SetRemoveAsync(key, contactId);
string userKey = GetUserKey(userId);
string contactKey = GetContactKey(contactId);
await Task.WhenAll(
_redisDb.SetRemoveAsync(userKey, contactId),
_redisDb.SetRemoveAsync(contactKey, userId.ToString())
);
}
/// <summary>
@@ -38,7 +45,7 @@ public class FavoriteContactsManager
/// </summary>
public async Task<bool> IsFavoriteAsync(long userId, string contactId)
{
string key = GetRedisKey(userId);
string key = GetUserKey(userId);
return await _redisDb.SetContainsAsync(key, contactId);
}
@@ -47,10 +54,16 @@ public class FavoriteContactsManager
/// </summary>
public async Task<List<string>> GetAllFavoritesAsync(long userId)
{
string key = GetRedisKey(userId);
string key = GetUserKey(userId);
var members = await _redisDb.SetMembersAsync(key);
return members.Select(m => m.ToString()).ToList();
return [.. members.Select(m => m.ToString())];
}
public async Task<List<long>> GetUsersWithFavoriteAsync(string contactId)
{
string contactKey = GetContactKey(contactId);
var members = await _redisDb.SetMembersAsync(contactKey);
return [.. members.Select(m => long.Parse(m.ToString()))];
}
private string GetRedisKey(long userId) => $"{KeyPrefix}{userId}";
}