2016-06-02 00:33:19 +08:00
|
|
|
import Chats from '/imports/api/chat';
|
|
|
|
import Users from '/imports/api/users';
|
2016-06-02 21:46:35 +08:00
|
|
|
import Auth from '/imports/ui/services/auth';
|
2016-06-02 21:00:57 +08:00
|
|
|
|
2016-06-03 02:40:27 +08:00
|
|
|
import { callServer } from '/imports/ui/services/api';
|
2016-06-02 21:00:57 +08:00
|
|
|
|
|
|
|
const GROUPING_MESSAGES_WINDOW = 60000;
|
2016-06-02 00:33:19 +08:00
|
|
|
|
|
|
|
const SYSTEM_CHAT_TYPE = 'SYSTEM_MESSAGE';
|
|
|
|
const PUBLIC_CHAT_TYPE = 'PUBLIC_CHAT';
|
|
|
|
const PRIVATE_CHAT_TYPE = 'PRIVATE_CHAT';
|
|
|
|
|
2016-06-03 02:40:27 +08:00
|
|
|
const PUBLIC_CHAT_ID = 'public';
|
2016-06-02 21:00:57 +08:00
|
|
|
|
|
|
|
/* TODO: Same map is done in the user-list/service we should share this someway */
|
|
|
|
|
|
|
|
const mapUser = (user) => ({
|
|
|
|
id: user.userid,
|
|
|
|
name: user.name,
|
|
|
|
isPresenter: user.presenter,
|
|
|
|
isModerator: user.role === 'MODERATOR',
|
2016-06-03 02:40:27 +08:00
|
|
|
isCurrent: user.userid === Auth.getUser(),
|
2016-06-02 21:00:57 +08:00
|
|
|
isVoiceUser: user.voiceUser.joined,
|
|
|
|
isMuted: user.voiceUser.muted,
|
|
|
|
isListenOnly: user.listenOnly,
|
|
|
|
isSharingWebcam: user.webcam_stream.length,
|
|
|
|
});
|
|
|
|
|
|
|
|
const mapMessage = (message) => {
|
|
|
|
let mappedMessage = {
|
2016-06-03 02:40:27 +08:00
|
|
|
content: [message.message],
|
2016-06-02 21:00:57 +08:00
|
|
|
time: +(message.from_time), //+ message.from_tz_offset,
|
|
|
|
sender: null,
|
|
|
|
};
|
|
|
|
|
2016-06-03 02:40:27 +08:00
|
|
|
if (message.chat_type !== SYSTEM_CHAT_TYPE) {
|
|
|
|
mappedMessage.sender = getUser(message.from_userid);
|
2016-06-02 00:33:19 +08:00
|
|
|
}
|
2016-06-02 21:00:57 +08:00
|
|
|
|
|
|
|
return mappedMessage;
|
|
|
|
};
|
|
|
|
|
|
|
|
const reduceMessages = (previous, current, index, array) => {
|
|
|
|
let lastMessage = previous[previous.length - 1];
|
|
|
|
|
|
|
|
if (!lastMessage || !lastMessage.sender || !current.sender) { // Skip system messages
|
|
|
|
return previous.concat(current);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the last message is from the same user and time discrepancy
|
|
|
|
// between the two messages exceeds window and then group current message
|
|
|
|
// with the last one
|
|
|
|
|
|
|
|
if (lastMessage.sender.id === current.sender.id
|
|
|
|
&& (current.time - lastMessage.time) <= GROUPING_MESSAGES_WINDOW) {
|
|
|
|
lastMessage.content = lastMessage.content.concat(current.content);
|
|
|
|
return previous;
|
|
|
|
} else {
|
|
|
|
return previous.concat(current);
|
|
|
|
}
|
|
|
|
};
|
2016-06-02 00:33:19 +08:00
|
|
|
|
2016-06-03 02:40:27 +08:00
|
|
|
const getUser = (userID) => {
|
|
|
|
const user = Users.findOne({ userId: userID });
|
|
|
|
if (user) {
|
|
|
|
return mapUser(user.user);
|
|
|
|
} else {
|
|
|
|
throw `User ${userID} not found`;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-06-02 00:33:19 +08:00
|
|
|
const getPublicMessages = () => {
|
2016-06-02 21:00:57 +08:00
|
|
|
let publicMessages = Chats.find({
|
|
|
|
'message.chat_type': { $in: [PUBLIC_CHAT_TYPE, SYSTEM_CHAT_TYPE] },
|
|
|
|
}, {
|
|
|
|
sort: ['message.from_time'],
|
|
|
|
})
|
|
|
|
.fetch();
|
|
|
|
|
|
|
|
let systemMessage = Chats.findOne({ 'message.chat_type': SYSTEM_CHAT_TYPE });
|
2016-06-02 00:33:19 +08:00
|
|
|
|
2016-06-02 21:00:57 +08:00
|
|
|
return publicMessages
|
|
|
|
.map(m => m.message)
|
|
|
|
.map(mapMessage)
|
|
|
|
.reduce(reduceMessages, []);
|
2016-06-02 00:33:19 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
const getPrivateMessages = (userID) => {
|
|
|
|
let messages = Chats.find({
|
2016-06-02 21:00:57 +08:00
|
|
|
'message.chat_type': PRIVATE_CHAT_TYPE,
|
|
|
|
$or: [
|
|
|
|
{ 'message.to_userid': userID },
|
|
|
|
{ 'message.from_userid': userID },
|
|
|
|
],
|
|
|
|
}, {
|
|
|
|
sort: ['message.from_time'],
|
|
|
|
}).fetch();
|
2016-06-02 00:33:19 +08:00
|
|
|
|
2016-06-02 21:00:57 +08:00
|
|
|
return messages
|
|
|
|
.map(m => m.message)
|
|
|
|
.map(mapMessage)
|
|
|
|
.reduce(reduceMessages, []);
|
2016-06-02 00:33:19 +08:00
|
|
|
};
|
|
|
|
|
2016-06-03 02:40:27 +08:00
|
|
|
const getChatTitle = (userID) => {
|
|
|
|
const user = getUser(userID);
|
|
|
|
return user.name;
|
|
|
|
};
|
|
|
|
|
|
|
|
const sendMessage = (receiverID, message) => {
|
|
|
|
const isPublic = receiverID === PUBLIC_CHAT_ID;
|
|
|
|
|
|
|
|
const sender = getUser(Auth.getUser());
|
|
|
|
const receiver = !isPublic ? getUser(receiverID) : {
|
|
|
|
id: 'public_chat_userid',
|
|
|
|
name: 'public_chat_username',
|
|
|
|
};
|
|
|
|
|
|
|
|
/* FIX: Why we need all this payload to send a message?
|
|
|
|
* The server only really needs the message, from_userid, to_userid and from_lang
|
|
|
|
*/
|
|
|
|
|
|
|
|
let messagePayload = {
|
|
|
|
message: message,
|
|
|
|
chat_type: isPublic ? PUBLIC_CHAT_TYPE : PRIVATE_CHAT_TYPE,
|
|
|
|
from_userid: sender.id,
|
|
|
|
from_username: sender.name,
|
|
|
|
from_tz_offset: (new Date()).getTimezoneOffset(),
|
|
|
|
to_username: receiver.name,
|
|
|
|
to_userid: receiver.id,
|
|
|
|
from_lang: window.navigator.userLanguage || window.navigator.language,
|
|
|
|
from_time: Date.now(),
|
|
|
|
from_color: 0,
|
|
|
|
};
|
2016-06-02 00:33:19 +08:00
|
|
|
|
2016-06-03 02:40:27 +08:00
|
|
|
return callServer('sendChatMessagetoServer', messagePayload);
|
2016-06-02 00:33:19 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
export default {
|
|
|
|
getPublicMessages,
|
|
|
|
getPrivateMessages,
|
2016-06-03 02:40:27 +08:00
|
|
|
getChatTitle,
|
2016-06-02 00:33:19 +08:00
|
|
|
sendMessage,
|
|
|
|
};
|