-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for unread counts endpoints
- Loading branch information
1 parent
5dfbfce
commit 9844ca1
Showing
2 changed files
with
116 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package stream_chat | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"net/url" | ||
"time" | ||
) | ||
|
||
type UnreadCountsChannel struct { | ||
ChannelID string `json:"channel_id"` | ||
UnreadCount int `json:"unread_count"` | ||
LastRead time.Time `json:"last_read"` | ||
} | ||
|
||
type UnreadCountsChannelType struct { | ||
ChannelType string `json:"channel_type"` | ||
ChannelCount int `json:"channel_count"` | ||
UnreadCount int `json:"unread_count"` | ||
} | ||
|
||
type UnreadCountsResponse struct { | ||
TotalUnreadCount int `json:"total_unread_count"` | ||
Channels []UnreadCountsChannel `json:"channels"` | ||
ChannelType []UnreadCountsChannelType `json:"channel_type"` | ||
Response | ||
} | ||
|
||
func (c *Client) UnreadCounts(ctx context.Context, userID string) (*UnreadCountsResponse, error) { | ||
var resp UnreadCountsResponse | ||
err := c.makeRequest(ctx, http.MethodGet, "unread", url.Values{"user_id": []string{userID}}, nil, &resp) | ||
return &resp, err | ||
} | ||
|
||
type UnreadCountsBatchResponse struct { | ||
CountsByUser map[string]*UnreadCountsResponse `json:"counts_by_user"` | ||
Response | ||
} | ||
|
||
func (c *Client) UnreadCountsBatch(ctx context.Context, userIDs []string) (*UnreadCountsBatchResponse, error) { | ||
var resp UnreadCountsBatchResponse | ||
err := c.makeRequest(ctx, http.MethodPost, "unread_batch", nil, map[string][]string{"user_ids": userIDs}, &resp) | ||
return &resp, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package stream_chat | ||
|
||
import ( | ||
"context" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestUnreadCounts(t *testing.T) { | ||
c := initClient(t) | ||
user := randomUser(t, c) | ||
ch := initChannel(t, c, user.ID) | ||
|
||
ctx := context.Background() | ||
msg := &Message{Text: "test message"} | ||
randSender := randomString(5) | ||
for i := 0; i < 5; i++ { | ||
_, err := ch.SendMessage(ctx, msg, randSender) | ||
require.NoError(t, err) | ||
} | ||
|
||
resp, err := c.UnreadCounts(ctx, user.ID) | ||
require.NoError(t, err) | ||
require.Equal(t, 5, resp.TotalUnreadCount) | ||
require.Equal(t, 1, len(resp.Channels)) | ||
require.Equal(t, ch.CID, resp.Channels[0].ChannelID) | ||
require.Equal(t, 5, resp.Channels[0].UnreadCount) | ||
require.Equal(t, 1, len(resp.ChannelType)) | ||
require.Equal(t, strings.Split(ch.CID, ":")[0], resp.ChannelType[0].ChannelType) | ||
require.Equal(t, 5, resp.ChannelType[0].UnreadCount) | ||
} | ||
func TestUnreadCountsBatch(t *testing.T) { | ||
c := initClient(t) | ||
user1 := randomUser(t, c) | ||
user2 := randomUser(t, c) | ||
ch := initChannel(t, c, user1.ID, user2.ID) | ||
|
||
ctx := context.Background() | ||
msg := &Message{Text: "test message"} | ||
randSender := randomString(5) | ||
for i := 0; i < 5; i++ { | ||
_, err := ch.SendMessage(ctx, msg, randSender) | ||
require.NoError(t, err) | ||
} | ||
|
||
nonexistant := randomString(5) | ||
resp, err := c.UnreadCountsBatch(ctx, []string{user1.ID, user2.ID, nonexistant}) | ||
require.NoError(t, err) | ||
require.Equal(t, 2, len(resp.CountsByUser)) | ||
require.Contains(t, resp.CountsByUser, user1.ID) | ||
require.Contains(t, resp.CountsByUser, user2.ID) | ||
|
||
// user 1 counts | ||
require.Equal(t, 5, resp.CountsByUser[user1.ID].TotalUnreadCount) | ||
require.Equal(t, 1, len(resp.CountsByUser[user1.ID].Channels)) | ||
require.Equal(t, ch.CID, resp.CountsByUser[user1.ID].Channels[0].ChannelID) | ||
require.Equal(t, 5, resp.CountsByUser[user1.ID].Channels[0].UnreadCount) | ||
require.Equal(t, 1, len(resp.CountsByUser[user1.ID].ChannelType)) | ||
require.Equal(t, strings.Split(ch.CID, ":")[0], resp.CountsByUser[user1.ID].ChannelType[0].ChannelType) | ||
require.Equal(t, 5, resp.CountsByUser[user1.ID].ChannelType[0].UnreadCount) | ||
|
||
// user 2 counts | ||
require.Equal(t, 5, resp.CountsByUser[user2.ID].TotalUnreadCount) | ||
require.Equal(t, 1, len(resp.CountsByUser[user2.ID].Channels)) | ||
require.Equal(t, ch.CID, resp.CountsByUser[user2.ID].Channels[0].ChannelID) | ||
require.Equal(t, 5, resp.CountsByUser[user2.ID].Channels[0].UnreadCount) | ||
require.Equal(t, 1, len(resp.CountsByUser[user2.ID].ChannelType)) | ||
require.Equal(t, strings.Split(ch.CID, ":")[0], resp.CountsByUser[user2.ID].ChannelType[0].ChannelType) | ||
require.Equal(t, 5, resp.CountsByUser[user2.ID].ChannelType[0].UnreadCount) | ||
} |