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
2 changes: 1 addition & 1 deletion lib/discordrb/api/channel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 = URI.encode_www_form({ limit: limit || 100, before: before_id, after: after_id }.compact)
swarley marked this conversation as resolved.
Show resolved Hide resolved
Discordrb::API.request(
:channels_cid_messages_mid_reactions_emoji,
channel_id,
Expand Down
55 changes: 55 additions & 0 deletions spec/api/channel_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# frozen_string_literal: true

require 'discordrb'

describe Discordrb::API::Channel do
let(:token) { double('token', to_s: 'bot_token') }
let(:channel_id) { instance_double(String, 'channel_id', to_s: 'channel_id') }

describe '.get_reactions' do
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 do
allow(Discordrb::API).to receive(:request)
.with(anything, channel_id, :get, kind_of(String), any_args)
end

it 'sends requests' do
expect(Discordrb::API).to receive(:request)
.with(
anything,
channel_id,
:get,
"#{Discordrb::API.api_base}/channels/#{channel_id}/messages/#{message_id}/reactions/test?limit=27&before=#{before_id}&after=#{after_id}",
any_args
)
Discordrb::API::Channel.get_reactions(token, channel_id, message_id, 'test', before_id, after_id, 27)
end

it 'percent-encodes emoji' do
expect(Discordrb::API).to receive(:request)
.with(
anything,
channel_id,
:get,
"#{Discordrb::API.api_base}/channels/#{channel_id}/messages/#{message_id}/reactions/%F0%9F%91%8D?limit=27&before=#{before_id}&after=#{after_id}",
any_args
)
Discordrb::API::Channel.get_reactions(token, channel_id, message_id, "\u{1F44D}", before_id, after_id, 27)
end

it 'uses the maximum limit of 100 if nil is provided' do
expect(Discordrb::API).to receive(:request)
.with(
anything,
channel_id,
:get,
"#{Discordrb::API.api_base}/channels/#{channel_id}/messages/#{message_id}/reactions/test?limit=100&before=#{before_id}&after=#{after_id}",
any_args
)
Discordrb::API::Channel.get_reactions(token, channel_id, message_id, 'test', before_id, after_id, nil)
end
end
end
47 changes: 47 additions & 0 deletions spec/data/message_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,53 @@
end
end

describe '#reacted_with' do
let(:message) { described_class.new(message_data, bot) }
let(:emoji) { double('emoji') }

fixture :user_data, %i[user]

before do
# Return the appropriate number of users based on after_id
allow(Discordrb::API::Channel).to receive(:get_reactions)
.with(any_args, nil, anything) # ..., after_id, limit
.and_return([user_data].to_json)

allow(Discordrb::API::Channel).to receive(:get_reactions)
.with(any_args, user_data['id'].to_i, anything)
.and_return([].to_json)
end

it 'calls the API method' do
expect(Discordrb::API::Channel).to receive(:get_reactions)
.with(any_args, '\u{1F44D}', nil, nil, 27)

message.reacted_with('\u{1F44D}', limit: 27)
end

it 'defaults to a limit of 100' do
expect(Discordrb::API::Channel).to receive(:get_reactions)
.with(any_args, '\u{1F44D}', nil, nil, 100)

message.reacted_with('\u{1F44D}')
end

it 'fetches all users when limit is nil' do
expect(Discordrb::Paginator).to receive(:new).with(nil, :down)

message.reacted_with('\u{1F44D}', limit: nil)
end

it 'converts Emoji to strings' do
allow(emoji).to receive(:to_reaction).and_return('123')

expect(Discordrb::API::Channel).to receive(:get_reactions)
.with(any_args, '123', nil, nil, anything)

message.reacted_with(emoji)
end
end

describe '#reply!' do
let(:message) { described_class.new(message_data, bot) }
let(:content) { instance_double('String', 'content') }
Expand Down