Skip to content

Commit

Permalink
Add IM integration with create and counters endpoints (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
christianmoretti authored and abrom committed Jan 5, 2019
1 parent adac25c commit 88034a8
Show file tree
Hide file tree
Showing 7 changed files with 325 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ This gem supports the following Rocket.Chat APIs (Tested against Rocket.Chat v0.
* [/api/v1/chat.postMessage](docs/chat.md#postmessage)
* [/api/v1/chat.update](docs/chat.md#update)

#### IM
* [/api/v1/im.create](docs/im.md#create)
* [/api/v1/im.counters](docs/im.md#counters)

#### Channels
* /api/v1/channels.archive
* [/api/v1/channels.create](docs/channels.md#channelscreate)
Expand Down
27 changes: 27 additions & 0 deletions docs/im.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
### IM API

RocketChat's IM interface.

#### im.create

Create a direct message.

```ruby
require 'rocketchat'

rocket_server = RocketChat::Server.new('http://your.server.address/')
session = rocket_server.login('username', 'password')
room = session.im.create(username: 'rocket.cat')
```

#### im.counters

Retrieves the count information using room_id *or* username

```ruby
require 'rocketchat'

rocket_server = RocketChat::Server.new('http://your.server.address/')
session = rocket_server.login('username', 'password')
im_summary = session.im.counters(room_id: 'room_id', username: 'rocket.cat')
```
54 changes: 54 additions & 0 deletions lib/rocket_chat/im_summary.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
module RocketChat
#
# Rocket.Chat Info
#
class ImSummary
# Raw info data
attr_reader :data

#
# @param [Hash] data Raw info data
#
def initialize(data)
@data = Util.stringify_hash_keys data
end

def joined
data['joined']
end

# Qty of menbers in the chat
def members
data['members']
end

# Qty of unread messages
def unreads
data['unreads']
end

# Timestamp
def unreads_from
data['unreadsFrom']
end

# Qty of messages in the chat
def msgs
data['msgs']
end

# Last message sent
def latest
data['latest']
end

# Qty of mentions
def user_mentions
data['userMentions']
end

def success
data['success']
end
end
end
52 changes: 52 additions & 0 deletions lib/rocket_chat/messages/im.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
module RocketChat
module Messages
#
# Rocket.Chat Direct messages
#
class Im
#
# @param [Session] session Session
#
def initialize(session)
@session = session
end

#
# im.create REST API
# @param [String] username Rocket.Chat username
# @return [RocketChat::Room]
# @raise [HTTPError, StatusError]
#
def create(username:)
response = session.request_json(
'/api/v1/im.create',
method: :post,
body: { username: username }
)
RocketChat::Room.new response['room']
end

#
# im.counters REST API
# @param [String] room_id Rocket.Chat roomId
# @param [String] username Rocket.Chat username
# @return [RocketChat::Im]
# @raise [HTTPError, StatusError]
#
def counters(room_id:, username: nil)
response = session.request_json(
'/api/v1/im.counters',
body: {
roomId: room_id,
username: username
}
)
RocketChat::ImSummary.new response
end

private

attr_reader :session
end
end
end
4 changes: 4 additions & 0 deletions lib/rocket_chat/session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ def chat
@chat ||= RocketChat::Messages::Chat.new(self)
end

def im
@im ||= RocketChat::Messages::Im.new(self)
end

#
# Settings messages proxy
# @return [Messages::Settings]
Expand Down
2 changes: 2 additions & 0 deletions lib/rocketchat.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
require 'rocket_chat/messages/channel'
require 'rocket_chat/messages/group'
require 'rocket_chat/messages/chat'
require 'rocket_chat/messages/im'

require 'rocket_chat/server'
require 'rocket_chat/session'
Expand All @@ -25,3 +26,4 @@
require 'rocket_chat/room'
require 'rocket_chat/user'
require 'rocket_chat/message'
require 'rocket_chat/im_summary'
182 changes: 182 additions & 0 deletions spec/rocket_chat/messages/im_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
require 'spec_helper'

describe RocketChat::Messages::Im do
let(:server) { RocketChat::Server.new(SERVER_URI) }
let(:token) { RocketChat::Token.new(authToken: AUTH_TOKEN, userId: USER_ID) }
let(:session) { RocketChat::Session.new(server, token) }

describe '#create' do
before do
# Stubs for /api/v1/im.create REST API
stub_unauthed_request :post, '/api/v1/im.create'

stub_authed_request(:post, '/api/v1/im.create')
.with(
body: {
username: 'rocket.cat'
}.to_json
).to_return(
body: {
room: {
_id: 'Lymsiu4Mn6xjTAan4RtMDEYc28fQ5aHpf4',
_updatedAt: '2018-03-26T19:11:50.711Z',
t: 'd',
msgs: 0,
ts: '2018-03-26T19:11:50.711Z',
meta: {
revision: 0,
created: 1522094603745,
version: 0
},
'$loki': 65,
usernames: [
'rocket.cat',
'user.test'
]
},
success: true
}.to_json,
status: 200
)

stub_authed_request(:post, '/api/v1/im.create')
.with(
body: {
username: 'non-existent.user'
}
).to_return(
body: { success: false }.to_json,
status: 400
)
end

context 'with a valid session' do
it 'create a new direct conversation' do
im = session.im.create username: 'rocket.cat'
expect(im).to be_a RocketChat::Room
expect(im.members).to eq %w[rocket.cat user.test]
end

it 'does not send attributes' do
expect do
session.im.create(username: nil)
end.to raise_error RocketChat::StatusError
end
end

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

it 'raises a status error' do
expect do
session.im.create username: 'rocket.cat'
end.to raise_error RocketChat::StatusError, 'You must be logged in to do this.'
end
end

context 'when the user no exists' do
it 'raises a status error' do
expect do
session.im.create(username: 'non-existent.user')
end.to raise_error RocketChat::StatusError
end
end
end

describe '#counters' do
before do
stub_unauthed_request :get, '/api/v1/im.counters?roomId=rocket.cat&username='

stub_authed_request(:get, '/api/v1/im.counters?roomId=rocket.cat&username=')
.to_return(
body: {
joined: true,
members: 2,
unreads: 0,
unreadsFrom: '2019-01-04T21:40:11.251Z',
msgs: 0,
latest: '2019-01-04T21:40:11.251Z',
userMentions: 0,
success: true
}.to_json,
status: 200
)

stub_authed_request(:get, '/api/v1/im.counters?roomId=rocket.cat&username=user.test')
.to_return(
body: {
joined: true,
members: 2,
unreads: 1,
unreadsFrom: '2019-01-05T20:37:09.130Z',
msgs: 0,
latest: '2019-01-05T20:37:09.130Z',
userMentions: 0,
success: true
}.to_json,
status: 200
)

stub_authed_request(:get, '/api/v1/im.counters?roomId=1234&username=')
.to_return(
body: {
success: false,
error: '[invalid-channel]',
errorType: 'invalid-channel'
}.to_json,
status: 400
)

stub_authed_request(:get, '/api/v1/im.counters?roomId=&username=')
.to_return(
body: {
success: false,
error: 'Body param "roomId" or "username" is required [error-room-param-not-provided]',
errorType: 'error-room-param-not-provided'
}.to_json,
status: 400
)
end

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

it 'raises a status error' do
expect do
session.im.counters room_id: 'rocket.cat'
end.to raise_error RocketChat::StatusError, 'You must be logged in to do this.'
end
end

context 'with a valid session' do
it 'get quantity of messages' do
im = session.im.counters room_id: 'rocket.cat'
expect(im).to be_a RocketChat::ImSummary
expect(im.members).to eq 2
expect(im.unreads).to eq 0
expect(im.msgs).to eq 0
expect(im.user_mentions).to eq 0
end

it 'get quantity of messages specifying the username' do
im = session.im.counters room_id: 'rocket.cat', username: 'user.test'
expect(im.joined).to eq true
expect(im.unreads_from).to eq '2019-01-05T20:37:09.130Z'
expect(im.latest).to eq '2019-01-05T20:37:09.130Z'
expect(im.success).to eq true
end

it 'does not send valid attributes' do
expect do
session.im.counters room_id: '1234'
end.to raise_error RocketChat::StatusError
end

it 'does not send any attributes' do
expect do
session.im.counters room_id: '', username: ''
end.to raise_error RocketChat::StatusError
end
end
end
end

0 comments on commit 88034a8

Please sign in to comment.