Added JSON serialization of reference ids

This commit is contained in:
RogueException
2015-11-27 02:52:35 -04:00
parent e189fa2461
commit 87115bc01e
9 changed files with 85 additions and 40 deletions

View File

@@ -132,7 +132,7 @@ namespace Discord
Channel channel = null;
if (user != null)
channel = user.GlobalUser.PrivateChannel;
channel = user.Global.PrivateChannel;
if (channel == null)
{
var response = await _api.CreatePMChannel(_userId.Value, user.Id).ConfigureAwait(false);

View File

@@ -134,7 +134,7 @@ namespace Discord
private User _privateUser;
/// <summary> Returns information about the currently logged-in account. </summary>
public GlobalUser CurrentUser => _privateUser.GlobalUser;
public GlobalUser CurrentUser => _privateUser.Global;
/// <summary> Returns a collection of all unique users this client can currently see. </summary>
public IEnumerable<GlobalUser> AllUsers => _globalUsers;
@@ -275,7 +275,7 @@ namespace Discord
CheckReady();
return _api.EditUser(currentPassword: currentPassword,
username: username ?? _privateUser?.Name, email: email ?? _privateUser?.GlobalUser.Email, password: password,
username: username ?? _privateUser?.Name, email: email ?? _privateUser?.Global.Email, password: password,
avatarType: avatarType, avatar: avatar);
}

View File

@@ -314,7 +314,7 @@ namespace Discord
var data = e.Payload.ToObject<ReadyEvent>(_serializer);
_privateUser = _users.GetOrAdd(data.User.Id, null);
_privateUser.Update(data.User);
_privateUser.GlobalUser.Update(data.User);
_privateUser.Global.Update(data.User);
foreach (var model in data.Guilds)
{
if (model.Unavailable != true)

View File

@@ -50,13 +50,18 @@ namespace Discord
/// <summary> Returns the server containing this channel. </summary>
[JsonIgnore]
public Server Server => _server.Value;
[JsonProperty]
private long? ServerId { get { return _server.Id; } set { _server.Id = value; } }
private readonly Reference<Server> _server;
/// For private chats, returns the target user, otherwise null.
[JsonIgnore]
public User Recipient => _recipient.Value;
private readonly Reference<User> _recipient;
[JsonProperty]
private long? RecipientId { get { return _recipient.Id; } set { _recipient.Id = value; } }
private readonly Reference<User> _recipient;
//Collections
/// <summary> Returns a collection of all users with read access to this channel. </summary>
[JsonIgnore]
public IEnumerable<User> Members
@@ -66,20 +71,23 @@ namespace Discord
if (Type == ChannelType.Text)
return _members.Values.Where(x => x.Permissions.ReadMessages == true).Select(x => x.User);
else if (Type == ChannelType.Voice)
return Server.Members.Where(x => x.VoiceChannel == this);
return _members.Values.Select(x => x.User).Where(x => x.VoiceChannel == this);
else
return Enumerable.Empty<User>();
}
}
[JsonProperty]
private IEnumerable<long> MemberIds => Members.Select(x => x.Id);
private ConcurrentDictionary<long, ChannelMember> _members;
/// <summary> Returns a collection of all messages the client has seen posted in this channel. This collection does not guarantee any ordering. </summary>
[JsonIgnore]
public IEnumerable<Message> Messages => _messages?.Values ?? Enumerable.Empty<Message>();
[JsonProperty]
private IEnumerable<long> MessageIds => Messages.Select(x => x.Id);
private readonly ConcurrentDictionary<long, Message> _messages;
/// <summary> Returns a collection of all custom permissions used for this channel. </summary>
private static readonly PermissionOverwrite[] _initialPermissionsOverwrites = new PermissionOverwrite[0];
private PermissionOverwrite[] _permissionOverwrites;
public IEnumerable<PermissionOverwrite> PermissionOverwrites { get { return _permissionOverwrites; } internal set { _permissionOverwrites = value.ToArray(); } }
@@ -96,14 +104,14 @@ namespace Discord
{
Name = "@" + x.Name;
if (_server.Id == null)
x.GlobalUser.PrivateChannel = this;
x.Global.PrivateChannel = this;
},
x =>
{
if (_server.Id == null)
x.GlobalUser.PrivateChannel = null;
x.Global.PrivateChannel = null;
});
_permissionOverwrites = _initialPermissionsOverwrites;
_permissionOverwrites = new PermissionOverwrite[0];
_members = new ConcurrentDictionary<long, ChannelMember>();
if (recipientId != null)

View File

@@ -8,7 +8,6 @@ namespace Discord
{
public sealed class GlobalUser : CachedObject<long>
{
private readonly ConcurrentDictionary<long, User> _users;
/// <summary> Returns the email for this user. Note: this field is only ever populated for the current logged in user. </summary>
[JsonIgnore]
@@ -19,7 +18,7 @@ namespace Discord
/// <summary> Returns the private messaging channel with this user, if one exists. </summary>
[JsonIgnore]
internal Channel PrivateChannel
public Channel PrivateChannel
{
get { return _privateChannel; }
set
@@ -29,11 +28,16 @@ namespace Discord
CheckUser();
}
}
private Channel _privateChannel;
[JsonProperty]
private long? PrivateChannelId => _privateChannel?.Id;
private Channel _privateChannel;
/// <summary> Returns a collection of all server-specific data for every server this user is a member of. </summary>
[JsonIgnore]
public IEnumerable<User> Memberships => _users.Select(x => x.Value);
[JsonProperty]
private IEnumerable<long> ServerIds => _users.Select(x => x.Key);
private readonly ConcurrentDictionary<long, User> _users;
internal GlobalUser(DiscordClient client, long id)
: base(client, id)

View File

@@ -94,6 +94,8 @@ namespace Discord
/// <summary> Returns true if the logged-in user was mentioned. </summary>
/// <remarks> This is not set to true if the user was mentioned with @everyone (see IsMentioningEverone). </remarks>
public bool IsMentioningMe { get; private set; }
/// <summary> Returns true if the current user created this message. </summary>
public bool IsAuthor => _client.CurrentUserId == _user.Id;
/// <summary> Returns true if the message was sent as text-to-speech by someone with permissions to do so. </summary>
public bool IsTTS { get; private set; }
/// <summary> Returns true if the message is still in the outgoing message queue. </summary>
@@ -118,28 +120,49 @@ namespace Discord
/// <summary> Returns a collection of all users mentioned in this message. </summary>
[JsonIgnore]
public IEnumerable<User> MentionedUsers { get; internal set; }
[JsonProperty]
private IEnumerable<long> MentionedUserIds
{
get { return MentionedUsers.Select(x => x.Id); }
set { MentionedUsers = value.Select(x => _client.GetUser(Server, x)).Where(x => x != null); }
}
/// <summary> Returns a collection of all channels mentioned in this message. </summary>
[JsonIgnore]
public IEnumerable<Channel> MentionedChannels { get; internal set; }
[JsonProperty]
private IEnumerable<long> MentionedChannelIds
{
get { return MentionedChannels.Select(x => x.Id); }
set { MentionedChannels = value.Select(x => _client.GetChannel(x)).Where(x => x != null); }
}
/// <summary> Returns a collection of all roles mentioned in this message. </summary>
[JsonIgnore]
public IEnumerable<Role> MentionedRoles { get; internal set; }
[JsonProperty]
private IEnumerable<long> MentionedRoleIds
{
get { return MentionedRoles.Select(x => x.Id); }
set { MentionedRoles = value.Select(x => _client.GetRole(x)).Where(x => x != null); }
}
/// <summary> Returns the server containing the channel this message was sent to. </summary>
[JsonIgnore]
public Server Server => _channel.Value.Server;
/// <summary> Returns the channel this message was sent to. </summary>
[JsonIgnore]
public Channel Channel => _channel.Value;
[JsonProperty]
private long? ChannelId { get { return _channel.Id; } set { _channel.Id = value; } }
private readonly Reference<Channel> _channel;
/// <summary> Returns true if the current user created this message. </summary>
public bool IsAuthor => _client.CurrentUserId == _user.Id;
/// <summary> Returns the author of this message. </summary>
[JsonIgnore]
public User User => _user.Value;
[JsonProperty]
private long? UserId { get { return _user.Id; } set { _user.Id = value; } }
private readonly Reference<User> _user;
internal Message(DiscordClient client, long id, long channelId, long userId)

View File

@@ -24,13 +24,18 @@ namespace Discord
/// <summary> Returns the server this role is a member of. </summary>
[JsonIgnore]
public Server Server => _server.Value;
[JsonProperty]
private long? ServerId { get { return _server.Id; } set { _server.Id = value; } }
private readonly Reference<Server> _server;
/// <summary> Returns true if this is the role representing all users in a server. </summary>
public bool IsEveryone => _server.Id == null || Id == _server.Id;
/// <summary> Returns a list of all members in this role. </summary>
[JsonIgnore]
public IEnumerable<User> Members => _server.Id != null ? (IsEveryone ? Server.Members : Server.Members.Where(x => x.HasRole(this))) : new User[0];
[JsonProperty]
private IEnumerable<long> MemberIds => Members.Select(x => x.Id);
//TODO: Add local members cache
internal Role(DiscordClient client, long id, long serverId)

View File

@@ -35,15 +35,20 @@ namespace Discord
public string Region { get; private set; }
/// <summary> Returns true if the current user created this server. </summary>
public bool IsOwner => _client.CurrentUserId == _ownerId;
public bool IsOwner => _client.CurrentUserId == _owner.Id;
/// <summary> Returns the user that first created this server. </summary>
[JsonIgnore]
public User Owner { get; private set; }
private long _ownerId;
public User Owner => _owner.Value;
[JsonProperty]
private long? OwnerId => _owner.Id;
private Reference<User> _owner;
/// <summary> Returns the AFK voice channel for this server (see AFKTimeout). </summary>
[JsonIgnore]
public Channel AFKChannel => _afkChannel.Value;
[JsonProperty]
private long? AFKChannelId => _afkChannel.Id;
private Reference<Channel> _afkChannel;
/// <summary> Returns the default channel for this server. </summary>
@@ -51,8 +56,7 @@ namespace Discord
public Channel DefaultChannel { get; private set; }
/// <summary> Returns a collection of the ids of all users banned on this server. </summary>
[JsonIgnore]
public IEnumerable<long> Bans => _bans.Select(x => x.Key);
public IEnumerable<long> BannedUsers => _bans.Select(x => x.Key);
private ConcurrentDictionary<long, bool> _bans;
/// <summary> Returns a collection of all channels within this server. </summary>
@@ -64,11 +68,15 @@ namespace Discord
/// <summary> Returns a collection of all voice channels within this server. </summary>
[JsonIgnore]
public IEnumerable<Channel> VoiceChannels => _channels.Select(x => x.Value).Where(x => x.Type == ChannelType.Voice);
[JsonProperty]
private IEnumerable<long> ChannelIds => Channels.Select(x => x.Id);
private ConcurrentDictionary<long, Channel> _channels;
/// <summary> Returns a collection of all users within this server with their server-specific data. </summary>
[JsonIgnore]
public IEnumerable<User> Members => _members.Select(x => x.Value.User);
[JsonProperty]
private IEnumerable<long> MemberIds => Members.Select(x => x.Id);
private ConcurrentDictionary<long, ServerMember> _members;
/// <summary> Return the the role representing all users in a server. </summary>
@@ -77,11 +85,14 @@ namespace Discord
/// <summary> Returns a collection of all roles within this server. </summary>
[JsonIgnore]
public IEnumerable<Role> Roles => _roles.Select(x => x.Value);
[JsonProperty]
private IEnumerable<long> RoleIds => Roles.Select(x => x.Id);
private ConcurrentDictionary<long, Role> _roles;
internal Server(DiscordClient client, long id)
: base(client, id)
{
_owner = new Reference<User>(x => _client.Users[x, Id]);
_afkChannel = new Reference<Channel>(x => _client.Channels[x]);
//Global Cache
@@ -138,11 +149,8 @@ namespace Discord
if (model.AFKChannelId != null)
if (model.JoinedAt != null)
JoinedAt = model.JoinedAt.Value;
if (model.OwnerId != null && _ownerId != model.OwnerId)
{
_ownerId = model.OwnerId.Value;
Owner = _client.Users[_ownerId, Id];
}
if (model.OwnerId != null)
_owner.Id = model.OwnerId.Value;
if (model.Region != null)
Region = model.Region;
@@ -216,9 +224,6 @@ namespace Discord
{
if (_members.TryAdd(user.Id, new ServerMember(user)))
{
if (user.Id == _ownerId)
Owner = user;
foreach (var channel in TextChannels)
channel.AddMember(user);
}
@@ -228,9 +233,6 @@ namespace Discord
ServerMember ignored;
if (_members.TryRemove(user.Id, out ignored))
{
if (user.Id == _ownerId)
Owner = null;
foreach (var channel in Channels)
channel.RemoveMember(user);
}

View File

@@ -1,7 +1,6 @@
using Discord.API;
using Newtonsoft.Json;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
@@ -60,25 +59,29 @@ namespace Discord
public DateTime? LastOnlineAt => Status != UserStatus.Offline ? DateTime.UtcNow : _lastOnline;
private DateTime? _lastOnline;
/// <summary> Returns the private messaging channel with this user, if one exists. </summary>
//References
[JsonIgnore]
public Channel PrivateChannel => GlobalUser.PrivateChannel;
[JsonIgnore]
internal GlobalUser GlobalUser => _globalUser.Value;
public GlobalUser Global => _globalUser.Value;
private readonly Reference<GlobalUser> _globalUser;
[JsonIgnore]
public Server Server => _server.Value;
private readonly Reference<Server> _server;
[JsonProperty]
private long? ServerId { get { return _server.Id; } set { _server.Id = value; } }
[JsonIgnore]
public Channel VoiceChannel => _voiceChannel.Value;
private Reference<Channel> _voiceChannel;
[JsonProperty]
private long? VoiceChannelId { get { return _voiceChannel.Id; } set { _voiceChannel.Id = value; } }
//Collections
[JsonIgnore]
public IEnumerable<Role> Roles => _roles.Select(x => x.Value);
private Dictionary<long, Role> _roles;
[JsonProperty]
private IEnumerable<long> RoleIds => _roles.Select(x => x.Key);
/// <summary> Returns a collection of all messages this user has sent on this server that are still in cache. </summary>
[JsonIgnore]
@@ -89,7 +92,7 @@ namespace Discord
if (_server.Id != null)
return Server.Channels.SelectMany(x => x.Messages.Where(y => y.User.Id == Id));
else
return GlobalUser.PrivateChannel.Messages.Where(x => x.User.Id == Id);
return Global.PrivateChannel.Messages.Where(x => x.User.Id == Id);
}
}
@@ -106,7 +109,7 @@ namespace Discord
}
else
{
var privateChannel = PrivateChannel;
var privateChannel = Global.PrivateChannel;
if (privateChannel != null)
return new Channel[] { privateChannel };
else