2019-08-08 23:12:53 +08:00
|
|
|
import { GroupChatMsg } from '/imports/api/group-chat-msg';
|
2018-03-10 02:42:14 +08:00
|
|
|
import Logger from '/imports/startup/server/logger';
|
2022-03-24 04:11:01 +08:00
|
|
|
import addSystemMsg from '/imports/api/group-chat-msg/server/modifiers/addSystemMsg';
|
2022-11-22 00:07:30 +08:00
|
|
|
import clearChatHasMessages from '/imports/api/users-persistent-data/server/modifiers/clearChatHasMessages';
|
2023-01-14 00:19:52 +08:00
|
|
|
import UsersPersistentData from '/imports/api/users-persistent-data';
|
2018-03-10 02:42:14 +08:00
|
|
|
|
2023-04-01 04:40:41 +08:00
|
|
|
export default async function clearGroupChatMsg(meetingId, chatId) {
|
2018-08-03 22:03:16 +08:00
|
|
|
const CHAT_CONFIG = Meteor.settings.public.chat;
|
|
|
|
const PUBLIC_CHAT_SYSTEM_ID = CHAT_CONFIG.system_userid;
|
|
|
|
const PUBLIC_GROUP_CHAT_ID = CHAT_CONFIG.public_group_id;
|
|
|
|
const CHAT_CLEAR_MESSAGE = CHAT_CONFIG.system_messages_keys.chat_clear;
|
2021-02-10 21:14:30 +08:00
|
|
|
const SYSTEM_CHAT_TYPE = CHAT_CONFIG.type_system;
|
2018-08-03 22:03:16 +08:00
|
|
|
|
2018-03-10 02:42:14 +08:00
|
|
|
if (chatId) {
|
2020-11-25 04:44:13 +08:00
|
|
|
try {
|
2023-04-01 04:40:41 +08:00
|
|
|
const numberAffected = await GroupChatMsg.removeAsync({ meetingId, chatId });
|
2020-11-25 04:44:13 +08:00
|
|
|
|
|
|
|
if (numberAffected) {
|
|
|
|
Logger.info(`Cleared GroupChatMsg (${meetingId}, ${chatId})`);
|
|
|
|
const clearMsg = {
|
2021-02-10 21:14:30 +08:00
|
|
|
id: `${SYSTEM_CHAT_TYPE}-${CHAT_CLEAR_MESSAGE}`,
|
2020-11-25 04:44:13 +08:00
|
|
|
timestamp: Date.now(),
|
|
|
|
correlationId: `${PUBLIC_CHAT_SYSTEM_ID}-${Date.now()}`,
|
|
|
|
sender: {
|
|
|
|
id: PUBLIC_CHAT_SYSTEM_ID,
|
|
|
|
name: '',
|
|
|
|
},
|
|
|
|
message: CHAT_CLEAR_MESSAGE,
|
|
|
|
};
|
2023-04-01 04:40:41 +08:00
|
|
|
await addSystemMsg(meetingId, PUBLIC_GROUP_CHAT_ID, clearMsg);
|
|
|
|
await clearChatHasMessages(meetingId, chatId);
|
2023-01-14 00:19:52 +08:00
|
|
|
|
|
|
|
//clear offline users' data
|
|
|
|
const selector = {
|
|
|
|
meetingId,
|
|
|
|
'shouldPersist.hasConnectionStatus': { $ne: true },
|
|
|
|
'shouldPersist.hasMessages.private': { $ne: true },
|
2023-04-01 04:40:41 +08:00
|
|
|
loggedOut: true,
|
2023-01-14 00:19:52 +08:00
|
|
|
};
|
2023-04-01 04:40:41 +08:00
|
|
|
await UsersPersistentData.removeAsync(selector);
|
2020-11-25 04:44:13 +08:00
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
Logger.error(`Error on clearing GroupChat (${meetingId}, ${chatId}). ${err}`);
|
|
|
|
}
|
2019-02-15 04:18:06 +08:00
|
|
|
return true;
|
2018-03-10 02:42:14 +08:00
|
|
|
}
|
|
|
|
|
2018-08-03 01:23:20 +08:00
|
|
|
if (meetingId) {
|
2020-11-25 04:44:13 +08:00
|
|
|
try {
|
2023-04-01 04:40:41 +08:00
|
|
|
const numberAffected = await GroupChatMsg.removeAsync({ meetingId });
|
2018-08-03 01:23:20 +08:00
|
|
|
|
2020-11-25 04:44:13 +08:00
|
|
|
if (numberAffected) {
|
|
|
|
Logger.info(`Cleared GroupChatMsg (${meetingId})`);
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
Logger.error(`Error on clearing GroupChatMsg (${meetingId}). ${err}`);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
try {
|
2023-04-01 04:40:41 +08:00
|
|
|
const numberAffected = await GroupChatMsg
|
|
|
|
.removeAsync({ chatId: { $eq: PUBLIC_GROUP_CHAT_ID } });
|
2020-11-25 04:44:13 +08:00
|
|
|
|
|
|
|
if (numberAffected) {
|
2023-04-01 04:40:41 +08:00
|
|
|
await clearChatHasMessages(meetingId, chatId=PUBLIC_GROUP_CHAT_ID);
|
2022-11-22 00:07:30 +08:00
|
|
|
|
2020-11-25 04:44:13 +08:00
|
|
|
Logger.info('Cleared GroupChatMsg (all)');
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
Logger.error(`Error on clearing GroupChatMsg (all). ${err}`);
|
|
|
|
}
|
|
|
|
}
|
2023-04-01 04:40:41 +08:00
|
|
|
//True resturned because the function requires a return.
|
|
|
|
return true;
|
2018-03-10 02:42:14 +08:00
|
|
|
}
|