Skip to content
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
12 changes: 12 additions & 0 deletions lib/stream-chat/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,18 @@ def mark_all_read(user_id)
post('channels/read', data: payload)
end

# Get unread count for a user.
sig { params(user_id: String).returns(StreamChat::StreamResponse) }
def unread_counts(user_id)
get('/unread', params: { user_id: user_id })
end

# Get unread counts for a batch of users.
sig { params(user_ids: T::Array[String]).returns(StreamChat::StreamResponse) }
def unread_counts_batch(user_ids)
post('/unread_batch', data: { user_ids: user_ids })
end

# Pins a message.
#
# Pinned messages allow users to highlight important messages, make announcements, or temporarily
Expand Down
69 changes: 69 additions & 0 deletions spec/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,75 @@ def loop_times(times)
end
end

describe 'unread count' do
before(:all) do
@user_id = SecureRandom.uuid
@client.update_users([{ id: @user_id }])
@channel = @client.channel('team', channel_id: SecureRandom.uuid)
@channel.create(@user_id)
@channel.add_members([@user_id])
end

before(:each) do
@client.mark_all_read(@user_id)
end

it 'gets unread count' do
resp = @client.unread_counts(@user_id)
expect(resp['total_unread_count']).to eq 0
end

it 'gets unread count if there are unread messages' do
@channel.send_message({ text: 'Hello world' }, @random_user[:id])
resp = @client.unread_counts(@user_id)
expect(resp['total_unread_count']).to eq 1
end

it 'gets unread count for a channel' do
@message = @channel.send_message({ text: 'Hello world' }, @random_user[:id])
resp = @client.unread_counts(@user_id)
expect(resp['total_unread_count']).to eq 1
expect(resp['channels'].length).to eq 1
expect(resp['channels'][0]['channel_id']).to eq @channel.cid
expect(resp['channels'][0]['unread_count']).to eq 1
expect(resp['channels'][0]['last_read']).not_to be_nil
end
end

describe 'unread counts batch' do
before(:all) do
@user_id1 = SecureRandom.uuid
@user_id2 = SecureRandom.uuid
@client.update_users([{ id: @user_id1 }, { id: @user_id2 }])
@channel = @client.channel('team', channel_id: SecureRandom.uuid)
@channel.create(@user_id1)
@channel.add_members([@user_id1, @user_id2])
end

before(:each) do
@client.mark_all_read(@user_id1)
@client.mark_all_read(@user_id2)
end

it 'gets unread counts for a batch of users' do
resp = @client.unread_counts_batch([@user_id1, @user_id2])
expect(resp['counts_by_user'].length).to eq 0
end

it 'gets unread counts for a batch of users with unread messages' do
@channel.send_message({ text: 'Hello world' }, @user_id1)
@channel.send_message({ text: 'Hello world' }, @user_id2)

resp = @client.unread_counts_batch([@user_id1, @user_id2])
expect(resp['counts_by_user'].length).to eq 2
expect(resp['counts_by_user'][@user_id1]['total_unread_count']).to eq 1
expect(resp['counts_by_user'][@user_id2]['total_unread_count']).to eq 1
expect(resp['counts_by_user'][@user_id1]['channels'].length).to eq 1
expect(resp['counts_by_user'][@user_id2]['channels'].length).to eq 1
expect(resp['counts_by_user'][@user_id1]['channels'][0]['channel_id']).to eq @channel.cid
end
end

describe 'blocklist' do
before(:all) do
@blocklist = SecureRandom.uuid
Expand Down