From 961fa231ba0347c05197e0db830e29b15b037158 Mon Sep 17 00:00:00 2001 From: Lennart Kuijs Date: Wed, 4 Dec 2024 11:00:51 +0100 Subject: [PATCH] added support for pinning / unpinning channels --- channel.go | 43 +++++++++++++++++++++++++++++++++++++++++++ channel_test.go | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) diff --git a/channel.go b/channel.go index bb1c5c3..f300a9e 100644 --- a/channel.go +++ b/channel.go @@ -27,6 +27,8 @@ type ChannelMember struct { InviteAcceptedAt *time.Time `json:"invite_accepted_at,omitempty"` InviteRejectedAt *time.Time `json:"invite_rejected_at,omitempty"` Role string `json:"role,omitempty"` + ArchivedAt *time.Time `json:"archived_at,omitempty"` + PinnedAt *time.Time `json:"pinned_at,omitempty"` CreatedAt time.Time `json:"created_at,omitempty"` UpdatedAt time.Time `json:"updated_at,omitempty"` @@ -855,3 +857,44 @@ func (ch *Channel) Unmute(ctx context.Context, userID string) (*Response, error) err := ch.client.makeRequest(ctx, http.MethodPost, "moderation/unmute/channel", nil, data, &resp) return &resp, err } + +type ChannelMemberResponse struct { + ChannelMember + Response +} + +func (ch *Channel) Pin(ctx context.Context, userID string) (*ChannelMemberResponse, error) { + if userID == "" { + return nil, errors.New("user ID must be not empty") + } + + p := path.Join("channels", url.PathEscape(ch.Type), url.PathEscape(ch.ID), "members", url.PathEscape(userID)) + + data := map[string]interface{}{ + "set": map[string]interface{}{ + "pinned": true, + }, + } + + resp := &ChannelMemberResponse{} + err := ch.client.makeRequest(ctx, http.MethodPost, p, nil, data, resp) + return resp, err +} + +func (ch *Channel) Unpin(ctx context.Context, userID string) (*ChannelMemberResponse, error) { + if userID == "" { + return nil, errors.New("user ID must be not empty") + } + + p := path.Join("channels", url.PathEscape(ch.Type), url.PathEscape(ch.ID), "members", url.PathEscape(userID)) + + data := map[string]interface{}{ + "set": map[string]interface{}{ + "pinned": false, + }, + } + + resp := &ChannelMemberResponse{} + err := ch.client.makeRequest(ctx, http.MethodPost, p, nil, data, resp) + return resp, err +} diff --git a/channel_test.go b/channel_test.go index f5750df..dfe97f2 100644 --- a/channel_test.go +++ b/channel_test.go @@ -6,6 +6,7 @@ import ( "os" "path" "testing" + "time" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -647,6 +648,39 @@ func TestChannel_Mute_Unmute(t *testing.T) { require.Len(t, queryChannResp.Channels, 1) } +func TestChannel_Pin(t *testing.T) { + c := initClient(t) + ctx := context.Background() + users := randomUsers(t, c, 5) + + members := make([]string, 0, len(users)) + for i := range users { + members = append(members, users[i].ID) + } + ch := initChannel(t, c, members...) + + //pin the channel + now := time.Now() + member, err := ch.Pin(ctx, users[0].ID) + require.NoError(t, err, "pin channel") + require.NotNil(t, member.PinnedAt) + require.GreaterOrEqual(t, member.PinnedAt.Unix(), now.Unix()) + + // query for pinned the channel + queryChannResp, err := c.QueryChannels(ctx, &QueryOption{ + UserID: members[0], + Filter: map[string]interface{}{ + "pinned": true, + "cid": ch.CID, + }, + }) + + channels := queryChannResp.Channels + require.NoError(t, err, "query pinned channel") + require.Len(t, channels, 1) + require.Equal(t, channels[0].CID, ch.CID) +} + func ExampleChannel_Update() { client := &Client{} ctx := context.Background()