Skip to content

Commit a64b64c

Browse files
authored
feat: add support for querying deactivated users (#288)
1 parent f94bd70 commit a64b64c

File tree

3 files changed

+81
-38
lines changed

3 files changed

+81
-38
lines changed

query.go

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,12 @@ type queryRequest struct {
3232
State bool `json:"state"`
3333
Presence bool `json:"presence"`
3434

35-
UserID string `json:"user_id,omitempty"`
36-
Limit int `json:"limit,omitempty"`
37-
Offset int `json:"offset,omitempty"`
38-
MemberLimit *int `json:"member_limit,omitempty"`
39-
MessageLimit *int `json:"message_limit,omitempty"`
35+
UserID string `json:"user_id,omitempty"`
36+
Limit int `json:"limit,omitempty"`
37+
Offset int `json:"offset,omitempty"`
38+
MemberLimit *int `json:"member_limit,omitempty"`
39+
MessageLimit *int `json:"message_limit,omitempty"`
40+
IncludeDeactivatedUsers bool `json:"include_deactivated_users,omitempty"`
4041

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

62+
type QueryUsersOptions struct {
63+
QueryOption
64+
65+
IncludeDeactivatedUsers bool `json:"include_deactivated_users"`
66+
}
67+
6168
type QueryUsersResponse struct {
6269
Users []*User `json:"users"`
6370
Response
6471
}
6572

66-
// QueryUsers returns list of users that match QueryOption.
73+
// QueryUsers returns list of users that match QueryUsersOptions.
6774
// If any number of SortOption are set, result will be sorted by field and direction in the order of sort options.
68-
func (c *Client) QueryUsers(ctx context.Context, q *QueryOption, sorters ...*SortOption) (*QueryUsersResponse, error) {
75+
func (c *Client) QueryUsers(ctx context.Context, q *QueryUsersOptions, sorters ...*SortOption) (*QueryUsersResponse, error) {
6976
qp := queryRequest{
70-
FilterConditions: q.Filter,
71-
Limit: q.Limit,
72-
Offset: q.Offset,
73-
Sort: sorters,
77+
FilterConditions: q.Filter,
78+
Limit: q.Limit,
79+
Offset: q.Offset,
80+
IncludeDeactivatedUsers: q.IncludeDeactivatedUsers,
81+
Sort: sorters,
7482
}
7583

7684
data, err := json.Marshal(&qp)

query_test.go

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ func TestClient_QueryUsers(t *testing.T) {
1414
c := initClient(t)
1515
ctx := context.Background()
1616

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

35+
_, err := c.DeactivateUser(ctx, ids[n-1])
36+
require.NoError(t, err)
37+
3538
t.Parallel()
3639
t.Run("Query all", func(tt *testing.T) {
37-
results, err := c.QueryUsers(ctx, &QueryOption{
38-
Filter: map[string]interface{}{
39-
"id": map[string]interface{}{
40-
"$in": ids,
40+
results, err := c.QueryUsers(ctx, &QueryUsersOptions{
41+
QueryOption: QueryOption{
42+
Filter: map[string]interface{}{
43+
"id": map[string]interface{}{
44+
"$in": ids,
45+
},
4146
},
4247
},
4348
})
4449

4550
require.NoError(tt, err)
46-
require.Len(tt, results.Users, len(ids))
51+
require.Len(tt, results.Users, len(ids)-1)
4752
})
4853

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

52-
results, err := c.QueryUsers(ctx,
53-
&QueryOption{
57+
results, err := c.QueryUsers(ctx, &QueryUsersOptions{
58+
QueryOption: QueryOption{
5459
Filter: map[string]interface{}{
5560
"id": map[string]interface{}{
5661
"$in": ids,
@@ -59,14 +64,30 @@ func TestClient_QueryUsers(t *testing.T) {
5964
Offset: offset,
6065
Limit: 2,
6166
},
62-
)
67+
})
6368

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

6772
require.Equal(tt, results.Users[0].ID, ids[offset])
6873
require.Equal(tt, results.Users[1].ID, ids[offset+1])
6974
})
75+
76+
t.Run("Query with deactivated", func(tt *testing.T) {
77+
results, err := c.QueryUsers(ctx, &QueryUsersOptions{
78+
QueryOption: QueryOption{
79+
Filter: map[string]interface{}{
80+
"id": map[string]interface{}{
81+
"$in": ids,
82+
},
83+
},
84+
},
85+
IncludeDeactivatedUsers: true,
86+
})
87+
88+
require.NoError(tt, err)
89+
require.Len(tt, results.Users, len(ids))
90+
})
7091
}
7192

7293
func TestClient_QueryChannels(t *testing.T) {

user_test.go

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@ func TestClient_MuteUser(t *testing.T) {
1717
_, err := c.MuteUser(ctx, randomUser(t, c).ID, user.ID)
1818
require.NoError(t, err, "MuteUser should not return an error")
1919

20-
resp, err := c.QueryUsers(ctx, &QueryOption{
21-
Filter: map[string]interface{}{
22-
"id": map[string]string{"$eq": user.ID},
20+
resp, err := c.QueryUsers(ctx, &QueryUsersOptions{
21+
QueryOption: QueryOption{
22+
Filter: map[string]interface{}{
23+
"id": map[string]string{"$eq": user.ID},
24+
},
2325
},
2426
})
2527

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

41-
resp, err = c.QueryUsers(ctx, &QueryOption{
42-
Filter: map[string]interface{}{
43-
"id": map[string]string{"$eq": user.ID},
43+
resp, err = c.QueryUsers(ctx, &QueryUsersOptions{
44+
QueryOption: QueryOption{
45+
Filter: map[string]interface{}{
46+
"id": map[string]string{"$eq": user.ID},
47+
},
4448
},
4549
})
4650

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

68-
resp, err := c.QueryUsers(ctx, &QueryOption{
69-
Filter: map[string]interface{}{
70-
"id": map[string]string{"$eq": user.ID},
72+
resp, err := c.QueryUsers(ctx, &QueryUsersOptions{
73+
QueryOption: QueryOption{
74+
Filter: map[string]interface{}{
75+
"id": map[string]string{"$eq": user.ID},
76+
},
7177
},
7278
})
7379

@@ -80,6 +86,7 @@ func TestClient_MuteUsers(t *testing.T) {
8086
assert.NotEmpty(t, mute.Expires, "mute should have Expires")
8187
}
8288
}
89+
8390
func TestClient_BlockUsers(t *testing.T) {
8491
c := initClient(t)
8592
ctx := context.Background()
@@ -90,9 +97,11 @@ func TestClient_BlockUsers(t *testing.T) {
9097
_, err := c.BlockUser(ctx, blockedUser.ID, blockingUser.ID)
9198
require.NoError(t, err, "BlockUser should not return an error")
9299

93-
resp, err := c.QueryUsers(ctx, &QueryOption{
94-
Filter: map[string]interface{}{
95-
"id": map[string]string{"$eq": blockingUser.ID},
100+
resp, err := c.QueryUsers(ctx, &QueryUsersOptions{
101+
QueryOption: QueryOption{
102+
Filter: map[string]interface{}{
103+
"id": map[string]string{"$eq": blockingUser.ID},
104+
},
96105
},
97106
})
98107

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

104113
require.Equal(t, users[0].BlockedUserIDs[0], blockedUser.ID)
105114
}
115+
106116
func TestClient_UnblockUsersGetBlockedUsers(t *testing.T) {
107117
c := initClient(t)
108118
ctx := context.Background()
@@ -113,9 +123,11 @@ func TestClient_UnblockUsersGetBlockedUsers(t *testing.T) {
113123
_, err := c.BlockUser(ctx, blockedUser.ID, blockingUser.ID)
114124
require.NoError(t, err, "BlockUser should not return an error")
115125

116-
resp, err := c.QueryUsers(ctx, &QueryOption{
117-
Filter: map[string]interface{}{
118-
"id": map[string]string{"$eq": blockingUser.ID},
126+
resp, err := c.QueryUsers(ctx, &QueryUsersOptions{
127+
QueryOption: QueryOption{
128+
Filter: map[string]interface{}{
129+
"id": map[string]string{"$eq": blockingUser.ID},
130+
},
119131
},
120132
})
121133

@@ -132,9 +144,11 @@ func TestClient_UnblockUsersGetBlockedUsers(t *testing.T) {
132144
_, err = c.UnblockUser(ctx, blockedUser.ID, blockingUser.ID)
133145
require.NoError(t, err, "UnblockUser should not return an error")
134146

135-
resp, err = c.QueryUsers(ctx, &QueryOption{
136-
Filter: map[string]interface{}{
137-
"id": map[string]string{"$eq": blockingUser.ID},
147+
resp, err = c.QueryUsers(ctx, &QueryUsersOptions{
148+
QueryOption: QueryOption{
149+
Filter: map[string]interface{}{
150+
"id": map[string]string{"$eq": blockingUser.ID},
151+
},
138152
},
139153
})
140154

0 commit comments

Comments
 (0)