* Implement Client Status Support Adds support for using the client_status as sent as part of the Presence model. This value can be used to determine if a user is active on the native desktop app, the mobile app, or the website. * lint: whitespace in IPresence * Remove breaking change to IPresence interface with a note for 2.1 * update comment to not reference 2.1 * re-add interface break to IPresence * add example payload for client_status * use inline declaration for Enum.TryParsepull/1333/head
@@ -0,0 +1,21 @@ | |||||
namespace Discord | |||||
{ | |||||
/// <summary> | |||||
/// Defines the types of clients a user can be active on. | |||||
/// </summary> | |||||
public enum ClientType | |||||
{ | |||||
/// <summary> | |||||
/// The user is active using the mobile application. | |||||
/// </summary> | |||||
Mobile, | |||||
/// <summary> | |||||
/// The user is active using the desktop application. | |||||
/// </summary> | |||||
Desktop, | |||||
/// <summary> | |||||
/// The user is active using the web application. | |||||
/// </summary> | |||||
Web | |||||
} | |||||
} |
@@ -1,3 +1,5 @@ | |||||
using System.Collections.Immutable; | |||||
namespace Discord | namespace Discord | ||||
{ | { | ||||
/// <summary> | /// <summary> | ||||
@@ -13,5 +15,9 @@ namespace Discord | |||||
/// Gets the current status of this user. | /// Gets the current status of this user. | ||||
/// </summary> | /// </summary> | ||||
UserStatus Status { get; } | UserStatus Status { get; } | ||||
/// <summary> | |||||
/// Gets the set of clients where this user is currently active. | |||||
/// </summary> | |||||
IImmutableSet<ClientType> ActiveClients { get; } | |||||
} | } | ||||
} | } |
@@ -1,5 +1,6 @@ | |||||
#pragma warning disable CS1591 | |||||
#pragma warning disable CS1591 | |||||
using Newtonsoft.Json; | using Newtonsoft.Json; | ||||
using System.Collections.Generic; | |||||
namespace Discord.API | namespace Discord.API | ||||
{ | { | ||||
@@ -18,5 +19,12 @@ namespace Discord.API | |||||
public Optional<ulong[]> Roles { get; set; } | public Optional<ulong[]> Roles { get; set; } | ||||
[JsonProperty("nick")] | [JsonProperty("nick")] | ||||
public Optional<string> Nick { get; set; } | public Optional<string> Nick { get; set; } | ||||
// This property is a Dictionary where each key is the ClientType | |||||
// and the values are the current client status. | |||||
// The client status values are all the same. | |||||
// Example: | |||||
// "client_status": { "desktop": "dnd", "mobile": "dnd" } | |||||
[JsonProperty("client_status")] | |||||
public Optional<Dictionary<string, string>> ClientStatus { get; set; } | |||||
} | } | ||||
} | } |
@@ -1,4 +1,5 @@ | |||||
using System; | using System; | ||||
using System.Collections.Immutable; | |||||
using System.Diagnostics; | using System.Diagnostics; | ||||
using System.Threading.Tasks; | using System.Threading.Tasks; | ||||
using Model = Discord.API.User; | using Model = Discord.API.User; | ||||
@@ -31,6 +32,8 @@ namespace Discord.Rest | |||||
/// <inheritdoc /> | /// <inheritdoc /> | ||||
public virtual UserStatus Status => UserStatus.Offline; | public virtual UserStatus Status => UserStatus.Offline; | ||||
/// <inheritdoc /> | /// <inheritdoc /> | ||||
public virtual IImmutableSet<ClientType> ActiveClients => ImmutableHashSet<ClientType>.Empty; | |||||
/// <inheritdoc /> | |||||
public virtual bool IsWebhook => false; | public virtual bool IsWebhook => false; | ||||
internal RestUser(BaseDiscordClient discord, ulong id) | internal RestUser(BaseDiscordClient discord, ulong id) | ||||
@@ -325,7 +325,7 @@ namespace Discord.WebSocket | |||||
{ | { | ||||
var user = SocketGlobalUser.Create(this, state, model); | var user = SocketGlobalUser.Create(this, state, model); | ||||
user.GlobalUser.AddRef(); | user.GlobalUser.AddRef(); | ||||
user.Presence = new SocketPresence(UserStatus.Online, null); | |||||
user.Presence = new SocketPresence(UserStatus.Online, null, null); | |||||
return user; | return user; | ||||
}); | }); | ||||
} | } | ||||
@@ -433,7 +433,7 @@ namespace Discord.WebSocket | |||||
return; | return; | ||||
var status = Status; | var status = Status; | ||||
var statusSince = _statusSince; | var statusSince = _statusSince; | ||||
CurrentUser.Presence = new SocketPresence(status, Activity); | |||||
CurrentUser.Presence = new SocketPresence(status, Activity, null); | |||||
var gameModel = new GameModel(); | var gameModel = new GameModel(); | ||||
// Discord only accepts rich presence over RPC, don't even bother building a payload | // Discord only accepts rich presence over RPC, don't even bother building a payload | ||||
@@ -1,3 +1,6 @@ | |||||
using System; | |||||
using System.Collections.Generic; | |||||
using System.Collections.Immutable; | |||||
using System.Diagnostics; | using System.Diagnostics; | ||||
using Model = Discord.API.Presence; | using Model = Discord.API.Presence; | ||||
@@ -13,15 +16,42 @@ namespace Discord.WebSocket | |||||
public UserStatus Status { get; } | public UserStatus Status { get; } | ||||
/// <inheritdoc /> | /// <inheritdoc /> | ||||
public IActivity Activity { get; } | public IActivity Activity { get; } | ||||
internal SocketPresence(UserStatus status, IActivity activity) | |||||
/// <inheritdoc /> | |||||
public IImmutableSet<ClientType> ActiveClients { get; } | |||||
internal SocketPresence(UserStatus status, IActivity activity, IImmutableSet<ClientType> activeClients) | |||||
{ | { | ||||
Status = status; | Status = status; | ||||
Activity= activity; | Activity= activity; | ||||
ActiveClients = activeClients; | |||||
} | } | ||||
internal static SocketPresence Create(Model model) | internal static SocketPresence Create(Model model) | ||||
{ | { | ||||
return new SocketPresence(model.Status, model.Game?.ToEntity()); | |||||
var clients = ConvertClientTypesDict(model.ClientStatus.GetValueOrDefault()); | |||||
return new SocketPresence(model.Status, model.Game?.ToEntity(), clients); | |||||
} | |||||
/// <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. | |||||
/// </summary> | |||||
/// <param name="clientTypesDict"> | |||||
/// A dictionary keyed by the <see cref="ClientType"/> | |||||
/// and where the value is the <see cref="UserStatus"/>. | |||||
/// </param> | |||||
/// <returns> | |||||
/// A collection of all <see cref="ClientType"/>s that this user is active. | |||||
/// </returns> | |||||
private static IImmutableSet<ClientType> ConvertClientTypesDict(IDictionary<string, string> clientTypesDict) | |||||
{ | |||||
if (clientTypesDict == null || clientTypesDict.Count == 0) | |||||
return ImmutableHashSet<ClientType>.Empty; | |||||
var set = new HashSet<ClientType>(); | |||||
foreach (var key in clientTypesDict.Keys) | |||||
{ | |||||
if (Enum.TryParse(key, true, out ClientType type)) | |||||
set.Add(type); | |||||
// quietly discard ClientTypes that do not match | |||||
} | |||||
return set.ToImmutableHashSet(); | |||||
} | } | ||||
/// <summary> | /// <summary> | ||||
@@ -24,8 +24,8 @@ namespace Discord.WebSocket | |||||
/// <inheritdoc /> | /// <inheritdoc /> | ||||
public override bool IsWebhook => false; | public override bool IsWebhook => false; | ||||
internal override SocketPresence Presence { get { return new SocketPresence(UserStatus.Offline, null); } set { } } | |||||
/// <inheritdoc /> | |||||
internal override SocketPresence Presence { get { return new SocketPresence(UserStatus.Offline, null, null); } set { } } | |||||
/// <inheritdoc /> | /// <inheritdoc /> | ||||
/// <exception cref="NotSupportedException">This field is not supported for an unknown user.</exception> | /// <exception cref="NotSupportedException">This field is not supported for an unknown user.</exception> | ||||
internal override SocketGlobalUser GlobalUser => | internal override SocketGlobalUser GlobalUser => | ||||
@@ -38,6 +38,8 @@ namespace Discord.WebSocket | |||||
public IActivity Activity => Presence.Activity; | public IActivity Activity => Presence.Activity; | ||||
/// <inheritdoc /> | /// <inheritdoc /> | ||||
public UserStatus Status => Presence.Status; | public UserStatus Status => Presence.Status; | ||||
/// <inheritdoc /> | |||||
public IImmutableSet<ClientType> ActiveClients => Presence.ActiveClients; | |||||
/// <summary> | /// <summary> | ||||
/// Gets mutual guilds shared with this user. | /// Gets mutual guilds shared with this user. | ||||
/// </summary> | /// </summary> | ||||
@@ -29,8 +29,8 @@ namespace Discord.WebSocket | |||||
/// <inheritdoc /> | /// <inheritdoc /> | ||||
public override bool IsWebhook => true; | public override bool IsWebhook => true; | ||||
internal override SocketPresence Presence { get { return new SocketPresence(UserStatus.Offline, null); } set { } } | |||||
/// <inheritdoc /> | |||||
internal override SocketPresence Presence { get { return new SocketPresence(UserStatus.Offline, null, null); } set { } } | |||||
internal override SocketGlobalUser GlobalUser => | internal override SocketGlobalUser GlobalUser => | ||||
throw new NotSupportedException(); | throw new NotSupportedException(); | ||||