Update socket presence and add new presence event (#1945)
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using Model = Discord.API.User;
|
||||
using PresenceModel = Discord.API.Presence;
|
||||
|
||||
namespace Discord.WebSocket
|
||||
{
|
||||
@@ -48,11 +47,6 @@ namespace Discord.WebSocket
|
||||
}
|
||||
}
|
||||
|
||||
internal void Update(ClientState state, PresenceModel model)
|
||||
{
|
||||
Presence = SocketPresence.Create(model);
|
||||
}
|
||||
|
||||
private string DebuggerDisplay => $"{Username}#{Discriminator} ({Id}{(IsBot ? ", Bot" : "")}, Global)";
|
||||
internal new SocketGlobalUser Clone() => MemberwiseClone() as SocketGlobalUser;
|
||||
}
|
||||
|
||||
@@ -164,8 +164,7 @@ namespace Discord.WebSocket
|
||||
{
|
||||
if (updatePresence)
|
||||
{
|
||||
Presence = SocketPresence.Create(model);
|
||||
GlobalUser.Update(state, model);
|
||||
Update(model);
|
||||
}
|
||||
if (model.Nick.IsSpecified)
|
||||
Nickname = model.Nick.Value;
|
||||
@@ -174,6 +173,13 @@ namespace Discord.WebSocket
|
||||
if (model.PremiumSince.IsSpecified)
|
||||
_premiumSinceTicks = model.PremiumSince.Value?.UtcTicks;
|
||||
}
|
||||
|
||||
internal override void Update(PresenceModel model)
|
||||
{
|
||||
Presence.Update(model);
|
||||
GlobalUser.Update(model);
|
||||
}
|
||||
|
||||
private void UpdateRoles(ulong[] roleIds)
|
||||
{
|
||||
var roles = ImmutableArray.CreateBuilder<ulong>(roleIds.Length + 1);
|
||||
|
||||
@@ -11,26 +11,37 @@ namespace Discord.WebSocket
|
||||
/// Represents the WebSocket user's presence status. This may include their online status and their activity.
|
||||
/// </summary>
|
||||
[DebuggerDisplay(@"{DebuggerDisplay,nq}")]
|
||||
public struct SocketPresence : IPresence
|
||||
public class SocketPresence : IPresence
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public UserStatus Status { get; }
|
||||
public UserStatus Status { get; private set; }
|
||||
/// <inheritdoc />
|
||||
public IImmutableSet<ClientType> ActiveClients { get; }
|
||||
public IReadOnlyCollection<ClientType> ActiveClients { get; private set; }
|
||||
/// <inheritdoc />
|
||||
public IImmutableList<IActivity> Activities { get; }
|
||||
public IReadOnlyCollection<IActivity> Activities { get; private set; }
|
||||
|
||||
internal SocketPresence() { }
|
||||
internal SocketPresence(UserStatus status, IImmutableSet<ClientType> activeClients, IImmutableList<IActivity> activities)
|
||||
{
|
||||
Status = status;
|
||||
ActiveClients = activeClients ?? ImmutableHashSet<ClientType>.Empty;
|
||||
Activities = activities ?? ImmutableList<IActivity>.Empty;
|
||||
}
|
||||
|
||||
internal static SocketPresence Create(Model model)
|
||||
{
|
||||
var clients = ConvertClientTypesDict(model.ClientStatus.GetValueOrDefault());
|
||||
var activities = ConvertActivitiesList(model.Activities);
|
||||
return new SocketPresence(model.Status, clients, activities);
|
||||
var entity = new SocketPresence();
|
||||
entity.Update(model);
|
||||
return entity;
|
||||
}
|
||||
|
||||
internal void Update(Model model)
|
||||
{
|
||||
Status = model.Status;
|
||||
ActiveClients = ConvertClientTypesDict(model.ClientStatus.GetValueOrDefault()) ?? ImmutableArray<ClientType>.Empty;
|
||||
Activities = ConvertActivitiesList(model.Activities) ?? ImmutableArray<IActivity>.Empty;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="IReadOnlyCollection{T}"/> containing all of the client types
|
||||
/// where a user is active from the data supplied in the Presence update frame.
|
||||
@@ -42,7 +53,7 @@ namespace Discord.WebSocket
|
||||
/// <returns>
|
||||
/// A collection of all <see cref="ClientType"/>s that this user is active.
|
||||
/// </returns>
|
||||
private static IImmutableSet<ClientType> ConvertClientTypesDict(IDictionary<string, string> clientTypesDict)
|
||||
private static IReadOnlyCollection<ClientType> ConvertClientTypesDict(IDictionary<string, string> clientTypesDict)
|
||||
{
|
||||
if (clientTypesDict == null || clientTypesDict.Count == 0)
|
||||
return ImmutableHashSet<ClientType>.Empty;
|
||||
@@ -84,6 +95,6 @@ namespace Discord.WebSocket
|
||||
public override string ToString() => Status.ToString();
|
||||
private string DebuggerDisplay => $"{Status}{(Activities?.FirstOrDefault()?.Name ?? "")}";
|
||||
|
||||
internal SocketPresence Clone() => this;
|
||||
internal SocketPresence Clone() => MemberwiseClone() as SocketPresence;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Discord.Rest;
|
||||
using Model = Discord.API.User;
|
||||
using PresenceModel = Discord.API.Presence;
|
||||
|
||||
namespace Discord.WebSocket
|
||||
{
|
||||
@@ -40,9 +41,9 @@ namespace Discord.WebSocket
|
||||
/// <inheritdoc />
|
||||
public UserStatus Status => Presence.Status;
|
||||
/// <inheritdoc />
|
||||
public IImmutableSet<ClientType> ActiveClients => Presence.ActiveClients ?? ImmutableHashSet<ClientType>.Empty;
|
||||
public IReadOnlyCollection<ClientType> ActiveClients => Presence.ActiveClients ?? ImmutableHashSet<ClientType>.Empty;
|
||||
/// <inheritdoc />
|
||||
public IImmutableList<IActivity> Activities => Presence.Activities ?? ImmutableList<IActivity>.Empty;
|
||||
public IReadOnlyCollection<IActivity> Activities => Presence.Activities ?? ImmutableList<IActivity>.Empty;
|
||||
/// <summary>
|
||||
/// Gets mutual guilds shared with this user.
|
||||
/// </summary>
|
||||
@@ -91,6 +92,11 @@ namespace Discord.WebSocket
|
||||
return hasChanges;
|
||||
}
|
||||
|
||||
internal virtual void Update(PresenceModel model)
|
||||
{
|
||||
Presence.Update(model);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<IDMChannel> CreateDMChannelAsync(RequestOptions options = null)
|
||||
=> await UserHelper.CreateDMChannelAsync(this, Discord, options).ConfigureAwait(false);
|
||||
|
||||
Reference in New Issue
Block a user