Skip to content
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

Don't send an empty limit query parameter for Channel#get_reactions #39

Merged
merged 12 commits into from
Apr 29, 2021

Conversation

kaine119
Copy link
Contributor

@kaine119 kaine119 commented Mar 6, 2021

Summary

Fixes #38.
Discord tries to parse the limit parameter as an integer, if nothing is sent a HTTP 400 is returned. This sets a default to the maximum instead of the usual 25, to reduce the number of requests needed to get all users when limit: nil is passed.


Added

Specs for Message#reacted_with and API::Channel.get_reactions

Changed

  • API::Channel.get_reactions: the query param limit defaults to 100 instead of the empty string when limit is nil.

@Daniel-Worrall
Copy link
Member

Been talking about this one in the discord before submission.
LGTM, would love some specs, and let's get CI before any merge

@kaine119 kaine119 force-pushed the get_reactions_limit_default branch from eb458d7 to 852958c Compare March 6, 2021 18:54
@@ -201,7 +201,7 @@ def delete_user_reaction(token, channel_id, message_id, emoji, user_id)
# https://discord.com/developers/docs/resources/channel#get-reactions
def get_reactions(token, channel_id, message_id, emoji, before_id, after_id, limit = 100)
emoji = URI.encode_www_form_component(emoji) unless emoji.ascii_only?
query_string = "limit=#{limit}#{"&before=#{before_id}" if before_id}#{"&after=#{after_id}" if after_id}"
query_string = "limit=#{limit || 100}#{"&before=#{before_id}" if before_id}#{"&after=#{after_id}" if after_id}"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer to see this as

query_string = URI.encode_www_form({ limit: limit, before: before_id, after: after_id }.compact)

@kaine119 kaine119 requested a review from swarley March 7, 2021 07:46
Copy link
Member

@Daniel-Worrall Daniel-Worrall left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm so far. Just some nitpicky spec changes going forward. I'm aware some parts of the library also use these things I've criticised which is where you may have taken inspiration from, but it's on the agenda to go over the specs to make them more consistent, and document guidelines in a Contributing section

Comment on lines 15 to 23
let(:message_id) { double('message_id') }
let(:before_id) { double('before_id') }
let(:after_id) { double('after_id') }

before do
allow(message_id).to receive(:to_s).and_return('message_id')
allow(before_id).to receive(:to_s).and_return('before_id')
allow(after_id).to receive(:to_s).and_return('after_id')

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and here

Suggested change
let(:message_id) { double('message_id') }
let(:before_id) { double('before_id') }
let(:after_id) { double('after_id') }
before do
allow(message_id).to receive(:to_s).and_return('message_id')
allow(before_id).to receive(:to_s).and_return('before_id')
allow(after_id).to receive(:to_s).and_return('after_id')
let(:message_id) { double('message_id', to_s: 'message_id') }
let(:before_id) { double('before_id', to_s: 'before_id') }
let(:after_id) { double('after_id', to_s: 'after_id') }
before

Comment on lines 6 to 12
let(:token) { double('token') }
let(:channel_id) { instance_double(String, 'channel_id') }

before do
allow(token).to receive(:to_s).and_return('bot_token')
allow(channel_id).to receive(:to_s).and_return('channel_id')
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can move these method stubs inside the double initialiser

Suggested change
let(:token) { double('token') }
let(:channel_id) { instance_double(String, 'channel_id') }
before do
allow(token).to receive(:to_s).and_return('bot_token')
allow(channel_id).to receive(:to_s).and_return('channel_id')
end
let(:token) { double('token', to_s: 'bot_token') }
let(:channel_id) { instance_double(String, 'channel_id', to_s: 'channel_id') }


it 'sends requests' do
Discordrb::API::Channel.get_reactions(token, channel_id, message_id, 'test', before_id, after_id, 27)
expect(Discordrb::API).to have_received(:request)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

while have_received is valid spec'ing known as using spies, I'd prefer we stay consistent and put the expectations before the call


it 'percent-encodes emoji' do
Discordrb::API::Channel.get_reactions(token, channel_id, message_id, "\u{1F44D}", before_id, after_id, 27)
expect(Discordrb::API).to have_received(:request)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here


it 'uses the maximum limit of 100 if nil is provided' do
Discordrb::API::Channel.get_reactions(token, channel_id, message_id, 'test', before_id, after_id, nil)
expect(Discordrb::API).to have_received(:request)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and here

it 'calls the API method' do
message.reacted_with('\u{1F44D}', limit: 27)

expect(Discordrb::API::Channel).to have_received(:get_reactions)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and here

it 'defaults to a limit of 100' do
message.reacted_with('\u{1F44D}')

expect(Discordrb::API::Channel).to have_received(:get_reactions)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto


message.reacted_with(emoji)

expect(Discordrb::API::Channel).to have_received(:get_reactions)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

@kaine119 kaine119 requested a review from Daniel-Worrall March 8, 2021 06:50
Copy link
Member

@Daniel-Worrall Daniel-Worrall left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm.

Was gonna say "let's see about CI" but it seems to be working now :)

Copy link
Member

@swarley swarley left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just one small fix and then things should be good to go!

lib/discordrb/api/channel.rb Show resolved Hide resolved
@swarley swarley merged commit 504285e into shardlab:main Apr 29, 2021
@swarley
Copy link
Member

swarley commented Apr 29, 2021

Thank you!

@Daniel-Worrall Daniel-Worrall changed the title Don't send an empty limit query parameter for Channel#get_channel Don't send an empty limit query parameter for Channel#get_reactions Jun 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bug: Message#reacted_with limit: nil gives a 400
3 participants