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

chore!: remove query field on custom emojis listing #33650

Merged
merged 6 commits into from
Oct 19, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
6 changes: 6 additions & 0 deletions .changeset/perfect-wolves-impress.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@rocket.chat/rest-typings': patch
'@rocket.chat/meteor': patch
MarcosSpessatto marked this conversation as resolved.
Show resolved Hide resolved
---

Changed custom emoji listing endpoint by moving query params from the 'query' attribute to standard query parameters.
ricardogarim marked this conversation as resolved.
Show resolved Hide resolved
1 change: 1 addition & 0 deletions apps/meteor/app/api/server/helpers/parseJsonQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export async function parseJsonQuery(api: PartialThis): Promise<{
'/api/v1/custom-sounds.list',
'/api/v1/channels.list',
'/api/v1/channels.online',
'/api/v1/emoji-custom.list',
].includes(route);

const isUnsafeQueryParamsAllowed = process.env.ALLOW_UNSAFE_QUERY_AND_FIELDS_API_PARAMS?.toUpperCase() === 'TRUE';
Expand Down
42 changes: 33 additions & 9 deletions apps/meteor/app/api/server/v1/emoji-custom.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Media } from '@rocket.chat/core-services';
import { EmojiCustom } from '@rocket.chat/models';
import { isEmojiCustomList } from '@rocket.chat/rest-typings';
import { Meteor } from 'meteor/meteor';

import { SystemLogger } from '../../../../server/lib/logger/system';
Expand All @@ -11,22 +12,41 @@ import { getPaginationItems } from '../helpers/getPaginationItems';
import { findEmojisCustom } from '../lib/emoji-custom';
import { getUploadFormData } from '../lib/getUploadFormData';

function validateDateParam(paramName: string, paramValue: string | undefined): Date | undefined {
if (!paramValue) {
return undefined;
}

const date = new Date(paramValue);
if (isNaN(date.getTime())) {
throw new Meteor.Error('error-roomId-param-invalid', `The "${paramName}" query parameter must be a valid date.`);
}

return date;
}

API.v1.addRoute(
'emoji-custom.list',
{ authRequired: true },
{ authRequired: true, validateParams: isEmojiCustomList },
{
async get() {
const { query } = await this.parseJsonQuery();
const { updatedSince } = this.queryParams;
if (updatedSince) {
const updatedSinceDate = new Date(updatedSince);
if (isNaN(Date.parse(updatedSince))) {
throw new Meteor.Error('error-roomId-param-invalid', 'The "updatedSince" query parameter must be a valid date.');
}
const { updatedSince, _updatedAt, _id } = this.queryParams;

const updatedSinceDate = validateDateParam('updatedSince', updatedSince);
const _updatedAtDate = validateDateParam('_updatedAt', _updatedAt);

if (updatedSinceDate) {
const [update, remove] = await Promise.all([
EmojiCustom.find({ ...query, _updatedAt: { $gt: updatedSinceDate } }).toArray(),
EmojiCustom.find({
...query,
...(_id ? { _id } : {}),
...(_updatedAtDate ? { _updatedAt: { $gt: _updatedAtDate } } : {}),
_updatedAt: { $gt: updatedSinceDate },
}).toArray(),
EmojiCustom.trashFindDeletedAfter(updatedSinceDate).toArray(),
]);

return API.v1.success({
emojis: {
update,
Expand All @@ -37,7 +57,11 @@ API.v1.addRoute(

return API.v1.success({
emojis: {
update: await EmojiCustom.find(query).toArray(),
update: await EmojiCustom.find({
...query,
...(_id ? { _id } : {}),
...(_updatedAtDate ? { _updatedAt: { $gt: _updatedAtDate } } : {}),
}).toArray(),
remove: [],
},
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type EditCustomEmojiWithDataProps = {

const EditCustomEmojiWithData = ({ _id, onChange, close, ...props }: EditCustomEmojiWithDataProps) => {
const t = useTranslation();
const query = useMemo(() => ({ query: JSON.stringify({ _id }) }), [_id]);
const query = useMemo(() => ({ _id }), [_id]);

const getEmojis = useEndpoint('GET', '/v1/emoji-custom.list');

Expand Down
4 changes: 2 additions & 2 deletions apps/meteor/tests/end-to-end/api/emoji-custom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ describe('[EmojiCustom]', () => {
it('should return emojis when use "query" query parameter', (done) => {
void request
.get(api('emoji-custom.list'))
.query({ query: `{ "_updatedAt": { "$gt": { "$date": "${new Date().toISOString()}" } } }` })
.query({ _updatedAt: new Date().toISOString() })
.set(credentials)
.expect(200)
.expect((res) => {
Expand Down Expand Up @@ -242,7 +242,7 @@ describe('[EmojiCustom]', () => {
it('should return emojis when use both, "updateSince" and "query" query parameter', (done) => {
void request
.get(api('emoji-custom.list'))
.query({ query: `{"_updatedAt": {"$gt": { "$date": "${new Date().toISOString()}" } }}`, updatedSince: new Date().toISOString() })
.query({ _updatedAt: new Date().toISOString(), updatedSince: new Date().toISOString() })
.set(credentials)
.expect(200)
.expect((res) => {
Expand Down
15 changes: 10 additions & 5 deletions packages/rest-typings/src/v1/emojiCustom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@ const emojiCustomDeletePropsSchema = {

export const isEmojiCustomDelete = ajv.compile<emojiCustomDeleteProps>(emojiCustomDeletePropsSchema);

type emojiCustomList = {
query: string;
updatedSince?: string;
};
type emojiCustomList = { query?: string; updatedSince?: string; _updatedAt?: string; _id?: string };

const emojiCustomListSchema = {
type: 'object',
Expand All @@ -40,8 +37,16 @@ const emojiCustomListSchema = {
type: 'string',
nullable: true,
},
_updatedAt: {
type: 'string',
nullable: true,
ricardogarim marked this conversation as resolved.
Show resolved Hide resolved
},
_id: {
type: 'string',
nullable: true,
ricardogarim marked this conversation as resolved.
Show resolved Hide resolved
},
},
required: ['query'],
required: [],
additionalProperties: false,
};

Expand Down
Loading