Concrete class prototype

This commit is contained in:
RogueException
2016-09-22 21:15:37 -03:00
parent ab42129eb9
commit 6319933ed0
394 changed files with 3648 additions and 3224 deletions

View File

@@ -0,0 +1,77 @@
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Threading.Tasks;
using MessageModel = Discord.API.Message;
using Model = Discord.API.Channel;
namespace Discord.WebSocket
{
internal class DMChannel : IDMChannel, ISocketChannel, ISocketMessageChannel, ISocketPrivateChannel
{
private readonly MessageManager _messages;
public new DiscordSocketClient Discord => base.Discord as DiscordSocketClient;
public new SocketDMUser Recipient => base.Recipient as SocketDMUser;
public IReadOnlyCollection<ISocketUser> Users => ImmutableArray.Create<ISocketUser>(Discord.CurrentUser, Recipient);
IReadOnlyCollection<ISocketUser> ISocketPrivateChannel.Recipients => ImmutableArray.Create(Recipient);
public SocketDMChannel(DiscordSocketClient discord, SocketDMUser recipient, Model model)
: base(discord, recipient, model)
{
if (Discord.MessageCacheSize > 0)
_messages = new MessageCache(Discord, this);
else
_messages = new MessageManager(Discord, this);
}
public override Task<IUser> GetUserAsync(ulong id) => Task.FromResult<IUser>(GetUser(id));
public override Task<IReadOnlyCollection<IUser>> GetUsersAsync() => Task.FromResult<IReadOnlyCollection<IUser>>(Users);
public ISocketUser GetUser(ulong id)
{
var currentUser = Discord.CurrentUser;
if (id == Recipient.Id)
return Recipient;
else if (id == currentUser.Id)
return currentUser;
else
return null;
}
public override async Task<IMessage> GetMessageAsync(ulong id)
{
return await _messages.DownloadAsync(id).ConfigureAwait(false);
}
public override async Task<IReadOnlyCollection<IMessage>> GetMessagesAsync(int limit)
{
return await _messages.DownloadAsync(null, Direction.Before, limit).ConfigureAwait(false);
}
public override async Task<IReadOnlyCollection<IMessage>> GetMessagesAsync(ulong fromMessageId, Direction dir, int limit)
{
return await _messages.DownloadAsync(fromMessageId, dir, limit).ConfigureAwait(false);
}
public ISocketMessage CreateMessage(ISocketUser author, MessageModel model)
{
return _messages.Create(author, model);
}
public ISocketMessage AddMessage(ISocketUser author, MessageModel model)
{
var msg = _messages.Create(author, model);
_messages.Add(msg);
return msg;
}
public ISocketMessage GetMessage(ulong id)
{
return _messages.Get(id);
}
public ISocketMessage RemoveMessage(ulong id)
{
return _messages.Remove(id);
}
public SocketDMChannel Clone() => MemberwiseClone() as SocketDMChannel;
IMessage IMessageChannel.GetCachedMessage(ulong id) => GetMessage(id);
ISocketUser ISocketMessageChannel.GetUser(ulong id, bool skipCheck) => GetUser(id);
ISocketChannel ISocketChannel.Clone() => Clone();
}
}

View File

@@ -0,0 +1,144 @@
using Discord.Rest;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Threading.Tasks;
using MessageModel = Discord.API.Message;
using Model = Discord.API.Channel;
using UserModel = Discord.API.User;
using VoiceStateModel = Discord.API.VoiceState;
namespace Discord.WebSocket
{
internal class SocketGroupChannel : IGroupChannel, ISocketChannel, ISocketMessageChannel, ISocketPrivateChannel
{
internal override bool IsAttached => true;
private readonly MessageManager _messages;
private ConcurrentDictionary<ulong, VoiceState> _voiceStates;
public new DiscordSocketClient Discord => base.Discord as DiscordSocketClient;
public IReadOnlyCollection<ISocketUser> Users
=> _users.Select(x => x.Value as ISocketUser).Concat(ImmutableArray.Create(Discord.CurrentUser)).ToReadOnlyCollection(() => _users.Count + 1);
public new IReadOnlyCollection<ISocketUser> Recipients => _users.Select(x => x.Value as ISocketUser).ToReadOnlyCollection(_users);
public SocketGroupChannel(DiscordSocketClient discord, Model model)
: base(discord, model)
{
if (Discord.MessageCacheSize > 0)
_messages = new MessageCache(Discord, this);
else
_messages = new MessageManager(Discord, this);
_voiceStates = new ConcurrentDictionary<ulong, VoiceState>(1, 5);
}
public override void Update(Model model)
{
if (source == UpdateSource.Rest && IsAttached) return;
base.Update(model, source);
}
internal void UpdateUsers(UserModel[] models, DataStore dataStore)
{
var users = new ConcurrentDictionary<ulong, GroupUser>(1, models.Length);
for (int i = 0; i < models.Length; i++)
{
var globalUser = Discord.GetOrAddUser(models[i], dataStore);
users[models[i].Id] = new SocketGroupUser(this, globalUser);
}
_users = users;
}
internal override void UpdateUsers(UserModel[] models)
=> UpdateUsers(models, source, Discord.DataStore);
public SocketGroupUser AddUser(UserModel model, DataStore dataStore)
{
GroupUser user;
if (_users.TryGetValue(model.Id, out user))
return user as SocketGroupUser;
else
{
var globalUser = Discord.GetOrAddUser(model, dataStore);
var privateUser = new SocketGroupUser(this, globalUser);
_users[privateUser.Id] = privateUser;
return privateUser;
}
}
public ISocketUser GetUser(ulong id)
{
GroupUser user;
if (_users.TryGetValue(id, out user))
return user as SocketGroupUser;
if (id == Discord.CurrentUser.Id)
return Discord.CurrentUser;
return null;
}
public SocketGroupUser RemoveUser(ulong id)
{
GroupUser user;
if (_users.TryRemove(id, out user))
return user as SocketGroupUser;
return null;
}
public VoiceState AddOrUpdateVoiceState(VoiceStateModel model, DataStore dataStore, ConcurrentDictionary<ulong, VoiceState> voiceStates = null)
{
var voiceChannel = dataStore.GetChannel(model.ChannelId.Value) as SocketVoiceChannel;
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)
{
return await _messages.DownloadAsync(id).ConfigureAwait(false);
}
public override async Task<IReadOnlyCollection<IMessage>> GetMessagesAsync(int limit)
{
return await _messages.DownloadAsync(null, Direction.Before, limit).ConfigureAwait(false);
}
public override async Task<IReadOnlyCollection<IMessage>> GetMessagesAsync(ulong fromMessageId, Direction dir, int limit)
{
return await _messages.DownloadAsync(fromMessageId, dir, limit).ConfigureAwait(false);
}
public ISocketMessage CreateMessage(ISocketUser author, MessageModel model)
{
return _messages.Create(author, model);
}
public ISocketMessage AddMessage(ISocketUser author, MessageModel model)
{
var msg = _messages.Create(author, model);
_messages.Add(msg);
return msg;
}
public ISocketMessage GetMessage(ulong id)
{
return _messages.Get(id);
}
public ISocketMessage RemoveMessage(ulong id)
{
return _messages.Remove(id);
}
public SocketDMChannel Clone() => MemberwiseClone() as SocketDMChannel;
IMessage IMessageChannel.GetCachedMessage(ulong id) => GetMessage(id);
ISocketUser ISocketMessageChannel.GetUser(ulong id, bool skipCheck) => GetUser(id);
ISocketChannel ISocketChannel.Clone() => Clone();
}
}

View File

@@ -0,0 +1,157 @@
using Discord.API.Rest;
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Model = Discord.API.Channel;
namespace Discord.Rest
{
[DebuggerDisplay(@"{DebuggerDisplay,nq}")]
internal abstract class SocketGuildChannel : ISnowflakeEntity, IGuildChannel
{
private List<Overwrite> _overwrites; //TODO: Is maintaining a list here too expensive? Is this threadsafe?
public string Name { get; private set; }
public int Position { get; private set; }
public Guild Guild { get; private set; }
public override DiscordRestClient Discord => Guild.Discord;
public GuildChannel(Guild guild, Model model)
: base(model.Id)
{
Guild = guild;
Update(model);
}
public virtual void Update(Model model)
{
if (source == UpdateSource.Rest && IsAttached) return;
Name = model.Name.Value;
Position = model.Position.Value;
var overwrites = model.PermissionOverwrites.Value;
var newOverwrites = new List<Overwrite>(overwrites.Length);
for (int i = 0; i < overwrites.Length; i++)
newOverwrites.Add(new Overwrite(overwrites[i]));
_overwrites = newOverwrites;
}
public async Task UpdateAsync()
{
if (IsAttached) throw new NotSupportedException();
var model = await Discord.ApiClient.GetChannelAsync(Id).ConfigureAwait(false);
Update(model, UpdateSource.Rest);
}
public async Task ModifyAsync(Action<ModifyGuildChannelParams> func)
{
if (func == null) throw new NullReferenceException(nameof(func));
var args = new ModifyGuildChannelParams();
func(args);
if (!args._name.IsSpecified)
args._name = Name;
var model = await Discord.ApiClient.ModifyGuildChannelAsync(Id, args).ConfigureAwait(false);
Update(model, UpdateSource.Rest);
}
public async Task DeleteAsync()
{
await Discord.ApiClient.DeleteChannelAsync(Id).ConfigureAwait(false);
}
public abstract Task<IGuildUser> GetUserAsync(ulong id);
public abstract Task<IReadOnlyCollection<IGuildUser>> GetUsersAsync();
public async Task<IReadOnlyCollection<IInviteMetadata>> GetInvitesAsync()
{
var models = await Discord.ApiClient.GetChannelInvitesAsync(Id).ConfigureAwait(false);
return models.Select(x => new InviteMetadata(Discord, x)).ToImmutableArray();
}
public async Task<IInviteMetadata> CreateInviteAsync(int? maxAge, int? maxUses, bool isTemporary)
{
var args = new CreateChannelInviteParams
{
MaxAge = maxAge ?? 0,
MaxUses = maxUses ?? 0,
Temporary = isTemporary
};
var model = await Discord.ApiClient.CreateChannelInviteAsync(Id, args).ConfigureAwait(false);
return new InviteMetadata(Discord, model);
}
public OverwritePermissions? GetPermissionOverwrite(IUser user)
{
for (int i = 0; i < _overwrites.Count; i++)
{
if (_overwrites[i].TargetId == user.Id)
return _overwrites[i].Permissions;
}
return null;
}
public OverwritePermissions? GetPermissionOverwrite(IRole role)
{
for (int i = 0; i < _overwrites.Count; i++)
{
if (_overwrites[i].TargetId == role.Id)
return _overwrites[i].Permissions;
}
return null;
}
public async Task AddPermissionOverwriteAsync(IUser user, OverwritePermissions perms)
{
var args = new ModifyChannelPermissionsParams { Allow = perms.AllowValue, Deny = perms.DenyValue, Type = "member" };
await Discord.ApiClient.ModifyChannelPermissionsAsync(Id, user.Id, args).ConfigureAwait(false);
_overwrites.Add(new Overwrite(new API.Overwrite { Allow = perms.AllowValue, Deny = perms.DenyValue, TargetId = user.Id, TargetType = PermissionTarget.User }));
}
public async Task AddPermissionOverwriteAsync(IRole role, OverwritePermissions perms)
{
var args = new ModifyChannelPermissionsParams { Allow = perms.AllowValue, Deny = perms.DenyValue, Type = "role" };
await Discord.ApiClient.ModifyChannelPermissionsAsync(Id, role.Id, args).ConfigureAwait(false);
_overwrites.Add(new Overwrite(new API.Overwrite { Allow = perms.AllowValue, Deny = perms.DenyValue, TargetId = role.Id, TargetType = PermissionTarget.Role }));
}
public async Task RemovePermissionOverwriteAsync(IUser user)
{
await Discord.ApiClient.DeleteChannelPermissionAsync(Id, user.Id).ConfigureAwait(false);
for (int i = 0; i < _overwrites.Count; i++)
{
if (_overwrites[i].TargetId == user.Id)
{
_overwrites.RemoveAt(i);
return;
}
}
}
public async Task RemovePermissionOverwriteAsync(IRole role)
{
await Discord.ApiClient.DeleteChannelPermissionAsync(Id, role.Id).ConfigureAwait(false);
for (int i = 0; i < _overwrites.Count; i++)
{
if (_overwrites[i].TargetId == role.Id)
{
_overwrites.RemoveAt(i);
return;
}
}
}
public override string ToString() => Name;
private string DebuggerDisplay => $"{Name} ({Id})";
IGuild IGuildChannel.Guild => Guild;
IReadOnlyCollection<Overwrite> IGuildChannel.PermissionOverwrites => _overwrites.AsReadOnly();
async Task<IUser> IChannel.GetUserAsync(ulong id) => await GetUserAsync(id).ConfigureAwait(false);
async Task<IReadOnlyCollection<IUser>> IChannel.GetUsersAsync() => await GetUsersAsync().ConfigureAwait(false);
}
}

View File

@@ -0,0 +1,88 @@
using Discord.Rest;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Threading.Tasks;
using MessageModel = Discord.API.Message;
using Model = Discord.API.Channel;
namespace Discord.WebSocket
{
internal class SocketTextChannel : TextChannel, ISocketGuildChannel, ISocketMessageChannel
{
internal override bool IsAttached => true;
private readonly MessageManager _messages;
public new DiscordSocketClient Discord => base.Discord as DiscordSocketClient;
public new SocketGuild Guild => base.Guild as SocketGuild;
public IReadOnlyCollection<SocketGuildUser> Members
=> Guild.Members.Where(x => Permissions.GetValue(Permissions.ResolveChannel(x, this, x.GuildPermissions.RawValue), ChannelPermission.ReadMessages)).ToImmutableArray();
public SocketTextChannel(SocketGuild guild, Model model)
: base(guild, model)
{
if (Discord.MessageCacheSize > 0)
_messages = new MessageCache(Discord, this);
else
_messages = new MessageManager(Discord, this);
}
public override Task<IGuildUser> GetUserAsync(ulong id) => Task.FromResult<IGuildUser>(GetUser(id));
public override Task<IReadOnlyCollection<IGuildUser>> GetUsersAsync() => Task.FromResult<IReadOnlyCollection<IGuildUser>>(Members);
public SocketGuildUser GetUser(ulong id, bool skipCheck = false)
{
var user = Guild.GetUser(id);
if (skipCheck) return user;
if (user != null)
{
ulong perms = Permissions.ResolveChannel(user, this, user.GuildPermissions.RawValue);
if (Permissions.GetValue(perms, ChannelPermission.ReadMessages))
return user;
}
return null;
}
public override async Task<IMessage> GetMessageAsync(ulong id)
{
return await _messages.DownloadAsync(id).ConfigureAwait(false);
}
public override async Task<IReadOnlyCollection<IMessage>> GetMessagesAsync(int limit = DiscordConfig.MaxMessagesPerBatch)
{
return await _messages.DownloadAsync(null, Direction.Before, limit).ConfigureAwait(false);
}
public override async Task<IReadOnlyCollection<IMessage>> GetMessagesAsync(ulong fromMessageId, Direction dir, int limit = DiscordConfig.MaxMessagesPerBatch)
{
return await _messages.DownloadAsync(fromMessageId, dir, limit).ConfigureAwait(false);
}
public ISocketMessage CreateMessage(ISocketUser author, MessageModel model)
{
return _messages.Create(author, model);
}
public ISocketMessage AddMessage(ISocketUser author, MessageModel model)
{
var msg = _messages.Create(author, model);
_messages.Add(msg);
return msg;
}
public ISocketMessage GetMessage(ulong id)
{
return _messages.Get(id);
}
public ISocketMessage RemoveMessage(ulong id)
{
return _messages.Remove(id);
}
public SocketTextChannel Clone() => MemberwiseClone() as SocketTextChannel;
IReadOnlyCollection<ISocketUser> ISocketMessageChannel.Users => Members;
IMessage IMessageChannel.GetCachedMessage(ulong id) => GetMessage(id);
ISocketUser ISocketMessageChannel.GetUser(ulong id, bool skipCheck) => GetUser(id, skipCheck);
ISocketChannel ISocketChannel.Clone() => Clone();
}
}

View File

@@ -0,0 +1,54 @@
using Discord.Audio;
using Discord.Rest;
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Threading.Tasks;
using Model = Discord.API.Channel;
namespace Discord.WebSocket
{
internal class SocketVoiceChannel : VoiceChannel, ISocketGuildChannel
{
internal override bool IsAttached => true;
public new DiscordSocketClient Discord => base.Discord as DiscordSocketClient;
public new SocketGuild Guild => base.Guild as SocketGuild;
public IReadOnlyCollection<IGuildUser> Members
=> Guild.VoiceStates.Where(x => x.Value.VoiceChannel.Id == Id).Select(x => Guild.GetUser(x.Key)).ToImmutableArray();
public SocketVoiceChannel(SocketGuild guild, Model model)
: base(guild, model)
{
}
public override Task<IGuildUser> GetUserAsync(ulong id)
=> Task.FromResult(GetUser(id));
public override Task<IReadOnlyCollection<IGuildUser>> GetUsersAsync()
=> Task.FromResult(Members);
public IGuildUser GetUser(ulong id)
{
var user = Guild.GetUser(id);
if (user != null && user.VoiceChannel.Id == Id)
return user;
return null;
}
public override async Task<IAudioClient> ConnectAsync()
{
var audioMode = Discord.AudioMode;
if (audioMode == AudioMode.Disabled)
throw new InvalidOperationException($"Audio is not enabled on this client, {nameof(DiscordSocketConfig.AudioMode)} in {nameof(DiscordSocketConfig)} must be set.");
return await Guild.ConnectAudioAsync(Id,
(audioMode & AudioMode.Incoming) == 0,
(audioMode & AudioMode.Outgoing) == 0).ConfigureAwait(false);
}
public SocketVoiceChannel Clone() => MemberwiseClone() as SocketVoiceChannel;
ISocketChannel ISocketChannel.Clone() => Clone();
}
}