Browse Source

Add API model for Reaction, implement REST methods for reactions

tags/1.0-rc
Christopher F 8 years ago
parent
commit
838d60e2c2
9 changed files with 146 additions and 1 deletions
  1. +20
    -0
      src/Discord.Net.Core/API/Common/Reaction.cs
  2. +42
    -0
      src/Discord.Net.Core/API/DiscordRestApiClient.cs
  3. +8
    -0
      src/Discord.Net.Core/API/Rest/GetReactionUsersParams.cs
  4. +12
    -1
      src/Discord.Net.Core/Entities/Messages/IUserMessage.cs
  5. +1
    -0
      src/Discord.Net.Core/Utils/Preconditions.cs
  6. +23
    -0
      src/Discord.Net.Rest/Entities/Messages/MessageHelper.cs
  7. +14
    -0
      src/Discord.Net.Rest/Entities/Messages/RestUserMessage.cs
  8. +13
    -0
      src/Discord.Net.Rpc/Entities/Messages/RpcUserMessage.cs
  9. +13
    -0
      src/Discord.Net.WebSocket/Entities/Messages/SocketUserMessage.cs

+ 20
- 0
src/Discord.Net.Core/API/Common/Reaction.cs View File

@@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace Discord.API.Common
{
public class Reaction
{
[JsonProperty("user_id")]
public ulong UserId { get; set; }
[JsonProperty("message_id")]
public ulong MessageId { get; set; }
[JsonProperty("emoji")]
public Emoji Emoji { get; set; }
[JsonProperty("channel_id")]
public ulong ChannelId { get; set; }
}
}

+ 42
- 0
src/Discord.Net.Core/API/DiscordRestApiClient.cs View File

@@ -512,6 +512,48 @@ namespace Discord.API
var ids = new BucketIds(channelId: channelId);
return await SendJsonAsync<Message>("PATCH", () => $"channels/{channelId}/messages/{messageId}", args, ids, clientBucketId: ClientBucket.SendEditId, options: options).ConfigureAwait(false);
}
public async Task AddReactionAsync(ulong channelId, ulong messageId, string emoji, RequestOptions options = null)
{
Preconditions.NotEqual(channelId, 0, nameof(channelId));
Preconditions.NotEqual(messageId, 0, nameof(messageId));
Preconditions.NotNullOrWhitespace(emoji, nameof(emoji));

options = RequestOptions.CreateOrClone(options);

var ids = new BucketIds(channelId: channelId);

await SendAsync("PUT", () => $"channels/{channelId}/messages/{messageId}/reactions/{emoji}/@me", ids, options: options).ConfigureAwait(false);
}
public async Task RemoveReactionAsync(ulong channelId, ulong messageId, ulong userId, string emoji, RequestOptions options = null)
{
Preconditions.NotEqual(channelId, 0, nameof(channelId));
Preconditions.NotEqual(messageId, 0, nameof(messageId));
Preconditions.NotNullOrWhitespace(emoji, nameof(emoji));

options = RequestOptions.CreateOrClone(options);

var ids = new BucketIds(channelId: channelId);

await SendAsync("DELETE", () => $"channels/{channelId}/messages/{messageId}/reactions/{emoji}/{userId}", ids, options: options).ConfigureAwait(false);
}
public async Task<IReadOnlyCollection<User>> GetReactionUsersAsync(ulong channelId, ulong messageId, string emoji, GetReactionUsersParams args, RequestOptions options = null)
{
Preconditions.NotEqual(channelId, 0, nameof(channelId));
Preconditions.NotEqual(messageId, 0, nameof(messageId));
Preconditions.NotNullOrWhitespace(emoji, nameof(emoji));
Preconditions.NotNull(args, nameof(args));
Preconditions.GreaterThan(args.Limit, 0, nameof(args.Limit));
Preconditions.AtMost(args.Limit, DiscordConfig.MaxUsersPerBatch, nameof(args.Limit));
Preconditions.GreaterThan(args.AfterUserId, 0, nameof(args.AfterUserId));
options = RequestOptions.CreateOrClone(options);

int limit = args.Limit.GetValueOrDefault(int.MaxValue);
ulong afterUserId = args.AfterUserId.GetValueOrDefault(0);

var ids = new BucketIds(channelId: channelId);
Expression<Func<string>> endpoint = () => $"channels/{channelId}/messages/{messageId}/reactions/{emoji}";
return await SendAsync<IReadOnlyCollection<User>>("GET", endpoint, ids, options: options).ConfigureAwait(false);
}
public async Task AckMessageAsync(ulong channelId, ulong messageId, RequestOptions options = null)
{
Preconditions.NotEqual(channelId, 0, nameof(channelId));


+ 8
- 0
src/Discord.Net.Core/API/Rest/GetReactionUsersParams.cs View File

@@ -0,0 +1,8 @@
namespace Discord.API.Rest
{
public class GetReactionUsersParams
{
public Optional<int> Limit { get; set; }
public Optional<ulong> AfterUserId { get; set; }
}
}

+ 12
- 1
src/Discord.Net.Core/Entities/Messages/IUserMessage.cs View File

@@ -1,5 +1,6 @@
using Discord.API.Rest;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Discord
@@ -12,7 +13,17 @@ namespace Discord
Task PinAsync(RequestOptions options = null);
/// <summary> Removes this message from its channel's pinned messages. </summary>
Task UnpinAsync(RequestOptions options = null);

/// <summary> Adds a reaction to this message. </summary>
Task AddReactionAsync(Emoji emoji, RequestOptions options = null);
/// <summary> Adds a reaction to this message. </summary>
Task AddReactionAsync(string emoji, RequestOptions options = null);
/// <summary> Removes a reaction from message. </summary>
Task RemoveReactionAsync(Emoji emoji, IUser user, RequestOptions options = null);
/// <summary> Removes a reaction from this message. </summary>
Task RemoveReactionAsync(string emoji, IUser user, RequestOptions options = null);
Task<IReadOnlyCollection<IUser>> GetReactionUsersAsync(string emoji, Action<GetReactionUsersParams> func, RequestOptions options = null);

/// <summary> Transforms this message's text into a human readable form by resolving its tags. </summary>
string Resolve(
TagHandling userHandling = TagHandling.Name,


+ 1
- 0
src/Discord.Net.Core/Utils/Preconditions.cs View File

@@ -45,6 +45,7 @@ namespace Discord
throw new ArgumentException("Argument cannot be blank.", name);
}
}

//Numerics
public static void NotEqual(sbyte obj, sbyte value, string name) { if (obj == value) throw new ArgumentOutOfRangeException(name); }


+ 23
- 0
src/Discord.Net.Rest/Entities/Messages/MessageHelper.cs View File

@@ -23,6 +23,29 @@ namespace Discord.Rest
await client.ApiClient.DeleteMessageAsync(msg.Channel.Id, msg.Id, options).ConfigureAwait(false);
}

public static async Task AddReactionAsync(IMessage msg, Emoji emoji, BaseDiscordClient client, RequestOptions options)
=> await AddReactionAsync(msg, $"{emoji.Name}:{emoji.Id}", client, options).ConfigureAwait(false);
public static async Task AddReactionAsync(IMessage msg, string emoji, BaseDiscordClient client, RequestOptions options)
{
await client.ApiClient.AddReactionAsync(msg.Channel.Id, msg.Id, emoji, options).ConfigureAwait(false);
}

public static async Task RemoveReactionAsync(IMessage msg, IUser user, Emoji emoji, BaseDiscordClient client, RequestOptions options)
=> await RemoveReactionAsync(msg, user, $"{emoji.Name}:{emoji.Id}", client, options).ConfigureAwait(false);
public static async Task RemoveReactionAsync(IMessage msg, IUser user, string emoji, BaseDiscordClient client,
RequestOptions options)
{
await client.ApiClient.RemoveReactionAsync(msg.Channel.Id, msg.Id, user.Id, emoji, options);
}

public static async Task<IReadOnlyCollection<IUser>> GetReactionUsersAsync(IMessage msg, string emoji,
Action<GetReactionUsersParams> func, BaseDiscordClient client, RequestOptions options)
{
var args = new GetReactionUsersParams();
func(args);
return (await client.ApiClient.GetReactionUsersAsync(msg.Channel.Id, msg.Id, emoji, args, options).ConfigureAwait(false)).Select(u => u as IUser).Where(u => u != null).ToImmutableArray();
}

public static async Task PinAsync(IMessage msg, BaseDiscordClient client,
RequestOptions options)
{


+ 14
- 0
src/Discord.Net.Rest/Entities/Messages/RestUserMessage.cs View File

@@ -117,6 +117,20 @@ namespace Discord.Rest
Update(model);
}

public Task AddReactionAsync(Emoji emoji, RequestOptions options)
=> MessageHelper.AddReactionAsync(this, emoji, Discord, options);
public Task AddReactionAsync(string emoji, RequestOptions options)
=> MessageHelper.AddReactionAsync(this, emoji, Discord, options);

public Task RemoveReactionAsync(Emoji emoji, IUser user, RequestOptions options)
=> MessageHelper.RemoveReactionAsync(this, user, emoji, Discord, options);
public Task RemoveReactionAsync(string emoji, IUser user, RequestOptions options)
=> MessageHelper.RemoveReactionAsync(this, user, emoji, Discord, options);
public Task<IReadOnlyCollection<IUser>> GetReactionUsersAsync(string emoji, Action<GetReactionUsersParams> func, RequestOptions options)
=> MessageHelper.GetReactionUsersAsync(this, emoji, func, Discord, options);

public Task PinAsync(RequestOptions options)
=> MessageHelper.PinAsync(this, Discord, options);
public Task UnpinAsync(RequestOptions options)


+ 13
- 0
src/Discord.Net.Rpc/Entities/Messages/RpcUserMessage.cs View File

@@ -101,6 +101,19 @@ namespace Discord.Rpc
public Task ModifyAsync(Action<ModifyMessageParams> func, RequestOptions options)
=> MessageHelper.ModifyAsync(this, Discord, func, options);

public Task AddReactionAsync(Emoji emoji, RequestOptions options)
=> MessageHelper.AddReactionAsync(this, emoji, Discord, options);
public Task AddReactionAsync(string emoji, RequestOptions options)
=> MessageHelper.AddReactionAsync(this, emoji, Discord, options);

public Task RemoveReactionAsync(Emoji emoji, IUser user, RequestOptions options)
=> MessageHelper.RemoveReactionAsync(this, user, emoji, Discord, options);
public Task RemoveReactionAsync(string emoji, IUser user, RequestOptions options)
=> MessageHelper.RemoveReactionAsync(this, user, emoji, Discord, options);
public Task<IReadOnlyCollection<IUser>> GetReactionUsersAsync(string emoji, Action<GetReactionUsersParams> func, RequestOptions options)
=> MessageHelper.GetReactionUsersAsync(this, emoji, func, Discord, options);

public Task PinAsync(RequestOptions options)
=> MessageHelper.PinAsync(this, Discord, options);
public Task UnpinAsync(RequestOptions options)


+ 13
- 0
src/Discord.Net.WebSocket/Entities/Messages/SocketUserMessage.cs View File

@@ -113,6 +113,19 @@ namespace Discord.WebSocket
public Task ModifyAsync(Action<ModifyMessageParams> func, RequestOptions options = null)
=> MessageHelper.ModifyAsync(this, Discord, func, options);

public Task AddReactionAsync(Emoji emoji, RequestOptions options)
=> MessageHelper.AddReactionAsync(this, emoji, Discord, options);
public Task AddReactionAsync(string emoji, RequestOptions options)
=> MessageHelper.AddReactionAsync(this, emoji, Discord, options);

public Task RemoveReactionAsync(Emoji emoji, IUser user, RequestOptions options)
=> MessageHelper.RemoveReactionAsync(this, user, emoji, Discord, options);
public Task RemoveReactionAsync(string emoji, IUser user, RequestOptions options)
=> MessageHelper.RemoveReactionAsync(this, user, emoji, Discord, options);
public Task<IReadOnlyCollection<IUser>> GetReactionUsersAsync(string emoji, Action<GetReactionUsersParams> func, RequestOptions options)
=> MessageHelper.GetReactionUsersAsync(this, emoji, func, Discord, options);

public Task PinAsync(RequestOptions options = null)
=> MessageHelper.PinAsync(this, Discord, options);
public Task UnpinAsync(RequestOptions options = null)


Loading…
Cancel
Save