Skip to content
This repository was archived by the owner on Oct 11, 2022. It is now read-only.
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
27 changes: 3 additions & 24 deletions api/mutations/message/addMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
canViewDMThread,
} from '../../utils/permissions';
import { trackQueue, calculateThreadScoreQueue } from 'shared/bull/queues';
import { validateRawContentState } from '../../utils/validate-draft-js-input';

type Input = {
message: {
Expand Down Expand Up @@ -117,7 +118,7 @@ export default requireAuth(async (_: any, args: Input, ctx: GraphQLContext) => {
'Please provide serialized raw DraftJS content state as content.body'
);
}
if (!body.blocks || !Array.isArray(body.blocks) || !body.entityMap) {
if (!validateRawContentState(body)) {
trackQueue.add({
userId: user.id,
event: eventFailed,
Expand All @@ -127,32 +128,10 @@ export default requireAuth(async (_: any, args: Input, ctx: GraphQLContext) => {
},
});

return new UserError(
throw new UserError(
'Please provide serialized raw DraftJS content state as content.body'
);
}
if (
body.blocks.some(
({ type }) =>
!type ||
(type !== 'unstyled' &&
type !== 'code-block' &&
type !== 'blockquote')
)
) {
trackQueue.add({
userId: user.id,
event: eventFailed,
properties: {
reason: 'invalid draftjs data',
message,
},
});

return new UserError(
'Invalid DraftJS block type specified. Supported block types: "unstyled", "code-block".'
);
}
}

if (message.parentId) {
Expand Down
27 changes: 3 additions & 24 deletions api/mutations/message/editMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { getUserPermissionsInCommunity } from '../../models/usersCommunities';
import { events } from 'shared/analytics';
import { isAuthedResolver as requireAuth } from '../../utils/permissions';
import { trackQueue } from 'shared/bull/queues';
import { validateRawContentState } from '../../utils/validate-draft-js-input';

type Args = {
input: {
Expand Down Expand Up @@ -83,7 +84,7 @@ export default requireAuth(async (_: any, args: Args, ctx: GraphQLContext) => {
'Please provide serialized raw DraftJS content state as content.body'
);
}
if (!parsed.blocks || !Array.isArray(parsed.blocks) || !parsed.entityMap) {
if (!validateRawContentState(body)) {
trackQueue.add({
userId: user.id,
event: eventFailed,
Expand All @@ -93,32 +94,10 @@ export default requireAuth(async (_: any, args: Args, ctx: GraphQLContext) => {
},
});

return new UserError(
throw new UserError(
'Please provide serialized raw DraftJS content state as content.body'
);
}
if (
parsed.blocks.some(
({ type }) =>
!type ||
(type !== 'unstyled' &&
type !== 'code-block' &&
type !== 'blockquote')
)
) {
trackQueue.add({
userId: user.id,
event: eventFailed,
properties: {
reason: 'invalid draftjs data',
message,
},
});

return new UserError(
'Invalid DraftJS block type specified. Supported block types: "unstyled", "code-block".'
);
}
}

if (body === message.content.body) {
Expand Down
13 changes: 13 additions & 0 deletions api/utils/validate-draft-js-input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// @flow
export const validateRawContentState = (input: any) => {
if (
!input ||
!input.blocks ||
!Array.isArray(input.blocks) ||
!input.entityMap
) {
return false;
}

return true;
};