Skip to content

Commit

Permalink
Add support for thread messages (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
MrRTi authored Apr 20, 2023
1 parent b588c42 commit 7201c8e
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 14 deletions.
8 changes: 7 additions & 1 deletion lib/rocket_chat/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ def id
data['_id']
end

# Message thread id
def tmid
data['tmid']
end

# Timestamp
def timestamp
Time.parse data['ts']
Expand Down Expand Up @@ -67,7 +72,8 @@ def inspect
object_id: object_id,
id: id,
room_id: room_id,
message: message
message: message,
tmid: tmid
)
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/rocket_chat/messages/chat.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def get_message(msg_id: nil)
# @param [String] room_id Rocket.Chat room id
# @param [String] name Rocket.Chat room name (coming soon)
# @param [String] channel Rocket.Chat channel name
# @param [Hash] params Optional params (text, alias, emoji, avatar & attachments)
# @param [Hash] params Optional params (text, alias, emoji, tmid, avatar & attachments)
# @return [RocketChat::Message]
# @raise [HTTPError, StatusError]
#
Expand All @@ -65,7 +65,7 @@ def post_message(room_id: nil, name: nil, channel: nil, **params)
method: :post,
body: room_params(room_id, name)
.merge(channel: channel)
.merge(Util.slice_hash(params, :text, :alias, :emoji, :avatar, :attachments))
.merge(Util.slice_hash(params, :text, :alias, :tmid, :emoji, :avatar, :attachments))
)
RocketChat::Message.new response['message'] if response['success']
end
Expand Down
7 changes: 6 additions & 1 deletion spec/rocket_chat/message_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
},
'rid' => 'GENERAL',
'_updatedAt' => '2016-12-14T20:57:05.119Z',
'_id' => 'jC9chsFddTvsbFQG7'
'_id' => 'jC9chsFddTvsbFQG7',
'tmid' => 'gcGai9bRREqokjyPc'
}
end
let(:message) { described_class.new data }
Expand Down Expand Up @@ -58,4 +59,8 @@
describe '#groupable' do
it { expect(message.groupable).to be false }
end

describe '#tmid' do
it { expect(message.tmid).to be 'gcGai9bRREqokjyPc' }
end
end
40 changes: 40 additions & 0 deletions spec/rocket_chat/messages/chat_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,37 @@
status: 200
)

stub_authed_request(:post, '/api/v1/chat.postMessage')
.with(
body: {
roomId: '1234',
channel: '#general',
text: 'Test message',
tmid: 'thread-12345'
}.to_json
).to_return(
body: {
success: true,
channel: 'general',
message: {
alias: '',
msg: 'Test message',
parseUrls: true,
groupable: false,
ts: '2016-12-14T20:56:05.117Z',
u: {
_id: 'y65tAmHs93aDChMWu',
username: 'graywolf336'
},
rid: '1234',
_updatedAt: '2016-12-14T20:56:05.119Z',
_id: 'jC9chsFddTvsbFQG7',
tmid: 'thread-12345'
}
}.to_json,
status: 200
)

stub_authed_request(:post, '/api/v1/chat.postMessage')
.with(
body: {
Expand Down Expand Up @@ -254,6 +285,15 @@
expect(message.message).to eq 'Test message'
end

it 'returns message for thread in room id' do
message = session.chat.post_message(
room_id: '1234', channel: '#general', text: 'Test message', tmid: 'thread-12345'
)
expect(message).to be_a RocketChat::Message
expect(message.tmid).to eq 'thread-12345'
expect(message.message).to eq 'Test message'
end

it 'does not send unknown attributes' do
message = session.chat.post_message(
room_id: '1234', channel: '#general', text: 'Other message', foo: 'bar'
Expand Down
10 changes: 5 additions & 5 deletions spec/rocket_chat/messages/group_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,14 @@
end

describe '#list_all' do
let(:room1) do
let(:room_one) do
{
_id: 123,
name: 'room-one'
}
end

let(:room2) do
let(:room_two) do
{
_id: 124,
name: 'room-two'
Expand All @@ -132,7 +132,7 @@
{
body: {
success: true,
groups: [room1]
groups: [room_one]
}.to_json,
status: 200
}
Expand All @@ -142,7 +142,7 @@
{
body: {
success: true,
groups: [room1, room2]
groups: [room_one, room_two]
}.to_json,
status: 200
}
Expand Down Expand Up @@ -176,7 +176,7 @@
end

context 'when searching for a valid room name' do
it 'returns room1' do
it 'returns room_one' do
rooms = scope.list_all(query: { name: 'room-one' })

expect(rooms.length).to eq 1
Expand Down
10 changes: 5 additions & 5 deletions spec/shared/room_behaviors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -205,14 +205,14 @@

describe '#list' do
let(:rooms_key) { described_class.collection.to_sym }
let(:room1) do
let(:room_one) do
{
_id: 123,
name: 'room-one'
}
end

let(:room2) do
let(:room_two) do
{
_id: 124,
name: 'room-two'
Expand All @@ -233,7 +233,7 @@
{
body: {
success: true,
rooms_key => [room1]
rooms_key => [room_one]
}.to_json,
status: 200
}
Expand All @@ -243,7 +243,7 @@
{
body: {
success: true,
rooms_key => [room1, room2]
rooms_key => [room_one, room_two]
}.to_json,
status: 200
}
Expand Down Expand Up @@ -280,7 +280,7 @@
end

context 'when searching for a valid room name' do
it 'returns room1' do
it 'returns room_one' do
rooms = scope.list(query: { name: 'room-one' })

expect(rooms.length).to eq 1
Expand Down

0 comments on commit 7201c8e

Please sign in to comment.