diff --git a/keyserver/src/creators/thread-creator.js b/keyserver/src/creators/thread-creator.js --- a/keyserver/src/creators/thread-creator.js +++ b/keyserver/src/creators/thread-creator.js @@ -9,6 +9,7 @@ generatePendingThreadColor, generateRandomColor, } from 'lib/shared/color-utils.js'; +import { isInvalidSidebarSource } from 'lib/shared/message-utils.js'; import { getThreadTypeParentRequirement } from 'lib/shared/thread-utils.js'; import type { Shape } from 'lib/types/core.js'; import { messageTypes } from 'lib/types/message-types-enum.js'; @@ -194,14 +195,7 @@ validateMembers: { initialMemberIDs, ghostMemberIDs }, } = await promiseAll(checkPromises); - if ( - sourceMessage && - (sourceMessage.type === messageTypes.REACTION || - sourceMessage.type === messageTypes.EDIT_MESSAGE || - sourceMessage.type === messageTypes.SIDEBAR_SOURCE || - sourceMessage.type === messageTypes.TOGGLE_PIN || - sourceMessage.type === messageTypes.CHANGE_ROLE) - ) { + if (sourceMessage && isInvalidSidebarSource(sourceMessage.type)) { throw new ServerError('invalid_parameters'); } @@ -425,6 +419,14 @@ if (!sourceMessage) { throw new ServerError('invalid_parameters'); } + invariant( + sourceMessage.type !== messageTypes.REACTION && + sourceMessage.type !== messageTypes.EDIT_MESSAGE && + sourceMessage.type !== messageTypes.SIDEBAR_SOURCE && + sourceMessage.type !== messageTypes.TOGGLE_PIN && + sourceMessage.type !== messageTypes.CHANGE_ROLE, + 'Invalid sidebar source type', + ); let editedSourceMessage = sourceMessage; if (sourceMessageID && sourceMessage.type === messageTypes.TEXT) { diff --git a/lib/shared/message-utils.js b/lib/shared/message-utils.js --- a/lib/shared/message-utils.js +++ b/lib/shared/message-utils.js @@ -642,6 +642,16 @@ return `${localIDPrefix}${nextLocalID}`; } +function isInvalidSidebarSource(messageType: number): boolean { + return ( + messageType === messageTypes.REACTION || + messageType === messageTypes.EDIT_MESSAGE || + messageType === messageTypes.SIDEBAR_SOURCE || + messageType === messageTypes.TOGGLE_PIN || + messageType === messageTypes.CHANGE_ROLE + ); +} + export { localIDPrefix, messageKey, @@ -671,4 +681,5 @@ modifyItemForResultScreen, constructChangeRoleEntityText, useNextLocalID, + isInvalidSidebarSource, }; diff --git a/lib/shared/message-utils.test.js b/lib/shared/message-utils.test.js new file mode 100644 --- /dev/null +++ b/lib/shared/message-utils.test.js @@ -0,0 +1,24 @@ +// @flow + +import { isInvalidSidebarSource } from './message-utils.js'; +import { messageTypes } from '../types/message-types-enum.js'; +import { values } from '../utils/objects.js'; + +describe('isInvalidSidebarSource', () => { + it('should return true for all message types except for the ones listed', () => { + values(messageTypes).forEach(messageType => { + const shouldBeInvalidSidebarSource = isInvalidSidebarSource(messageType); + if ( + messageType === messageTypes.REACTION || + messageType === messageTypes.EDIT_MESSAGE || + messageType === messageTypes.SIDEBAR_SOURCE || + messageType === messageTypes.TOGGLE_PIN || + messageType === messageTypes.CHANGE_ROLE + ) { + expect(shouldBeInvalidSidebarSource).toBe(true); + } else { + expect(shouldBeInvalidSidebarSource).toBe(false); + } + }); + }); +}); diff --git a/lib/shared/thread-utils.js b/lib/shared/thread-utils.js --- a/lib/shared/thread-utils.js +++ b/lib/shared/thread-utils.js @@ -9,7 +9,7 @@ import { generatePendingThreadColor } from './color-utils.js'; import { type ParserRules } from './markdown.js'; import { extractMentionsFromText } from './mention-utils.js'; -import { getMessageTitle } from './message-utils.js'; +import { getMessageTitle, isInvalidSidebarSource } from './message-utils.js'; import { relationshipBlockedInEitherDirection } from './relationship-utils.js'; import threadWatcher from './thread-watcher.js'; import { @@ -1254,17 +1254,10 @@ state => state.userStore.userInfos[messageInfo.creator.id], ); - if (!messageInfo.id || threadInfo.sourceMessageID === messageInfo.id) { - return false; - } - - const { type } = messageInfo; if ( - type === messageTypes.REACTION || - type === messageTypes.EDIT_MESSAGE || - type === messageTypes.SIDEBAR_SOURCE || - type === messageTypes.TOGGLE_PIN || - type === messageTypes.CHANGE_ROLE + !messageInfo.id || + threadInfo.sourceMessageID === messageInfo.id || + isInvalidSidebarSource(messageInfo.type) ) { return false; }