Skip to content

Commit 7444be9

Browse files
[CHA-951] added unread count (#173)
* feature: added unread count * chore: fixed linting errors * feature: renamed the feature to be more inline with other SDKs * chore: added sorbet type checking * chore: linting errors fix * chore: added isloation for the unread count specs
1 parent a3434ff commit 7444be9

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

lib/stream-chat/client.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,18 @@ 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 unread_counts(user_id)
366+
get('/unread', params: { user_id: user_id })
367+
end
368+
369+
# Get unread counts for a batch of users.
370+
sig { params(user_ids: T::Array[String]).returns(StreamChat::StreamResponse) }
371+
def unread_counts_batch(user_ids)
372+
post('/unread_batch', data: { user_ids: user_ids })
373+
end
374+
363375
# Pins a message.
364376
#
365377
# Pinned messages allow users to highlight important messages, make announcements, or temporarily

spec/client_spec.rb

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,75 @@ 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 = @client.channel('team', channel_id: SecureRandom.uuid)
554+
@channel.create(@user_id)
555+
@channel.add_members([@user_id])
556+
end
557+
558+
before(:each) do
559+
@client.mark_all_read(@user_id)
560+
end
561+
562+
it 'gets unread count' do
563+
resp = @client.unread_counts(@user_id)
564+
expect(resp['total_unread_count']).to eq 0
565+
end
566+
567+
it 'gets unread count if there are unread messages' do
568+
@channel.send_message({ text: 'Hello world' }, @random_user[:id])
569+
resp = @client.unread_counts(@user_id)
570+
expect(resp['total_unread_count']).to eq 1
571+
end
572+
573+
it 'gets unread count for a channel' do
574+
@message = @channel.send_message({ text: 'Hello world' }, @random_user[:id])
575+
resp = @client.unread_counts(@user_id)
576+
expect(resp['total_unread_count']).to eq 1
577+
expect(resp['channels'].length).to eq 1
578+
expect(resp['channels'][0]['channel_id']).to eq @channel.cid
579+
expect(resp['channels'][0]['unread_count']).to eq 1
580+
expect(resp['channels'][0]['last_read']).not_to be_nil
581+
end
582+
end
583+
584+
describe 'unread counts batch' do
585+
before(:all) do
586+
@user_id1 = SecureRandom.uuid
587+
@user_id2 = SecureRandom.uuid
588+
@client.update_users([{ id: @user_id1 }, { id: @user_id2 }])
589+
@channel = @client.channel('team', channel_id: SecureRandom.uuid)
590+
@channel.create(@user_id1)
591+
@channel.add_members([@user_id1, @user_id2])
592+
end
593+
594+
before(:each) do
595+
@client.mark_all_read(@user_id1)
596+
@client.mark_all_read(@user_id2)
597+
end
598+
599+
it 'gets unread counts for a batch of users' do
600+
resp = @client.unread_counts_batch([@user_id1, @user_id2])
601+
expect(resp['counts_by_user'].length).to eq 0
602+
end
603+
604+
it 'gets unread counts for a batch of users with unread messages' do
605+
@channel.send_message({ text: 'Hello world' }, @user_id1)
606+
@channel.send_message({ text: 'Hello world' }, @user_id2)
607+
608+
resp = @client.unread_counts_batch([@user_id1, @user_id2])
609+
expect(resp['counts_by_user'].length).to eq 2
610+
expect(resp['counts_by_user'][@user_id1]['total_unread_count']).to eq 1
611+
expect(resp['counts_by_user'][@user_id2]['total_unread_count']).to eq 1
612+
expect(resp['counts_by_user'][@user_id1]['channels'].length).to eq 1
613+
expect(resp['counts_by_user'][@user_id2]['channels'].length).to eq 1
614+
expect(resp['counts_by_user'][@user_id1]['channels'][0]['channel_id']).to eq @channel.cid
615+
end
616+
end
617+
549618
describe 'blocklist' do
550619
before(:all) do
551620
@blocklist = SecureRandom.uuid

0 commit comments

Comments
 (0)