Skip to content

Commit

Permalink
added support for pinning / unpinning channels
Browse files Browse the repository at this point in the history
  • Loading branch information
totalimmersion committed Dec 4, 2024
1 parent de807e7 commit 961fa23
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
43 changes: 43 additions & 0 deletions channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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
}
34 changes: 34 additions & 0 deletions channel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"path"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 961fa23

Please sign in to comment.