Fixed TTS using StreamElements. Fixed several issues.
This commit is contained in:
@@ -1,5 +1,3 @@
|
||||
|
||||
using System.Collections.Concurrent;
|
||||
using HermesSocketLibrary.Requests.Messages;
|
||||
using Serilog;
|
||||
|
||||
@@ -9,109 +7,199 @@ namespace TwitchChatTTS.Chat.Groups
|
||||
{
|
||||
private readonly IDictionary<string, Group> _groups;
|
||||
private readonly IDictionary<long, ICollection<string>> _chatters;
|
||||
private readonly ReaderWriterLockSlim _rwls;
|
||||
private readonly ILogger _logger;
|
||||
|
||||
|
||||
public ChatterGroupManager(ILogger logger)
|
||||
{
|
||||
_logger = logger;
|
||||
_groups = new ConcurrentDictionary<string, Group>();
|
||||
_chatters = new ConcurrentDictionary<long, ICollection<string>>();
|
||||
_groups = new Dictionary<string, Group>();
|
||||
_chatters = new Dictionary<long, ICollection<string>>();
|
||||
_rwls = new ReaderWriterLockSlim();
|
||||
}
|
||||
|
||||
public void Add(Group group)
|
||||
{
|
||||
_groups.Add(group.Id, group);
|
||||
_rwls.EnterWriteLock();
|
||||
try
|
||||
{
|
||||
_groups.Add(group.Id, group);
|
||||
}
|
||||
finally
|
||||
{
|
||||
_rwls.ExitWriteLock();
|
||||
}
|
||||
}
|
||||
|
||||
public void Add(long chatterId, string groupId)
|
||||
{
|
||||
if (_chatters.TryGetValue(chatterId, out var list))
|
||||
_rwls.EnterWriteLock();
|
||||
try
|
||||
{
|
||||
if (!list.Contains(groupId))
|
||||
list.Add(groupId);
|
||||
if (_chatters.TryGetValue(chatterId, out var list))
|
||||
{
|
||||
if (!list.Contains(groupId))
|
||||
list.Add(groupId);
|
||||
}
|
||||
else
|
||||
_chatters.Add(chatterId, new List<string>() { groupId });
|
||||
}
|
||||
finally
|
||||
{
|
||||
_rwls.ExitWriteLock();
|
||||
}
|
||||
else
|
||||
_chatters.Add(chatterId, new List<string>() { groupId });
|
||||
}
|
||||
|
||||
public void Add(long chatter, ICollection<string> groupIds)
|
||||
{
|
||||
if (_chatters.TryGetValue(chatter, out var list))
|
||||
_rwls.EnterWriteLock();
|
||||
try
|
||||
{
|
||||
foreach (var groupId in groupIds)
|
||||
if (!list.Contains(groupId))
|
||||
list.Add(groupId);
|
||||
if (_chatters.TryGetValue(chatter, out var list))
|
||||
{
|
||||
foreach (var groupId in groupIds)
|
||||
if (!list.Contains(groupId))
|
||||
list.Add(groupId);
|
||||
}
|
||||
else
|
||||
_chatters.Add(chatter, groupIds);
|
||||
}
|
||||
finally
|
||||
{
|
||||
_rwls.ExitWriteLock();
|
||||
}
|
||||
else
|
||||
_chatters.Add(chatter, groupIds);
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
_groups.Clear();
|
||||
_chatters.Clear();
|
||||
_rwls.EnterWriteLock();
|
||||
try
|
||||
{
|
||||
_groups.Clear();
|
||||
_chatters.Clear();
|
||||
}
|
||||
finally
|
||||
{
|
||||
_rwls.ExitWriteLock();
|
||||
}
|
||||
}
|
||||
|
||||
public Group? Get(string groupId)
|
||||
{
|
||||
if (_groups.TryGetValue(groupId, out var group))
|
||||
return group;
|
||||
return null;
|
||||
_rwls.EnterReadLock();
|
||||
try
|
||||
{
|
||||
if (_groups.TryGetValue(groupId, out var group))
|
||||
return group;
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
_rwls.ExitReadLock();
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerable<string> GetGroupNamesFor(long chatter)
|
||||
{
|
||||
if (_chatters.TryGetValue(chatter, out var groups))
|
||||
return groups.Select(g => _groups.TryGetValue(g, out var group) ? group.Name : null)
|
||||
.Where(g => g != null)
|
||||
.Cast<string>();
|
||||
_rwls.EnterReadLock();
|
||||
try
|
||||
{
|
||||
if (_chatters.TryGetValue(chatter, out var groups))
|
||||
return groups.Select(g => _groups.TryGetValue(g, out var group) ? group.Name : null)
|
||||
.Where(g => g != null)
|
||||
.Cast<string>();
|
||||
|
||||
return Array.Empty<string>();
|
||||
return Array.Empty<string>();
|
||||
}
|
||||
finally
|
||||
{
|
||||
_rwls.ExitReadLock();
|
||||
}
|
||||
}
|
||||
|
||||
public int GetPriorityFor(long chatter)
|
||||
{
|
||||
if (!_chatters.TryGetValue(chatter, out var groups))
|
||||
return 0;
|
||||
_rwls.EnterReadLock();
|
||||
try
|
||||
{
|
||||
if (!_chatters.TryGetValue(chatter, out var groups))
|
||||
return 0;
|
||||
|
||||
return GetPriorityFor(groups);
|
||||
return GetPriorityFor(groups);
|
||||
}
|
||||
finally
|
||||
{
|
||||
_rwls.ExitReadLock();
|
||||
}
|
||||
}
|
||||
|
||||
public int GetPriorityFor(IEnumerable<string> groupIds)
|
||||
{
|
||||
var values = groupIds.Select(g => _groups.TryGetValue(g, out var group) ? group : null).Where(g => g != null);
|
||||
if (values.Any())
|
||||
return values.Max(g => g!.Priority);
|
||||
return 0;
|
||||
_rwls.EnterReadLock();
|
||||
try
|
||||
{
|
||||
var values = groupIds.Select(g => _groups.TryGetValue(g, out var group) ? group : null).Where(g => g != null);
|
||||
if (values.Any())
|
||||
return values.Max(g => g!.Priority);
|
||||
return 0;
|
||||
}
|
||||
finally
|
||||
{
|
||||
_rwls.ExitReadLock();
|
||||
}
|
||||
}
|
||||
|
||||
public void Modify(Group group)
|
||||
{
|
||||
_groups[group.Id] = group;
|
||||
_rwls.EnterWriteLock();
|
||||
try
|
||||
{
|
||||
_groups[group.Id] = group;
|
||||
}
|
||||
finally
|
||||
{
|
||||
_rwls.ExitWriteLock();
|
||||
}
|
||||
}
|
||||
|
||||
public bool Remove(string groupId)
|
||||
{
|
||||
if (_groups.Remove(groupId))
|
||||
_rwls.EnterWriteLock();
|
||||
try
|
||||
{
|
||||
foreach (var entry in _chatters)
|
||||
entry.Value.Remove(groupId);
|
||||
return true;
|
||||
if (_groups.Remove(groupId))
|
||||
{
|
||||
foreach (var entry in _chatters)
|
||||
entry.Value.Remove(groupId);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
finally
|
||||
{
|
||||
_rwls.ExitReadLock();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public bool Remove(long chatterId, string groupId)
|
||||
{
|
||||
if (_chatters.TryGetValue(chatterId, out var groups))
|
||||
_rwls.EnterWriteLock();
|
||||
try
|
||||
{
|
||||
groups.Remove(groupId);
|
||||
_logger.Debug($"Removed chatter from group [chatter id: {chatterId}][group name: {_groups[groupId].Name}][group id: {groupId}]");
|
||||
return true;
|
||||
if (_chatters.TryGetValue(chatterId, out var groups))
|
||||
{
|
||||
groups.Remove(groupId);
|
||||
_logger.Debug($"Removed chatter from group [chatter id: {chatterId}][group name: {_groups[groupId].Name}][group id: {groupId}]");
|
||||
return true;
|
||||
}
|
||||
_logger.Debug($"Failed to remove chatter from group [chatter id: {chatterId}][group name: {_groups[groupId].Name}][group id: {groupId}]");
|
||||
return false;
|
||||
}
|
||||
finally
|
||||
{
|
||||
_rwls.ExitReadLock();
|
||||
}
|
||||
_logger.Debug($"Failed to remove chatter from group [chatter id: {chatterId}][group name: {_groups[groupId].Name}][group id: {groupId}]");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,33 +5,51 @@ namespace TwitchChatTTS.Chat.Groups.Permissions
|
||||
{
|
||||
public class GroupPermissionManager : IGroupPermissionManager
|
||||
{
|
||||
private PermissionNode _root;
|
||||
private ILogger _logger;
|
||||
private readonly PermissionNode _root;
|
||||
private readonly ILogger _logger;
|
||||
private readonly ReaderWriterLockSlim _rwls;
|
||||
|
||||
|
||||
public GroupPermissionManager(ILogger logger)
|
||||
{
|
||||
_logger = logger;
|
||||
_root = new PermissionNode(string.Empty, null, null);
|
||||
_rwls = new ReaderWriterLockSlim();
|
||||
}
|
||||
|
||||
|
||||
public bool? CheckIfAllowed(string path)
|
||||
{
|
||||
var res = Get(path)!.Allow;
|
||||
_logger.Debug($"Permission Node GET {path} = {res?.ToString() ?? "null"}");
|
||||
return res;
|
||||
_rwls.EnterReadLock();
|
||||
try
|
||||
{
|
||||
var res = Get(path)!.Allow;
|
||||
_logger.Debug($"Permission Node GET {path} = {res?.ToString() ?? "null"}");
|
||||
return res;
|
||||
}
|
||||
finally
|
||||
{
|
||||
_rwls.ExitReadLock();
|
||||
}
|
||||
}
|
||||
|
||||
public bool? CheckIfDirectAllowed(string path)
|
||||
{
|
||||
var node = Get(path, nullIfMissing: true);
|
||||
if (node == null)
|
||||
return null;
|
||||
|
||||
var res = node.DirectAllow;
|
||||
_logger.Debug($"Permission Node GET {path} = {res?.ToString() ?? "null"} [direct]");
|
||||
return res;
|
||||
_rwls.EnterReadLock();
|
||||
try
|
||||
{
|
||||
var node = Get(path, nullIfMissing: true);
|
||||
if (node == null)
|
||||
return null;
|
||||
|
||||
var res = node.DirectAllow;
|
||||
_logger.Debug($"Permission Node GET {path} = {res?.ToString() ?? "null"} [direct]");
|
||||
return res;
|
||||
}
|
||||
finally
|
||||
{
|
||||
_rwls.ExitReadLock();
|
||||
}
|
||||
}
|
||||
|
||||
public bool? CheckIfAllowed(IEnumerable<string> groups, string path)
|
||||
@@ -64,31 +82,63 @@ namespace TwitchChatTTS.Chat.Groups.Permissions
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
_root.Clear();
|
||||
_rwls.EnterWriteLock();
|
||||
try
|
||||
{
|
||||
_root.Clear();
|
||||
}
|
||||
finally
|
||||
{
|
||||
_rwls.ExitWriteLock();
|
||||
}
|
||||
}
|
||||
|
||||
public bool Remove(string path)
|
||||
{
|
||||
var node = Get(path);
|
||||
if (node == null || node.Parent == null)
|
||||
return false;
|
||||
|
||||
var parts = path.Split('.');
|
||||
var last = parts.Last();
|
||||
if (parts.Length > 1 && parts[parts.Length - 1] == node.Parent.Name || parts.Length == 1 && node.Parent.Name == null)
|
||||
_rwls.EnterUpgradeableReadLock();
|
||||
try
|
||||
{
|
||||
node.Parent.Remove(last);
|
||||
_logger.Debug($"Permission Node REMOVE priv {path}");
|
||||
return true;
|
||||
var node = Get(path);
|
||||
if (node == null || node.Parent == null)
|
||||
return false;
|
||||
|
||||
_rwls.EnterWriteLock();
|
||||
try
|
||||
{
|
||||
var parts = path.Split('.');
|
||||
var last = parts.Last();
|
||||
if (parts.Length > 1 && parts[parts.Length - 1] == node.Parent.Name || parts.Length == 1 && node.Parent.Name == null)
|
||||
{
|
||||
node.Parent.Remove(last);
|
||||
_logger.Debug($"Permission Node REMOVE priv {path}");
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
finally
|
||||
{
|
||||
_rwls.ExitWriteLock();
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
_rwls.ExitUpgradeableReadLock();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public void Set(string path, bool? allow)
|
||||
{
|
||||
var node = Get(path, true);
|
||||
node!.Allow = allow;
|
||||
_logger.Debug($"Permission Node ADD {path} = {allow?.ToString() ?? "null"}");
|
||||
_rwls.EnterWriteLock();
|
||||
try
|
||||
{
|
||||
var node = Get(path, true);
|
||||
node!.Allow = allow;
|
||||
_logger.Debug($"Permission Node ADD {path} = {allow?.ToString() ?? "null"}");
|
||||
}
|
||||
finally
|
||||
{
|
||||
_rwls.ExitWriteLock();
|
||||
}
|
||||
}
|
||||
|
||||
private PermissionNode? Get(string path, bool edit = false, bool nullIfMissing = false)
|
||||
|
||||
Reference in New Issue
Block a user