// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved. // See License.txt for license information. import React from 'react'; import {FormattedMessage} from 'react-intl'; import {PostTypes} from 'utils/constants.jsx'; import {formatText} from 'utils/text_formatting.jsx'; const joinLeaveMessageGetters = { [PostTypes.JOIN_CHANNEL]: getJoinChannelMessage, [PostTypes.LEAVE_CHANNEL]: getLeaveChannelMessage, [PostTypes.ADD_TO_CHANNEL]: getAddToChannelMessage, [PostTypes.REMOVE_FROM_CHANNEL]: getRemoveFromChannelMessage }; function renderUsername(value, options) { return renderFormattedText(value, {...options, markdown: false}); } function renderFormattedText(value, options) { return ; } function renderJoinLeaveMessage(post, options, messageFunction) { if (post.props.messages) { return (
{post.props.messages.map((message, key) => {joinLeaveMessageGetters[message.type](message, options)})}
); } return messageFunction(post.props, options); } function renderJoinChannelMessage(post, options) { return renderJoinLeaveMessage(post, options, getJoinChannelMessage); } function getJoinChannelMessage(messageProps, options) { const username = renderUsername(messageProps.username, options); return ( ); } function renderLeaveChannelMessage(post, options) { return renderJoinLeaveMessage(post, options, getLeaveChannelMessage); } function getLeaveChannelMessage(messageProps, options) { const username = renderUsername(messageProps.username, options); return ( ); } function renderAddToChannelMessage(post, options) { return renderJoinLeaveMessage(post, options, getAddToChannelMessage); } function getAddToChannelMessage(messageProps, options) { const username = renderUsername(messageProps.username, options); const addedUsername = renderUsername(messageProps.addedUsername, options); return ( ); } function renderRemoveFromChannelMessage(post, options) { return renderJoinLeaveMessage(post, options, getRemoveFromChannelMessage); } function getRemoveFromChannelMessage(messageProps, options) { const removedUsername = renderUsername(messageProps.removedUsername, options); return ( ); } function renderHeaderChangeMessage(post, options) { if (!post.props.username) { return null; } const headerOptions = { ...options, singleline: true }; const username = renderUsername(post.props.username, options); const oldHeader = post.props.old_header ? renderFormattedText(post.props.old_header, headerOptions) : null; const newHeader = post.props.new_header ? renderFormattedText(post.props.new_header, headerOptions) : null; if (post.props.new_header) { if (post.props.old_header) { return ( ); } return ( ); } else if (post.props.old_header) { return ( ); } return null; } function renderDisplayNameChangeMessage(post, options) { if (!(post.props.username && post.props.old_displayname && post.props.new_displayname)) { return null; } const username = renderUsername(post.props.username, options); const oldDisplayName = post.props.old_displayname; const newDisplayName = post.props.new_displayname; return ( ); } function renderPurposeChangeMessage(post, options) { if (!post.props.username) { return null; } const username = renderUsername(post.props.username, options); const oldPurpose = post.props.old_purpose; const newPurpose = post.props.new_purpose; if (post.props.new_purpose) { if (post.props.old_purpose) { return ( ); } return ( ); } else if (post.props.old_purpose) { return ( ); } return null; } function renderChannelDeletedMessage(post, options) { if (!post.props.username) { return null; } const username = renderUsername(post.props.username, options); return ( ); } const systemMessageRenderers = { [PostTypes.JOIN_CHANNEL]: renderJoinChannelMessage, [PostTypes.LEAVE_CHANNEL]: renderLeaveChannelMessage, [PostTypes.ADD_TO_CHANNEL]: renderAddToChannelMessage, [PostTypes.REMOVE_FROM_CHANNEL]: renderRemoveFromChannelMessage, [PostTypes.HEADER_CHANGE]: renderHeaderChangeMessage, [PostTypes.DISPLAYNAME_CHANGE]: renderDisplayNameChangeMessage, [PostTypes.PURPOSE_CHANGE]: renderPurposeChangeMessage, [PostTypes.CHANNEL_DELETED]: renderChannelDeletedMessage }; export function renderSystemMessage(post, options) { if (!systemMessageRenderers[post.type]) { return null; } return systemMessageRenderers[post.type](post, options); }