Skip to content

Commit

Permalink
chore: add block user feature
Browse files Browse the repository at this point in the history
  • Loading branch information
itsmeadi committed Jun 12, 2024
1 parent b205b63 commit e544983
Show file tree
Hide file tree
Showing 2 changed files with 152 additions and 0 deletions.
89 changes: 89 additions & 0 deletions user.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type User struct {
LastActive *time.Time `json:"last_active,omitempty"`

Mutes []*Mute `json:"mutes,omitempty"`
BlockedUserIDs []string `json:"blocked_user_ids"`
ChannelMutes []*ChannelMute `json:"channel_mutes,omitempty"`
ExtraData map[string]interface{} `json:"-"`
RevokeTokensIssuedBefore *time.Time `json:"revoke_tokens_issued_before,omitempty"`
Expand Down Expand Up @@ -112,6 +113,94 @@ func (c *Client) MuteUser(ctx context.Context, targetID, mutedBy string, options
return &resp, err
}

type BlockUsersResponse struct {
Response
BlockedByUserID string `json:"blocked_by_user_id"`
BlockedUserID string `json:"blocked_user_id"`
CreatedAt time.Time `json:"created_at"`
}

type userBlockOptions struct {
BlockedUserID string `json:"blocked_user_id"`
UserID string `json:"user_id"`
}

// BlockUser blocks targetID.
func (c *Client) BlockUser(ctx context.Context, targetID, userID string) (*BlockUsersResponse, error) {
switch {
case targetID == "":
return nil, errors.New("targetID should not be empty")
case userID == "":
return nil, errors.New("userID should not be empty")
}

opts := &userBlockOptions{
BlockedUserID: targetID,
UserID: userID,
}

var resp BlockUsersResponse
err := c.makeRequest(ctx, http.MethodPost, "users/block", nil, opts, &resp)
return &resp, err
}

type UnblockUsersResponse struct {
Response
}

type userUnblockOptions struct {
BlockedUserID string `json:"blocked_user_id"`
UserID string `json:"user_id"`
}

// UnblockUser Unblocks targetID.
func (c *Client) UnblockUser(ctx context.Context, targetID, userID string) (*UnblockUsersResponse, error) {
switch {
case targetID == "":
return nil, errors.New("targetID should not be empty")
case userID == "":
return nil, errors.New("userID should not be empty")
}

opts := &userUnblockOptions{
BlockedUserID: targetID,
UserID: userID,
}

var resp UnblockUsersResponse
err := c.makeRequest(ctx, http.MethodPost, "users/unblock", nil, opts, &resp)
return &resp, err
}

type GetBlockedUsersResponse struct {
Response
BlockedUsers []*BlockedUserResponse `json:"blocks"`
}

type BlockedUserResponse struct {
BlockedByUser UsersResponse `json:"user"`
BlockedByUserID string `json:"user_id"`

BlockedUser UsersResponse `json:"blocked_user"`
BlockedUserID string `json:"blocked_user_id"`
CreatedAt time.Time `json:"created_at"`
}

// GetBlockedUser returns blocked user
func (c *Client) GetBlockedUser(ctx context.Context, blockedBy string) (*GetBlockedUsersResponse, error) {
switch {
case blockedBy == "":
return nil, errors.New("user_id should not be empty")
}

var resp GetBlockedUsersResponse

params := make(url.Values)
params.Set("user_id", blockedBy)
err := c.makeRequest(ctx, http.MethodGet, "users/block", params, nil, &resp)
return &resp, err
}

// MuteUsers mutes all users in targetIDs.
func (c *Client) MuteUsers(ctx context.Context, targetIDs []string, mutedBy string, options ...MuteOption) (*Response, error) {
switch {
Expand Down
63 changes: 63 additions & 0 deletions user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,69 @@ func TestClient_MuteUsers(t *testing.T) {
assert.NotEmpty(t, mute.Expires, "mute should have Expires")
}
}
func TestClient_BlockUsers(t *testing.T) {
c := initClient(t)
ctx := context.Background()

blockingUser := randomUser(t, c)
blockedUser := randomUser(t, c)

_, err := c.BlockUser(ctx, blockedUser.ID, blockingUser.ID)
require.NoError(t, err, "BlockUser should not return an error")

resp, err := c.QueryUsers(ctx, &QueryOption{
Filter: map[string]interface{}{
"id": map[string]string{"$eq": blockingUser.ID},
},
})

users := resp.Users
require.NoError(t, err, "QueryUsers should not return an error")
require.NotEmptyf(t, users, "QueryUsers should return a user: %+v", users)
require.Equal(t, len(users[0].BlockedUserIDs), 1)

require.Equal(t, users[0].BlockedUserIDs[0], blockedUser.ID)
}
func TestClient_UnblockUsersGetBlockedUsers(t *testing.T) {
c := initClient(t)
ctx := context.Background()

blockingUser := randomUser(t, c)
blockedUser := randomUser(t, c)

_, err := c.BlockUser(ctx, blockedUser.ID, blockingUser.ID)
require.NoError(t, err, "BlockUser should not return an error")

resp, err := c.QueryUsers(ctx, &QueryOption{
Filter: map[string]interface{}{
"id": map[string]string{"$eq": blockingUser.ID},
},
})

users := resp.Users
require.NoError(t, err, "QueryUsers should not return an error")
require.NotEmptyf(t, users, "QueryUsers should return a user: %+v", users)
require.Equal(t, len(users[0].BlockedUserIDs), 1)
require.Equal(t, users[0].BlockedUserIDs[0], blockedUser.ID)

getRes, err := c.GetBlockedUser(ctx, blockingUser.ID)
require.Equal(t, 1, len(getRes.BlockedUsers))
require.Equal(t, blockedUser.ID, getRes.BlockedUsers[0].BlockedUserID)

_, err = c.UnblockUser(ctx, blockedUser.ID, blockingUser.ID)
require.NoError(t, err, "UnblockUser should not return an error")

resp, err = c.QueryUsers(ctx, &QueryOption{
Filter: map[string]interface{}{
"id": map[string]string{"$eq": blockingUser.ID},
},
})

users = resp.Users
require.NoError(t, err, "QueryUsers should not return an error")
require.NotEmptyf(t, users, "QueryUsers should return a user: %+v", users)
require.Equal(t, 0, len(users[0].BlockedUserIDs))
}

func TestClient_UnmuteUser(t *testing.T) {
c := initClient(t)
Expand Down

0 comments on commit e544983

Please sign in to comment.