Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PBE-0] - Allow to hide the channel for the creator #258

Merged
merged 7 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
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
Loading