Skip to content

Commit

Permalink
chore!: make get directory stop to use query param (#33563)
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardogarim authored and ggazzo committed Oct 17, 2024
1 parent f314767 commit a23e699
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 44 deletions.
7 changes: 6 additions & 1 deletion apps/meteor/app/api/server/helpers/parseJsonQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,12 @@ export async function parseJsonQuery(api: PartialThis): Promise<{
}

// TODO: Remove this once we have all routes migrated to the new API params
const hasSupportedRoutes = ['/api/v1/channels.files', '/api/v1/integrations.list', '/api/v1/custom-user-status.list'].includes(route);
const hasSupportedRoutes = [
'/api/v1/directory',
'/api/v1/channels.files',
'/api/v1/integrations.list',
'/api/v1/custom-user-status.list',
].includes(route);

const isUnsafeQueryParamsAllowed = process.env.ALLOW_UNSAFE_QUERY_AND_FIELDS_API_PARAMS?.toUpperCase() === 'TRUE';
const messageGenerator = ({ endpoint, version, parameter }: { endpoint: string; version: string; parameter: string }): string =>
Expand Down
12 changes: 8 additions & 4 deletions apps/meteor/app/api/server/v1/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -363,8 +363,14 @@ API.v1.addRoute(
async get() {
const { offset, count } = await getPaginationItems(this.queryParams);
const { sort, query } = await this.parseJsonQuery();
const { text, type, workspace = 'local' } = this.queryParams;

const { text, type, workspace = 'local' } = query;
const filter = {
...(query ? { ...query } : {}),
...(text ? { text } : {}),
...(type ? { type } : {}),
...(workspace ? { workspace } : {}),
};

if (sort && Object.keys(sort).length > 1) {
return API.v1.failure('This method support only one "sort" parameter');
Expand All @@ -373,9 +379,7 @@ API.v1.addRoute(
const sortDirection = sort && Object.values(sort)[0] === 1 ? 'asc' : 'desc';

const result = await Meteor.callAsync('browseChannels', {
text,
type,
workspace,
...filter,
sortBy,
sortDirection,
offset: Math.max(0, offset),
Expand Down
10 changes: 4 additions & 6 deletions apps/meteor/client/views/directory/hooks/useDirectoryQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@ export function useDirectoryQuery(
[column, direction]: [string, 'asc' | 'desc'],
type: string,
workspace = 'local',
): { offset?: number | undefined; count?: number; query: string; sort: string } {
): { sort: string; offset?: number | undefined; count?: number; query?: string; text?: string; type?: string; workspace?: string } {
return useMemo(
() => ({
query: JSON.stringify({
type,
text,
workspace,
}),
text,
type,
workspace,
sort: JSON.stringify({ [column]: direction === 'asc' ? 1 : -1 }),
...(itemsPerPage && { count: itemsPerPage }),
...(current && { offset: current }),
Expand Down
42 changes: 14 additions & 28 deletions apps/meteor/tests/end-to-end/api/miscellaneous.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,10 +233,8 @@ describe('miscellaneous', () => {
.get(api('directory'))
.set(credentials)
.query({
query: JSON.stringify({
text: user.username,
type: 'users',
}),
text: user.username,
type: 'users',
})
.expect('Content-Type', 'application/json')
.expect(200)
Expand All @@ -260,10 +258,8 @@ describe('miscellaneous', () => {
.get(api('directory'))
.set(normalUserCredentials)
.query({
query: JSON.stringify({
text: user.username,
type: 'users',
}),
text: user.username,
type: 'users',
})
.expect('Content-Type', 'application/json')
.expect(200)
Expand All @@ -286,10 +282,8 @@ describe('miscellaneous', () => {
.get(api('directory'))
.set(credentials)
.query({
query: JSON.stringify({
text: testChannel.name,
type: 'channels',
}),
text: testChannel.name,
type: 'channels',
})
.expect('Content-Type', 'application/json')
.expect(200)
Expand All @@ -311,10 +305,8 @@ describe('miscellaneous', () => {
.get(api('directory'))
.set(credentials)
.query({
query: JSON.stringify({
text: testChannel.name,
type: 'channels',
}),
text: testChannel.name,
type: 'channels',
sort: JSON.stringify({
name: 1,
}),
Expand All @@ -339,10 +331,8 @@ describe('miscellaneous', () => {
.get(api('directory'))
.set(credentials)
.query({
query: JSON.stringify({
text: 'invalid channel',
type: 'invalid',
}),
text: 'invalid channel',
type: 'invalid',
})
.expect('Content-Type', 'application/json')
.expect(400)
Expand All @@ -356,10 +346,8 @@ describe('miscellaneous', () => {
.get(api('directory'))
.set(credentials)
.query({
query: JSON.stringify({
text: testChannel.name,
type: 'channels',
}),
text: testChannel.name,
type: 'channels',
sort: JSON.stringify({
name: 1,
test: 1,
Expand All @@ -378,10 +366,8 @@ describe('miscellaneous', () => {
.get(api('directory'))
.set(normalUserCredentials)
.query({
query: JSON.stringify({
text: '',
type: 'teams',
}),
text: '',
type: 'teams',
sort: JSON.stringify({
name: 1,
}),
Expand Down
23 changes: 18 additions & 5 deletions packages/rest-typings/src/v1/directory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,11 @@ const ajv = new Ajv({
coerceTypes: true,
});

type DirectoryProps = PaginatedRequest;
type DirectoryProps = PaginatedRequest<{ text?: string; type?: string; workspace?: string; query?: string }>;

const DirectorySchema = {
type: 'object',
properties: {
query: {
type: 'string',
nullable: true,
},
count: {
type: 'number',
nullable: true,
Expand All @@ -29,7 +25,24 @@ const DirectorySchema = {
type: 'string',
nullable: true,
},
text: {
type: 'string',
nullable: true,
},
type: {
type: 'string',
nullable: true,
},
workspace: {
type: 'string',
nullable: true,
},
query: {
type: 'string',
nullable: true,
},
},
required: [],
additionalProperties: false,
};

Expand Down

0 comments on commit a23e699

Please sign in to comment.