Skip to content

Commit 4eed979

Browse files
feat: added support for pinning / unpinning channels
1 parent de807e7 commit 4eed979

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

channel.go

+43
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ type ChannelMember struct {
2727
InviteAcceptedAt *time.Time `json:"invite_accepted_at,omitempty"`
2828
InviteRejectedAt *time.Time `json:"invite_rejected_at,omitempty"`
2929
Role string `json:"role,omitempty"`
30+
ArchivedAt *time.Time `json:"archived_at,omitempty"`
31+
PinnedAt *time.Time `json:"pinned_at,omitempty"`
3032

3133
CreatedAt time.Time `json:"created_at,omitempty"`
3234
UpdatedAt time.Time `json:"updated_at,omitempty"`
@@ -855,3 +857,44 @@ func (ch *Channel) Unmute(ctx context.Context, userID string) (*Response, error)
855857
err := ch.client.makeRequest(ctx, http.MethodPost, "moderation/unmute/channel", nil, data, &resp)
856858
return &resp, err
857859
}
860+
861+
type ChannelMemberResponse struct {
862+
ChannelMember
863+
Response
864+
}
865+
866+
func (ch *Channel) Pin(ctx context.Context, userID string) (*ChannelMemberResponse, error) {
867+
if userID == "" {
868+
return nil, errors.New("user ID must be not empty")
869+
}
870+
871+
p := path.Join("channels", url.PathEscape(ch.Type), url.PathEscape(ch.ID), "members", url.PathEscape(userID))
872+
873+
data := map[string]interface{}{
874+
"set": map[string]interface{}{
875+
"pinned": true,
876+
},
877+
}
878+
879+
resp := &ChannelMemberResponse{}
880+
err := ch.client.makeRequest(ctx, http.MethodPost, p, nil, data, resp)
881+
return resp, err
882+
}
883+
884+
func (ch *Channel) Unpin(ctx context.Context, userID string) (*ChannelMemberResponse, error) {
885+
if userID == "" {
886+
return nil, errors.New("user ID must be not empty")
887+
}
888+
889+
p := path.Join("channels", url.PathEscape(ch.Type), url.PathEscape(ch.ID), "members", url.PathEscape(userID))
890+
891+
data := map[string]interface{}{
892+
"set": map[string]interface{}{
893+
"pinned": false,
894+
},
895+
}
896+
897+
resp := &ChannelMemberResponse{}
898+
err := ch.client.makeRequest(ctx, http.MethodPost, p, nil, data, resp)
899+
return resp, err
900+
}

channel_test.go

+34
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"os"
77
"path"
88
"testing"
9+
"time"
910

1011
"github.com/stretchr/testify/assert"
1112
"github.com/stretchr/testify/require"
@@ -647,6 +648,39 @@ func TestChannel_Mute_Unmute(t *testing.T) {
647648
require.Len(t, queryChannResp.Channels, 1)
648649
}
649650

651+
func TestChannel_Pin(t *testing.T) {
652+
c := initClient(t)
653+
ctx := context.Background()
654+
users := randomUsers(t, c, 5)
655+
656+
members := make([]string, 0, len(users))
657+
for i := range users {
658+
members = append(members, users[i].ID)
659+
}
660+
ch := initChannel(t, c, members...)
661+
662+
//pin the channel
663+
now := time.Now()
664+
member, err := ch.Pin(ctx, users[0].ID)
665+
require.NoError(t, err, "pin channel")
666+
require.NotNil(t, member.PinnedAt)
667+
require.GreaterOrEqual(t, member.PinnedAt.Unix(), now.Unix())
668+
669+
// query for pinned the channel
670+
queryChannResp, err := c.QueryChannels(ctx, &QueryOption{
671+
UserID: members[0],
672+
Filter: map[string]interface{}{
673+
"pinned": true,
674+
"cid": ch.CID,
675+
},
676+
})
677+
678+
channels := queryChannResp.Channels
679+
require.NoError(t, err, "query pinned channel")
680+
require.Len(t, channels, 1)
681+
require.Equal(t, channels[0].CID, ch.CID)
682+
}
683+
650684
func ExampleChannel_Update() {
651685
client := &Client{}
652686
ctx := context.Background()

0 commit comments

Comments
 (0)