Skip to content

Commit f3ee91b

Browse files
added unread count
1 parent a3434ff commit f3ee91b

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

lib/stream-chat/client.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,12 @@ def mark_all_read(user_id)
360360
post('channels/read', data: payload)
361361
end
362362

363+
# Get unread count for a user.
364+
sig { params(user_id: String).returns(StreamChat::StreamResponse) }
365+
def get_unread_count(user_id)
366+
get("/unread", params: { user_id: user_id })
367+
end
368+
363369
# Pins a message.
364370
#
365371
# Pinned messages allow users to highlight important messages, make announcements, or temporarily

spec/client_spec.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,39 @@ def loop_times(times)
546546
end
547547
end
548548

549+
describe 'unread count' do
550+
before(:all) do
551+
@user_id = SecureRandom.uuid
552+
@client.update_users([{ id: @user_id }])
553+
@channel.add_members([@user_id])
554+
end
555+
556+
before(:each) do
557+
@client.mark_all_read(@user_id)
558+
end
559+
560+
it 'gets unread count' do
561+
resp = @client.get_unread_count(@user_id)
562+
expect(resp['total_unread_count']).to eq 0
563+
end
564+
565+
it "gets unread count if there are unread messages" do
566+
@channel.send_message({ text: 'Hello world' }, @random_user[:id])
567+
resp = @client.get_unread_count(@user_id)
568+
expect(resp['total_unread_count']).to eq 1
569+
end
570+
571+
it "gets unread count for a channel" do
572+
@message = @channel.send_message({ text: 'Hello world' }, @random_user[:id])
573+
resp = @client.get_unread_count(@user_id)
574+
expect(resp['total_unread_count']).to eq 1
575+
expect(resp['channels'].length).to eq 1
576+
expect(resp['channels'][0]['channel_id']).to eq @channel.cid
577+
expect(resp['channels'][0]['unread_count']).to eq 1
578+
expect(resp['channels'][0]['last_read']).not_to be_nil
579+
end
580+
end
581+
549582
describe 'blocklist' do
550583
before(:all) do
551584
@blocklist = SecureRandom.uuid

0 commit comments

Comments
 (0)