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

feat: add support for querying deactivated users #288

Merged
merged 1 commit into from
Sep 10, 2024
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
30 changes: 19 additions & 11 deletions query.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,12 @@ type queryRequest struct {
State bool `json:"state"`
Presence bool `json:"presence"`

UserID string `json:"user_id,omitempty"`
Limit int `json:"limit,omitempty"`
Offset int `json:"offset,omitempty"`
MemberLimit *int `json:"member_limit,omitempty"`
MessageLimit *int `json:"message_limit,omitempty"`
UserID string `json:"user_id,omitempty"`
Limit int `json:"limit,omitempty"`
Offset int `json:"offset,omitempty"`
MemberLimit *int `json:"member_limit,omitempty"`
MessageLimit *int `json:"message_limit,omitempty"`
IncludeDeactivatedUsers bool `json:"include_deactivated_users,omitempty"`

FilterConditions map[string]interface{} `json:"filter_conditions,omitempty"`
Sort []*SortOption `json:"sort,omitempty"`
Expand All @@ -58,19 +59,26 @@ type FlagReport struct {
UpdatedAt time.Time `json:"updated_at"`
}

type QueryUsersOptions struct {
QueryOption

IncludeDeactivatedUsers bool `json:"include_deactivated_users"`
}

type QueryUsersResponse struct {
Users []*User `json:"users"`
Response
}

// QueryUsers returns list of users that match QueryOption.
// QueryUsers returns list of users that match QueryUsersOptions.
// If any number of SortOption are set, result will be sorted by field and direction in the order of sort options.
func (c *Client) QueryUsers(ctx context.Context, q *QueryOption, sorters ...*SortOption) (*QueryUsersResponse, error) {
func (c *Client) QueryUsers(ctx context.Context, q *QueryUsersOptions, sorters ...*SortOption) (*QueryUsersResponse, error) {
qp := queryRequest{
FilterConditions: q.Filter,
Limit: q.Limit,
Offset: q.Offset,
Sort: sorters,
FilterConditions: q.Filter,
Limit: q.Limit,
Offset: q.Offset,
IncludeDeactivatedUsers: q.IncludeDeactivatedUsers,
Sort: sorters,
}

data, err := json.Marshal(&qp)
Expand Down
39 changes: 30 additions & 9 deletions query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func TestClient_QueryUsers(t *testing.T) {
c := initClient(t)
ctx := context.Background()

const n = 4
const n = 5
ids := make([]string, n)
t.Cleanup(func() {
for _, id := range ids {
Expand All @@ -32,25 +32,30 @@ func TestClient_QueryUsers(t *testing.T) {
time.Sleep(200 * time.Millisecond)
}

_, err := c.DeactivateUser(ctx, ids[n-1])
require.NoError(t, err)

t.Parallel()
t.Run("Query all", func(tt *testing.T) {
results, err := c.QueryUsers(ctx, &QueryOption{
Filter: map[string]interface{}{
"id": map[string]interface{}{
"$in": ids,
results, err := c.QueryUsers(ctx, &QueryUsersOptions{
QueryOption: QueryOption{
Filter: map[string]interface{}{
"id": map[string]interface{}{
"$in": ids,
},
},
},
})

require.NoError(tt, err)
require.Len(tt, results.Users, len(ids))
require.Len(tt, results.Users, len(ids)-1)
})

t.Run("Query with offset/limit", func(tt *testing.T) {
offset := 1

results, err := c.QueryUsers(ctx,
&QueryOption{
results, err := c.QueryUsers(ctx, &QueryUsersOptions{
QueryOption: QueryOption{
Filter: map[string]interface{}{
"id": map[string]interface{}{
"$in": ids,
Expand All @@ -59,14 +64,30 @@ func TestClient_QueryUsers(t *testing.T) {
Offset: offset,
Limit: 2,
},
)
})

require.NoError(tt, err)
require.Len(tt, results.Users, 2)

require.Equal(tt, results.Users[0].ID, ids[offset])
require.Equal(tt, results.Users[1].ID, ids[offset+1])
})

t.Run("Query with deactivated", func(tt *testing.T) {
results, err := c.QueryUsers(ctx, &QueryUsersOptions{
QueryOption: QueryOption{
Filter: map[string]interface{}{
"id": map[string]interface{}{
"$in": ids,
},
},
},
IncludeDeactivatedUsers: true,
})

require.NoError(tt, err)
require.Len(tt, results.Users, len(ids))
})
}

func TestClient_QueryChannels(t *testing.T) {
Expand Down
50 changes: 32 additions & 18 deletions user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ func TestClient_MuteUser(t *testing.T) {
_, err := c.MuteUser(ctx, randomUser(t, c).ID, user.ID)
require.NoError(t, err, "MuteUser should not return an error")

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

Expand All @@ -38,9 +40,11 @@ func TestClient_MuteUser(t *testing.T) {
_, err = c.MuteUser(ctx, randomUser(t, c).ID, user.ID, MuteWithExpiration(60))
require.NoError(t, err, "MuteUser should not return an error")

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

Expand All @@ -65,9 +69,11 @@ func TestClient_MuteUsers(t *testing.T) {
_, err := c.MuteUsers(ctx, targetIDs, user.ID, MuteWithExpiration(60))
require.NoError(t, err, "MuteUsers should not return an error")

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

Expand All @@ -80,6 +86,7 @@ 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()
Expand All @@ -90,9 +97,11 @@ func TestClient_BlockUsers(t *testing.T) {
_, 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},
resp, err := c.QueryUsers(ctx, &QueryUsersOptions{
QueryOption: QueryOption{
Filter: map[string]interface{}{
"id": map[string]string{"$eq": blockingUser.ID},
},
},
})

Expand All @@ -103,6 +112,7 @@ func TestClient_BlockUsers(t *testing.T) {

require.Equal(t, users[0].BlockedUserIDs[0], blockedUser.ID)
}

func TestClient_UnblockUsersGetBlockedUsers(t *testing.T) {
c := initClient(t)
ctx := context.Background()
Expand All @@ -113,9 +123,11 @@ func TestClient_UnblockUsersGetBlockedUsers(t *testing.T) {
_, 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},
resp, err := c.QueryUsers(ctx, &QueryUsersOptions{
QueryOption: QueryOption{
Filter: map[string]interface{}{
"id": map[string]string{"$eq": blockingUser.ID},
},
},
})

Expand All @@ -132,9 +144,11 @@ func TestClient_UnblockUsersGetBlockedUsers(t *testing.T) {
_, 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},
resp, err = c.QueryUsers(ctx, &QueryUsersOptions{
QueryOption: QueryOption{
Filter: map[string]interface{}{
"id": map[string]string{"$eq": blockingUser.ID},
},
},
})

Expand Down
Loading