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

fix: save/copy chat if chat has been cleared #18692

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ export const ChatActions: React.FC = () => {
const exportedString = generateExportedMessages(
dataHistory.chat_message_public,
dataHistory.user_welcomeMsgs[0],
intl,
);
if (downloadOrCopyRef.current === 'download') {
const link = document.createElement('a');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ query getChatMessageHistory {
chat_message_public(order_by: {createdTime: asc}) {
message
messageId
messageType
createdTime
user {
userId
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,45 @@
import { Message } from "/imports/ui/Types/message";
import { makeCall } from "/imports/ui/services/api";
import { stripTags, unescapeHtml } from '/imports/utils/string-utils';
import { defineMessages } from 'react-intl';
import { ChatMessageType } from '/imports/ui/core/enums/chat';

const intlMessages = defineMessages({
chatClear: {
id: 'app.chat.clearPublicChatMessage',
description: 'message of when clear the public chat',
},
});

export const htmlDecode = (input: string) => {
const replacedBRs = input.replaceAll('<br/>', '\n');
return unescapeHtml(stripTags(replacedBRs));
};

export const generateExportedMessages = (messages: Array<Message>, welcomeSettings: {welcomeMsg: string, welcomeMsgForModerators: string | null} ): string => {
export const generateExportedMessages = (messages: Array<Message>, welcomeSettings: { welcomeMsg: string, welcomeMsgForModerators: string | null }, intl): string => {
const welcomeMessage = htmlDecode(welcomeSettings.welcomeMsg);
const modOnlyMessage = welcomeSettings.welcomeMsgForModerators && htmlDecode(welcomeSettings.welcomeMsgForModerators);
const systemMessages = `${welcomeMessage ? `system: ${welcomeMessage}`: ''}\n ${modOnlyMessage ? `system: ${modOnlyMessage}`: ''}\n`
const systemMessages = `${welcomeMessage ? `system: ${welcomeMessage}` : ''}\n ${modOnlyMessage ? `system: ${modOnlyMessage}` : ''}\n`

const text = messages.reduce((acc, message) => {
const date = new Date(message.createdTime);
const hour = date.getHours().toString().padStart(2, '0');
const min = date.getMinutes().toString().padStart(2, '0');
const hourMin = `[${hour}:${min}]`;
const userName = message.user.name;
const messageText = htmlDecode(message.message);
return `${acc}${hourMin} [${userName} : ${message.user.role}]: ${messageText}\n`;
},welcomeMessage? systemMessages : '');
const userName = message.user ? `[${message.user.name} : ${message.user.role}]: ` : '';
let messageText = '';

switch (message.messageType) {
case ChatMessageType.CHAT_CLEAR:
messageText = intl.formatMessage(intlMessages.chatClear);
break;
case ChatMessageType.TEXT:
default:
messageText = htmlDecode(message.message);
break;
}
return `${acc}${hourMin} ${userName}${messageText}\n`;
}, welcomeMessage ? systemMessages : '');
return text;
};

Expand All @@ -34,7 +53,5 @@ export const getDateString = (date = new Date()) => {
return dateString;
};


// TODO: Make action using mutations
export const clearPublicChatHistory = () => (makeCall('clearPublicChatHistory'));