@@ -6,16 +6,18 @@ using System.Linq;
using System.Threading.Tasks;
using System.Threading.Tasks;
using MessageModel = Discord.API.Message;
using MessageModel = Discord.API.Message;
using Model = Discord.API.Channel;
using Model = Discord.API.Channel;
using VoiceStateModel = Discord.API.VoiceState;
namespace Discord
namespace Discord
{
{
internal class CachedGroupChannel : GroupChannel, IGroupChannel, ICachedChannel, ICachedMessageChannel, ICachedPrivateChannel
internal class CachedGroupChannel : GroupChannel, IGroupChannel, ICachedChannel, ICachedMessageChannel, ICachedPrivateChannel
{
{
private readonly MessageManager _messages;
private readonly MessageManager _messages;
private ConcurrentDictionary<ulong, VoiceState> _voiceStates;
public new DiscordSocketClient Discord => base.Discord as DiscordSocketClient;
public new DiscordSocketClient Discord => base.Discord as DiscordSocketClient;
public IReadOnlyCollection<ICachedUser> Members
public IReadOnlyCollection<ICachedUser> Members
=> _users.Select(x => x.Value).Concat(ImmutableArray.Create(Discord.CurrentUser)).Cast<ICachedUser>().ToReadOnlyCollection(_users, 1);
=> _users.Select(x => x.Value).Concat(ImmutableArray.Create(Discord.CurrentUser)).Cast<ICachedUser>().ToReadOnlyCollection(() => _users.Count + 1);
public new IReadOnlyCollection<CachedPrivateUser> Recipients => _users.Cast<CachedPrivateUser>().ToReadOnlyCollection(_users);
public new IReadOnlyCollection<CachedPrivateUser> Recipients => _users.Cast<CachedPrivateUser>().ToReadOnlyCollection(_users);
public CachedGroupChannel(DiscordSocketClient discord, ConcurrentDictionary<ulong, IUser> recipients, Model model)
public CachedGroupChannel(DiscordSocketClient discord, ConcurrentDictionary<ulong, IUser> recipients, Model model)
@@ -25,6 +27,13 @@ namespace Discord
_messages = new MessageCache(Discord, this);
_messages = new MessageCache(Discord, this);
else
else
_messages = new MessageManager(Discord, this);
_messages = new MessageManager(Discord, this);
_voiceStates = new ConcurrentDictionary<ulong, VoiceState>(1, 5);
}
public override void Update(Model model, UpdateSource source)
{
if (source == UpdateSource.Rest && IsAttached) return;
base.Update(model, source);
}
}
protected override void UpdateUsers(API.User[] models, UpdateSource source)
protected override void UpdateUsers(API.User[] models, UpdateSource source)
@@ -35,6 +44,38 @@ namespace Discord
_users = users;
_users = users;
}
}
public ICachedUser GetUser(ulong id)
{
IUser user;
if (_users.TryGetValue(id, out user))
return user as ICachedUser;
if (id == Discord.CurrentUser.Id)
return Discord.CurrentUser;
return null;
}
public VoiceState AddOrUpdateVoiceState(VoiceStateModel model, DataStore dataStore, ConcurrentDictionary<ulong, VoiceState> voiceStates = null)
{
var voiceChannel = dataStore.GetChannel(model.ChannelId.Value) as CachedVoiceChannel;
var voiceState = new VoiceState(voiceChannel, model);
(voiceStates ?? _voiceStates)[model.UserId] = voiceState;
return voiceState;
}
public VoiceState? GetVoiceState(ulong id)
{
VoiceState voiceState;
if (_voiceStates.TryGetValue(id, out voiceState))
return voiceState;
return null;
}
public VoiceState? RemoveVoiceState(ulong id)
{
VoiceState voiceState;
if (_voiceStates.TryRemove(id, out voiceState))
return voiceState;
return null;
}
public override async Task<IMessage> GetMessageAsync(ulong id)
public override async Task<IMessage> GetMessageAsync(ulong id)
{
{
return await _messages.DownloadAsync(id).ConfigureAwait(false);
return await _messages.DownloadAsync(id).ConfigureAwait(false);