Skip to content

Commit

Permalink
Move members API to Room
Browse files Browse the repository at this point in the history
  • Loading branch information
alinavancea committed Mar 29, 2021
1 parent b16f0ca commit 4eae9ef
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 109 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ This gem supports the following Rocket.Chat APIs (Tested against Rocket.Chat v0.
* /api/v1/groups.setReadOnly
* /api/v1/groups.setTopic
* /api/v1/groups.setType
* [/api/v1/groups.members](docs/groups.md#groupsmembers)
* /api/v1/groups.unarchive

#### Users
Expand Down
13 changes: 13 additions & 0 deletions docs/groups.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,16 @@ To add a moderator to a group, the same options as an `add_owner` request can be
#### groups.removeModerator

To remove a moderator from a group, the same options as an `add_owner` request can be used.

### groups.members

This method returns the users of participants of a private group.

```ruby
require 'rocketchat'

rocket_server = RocketChat::Server.new('http://your.server.address/')
session = rocket_server.login('username', 'password')
session.groups.members(name: 'some_channel_name')

```
21 changes: 0 additions & 21 deletions lib/rocket_chat/messages/channel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,27 +58,6 @@ def online(room_id: nil, name: nil)
response['online'].map { |hash| RocketChat::User.new hash } if response['success']
end

#
# channels.members REST API
# @param [String] room_id Rocket.Chat room id
# @param [String] name Rocket.Chat room name
# @param [Integer] offset Query offset
# @param [Integer] count Query count/limit
# @param [Hash] sort Query field sort hash. eg `{ msgs: 1, name: -1 }`
# @param [Hash] fields Query fields to return. eg `{ name: 1, ro: 0 }`
# @param [Hash] query The query. `{ active: true, type: { '$in': ['name', 'general'] } }`
# @return [Room[]]
# @raise [HTTPError, StatusError]
#
def members(room_id: nil, name: nil, offset: nil, count: nil, sort: nil, fields: nil, query: {})
response = session.request_json(
'/api/v1/channels.members',
body: build_list_body(offset, count, sort, fields, query).merge(room_params(room_id, name))
)

response['members'].map { |hash| RocketChat::User.new hash } if response['success']
end

# Keys for set_attr:
# * [String] description A room's description
# * [String] join_code Code to join a channel
Expand Down
19 changes: 19 additions & 0 deletions lib/rocket_chat/messages/room.rb
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,25 @@ def set_attr(room_id: nil, name: nil, **setting)
)['success']
end

#
# *.members* REST API
# @param [String] room_id Rocket.Chat room id
# @param [String] name Rocket.Chat room name
# @param [Integer] offset Query offset
# @param [Integer] count Query count/limit
# @param [Hash] sort Query field sort hash. eg `{ msgs: 1, name: -1 }`
# @return [Users[]]
# @raise [HTTPError, StatusError]
#
def members(room_id: nil, name: nil, offset: nil, count: nil, sort: nil)
response = session.request_json(
self.class.api_path('members'),
body: room_params(room_id, name).merge(build_list_body(offset, count, sort, nil, nil))
)

response['members'].map { |hash| RocketChat::User.new hash } if response['success']
end

private

attr_reader :session
Expand Down
88 changes: 0 additions & 88 deletions spec/rocket_chat/messages/channel_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -141,92 +141,4 @@
end
end
end

describe '#members' do
let(:members_response) {
{
body: {
success: true,
members: [
{
_id: 'rocketID1',
username: 'rocketUserName1'
},
{
_id: 'rocketID2',
username: 'rocketUserName2'
}
]
}.to_json,
status: 200
}
}
let(:empty_channel_response) do
{
body: {
success: true,
members: []
}.to_json,
status: 200
}
end

let(:invalid_channel_response) do
{
body: {
success: false,
error: 'Channel does not exists'
}.to_json,
status: 400
}
end

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

stub_authed_request(:get, described_class.api_path('members?query=%7B%7D&roomName=wrong-room'))
.to_return(invalid_channel_response)

stub_authed_request(:get, described_class.api_path('members?query=%7B%7D&roomName=room-one'))
.to_return(members_response)

stub_authed_request(:get, described_class.api_path('members?query=%7B%7D&roomName=empty-room'))
.to_return(empty_channel_response)
end

context 'with an invalid room name' do
it 'raises a channel existence error' do
expect do
scope.members(name: 'wrong-room')
end.to raise_error RocketChat::StatusError, 'Channel does not exists'
end
end

context 'with a valid room name' do
it 'returns no users for an empty room' do
expect(scope.members(name: 'empty-room')).to eq []
end

it 'returns online users for a filled room' do
members = scope.members(name: 'room-one')

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

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

it 'raises an authentication status error' do
expect do
scope.members(name: 'authed')
end.to raise_error RocketChat::StatusError, 'You must be logged in to do this.'
end
end
end
end
89 changes: 89 additions & 0 deletions spec/shared/room_behaviors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,95 @@
end
end

describe '#members' do
let(:members_response) do
{
body: {
success: true,
members: [
{
_id: 'rocketID1',
username: 'rocketUserName1'
},
{
_id: 'rocketID2',
username: 'rocketUserName2'
}
]
}.to_json,
status: 200
}
end

let(:empty_channel_response) do
{
body: {
success: true,
members: []
}.to_json,
status: 200
}
end

let(:invalid_channel_response) do
{
body: {
success: false,
error: 'Channel does not exists'
}.to_json,
status: 400
}
end

before do
# Stubs for /api/v1/channels.members REST API
stub_unauthed_request :get, described_class.api_path('members?roomName=authed')

stub_authed_request(:get, described_class.api_path('members?roomName=wrong-room'))
.to_return(invalid_channel_response)

stub_authed_request(:get, described_class.api_path('members?roomName=room-one'))
.to_return(members_response)

stub_authed_request(:get, described_class.api_path('members?roomName=empty-room'))
.to_return(empty_channel_response)
end

context 'with an invalid room name' do
it 'raises a channel existence error' do
expect do
scope.members(name: 'wrong-room')
end.to raise_error RocketChat::StatusError, 'Channel does not exists'
end
end

context 'with a valid room name' do
it 'returns no users for an empty room' do
expect(scope.members(name: 'empty-room')).to eq []
end

it 'returns online users for a filled room' do
members = scope.members(name: 'room-one')

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

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

it 'raises an authentication status error' do
expect do
scope.members(name: 'authed')
end.to raise_error RocketChat::StatusError, 'You must be logged in to do this.'
end
end
end

describe '#set_attr' do
before do
# Stubs for /api/v1/?.leave REST API
Expand Down

0 comments on commit 4eae9ef

Please sign in to comment.