Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
11 changes: 9 additions & 2 deletions channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -701,8 +701,9 @@ func (ch *Channel) MarkRead(ctx context.Context, userID string, options ...MarkR
}

type markUnreadOption struct {
MessageID string `json:"message_id"`
ThreadID string `json:"thread_id"`
MessageID string `json:"message_id"`
ThreadID string `json:"thread_id"`
MessageTimestamp time.Time `json:"message_timestamp"`

UserID string `json:"user_id"`
}
Expand All @@ -722,6 +723,12 @@ func MarkUnreadThread(id string) func(*markUnreadOption) {
}
}

func MarkUnreadFromTimestamp(timestamp time.Time) func(*markUnreadOption) {
return func(opt *markUnreadOption) {
opt.MessageTimestamp = timestamp
}
}

// MarkUnread message or thread (not both) for specified user.
func (ch *Channel) MarkUnread(ctx context.Context, userID string, options ...MarkUnreadOption) (*Response, error) {
if userID == "" {
Expand Down
47 changes: 47 additions & 0 deletions channel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -993,3 +993,50 @@ func TestChannel_MessageCount_Disabled(t *testing.T) {
// message_count should be nil when CountMessages is disabled
assert.Nil(t, ch.MessageCount, "message_count should be nil when CountMessages is disabled")
}

func TestChannel_MarkUnread(t *testing.T) {
c := initClient(t)
ctx := context.Background()
user1 := randomUser(t, c)
user2 := randomUser(t, c)
ch := initChannel(t, c, user1.ID, user2.ID)

t.Run("successful mark unread with message ID", func(t *testing.T) {
// Send a message first
msgResp, err := ch.SendMessage(ctx, &Message{Text: "test message"}, user2.ID)
require.NoError(t, err, "send message should not return an error")

resp, err := ch.MarkUnread(ctx, user1.ID, MarkUnreadFromMessage(msgResp.Message.ID))
require.NoError(t, err, "mark unread with message ID should not return an error")
require.NotNil(t, resp, "response should not be nil")
})

t.Run("successful mark unread with thread ID", func(t *testing.T) {
// Send a message first
msgResp, err := ch.SendMessage(ctx, &Message{Text: "parent message"}, user2.ID)
require.NoError(t, err, "send message should not return an error")

// Send a reply to create a thread
replyResp, err := ch.SendMessage(ctx, &Message{Text: "reply", ParentID: msgResp.Message.ID}, user1.ID)
require.NoError(t, err, "send reply should not return an error")
require.NotEmpty(t, replyResp.Message.ID, "reply should have an ID")

resp, err := ch.MarkUnread(ctx, user2.ID, MarkUnreadThread(msgResp.Message.ID))
require.NoError(t, err, "mark unread with thread ID should not return an error")
require.NotNil(t, resp, "response should not be nil")
})

t.Run("successful mark unread with timestamp", func(t *testing.T) {
timestamp := time.Now().Add(-1 * time.Hour)
resp, err := ch.MarkUnread(ctx, user1.ID, MarkUnreadFromTimestamp(timestamp))
require.NoError(t, err, "mark unread with timestamp should not return an error")
require.NotNil(t, resp, "response should not be nil")
})

t.Run("error when userID is empty", func(t *testing.T) {
resp, err := ch.MarkUnread(ctx, "")
require.Error(t, err, "mark unread with empty userID should return an error")
require.Nil(t, resp, "response should be nil on error")
require.Contains(t, err.Error(), "user ID must be not empty", "error message should mention user ID")
})
}
Loading