2017-10-12 10:00:28 +08:00
|
|
|
import Users from '/imports/api/users';
|
|
|
|
import Meetings from '/imports/api/meetings';
|
2019-08-03 02:18:33 +08:00
|
|
|
import { GroupChatMsg } from '/imports/api/group-chat-msg';
|
2018-07-26 22:56:26 +08:00
|
|
|
import GroupChat from '/imports/api/group-chat';
|
2016-06-02 21:46:35 +08:00
|
|
|
import Auth from '/imports/ui/services/auth';
|
2016-07-05 02:53:47 +08:00
|
|
|
import UnreadMessages from '/imports/ui/services/unread-messages';
|
2017-03-01 06:40:16 +08:00
|
|
|
import Storage from '/imports/ui/services/storage/session';
|
2017-04-26 21:47:44 +08:00
|
|
|
import { makeCall } from '/imports/ui/services/api';
|
2017-03-22 05:52:10 +08:00
|
|
|
import _ from 'lodash';
|
2020-05-01 05:50:34 +08:00
|
|
|
import { meetingIsBreakout } from '/imports/ui/components/app/service';
|
2016-06-02 21:00:57 +08:00
|
|
|
|
2016-08-17 23:48:03 +08:00
|
|
|
const CHAT_CONFIG = Meteor.settings.public.chat;
|
|
|
|
const GROUPING_MESSAGES_WINDOW = CHAT_CONFIG.grouping_messages_window;
|
2016-06-02 00:33:19 +08:00
|
|
|
|
2016-08-17 23:48:03 +08:00
|
|
|
const SYSTEM_CHAT_TYPE = CHAT_CONFIG.type_system;
|
2016-06-02 00:33:19 +08:00
|
|
|
|
2016-08-17 23:48:03 +08:00
|
|
|
const PUBLIC_CHAT_ID = CHAT_CONFIG.public_id;
|
2018-07-27 21:44:21 +08:00
|
|
|
const PUBLIC_GROUP_CHAT_ID = CHAT_CONFIG.public_group_id;
|
|
|
|
const PRIVATE_CHAT_TYPE = CHAT_CONFIG.type_private;
|
2016-06-02 21:00:57 +08:00
|
|
|
|
2019-02-26 07:31:33 +08:00
|
|
|
const ROLE_MODERATOR = Meteor.settings.public.user.role_moderator;
|
|
|
|
|
2016-07-01 01:10:36 +08:00
|
|
|
const ScrollCollection = new Mongo.Collection(null);
|
|
|
|
|
2019-02-05 20:24:45 +08:00
|
|
|
const UnsentMessagesCollection = new Mongo.Collection(null);
|
|
|
|
|
2021-01-20 01:06:32 +08:00
|
|
|
export const UserSentMessageCollection = new Mongo.Collection(null);
|
|
|
|
|
2017-03-17 23:27:37 +08:00
|
|
|
// session for closed chat list
|
|
|
|
const CLOSED_CHAT_LIST_KEY = 'closedChatList';
|
|
|
|
|
2020-07-28 20:55:21 +08:00
|
|
|
const POLL_MESSAGE_PREFIX = 'bbb-published-poll-<br/>';
|
|
|
|
|
2021-01-20 01:06:32 +08:00
|
|
|
const setUserSentMessage = (bool) => {
|
|
|
|
UserSentMessageCollection.upsert(
|
|
|
|
{ userId: Auth.userID },
|
|
|
|
{ $set: { sent: bool } },
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-08-30 00:26:07 +08:00
|
|
|
const getUser = userId => Users.findOne({ userId });
|
2017-07-12 21:18:26 +08:00
|
|
|
|
2020-08-20 00:22:43 +08:00
|
|
|
const getPrivateChatByUsers = userId => GroupChat
|
|
|
|
.findOne({ users: { $all: [userId, Auth.userID] } });
|
|
|
|
|
2019-08-22 20:05:06 +08:00
|
|
|
const getWelcomeProp = () => Meetings.findOne({ meetingId: Auth.meetingID },
|
|
|
|
{ fields: { welcomeProp: 1 } });
|
2018-10-08 22:22:45 +08:00
|
|
|
|
2018-07-26 22:56:26 +08:00
|
|
|
const mapGroupMessage = (message) => {
|
|
|
|
const mappedMessage = {
|
2020-01-22 05:08:48 +08:00
|
|
|
id: message._id || message.id,
|
2018-07-26 22:56:26 +08:00
|
|
|
content: message.content,
|
2020-01-22 05:08:48 +08:00
|
|
|
time: message.timestamp || message.time,
|
2018-07-26 22:56:26 +08:00
|
|
|
sender: null,
|
2021-01-20 01:06:32 +08:00
|
|
|
key: message.key
|
2018-07-26 22:56:26 +08:00
|
|
|
};
|
|
|
|
|
2020-08-20 00:22:43 +08:00
|
|
|
if (message.sender && message.sender.id !== SYSTEM_CHAT_TYPE) {
|
2020-10-13 21:49:12 +08:00
|
|
|
const sender = Users.findOne({ userId: message.sender.id }, { fields: { avatar: 1, role: 1 } });
|
2018-12-20 02:51:03 +08:00
|
|
|
|
|
|
|
const mappedSender = {
|
2020-10-13 21:49:12 +08:00
|
|
|
avatar: sender?.avatar,
|
2020-08-20 00:22:43 +08:00
|
|
|
color: message.color,
|
|
|
|
isModerator: sender?.role === ROLE_MODERATOR,
|
|
|
|
name: message.sender.name,
|
|
|
|
isOnline: !!sender,
|
2018-12-20 02:51:03 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
mappedMessage.sender = mappedSender;
|
2018-07-26 22:56:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return mappedMessage;
|
|
|
|
};
|
|
|
|
|
|
|
|
const reduceGroupMessages = (previous, current) => {
|
|
|
|
const lastMessage = previous[previous.length - 1];
|
|
|
|
const currentMessage = current;
|
2018-07-27 21:44:21 +08:00
|
|
|
currentMessage.content = [{
|
2018-07-26 22:56:26 +08:00
|
|
|
id: current.id,
|
|
|
|
text: current.message,
|
|
|
|
time: current.timestamp,
|
2020-08-20 00:22:43 +08:00
|
|
|
color: current.color,
|
2018-07-26 22:56:26 +08:00
|
|
|
}];
|
2021-01-20 01:06:32 +08:00
|
|
|
if (!lastMessage) {
|
2018-07-26 22:56:26 +08:00
|
|
|
return previous.concat(currentMessage);
|
|
|
|
}
|
|
|
|
// Check if the last message is from the same user and time discrepancy
|
2020-07-28 20:55:21 +08:00
|
|
|
// between the two messages exceeds window and then group current
|
|
|
|
// message with the last one
|
2018-07-26 22:56:26 +08:00
|
|
|
const timeOfLastMessage = lastMessage.content[lastMessage.content.length - 1].time;
|
2020-07-28 20:55:21 +08:00
|
|
|
const isOrWasPoll = currentMessage.message.includes(POLL_MESSAGE_PREFIX)
|
|
|
|
|| lastMessage.message.includes(POLL_MESSAGE_PREFIX);
|
|
|
|
const groupingWindow = isOrWasPoll ? 0 : GROUPING_MESSAGES_WINDOW;
|
|
|
|
|
2020-08-20 00:22:43 +08:00
|
|
|
if (lastMessage.sender.id === currentMessage.sender.id
|
2020-07-28 20:55:21 +08:00
|
|
|
&& (currentMessage.timestamp - timeOfLastMessage) <= groupingWindow) {
|
2018-07-26 22:56:26 +08:00
|
|
|
lastMessage.content.push(currentMessage.content.pop());
|
|
|
|
return previous;
|
|
|
|
}
|
|
|
|
|
|
|
|
return previous.concat(currentMessage);
|
|
|
|
};
|
|
|
|
|
2021-01-20 01:06:32 +08:00
|
|
|
const getChatMessages = (chatId) => {
|
2021-02-04 04:49:58 +08:00
|
|
|
return []
|
2021-01-20 01:06:32 +08:00
|
|
|
if (chatId === PUBLIC_CHAT_ID) {
|
|
|
|
return GroupChatMsg.find({
|
|
|
|
meetingId: Auth.meetingID,
|
|
|
|
chatId: PUBLIC_GROUP_CHAT_ID,
|
|
|
|
|
|
|
|
}, { sort: ['timestamp'] }).fetch();
|
|
|
|
}
|
|
|
|
const senderId = Auth.userID;
|
|
|
|
|
|
|
|
const privateChat = GroupChat.findOne({
|
|
|
|
meetingId: Auth.meetingID,
|
|
|
|
users: { $all: [chatId, senderId] },
|
|
|
|
access: PRIVATE_CHAT_TYPE,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (privateChat) {
|
|
|
|
const {
|
|
|
|
chatId: id,
|
|
|
|
} = privateChat;
|
|
|
|
|
|
|
|
return GroupChatMsg.find({
|
|
|
|
meetingId: Auth.meetingID,
|
|
|
|
chatId: id,
|
|
|
|
}, { sort: ['timestamp'] }).fetch();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-01-14 21:23:35 +08:00
|
|
|
const reduceAndMapGroupMessages = messages => (messages
|
|
|
|
.reduce(reduceGroupMessages, []).map(mapGroupMessage));
|
2018-07-27 21:44:21 +08:00
|
|
|
|
2020-01-22 05:08:48 +08:00
|
|
|
const reduceAndDontMapGroupMessages = messages => (messages
|
|
|
|
.reduce(reduceGroupMessages, []));
|
|
|
|
|
2018-07-26 22:56:26 +08:00
|
|
|
const getPublicGroupMessages = () => {
|
2021-02-04 04:49:58 +08:00
|
|
|
return [];
|
2018-07-26 22:56:26 +08:00
|
|
|
const publicGroupMessages = GroupChatMsg.find({
|
2019-08-22 20:05:06 +08:00
|
|
|
meetingId: Auth.meetingID,
|
2018-07-27 21:44:21 +08:00
|
|
|
chatId: PUBLIC_GROUP_CHAT_ID,
|
2019-01-14 21:23:35 +08:00
|
|
|
}, { sort: ['timestamp'] }).fetch();
|
2018-07-26 22:56:26 +08:00
|
|
|
return publicGroupMessages;
|
|
|
|
};
|
|
|
|
|
2018-10-16 04:03:17 +08:00
|
|
|
const getPrivateGroupMessages = () => {
|
2021-02-04 04:49:58 +08:00
|
|
|
return [];
|
2018-10-16 04:03:17 +08:00
|
|
|
const chatID = Session.get('idChatOpen');
|
2019-08-09 02:53:08 +08:00
|
|
|
const senderId = Auth.userID;
|
2018-07-26 22:56:26 +08:00
|
|
|
|
2018-07-27 21:44:21 +08:00
|
|
|
const privateChat = GroupChat.findOne({
|
2019-08-22 20:05:06 +08:00
|
|
|
meetingId: Auth.meetingID,
|
2019-08-09 02:53:08 +08:00
|
|
|
users: { $all: [chatID, senderId] },
|
2018-08-01 01:13:36 +08:00
|
|
|
access: PRIVATE_CHAT_TYPE,
|
2019-08-23 03:14:36 +08:00
|
|
|
});
|
2018-07-26 22:56:26 +08:00
|
|
|
|
|
|
|
let messages = [];
|
|
|
|
|
|
|
|
if (privateChat) {
|
|
|
|
const {
|
2018-07-27 21:44:21 +08:00
|
|
|
chatId,
|
2018-07-26 22:56:26 +08:00
|
|
|
} = privateChat;
|
|
|
|
|
|
|
|
messages = GroupChatMsg.find({
|
2019-08-22 20:05:06 +08:00
|
|
|
meetingId: Auth.meetingID,
|
2018-07-27 21:44:21 +08:00
|
|
|
chatId,
|
2019-01-14 21:23:35 +08:00
|
|
|
}, { sort: ['timestamp'] }).fetch();
|
2018-07-26 22:56:26 +08:00
|
|
|
}
|
|
|
|
|
2020-01-22 05:08:48 +08:00
|
|
|
return reduceAndDontMapGroupMessages(messages, []);
|
2018-07-26 22:56:26 +08:00
|
|
|
};
|
|
|
|
|
2016-06-14 01:00:38 +08:00
|
|
|
const isChatLocked = (receiverID) => {
|
|
|
|
const isPublic = receiverID === PUBLIC_CHAT_ID;
|
2019-08-22 20:05:06 +08:00
|
|
|
const meeting = Meetings.findOne({ meetingId: Auth.meetingID },
|
2020-04-28 21:24:44 +08:00
|
|
|
{ fields: { 'lockSettingsProps.disablePublicChat': 1, 'lockSettingsProps.disablePrivateChat': 1 } });
|
2019-08-22 20:05:06 +08:00
|
|
|
const user = Users.findOne({ meetingId: Auth.meetingID, userId: Auth.userID },
|
|
|
|
{ fields: { locked: 1, role: 1 } });
|
2019-09-07 04:28:02 +08:00
|
|
|
const receiver = Users.findOne({ meetingId: Auth.meetingID, userId: receiverID },
|
|
|
|
{ fields: { role: 1 } });
|
|
|
|
const isReceiverModerator = receiver && receiver.role === ROLE_MODERATOR;
|
2017-07-13 04:02:55 +08:00
|
|
|
|
2020-05-01 05:50:34 +08:00
|
|
|
// disable private chat in breakouts
|
|
|
|
if (meetingIsBreakout()) {
|
|
|
|
return !isPublic;
|
|
|
|
}
|
|
|
|
|
2019-04-04 01:23:31 +08:00
|
|
|
if (meeting.lockSettingsProps !== undefined) {
|
2019-08-09 02:53:08 +08:00
|
|
|
if (user.locked && user.role !== ROLE_MODERATOR) {
|
2019-02-26 07:31:33 +08:00
|
|
|
if (isPublic) {
|
2019-04-10 21:44:34 +08:00
|
|
|
return meeting.lockSettingsProps.disablePublicChat;
|
2019-02-26 07:31:33 +08:00
|
|
|
}
|
2019-09-07 04:28:02 +08:00
|
|
|
return !isReceiverModerator
|
2019-08-30 00:26:07 +08:00
|
|
|
&& meeting.lockSettingsProps.disablePrivateChat;
|
2019-02-26 07:31:33 +08:00
|
|
|
}
|
2016-06-14 01:00:38 +08:00
|
|
|
}
|
|
|
|
|
2017-09-07 03:57:24 +08:00
|
|
|
return false;
|
2016-06-14 01:00:38 +08:00
|
|
|
};
|
|
|
|
|
2016-07-05 02:53:47 +08:00
|
|
|
const hasUnreadMessages = (receiverID) => {
|
|
|
|
const isPublic = receiverID === PUBLIC_CHAT_ID;
|
2018-07-27 21:44:21 +08:00
|
|
|
const chatType = isPublic ? PUBLIC_GROUP_CHAT_ID : receiverID;
|
2017-07-12 21:18:26 +08:00
|
|
|
return UnreadMessages.count(chatType) > 0;
|
2016-07-05 02:53:47 +08:00
|
|
|
};
|
|
|
|
|
2016-07-12 03:42:54 +08:00
|
|
|
const lastReadMessageTime = (receiverID) => {
|
|
|
|
const isPublic = receiverID === PUBLIC_CHAT_ID;
|
2018-07-27 21:44:21 +08:00
|
|
|
const chatType = isPublic ? PUBLIC_GROUP_CHAT_ID : receiverID;
|
2016-07-12 03:42:54 +08:00
|
|
|
|
2017-07-12 21:18:26 +08:00
|
|
|
return UnreadMessages.get(chatType);
|
2016-07-12 03:42:54 +08:00
|
|
|
};
|
|
|
|
|
2018-10-16 04:03:17 +08:00
|
|
|
const sendGroupMessage = (message) => {
|
2021-02-02 06:12:04 +08:00
|
|
|
// TODO: Refactor to use chatId directly
|
|
|
|
const chatIdToSent = Session.get('idChatOpen') === PUBLIC_CHAT_ID ? PUBLIC_GROUP_CHAT_ID : Session.get('idChatOpen')
|
|
|
|
const chat = GroupChat.findOne({ chatId: chatIdToSent },
|
|
|
|
{ fields: { users: 1 } });
|
|
|
|
const chatID = Session.get('idChatOpen') === PUBLIC_CHAT_ID ? PUBLIC_GROUP_CHAT_ID : chat.users.filter(id => id !== Auth.userID)[0];
|
2018-07-27 21:44:21 +08:00
|
|
|
const isPublicChat = chatID === PUBLIC_CHAT_ID;
|
2018-07-26 22:56:26 +08:00
|
|
|
|
2019-08-22 23:19:54 +08:00
|
|
|
let destinationChatId = PUBLIC_GROUP_CHAT_ID;
|
2018-07-26 22:56:26 +08:00
|
|
|
|
2019-08-30 00:26:07 +08:00
|
|
|
const { fullname: senderName, userID: senderUserId } = Auth;
|
2019-08-09 02:53:08 +08:00
|
|
|
const receiverId = { id: chatID };
|
2018-07-27 21:44:21 +08:00
|
|
|
|
2018-07-26 22:56:26 +08:00
|
|
|
if (!isPublicChat) {
|
2019-08-30 00:26:07 +08:00
|
|
|
const privateChat = GroupChat.findOne({ users: { $all: [chatID, senderUserId] } },
|
|
|
|
{ fields: { chatId: 1 } });
|
2018-07-26 22:56:26 +08:00
|
|
|
|
|
|
|
if (privateChat) {
|
2018-07-27 21:44:21 +08:00
|
|
|
const { chatId: privateChatId } = privateChat;
|
|
|
|
|
2019-08-22 23:19:54 +08:00
|
|
|
destinationChatId = privateChatId;
|
2018-07-26 22:56:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-20 00:22:43 +08:00
|
|
|
const userAvatarColor = Users.findOne({ userId: senderUserId }, { fields: { color: 1 } });
|
|
|
|
|
2018-07-26 22:56:26 +08:00
|
|
|
const payload = {
|
2020-08-20 00:22:43 +08:00
|
|
|
color: userAvatarColor?.color || '0',
|
2019-08-30 00:26:07 +08:00
|
|
|
correlationId: `${senderUserId}-${Date.now()}`,
|
2018-07-26 22:56:26 +08:00
|
|
|
sender: {
|
2019-08-30 00:26:07 +08:00
|
|
|
id: senderUserId,
|
|
|
|
name: senderName,
|
2018-07-26 22:56:26 +08:00
|
|
|
},
|
2017-06-03 03:25:02 +08:00
|
|
|
message,
|
2016-06-03 02:40:27 +08:00
|
|
|
};
|
2016-06-02 00:33:19 +08:00
|
|
|
|
2017-06-03 03:25:02 +08:00
|
|
|
const currentClosedChats = Storage.getItem(CLOSED_CHAT_LIST_KEY);
|
2017-03-02 05:27:05 +08:00
|
|
|
|
2017-03-03 05:42:39 +08:00
|
|
|
// Remove the chat that user send messages from the session.
|
2019-08-09 02:53:08 +08:00
|
|
|
if (_.indexOf(currentClosedChats, receiverId.id) > -1) {
|
|
|
|
Storage.setItem(CLOSED_CHAT_LIST_KEY, _.without(currentClosedChats, receiverId.id));
|
2017-03-01 06:40:16 +08:00
|
|
|
}
|
|
|
|
|
2019-08-22 23:19:54 +08:00
|
|
|
return makeCall('sendGroupChatMsg', destinationChatId, payload);
|
2016-06-02 00:33:19 +08:00
|
|
|
};
|
|
|
|
|
2016-07-01 01:10:36 +08:00
|
|
|
const getScrollPosition = (receiverID) => {
|
2019-08-22 20:05:06 +08:00
|
|
|
const scroll = ScrollCollection.findOne({ receiver: receiverID },
|
|
|
|
{ fields: { position: 1 } }) || { position: null };
|
2016-07-01 01:10:36 +08:00
|
|
|
return scroll.position;
|
|
|
|
};
|
|
|
|
|
2019-01-14 21:23:35 +08:00
|
|
|
const updateScrollPosition = position => ScrollCollection.upsert(
|
|
|
|
{ receiver: Session.get('idChatOpen') },
|
|
|
|
{ $set: { position } },
|
|
|
|
);
|
2016-07-05 02:53:47 +08:00
|
|
|
|
2018-10-16 04:03:17 +08:00
|
|
|
const updateUnreadMessage = (timestamp) => {
|
2020-03-06 07:14:22 +08:00
|
|
|
const chatID = Session.get('idChatOpen');
|
2018-10-16 04:03:17 +08:00
|
|
|
const isPublic = chatID === PUBLIC_CHAT_ID;
|
|
|
|
const chatType = isPublic ? PUBLIC_GROUP_CHAT_ID : chatID;
|
2017-07-12 21:18:26 +08:00
|
|
|
return UnreadMessages.update(chatType, timestamp);
|
2016-07-01 01:10:36 +08:00
|
|
|
};
|
|
|
|
|
2017-08-03 01:05:20 +08:00
|
|
|
const clearPublicChatHistory = () => (makeCall('clearPublicChatHistory'));
|
|
|
|
|
2021-02-12 22:07:53 +08:00
|
|
|
const closePrivateChat = (chatId) => {
|
2017-06-03 03:25:02 +08:00
|
|
|
const currentClosedChats = Storage.getItem(CLOSED_CHAT_LIST_KEY) || [];
|
2017-03-03 04:33:49 +08:00
|
|
|
|
2021-02-12 22:07:53 +08:00
|
|
|
if (_.indexOf(currentClosedChats, chatId) < 0) {
|
|
|
|
currentClosedChats.push(chatId);
|
2017-03-03 04:33:49 +08:00
|
|
|
|
2017-03-17 23:27:37 +08:00
|
|
|
Storage.setItem(CLOSED_CHAT_LIST_KEY, currentClosedChats);
|
2017-03-07 01:25:35 +08:00
|
|
|
}
|
2017-03-03 04:33:49 +08:00
|
|
|
};
|
|
|
|
|
2017-12-19 01:38:46 +08:00
|
|
|
// if this private chat has been added to the list of closed ones, remove it
|
2018-10-16 04:03:17 +08:00
|
|
|
const removeFromClosedChatsSession = () => {
|
|
|
|
const chatID = Session.get('idChatOpen');
|
2017-12-19 01:38:46 +08:00
|
|
|
const currentClosedChats = Storage.getItem(CLOSED_CHAT_LIST_KEY);
|
2017-12-19 01:41:26 +08:00
|
|
|
if (_.indexOf(currentClosedChats, chatID) > -1) {
|
2017-12-19 01:38:46 +08:00
|
|
|
Storage.setItem(CLOSED_CHAT_LIST_KEY, _.without(currentClosedChats, chatID));
|
|
|
|
}
|
2017-12-19 01:41:26 +08:00
|
|
|
};
|
2017-12-19 01:38:46 +08:00
|
|
|
|
2017-07-21 22:16:45 +08:00
|
|
|
// We decode to prevent HTML5 escaped characters.
|
|
|
|
const htmlDecode = (input) => {
|
|
|
|
const e = document.createElement('div');
|
|
|
|
e.innerHTML = input;
|
2019-03-06 02:21:33 +08:00
|
|
|
const messages = Array.from(e.childNodes);
|
2019-02-26 05:38:57 +08:00
|
|
|
const message = messages.map(chatMessage => chatMessage.textContent);
|
|
|
|
return message.join('');
|
2017-07-21 22:16:45 +08:00
|
|
|
};
|
|
|
|
|
2017-08-08 22:52:26 +08:00
|
|
|
// Export the chat as [Hour:Min] user: message
|
2021-03-25 04:20:33 +08:00
|
|
|
const exportChat = (timeWindowList, users) => {
|
|
|
|
// const messageList = timeWindowList.reduce( (acc, timeWindow) => [...acc, ...timeWindow.content], []);
|
|
|
|
// messageList.sort((a, b) => a.time - b.time);
|
|
|
|
|
|
|
|
const messageList = timeWindowList.reduce((acc, timeWindow) => {
|
|
|
|
|
|
|
|
const msgs = timeWindow.content.map(message => {
|
|
|
|
const date = new Date(message.time);
|
|
|
|
const hour = date.getHours().toString().padStart(2, 0);
|
|
|
|
const min = date.getMinutes().toString().padStart(2, 0);
|
|
|
|
const hourMin = `[${hour}:${min}]`;
|
|
|
|
console.log('message', message);
|
|
|
|
const userName = message.id.endsWith('welcome-msg')
|
|
|
|
? ''
|
|
|
|
: `${users[timeWindow.sender].name} :`;
|
|
|
|
return `${hourMin} ${userName} ${htmlDecode(message.text)}`;
|
|
|
|
});
|
|
|
|
|
|
|
|
return [...acc, ...msgs];
|
|
|
|
}, [])
|
|
|
|
|
|
|
|
return messageList.join('\n');
|
2021-02-13 03:13:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-07-20 20:45:27 +08:00
|
|
|
|
2019-01-14 21:23:35 +08:00
|
|
|
const getAllMessages = (chatID) => {
|
2021-02-04 04:49:58 +08:00
|
|
|
return [];
|
2019-01-14 21:23:35 +08:00
|
|
|
const filter = {
|
2020-08-20 00:22:43 +08:00
|
|
|
'sender.id': { $ne: Auth.userID },
|
2019-01-14 21:23:35 +08:00
|
|
|
};
|
|
|
|
if (chatID === PUBLIC_GROUP_CHAT_ID) {
|
|
|
|
filter.chatId = { $eq: chatID };
|
|
|
|
} else {
|
2019-08-23 03:14:36 +08:00
|
|
|
const privateChat = GroupChat.findOne({ users: { $all: [chatID, Auth.userID] } });
|
2019-01-14 21:23:35 +08:00
|
|
|
|
|
|
|
filter.chatId = { $ne: PUBLIC_GROUP_CHAT_ID };
|
|
|
|
|
|
|
|
if (privateChat) {
|
|
|
|
filter.chatId = privateChat.chatId;
|
|
|
|
}
|
2018-05-30 00:43:11 +08:00
|
|
|
}
|
2019-01-14 21:23:35 +08:00
|
|
|
const messages = GroupChatMsg.find(filter).fetch();
|
|
|
|
return messages;
|
2018-05-30 00:43:11 +08:00
|
|
|
};
|
|
|
|
|
2019-01-14 21:23:35 +08:00
|
|
|
const maxTimestampReducer = (max, el) => ((el.timestamp > max) ? el.timestamp : max);
|
|
|
|
|
|
|
|
const maxNumberReducer = (max, el) => ((el > max) ? el : max);
|
|
|
|
|
|
|
|
const getLastMessageTimestampFromChatList = activeChats => activeChats
|
2019-08-23 01:23:18 +08:00
|
|
|
.map(chat => ((chat.userId === 'public') ? 'MAIN-PUBLIC-GROUP-CHAT' : chat.userId))
|
2019-01-14 21:23:35 +08:00
|
|
|
.map(chatId => getAllMessages(chatId).reduce(maxTimestampReducer, 0))
|
|
|
|
.reduce(maxNumberReducer, 0);
|
2018-05-30 00:43:11 +08:00
|
|
|
|
2021-03-29 10:12:58 +08:00
|
|
|
const removePackagedClassAttribute = (classnames, attribute) => {
|
|
|
|
classnames.map(c => {
|
|
|
|
const elements = document.getElementsByClassName(c);
|
|
|
|
if (elements) {
|
2021-04-09 03:01:49 +08:00
|
|
|
for (const [,v] of Object.entries(elements)) {
|
2021-03-29 10:12:58 +08:00
|
|
|
v.removeAttribute(attribute);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-06-02 00:33:19 +08:00
|
|
|
export default {
|
2021-01-20 01:06:32 +08:00
|
|
|
setUserSentMessage,
|
2020-01-22 05:08:48 +08:00
|
|
|
mapGroupMessage,
|
2018-07-26 22:56:26 +08:00
|
|
|
reduceAndMapGroupMessages,
|
2020-01-22 05:08:48 +08:00
|
|
|
reduceAndDontMapGroupMessages,
|
2021-01-20 01:06:32 +08:00
|
|
|
getChatMessages,
|
2018-07-26 22:56:26 +08:00
|
|
|
getPublicGroupMessages,
|
|
|
|
getPrivateGroupMessages,
|
2016-06-07 22:19:19 +08:00
|
|
|
getUser,
|
2020-08-20 00:22:43 +08:00
|
|
|
getPrivateChatByUsers,
|
2019-08-22 20:05:06 +08:00
|
|
|
getWelcomeProp,
|
2016-07-01 01:10:36 +08:00
|
|
|
getScrollPosition,
|
2016-07-05 02:53:47 +08:00
|
|
|
hasUnreadMessages,
|
2016-07-12 03:42:54 +08:00
|
|
|
lastReadMessageTime,
|
2016-06-14 01:00:38 +08:00
|
|
|
isChatLocked,
|
2016-07-05 02:53:47 +08:00
|
|
|
updateScrollPosition,
|
|
|
|
updateUnreadMessage,
|
2018-07-26 22:56:26 +08:00
|
|
|
sendGroupMessage,
|
2017-03-24 03:02:40 +08:00
|
|
|
closePrivateChat,
|
2017-12-19 01:38:46 +08:00
|
|
|
removeFromClosedChatsSession,
|
2017-07-20 20:45:27 +08:00
|
|
|
exportChat,
|
2017-08-03 01:05:20 +08:00
|
|
|
clearPublicChatHistory,
|
2019-01-14 21:23:35 +08:00
|
|
|
maxTimestampReducer,
|
|
|
|
getLastMessageTimestampFromChatList,
|
2019-02-05 20:24:45 +08:00
|
|
|
UnsentMessagesCollection,
|
2021-03-29 10:12:58 +08:00
|
|
|
removePackagedClassAttribute,
|
2016-06-02 00:33:19 +08:00
|
|
|
};
|