Skip to content

Commit

Permalink
[PBE-0] - Allow to hide the channel for the creator (#258)
Browse files Browse the repository at this point in the history
* feat: allow to hide the channel for the creator
  • Loading branch information
totalimmersion authored Dec 7, 2023
1 parent ceed073 commit 5faaa27
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 15 deletions.
43 changes: 31 additions & 12 deletions channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,13 +163,14 @@ type MessagePaginationParamsRequest struct {
}

type QueryRequest struct {
Data *ChannelRequest `json:"data,omitempty"`
Watch bool `json:"watch,omitempty"`
State bool `json:"state,omitempty"`
Presence bool `json:"presence,omitempty"`
Messages *MessagePaginationParamsRequest `json:"messages,omitempty"`
Members *PaginationParamsRequest `json:"members,omitempty"`
Watchers *PaginationParamsRequest `json:"watchers,omitempty"`
Data *ChannelRequest `json:"data,omitempty"`
Watch bool `json:"watch,omitempty"`
State bool `json:"state,omitempty"`
Presence bool `json:"presence,omitempty"`
Messages *MessagePaginationParamsRequest `json:"messages,omitempty"`
Members *PaginationParamsRequest `json:"members,omitempty"`
Watchers *PaginationParamsRequest `json:"watchers,omitempty"`
HideForCreator bool `json:"hide_for_creator,omitempty"`
}

func (q QueryResponse) updateChannel(ch *Channel) {
Expand Down Expand Up @@ -663,8 +664,20 @@ type CreateChannelResponse struct {
*Response
}

type CreateChannelOptions struct {
HideForCreator bool
}

type CreateChannelOptionFunc func(*CreateChannelOptions)

func HideForCreator(hideForCreator bool) CreateChannelOptionFunc {
return func(options *CreateChannelOptions) {
options.HideForCreator = hideForCreator
}
}

// CreateChannel creates new channel of given type and id or returns already created one.
func (c *Client) CreateChannel(ctx context.Context, chanType, chanID, userID string, data *ChannelRequest) (*CreateChannelResponse, error) {
func (c *Client) CreateChannel(ctx context.Context, chanType, chanID, userID string, data *ChannelRequest, opts ...CreateChannelOptionFunc) (*CreateChannelResponse, error) {
switch {
case chanType == "":
return nil, errors.New("channel type is empty")
Expand All @@ -674,6 +687,11 @@ func (c *Client) CreateChannel(ctx context.Context, chanType, chanID, userID str
return nil, errors.New("user ID is empty")
}

options := CreateChannelOptions{}
for _, opt := range opts {
opt(&options)
}

ch := &Channel{
Type: chanType,
ID: chanID,
Expand All @@ -688,10 +706,11 @@ func (c *Client) CreateChannel(ctx context.Context, chanType, chanID, userID str
}

q := &QueryRequest{
Watch: false,
State: true,
Presence: false,
Data: data,
Watch: false,
State: true,
Presence: false,
Data: data,
HideForCreator: options.HideForCreator,
}

resp, err := ch.Query(ctx, q)
Expand Down
14 changes: 11 additions & 3 deletions channel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,21 +60,28 @@ func TestClient_CreateChannel(t *testing.T) {
id string
userID string
data *ChannelRequest
options []CreateChannelOptionFunc
wantErr bool
}{
{"create channel with ID", "messaging", randomString(12), userID, nil, false},
{"create channel without ID and members", "messaging", "", userID, nil, true},
{"create channel with ID", "messaging", randomString(12), userID, nil, nil, false},
{"create channel without ID and members", "messaging", "", userID, nil, nil, true},
{
"create channel without ID but with members", "messaging", "", userID,
&ChannelRequest{Members: randomUsersID(t, c, 2)},
nil, false,
},
{
"create channel with HideForCreator", "messaging", "", userID,
&ChannelRequest{Members: []string{userID, randomUsersID(t, c, 1)[0]}},
[]CreateChannelOptionFunc{HideForCreator(true)},
false,
},
}

for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
resp, err := c.CreateChannel(ctx, tt.channelType, tt.id, tt.userID, tt.data)
resp, err := c.CreateChannel(ctx, tt.channelType, tt.id, tt.userID, tt.data, tt.options...)
if tt.wantErr {
require.Error(t, err, "create channel", tt)
return
Expand All @@ -87,6 +94,7 @@ func TestClient_CreateChannel(t *testing.T) {
if tt.id != "" {
assert.Equal(t, tt.id, channel.ID, "channel id")
}

assert.Equal(t, tt.userID, channel.CreatedBy.ID, "channel created by")
})
}
Expand Down

0 comments on commit 5faaa27

Please sign in to comment.