Skip to content

Add support for thread messages #44

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

Merged
merged 4 commits into from
Apr 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
2 changes: 1 addition & 1 deletion lib/rocket_chat/util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def stringify_hash_keys(hash)
# @return Hash filtered by keys
#
def slice_hash(hash, *keys)
return {} if keys.length.zero?
return {} if keys.empty?

new_hash = {}
hash.each do |key, value|
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
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

although I get why the lint exists, in the this case it's not really an issue. I've already "fixed" this by just allowing up to 2 indexed let variables. But this is fine too

{
_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
4 changes: 3 additions & 1 deletion spec/rocket_chat/room_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@
require 'spec_helper'

describe RocketChat::Room do
pending 'Add some specs for RocketChat::Room'
it 'RocketChat::Room spec', pending: 'Add some specs for RocketChat::Room' do
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure why you've changed this? If it's for linter reasons, the previous form was just fine?!

Copy link
Contributor Author

@MrRTi MrRTi Apr 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, RSpec/PendingWithoutReason was raised. Rubocop in actions is not using rubocop.yml from project root, and pipeline was failing because of these two issues I've updated in this PR. All was good when I run rubocop locally

raise NotImplementedError, 'Add some specs for RocketChat::Room'
end
end
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