XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

XFEToolBox

【WPF】XFE工具箱

公开
关注 0 Fork 0 Star 0
UTF-8
using System.Text.Json;
using XFEToolBox.Client.Profiles.CrossVersionProfiles;

namespace XFEToolBox.Client.Utilities.Chat;

public static class ChatNotificationPreferences
{
    private static readonly object SyncRoot = new();

    public static bool IsGroupMuted(string? groupId)
    {
        if (string.IsNullOrWhiteSpace(groupId)) return false;
        lock (SyncRoot)
            return ParseMutedGroupIds(SystemProfile.ChatMutedGroupIdsJson).Contains(groupId);
    }

    public static void SetGroupMuted(string? groupId, bool muted)
    {
        if (string.IsNullOrWhiteSpace(groupId)) return;
        lock (SyncRoot)
        {
            var ids = ParseMutedGroupIds(SystemProfile.ChatMutedGroupIdsJson);
            var changed = muted ? ids.Add(groupId) : ids.Remove(groupId);
            if (!changed) return;
            SystemProfile.ChatMutedGroupIdsJson = JsonSerializer.Serialize(ids.Order(StringComparer.Ordinal));
            SystemProfile.SaveProfile();
        }
    }

    public static HashSet<string> ParseMutedGroupIds(string? json)
    {
        if (string.IsNullOrWhiteSpace(json)) return new(StringComparer.Ordinal);
        try
        {
            var values = JsonSerializer.Deserialize<string[]>(json) ?? [];
            return values
                .Where(value => !string.IsNullOrWhiteSpace(value))
                .ToHashSet(StringComparer.Ordinal);
        }
        catch (JsonException)
        {
            return new(StringComparer.Ordinal);
        }
    }
}