Skip to content

Commit

Permalink
Fix room (channel/group) online API queries (#38)
Browse files Browse the repository at this point in the history
These RocketChat APIs were updated to use the more flexible query
syntax in favour of limiting to just room ID and name.
  • Loading branch information
abrom authored Jul 21, 2022
1 parent f0c7188 commit 9c90f89
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 16 deletions.
4 changes: 3 additions & 1 deletion lib/rocket_chat/messages/channel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,15 @@ def list(offset: nil, count: nil, sort: nil, fields: nil, query: nil)
#
# channels.online REST API
# @param [String] room_id Rocket.Chat room id
# @param [String] name Rocket.Chat room name
# @return [Users[]]
# @note Must provide either `room_id` or `name` parameter. `room_id` will take precedence if providing both
# @raise [HTTPError, StatusError]
#
def online(room_id: nil, name: nil)
response = session.request_json(
'/api/v1/channels.online',
body: room_params(room_id, name)
body: { query: room_query_params(room_id, name) }
)

response['online'].map { |hash| RocketChat::User.new hash } if response['success']
Expand Down
4 changes: 3 additions & 1 deletion lib/rocket_chat/messages/group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,15 @@ def list_all(offset: nil, count: nil, sort: nil, fields: nil, query: nil)
#
# groups.online REST API
# @param [String] room_id Rocket.Chat room id
# @param [String] name Rocket.Chat room name
# @return [Users[]]
# @note Must provide either `room_id` or `name` parameter. `room_id` will take precedence if providing both
# @raise [HTTPError, StatusError]
#
def online(room_id: nil, name: nil)
response = session.request_json(
'/api/v1/groups.online',
body: room_params(room_id, name)
body: { query: room_query_params(room_id, name) }
)

response['online'].map { |hash| RocketChat::User.new hash } if response['success']
Expand Down
10 changes: 10 additions & 0 deletions lib/rocket_chat/messages/room_support.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ def room_params(id, name)
{}
end
end

def room_query_params(id, name)
if id
{ _id: id }
elsif name
{ name: name }
else
{}
end.to_json
end
end
end
end
8 changes: 4 additions & 4 deletions rocketchat.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ Gem::Specification.new do |spec|
spec.add_development_dependency 'bundler', ['>= 1.11', '< 3.0']
spec.add_development_dependency 'rake', '>= 12.3.3'
spec.add_development_dependency 'rspec', '~> 3.0'
spec.add_development_dependency 'rubocop', '~> 1.12'
spec.add_development_dependency 'rubocop-performance', '~> 1.10'
spec.add_development_dependency 'rubocop-rake', '~> 0.5'
spec.add_development_dependency 'rubocop-rspec', '~> 2.2'
spec.add_development_dependency 'rubocop', '~> 1.28'
spec.add_development_dependency 'rubocop-performance', '~> 1.13'
spec.add_development_dependency 'rubocop-rake', '~> 0.6'
spec.add_development_dependency 'rubocop-rspec', '~> 2.10'
spec.add_development_dependency 'simplecov', '~> 0.16', '< 0.18'
spec.add_development_dependency 'webmock', '~> 3.12'
spec.add_development_dependency 'yard', '~> 0.9.11'
Expand Down
23 changes: 19 additions & 4 deletions spec/rocket_chat/messages/channel_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,18 @@

before do
# Stubs for /api/v1/channels.online REST API
stub_unauthed_request :get, described_class.api_path('online?roomName=authed')
stub_unauthed_request :get, described_class.api_path('online?query=%7B%22name%22:%22authed%22%7D')

stub_authed_request(:get, described_class.api_path('online?roomName=wrong-room'))
stub_authed_request(:get, described_class.api_path('online?query=%7B%22name%22:%22wrong-room%22%7D'))
.to_return(invalid_room_response)

stub_authed_request(:get, described_class.api_path('online?roomName=room-one'))
stub_authed_request(:get, described_class.api_path('online?query=%7B%22name%22:%22room-one%22%7D'))
.to_return(online_users_response)

stub_authed_request(:get, described_class.api_path('online?roomName=empty-room'))
stub_authed_request(:get, described_class.api_path('online?query=%7B%22_id%22:%22TZtANZwQt369rR4UR%22%7D'))
.to_return(online_users_response)

stub_authed_request(:get, described_class.api_path('online?query=%7B%22name%22:%22empty-room%22%7D'))
.to_return(empty_room_response)
end

Expand Down Expand Up @@ -131,6 +134,18 @@
end
end

context 'with a valid room id' do
it 'returns online users for a filled room' do
online_users = scope.online(room_id: 'TZtANZwQt369rR4UR')

expect(online_users.map(&:class)).to eq [RocketChat::User, RocketChat::User]
expect(online_users[0].id).to eq 'rocketID1'
expect(online_users[0].username).to eq 'rocketUserName1'
expect(online_users[1].id).to eq 'rocketID2'
expect(online_users[1].username).to eq 'rocketUserName2'
end
end

context 'with an invalid session token' do
let(:token) { RocketChat::Token.new(authToken: nil, groupId: nil) }

Expand Down
23 changes: 19 additions & 4 deletions spec/rocket_chat/messages/group_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -251,15 +251,18 @@

before do
# Stubs for /api/v1/groups.online REST API
stub_unauthed_request :get, described_class.api_path('online?roomName=authed')
stub_unauthed_request :get, described_class.api_path('online?query=%7B%22name%22:%22authed%22%7D')

stub_authed_request(:get, described_class.api_path('online?roomName=wrong-room'))
stub_authed_request(:get, described_class.api_path('online?query=%7B%22name%22:%22wrong-room%22%7D'))
.to_return(invalid_room_response)

stub_authed_request(:get, described_class.api_path('online?roomName=room-one'))
stub_authed_request(:get, described_class.api_path('online?query=%7B%22name%22:%22room-one%22%7D'))
.to_return(online_users_response)

stub_authed_request(:get, described_class.api_path('online?roomName=empty-room'))
stub_authed_request(:get, described_class.api_path('online?query=%7B%22_id%22:%22TZtANZwQt369rR4UR%22%7D'))
.to_return(online_users_response)

stub_authed_request(:get, described_class.api_path('online?query=%7B%22name%22:%22empty-room%22%7D'))
.to_return(empty_room_response)
end

Expand Down Expand Up @@ -287,6 +290,18 @@
end
end

context 'with a valid room id' do
it 'returns online users for a filled room' do
online_users = scope.online(room_id: 'TZtANZwQt369rR4UR')

expect(online_users.map(&:class)).to eq [RocketChat::User, RocketChat::User]
expect(online_users[0].id).to eq 'rocketID1'
expect(online_users[0].username).to eq 'rocketUserName1'
expect(online_users[1].id).to eq 'rocketID2'
expect(online_users[1].username).to eq 'rocketUserName2'
end
end

context 'with an invalid session token' do
let(:token) { RocketChat::Token.new(authToken: nil, groupId: nil) }

Expand Down
4 changes: 2 additions & 2 deletions spec/shared/room_behaviors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
}
end
let(:invalid_room_message) do
%(The required "roomId" or "roomName" param provided does not match any #{described_class.field}) \
' [error-room-not-found]'
%(The required "roomId" or "roomName" param provided does not match any #{described_class.field} ) \
'[error-room-not-found]'
end
let(:invalid_room_body) do
{
Expand Down

0 comments on commit 9c90f89

Please sign in to comment.