-
-
Notifications
You must be signed in to change notification settings - Fork 741
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Paginate reactions - solved #1007 #1022
Changes from 11 commits
c5676a1
4c36ad5
17c332f
0862674
a394224
f3b8419
18b7c21
57c55a7
87449cd
e819eac
64c8eb4
63da572
424259f
dbba84d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
namespace Discord.API.Rest | ||
namespace Discord.API.Rest | ||
{ | ||
internal class GetReactionUsersParams | ||
{ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,13 +46,46 @@ public static async Task RemoveAllReactionsAsync(IMessage msg, BaseDiscordClient | |
await client.ApiClient.RemoveAllReactionsAsync(msg.Channel.Id, msg.Id, options); | ||
} | ||
|
||
public static async Task<IReadOnlyCollection<IUser>> GetReactionUsersAsync(IMessage msg, IEmote emote, | ||
public static IAsyncEnumerable<IReadOnlyCollection<IUser>> GetReactionUsersAsync(IMessage msg, IEmote emote, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove GetReactionUserParams func here and add |
||
Action<GetReactionUsersParams> func, BaseDiscordClient client, RequestOptions options) | ||
{ | ||
var args = new GetReactionUsersParams(); | ||
func(args); | ||
string emoji = (emote is Emote e ? $"{e.Name}:{e.Id}" : emote.Name); | ||
return (await client.ApiClient.GetReactionUsersAsync(msg.Channel.Id, msg.Id, emoji, args, options).ConfigureAwait(false)).Select(u => RestUser.Create(client, u)).ToImmutableArray(); | ||
Preconditions.NotNull(emote, nameof(emote)); | ||
var emoji = (emote is Emote e ? $"{e.Name}:{e.Id}" : emote.Name); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Better check for nulls before (msg and emote). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The message shouldn't be null, since the whole class is internal, and the places where it is called (Message objects) can't be null else Nullrefs will be thrown before it enters that method. So is it strictly necessary? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Or is it standard throughout the rest of the library? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You do have a point about the message, but |
||
|
||
var arguments = new GetReactionUsersParams(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove this and the func invocation |
||
func(arguments); | ||
|
||
return new PagedAsyncEnumerable<IUser>( | ||
DiscordConfig.MaxUserReactionsPerBatch, | ||
async (info, ct) => | ||
{ | ||
var args = new GetReactionUsersParams(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Replace with ctor which sets Limits here, and remove func invocation |
||
func(args); | ||
args.Limit = info.PageSize; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
if (info.Position != null) | ||
args.AfterUserId = info.Position.Value; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The correct way imo would be creating a local "args" with the page size and the position. |
||
|
||
var models = await client.ApiClient.GetReactionUsersAsync(msg.Channel.Id, msg.Id, emoji, args, options).ConfigureAwait(false); | ||
var builder = ImmutableArray.CreateBuilder<IUser>(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IMO this should be replaced with models.Select and you should call ToImmutableArray on it |
||
|
||
foreach (var model in models) | ||
builder.Add(RestUser.Create(client, model)); | ||
|
||
return builder.ToImmutable(); | ||
}, | ||
nextPage: (info, lastPage) => | ||
{ | ||
if (lastPage.Count != DiscordConfig.MaxUsersPerBatch) | ||
return false; | ||
|
||
info.Position = lastPage.Max(x => x.Id); | ||
return true; | ||
}, | ||
start: arguments.AfterUserId.IsSpecified ? arguments.AfterUserId.Value : (ulong?)null, | ||
count: arguments.Limit.GetValueOrDefault(DiscordConfig.MaxUserReactionsPerBatch) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Replace these with from and limit suggested earlier |
||
); | ||
|
||
} | ||
|
||
public static async Task PinAsync(IMessage msg, BaseDiscordClient client, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -136,7 +136,7 @@ public Task RemoveReactionAsync(IEmote emote, IUser user, RequestOptions options | |
=> MessageHelper.RemoveReactionAsync(this, user, emote, Discord, options); | ||
public Task RemoveAllReactionsAsync(RequestOptions options = null) | ||
=> MessageHelper.RemoveAllReactionsAsync(this, Discord, options); | ||
public Task<IReadOnlyCollection<IUser>> GetReactionUsersAsync(IEmote emote, int limit = 100, ulong? afterUserId = null, RequestOptions options = null) | ||
public IAsyncEnumerable<IReadOnlyCollection<IUser>> GetReactionUsersAsync(IEmote emote, int limit = 100, ulong? afterUserId = null, RequestOptions options = null) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make limit required and remove afterUserId There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Which should afterUserId go then? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's handled as part of the async enumerable returned, so it's unnecessary. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How does the parameter get passed to MessageHelper.GetReactionUsersAsync then? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It doesn't need to be; our async enumerable code handles it automatically. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ohhhh, I see what you mean, where do I get the starting point for the Paged Enumerable then or should I leave that out? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The paged enumerable defaults to 0, which would retrieve all of the results anyway. |
||
=> MessageHelper.GetReactionUsersAsync(this, emote, x => { x.Limit = limit; x.AfterUserId = afterUserId ?? Optional.Create<ulong>(); }, Discord, options); | ||
|
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -130,7 +130,7 @@ public Task RemoveReactionAsync(IEmote emote, IUser user, RequestOptions options | |
=> MessageHelper.RemoveReactionAsync(this, user, emote, Discord, options); | ||
public Task RemoveAllReactionsAsync(RequestOptions options = null) | ||
=> MessageHelper.RemoveAllReactionsAsync(this, Discord, options); | ||
public Task<IReadOnlyCollection<IUser>> GetReactionUsersAsync(IEmote emote, int limit = 100, ulong? afterUserId = null, RequestOptions options = null) | ||
public IAsyncEnumerable<IReadOnlyCollection<IUser>> GetReactionUsersAsync(IEmote emote, int limit = 100, ulong? afterUserId = null, RequestOptions options = null) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make limit required and remove afterUserId |
||
=> MessageHelper.GetReactionUsersAsync(this, emote, x => { x.Limit = limit; x.AfterUserId = afterUserId ?? Optional.Create<ulong>(); }, Discord, options); | ||
|
||
public Task PinAsync(RequestOptions options = null) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Make limit required and remove afterUserId
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why make it required when we dont do that for
GetMessagesAsync
that follow a similar pattern? (Could change the= 100
to= DiscordConfig.MaxUserReactionsPerBatch
tho)And why remove an option that the discord api gives to us?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like GetMessagesAsync is the only one where we have a default: everywhere else limit is required (and even in IMessageChannel, limit is required!) or is not present at all.