Merge pull request #17800 from Tainan404/fix-message-duplication-chat

Fix: duplicate messages when start a new chat
This commit is contained in:
Anton Georgiev 2023-05-03 19:57:18 -04:00 committed by GitHub
commit a295cc93b5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 17 additions and 17 deletions

View File

@ -1,4 +1,5 @@
import { GroupChatMsg } from '/imports/api/group-chat-msg';
import GroupChat from '/imports/api/group-chat';
import Logger from '/imports/startup/server/logger';
import flat from 'flat';
import { parseMessage } from './addGroupChatMsg';
@ -24,7 +25,14 @@ export default async function addBulkGroupChatMsgs(msgs) {
senderRole: sender.role,
};
})
.map((el) => flat(el, { safe: true }));
.map((el) => flat(el, { safe: true }))
.map((msg)=>{
const groupChat = GroupChat.findOne({ meetingId: msg.meetingId, chatId: msg.chatId });
return {
...msg,
participants: [...groupChat.users],
};
});
try {
const { insertedCount } = await GroupChatMsg.rawCollection().insertMany(mappedMsgs);

View File

@ -3,6 +3,7 @@ import Logger from '/imports/startup/server/logger';
import { GroupChatMsg } from '/imports/api/group-chat-msg';
import { BREAK_LINE } from '/imports/utils/lineEndings';
import changeHasMessages from '/imports/api/users-persistent-data/server/modifiers/changeHasMessages';
import GroupChat from '/imports/api/group-chat';
export function parseMessage(message) {
let parsedMessage = message || '';
@ -34,6 +35,8 @@ export default async function addGroupChatMsg(meetingId, chatId, msg) {
...restMsg
} = msg;
const groupChat = GroupChat.findOne({ meetingId, chatId });
const msgDocument = {
...restMsg,
sender: sender.id,
@ -41,6 +44,7 @@ export default async function addGroupChatMsg(meetingId, chatId, msg) {
senderRole: sender.role,
meetingId,
chatId,
participants: [...groupChat.users],
message: parseMessage(msg.message),
};

View File

@ -6,8 +6,7 @@ import GroupChat from '/imports/api/group-chat';
import Logger from '/imports/startup/server/logger';
import AuthTokenValidation, { ValidationStates } from '/imports/api/auth-token-validation';
async function groupChatMsg(chatCount) {
check(chatCount, Number);
async function groupChatMsg() {
const tokenValidation = await AuthTokenValidation
.findOneAsync({ connectionId: this.connection.id });
@ -36,10 +35,10 @@ async function groupChatMsg(chatCount) {
timestamp: { $gte: User.authTokenValidatedTime },
$or: [
{ meetingId, chatId: { $eq: PUBLIC_GROUP_CHAT_ID } },
{ meetingId, chatId: { $in: chatsIds } },
{ meetingId, participants: { $in: [userId] } },
],
};
return GroupChatMsg.find(selector);
return GroupChatMsg.find(selector, { fields: { participants: 0 } });
}
function publish(...args) {

View File

@ -131,22 +131,11 @@ export default withTracker(() => {
let groupChatMessageHandler = {};
if (isChatEnabled() && ready) {
const chatsCount = GroupChat.find({
$or: [
{
meetingId,
access: PUBLIC_CHAT_TYPE,
chatId: { $ne: PUBLIC_GROUP_CHAT_ID },
},
{ meetingId, users: { $all: [requesterUserId] } },
],
}).count();
const subHandler = {
...subscriptionErrorHandler,
};
groupChatMessageHandler = Meteor.subscribe('group-chat-msg', chatsCount, subHandler);
groupChatMessageHandler = Meteor.subscribe('group-chat-msg', subHandler);
}
// TODO: Refactor all the late subscribers