isolate message form service

This commit is contained in:
Ramón Souza 2023-05-17 09:43:37 -03:00
parent f9b0157cb0
commit 54feedd721
2 changed files with 62 additions and 4 deletions

View File

@ -1,7 +1,7 @@
import React, { useEffect, useRef } from 'react';
import { throttle } from '/imports/utils/throttle';
import { makeCall } from '/imports/ui/services/api';
import ChatService from '/imports/ui/components/chat/service';
import Service from './service';
import { layoutSelect } from '/imports/ui/components/layout/context';
import { defineMessages, useIntl } from 'react-intl';
import { isChatEnabled } from '/imports/ui/services/features';
@ -338,8 +338,8 @@ const ChatMessageFormContainer: React.FC = ({
const idChatOpen = layoutSelect((i) => i.idChatOpen);
const handleSendMessage = (message) => {
ChatService.setUserSentMessage(true);
return ChatService.sendGroupMessage(message, idChatOpen);
Service.setUserSentMessage(true);
return Service.sendGroupMessage(message, idChatOpen);
};
const startUserTyping = throttle(
(chatId) => makeCall('startUserTyping', chatId),
@ -357,7 +357,7 @@ const ChatMessageFormContainer: React.FC = ({
{...{
startUserTyping,
stopUserTyping,
UnsentMessagesCollection: ChatService.UnsentMessagesCollection,
UnsentMessagesCollection: Service.UnsentMessagesCollection,
minMessageLength: CHAT_CONFIG.min_message_length,
maxMessageLength: CHAT_CONFIG.max_message_length,
handleSendMessage,

View File

@ -0,0 +1,58 @@
import Auth from '/imports/ui/services/auth';
import Storage from '/imports/ui/services/storage/session';
import { makeCall } from '/imports/ui/services/api';
import { indexOf, without } from '/imports/utils/array-utils';
const CHAT_CONFIG = Meteor.settings.public.chat;
const PUBLIC_CHAT_ID = CHAT_CONFIG.public_id;
const PUBLIC_GROUP_CHAT_ID = CHAT_CONFIG.public_group_id;
const CHAT_EMPHASIZE_TEXT = CHAT_CONFIG.moderatorChatEmphasized;
const UnsentMessagesCollection = new Mongo.Collection(null);
export const UserSentMessageCollection = new Mongo.Collection(null);
// session for closed chat list
const CLOSED_CHAT_LIST_KEY = 'closedChatList';
const setUserSentMessage = (bool) => {
UserSentMessageCollection.upsert(
{ userId: Auth.userID },
{ $set: { sent: bool } },
);
};
const sendGroupMessage = (message, idChatOpen) => {
const { userID: senderUserId } = Auth;
const chatID = idChatOpen === PUBLIC_CHAT_ID
? PUBLIC_GROUP_CHAT_ID
: idChatOpen;
const receiverId = { id: chatID };
const payload = {
correlationId: `${senderUserId}-${Date.now()}`,
sender: {
id: senderUserId,
name: '',
role: '',
},
chatEmphasizedText: CHAT_EMPHASIZE_TEXT,
message,
};
const currentClosedChats = Storage.getItem(CLOSED_CHAT_LIST_KEY);
// Remove the chat that user send messages from the session.
if (indexOf(currentClosedChats, receiverId.id) > -1) {
Storage.setItem(CLOSED_CHAT_LIST_KEY, without(currentClosedChats, receiverId.id));
}
return makeCall('sendGroupChatMsg', chatID, payload);
};
export default {
setUserSentMessage,
sendGroupMessage,
UnsentMessagesCollection,
};