bigbluebutton-Github/bigbluebutton-html5/imports/ui/components/user-list/service.js

218 lines
4.6 KiB
JavaScript
Raw Normal View History

2016-05-20 02:22:56 +08:00
import Users from '/imports/api/users';
2016-06-28 21:10:20 +08:00
import auth from '/imports/ui/services/auth';
import { callServer } from '/imports/ui/services/api';
2016-06-07 00:45:30 +08:00
const ROLE_MODERATOR = 'MODERATOR';
const EMOJI_STATUSES = ['raiseHand', 'happy', 'smile', 'neutral', 'sad', 'confused', 'away'];
2016-06-28 21:10:20 +08:00
const PRIVATE_CHAT_TYPE = 'PRIVATE_CHAT';
2016-06-07 00:45:30 +08:00
/* TODO: Same map is done in the chat/service we should share this someway */
2016-06-28 21:10:20 +08:00
const mapUser = user => ({
2016-06-07 00:45:30 +08:00
id: user.userid,
name: user.name,
emoji: {
status: user.emoji_status,
changedAt: user.set_emoji_time,
},
2016-06-07 00:45:30 +08:00
isPresenter: user.presenter,
isModerator: user.role === ROLE_MODERATOR,
2016-06-28 21:10:20 +08:00
isCurrent: user.userid === auth.getUser(),
2016-06-07 00:45:30 +08:00
isVoiceUser: user.voiceUser.joined,
isMuted: user.voiceUser.muted,
isListenOnly: user.listenOnly,
isSharingWebcam: user.webcam_stream.length,
2016-06-07 21:58:51 +08:00
isPhoneUser: user.phone_user,
2016-06-07 00:45:30 +08:00
});
2016-06-28 21:10:20 +08:00
const mapOpenChats = chat => {
let currentUserId = auth.getUser();
return chat.message.from_userid !== auth.getUser()
? chat.message.from_userid
: chat.message.to_userid;
};
2016-06-07 21:58:51 +08:00
const sortUsersByName = (a, b) => {
if (a.name.toLowerCase() < b.name.toLowerCase()) {
2016-06-07 00:45:30 +08:00
return -1;
2016-06-07 21:58:51 +08:00
} else if (a.name.toLowerCase() > b.name.toLowerCase()) {
2016-06-07 00:45:30 +08:00
return 1;
2016-06-07 21:58:51 +08:00
} else if (a.id.toLowerCase() > b.id.toLowerCase()) {
2016-06-07 00:45:30 +08:00
return -1;
2016-06-07 21:58:51 +08:00
} else if (a.id.toLowerCase() < b.id.toLowerCase()) {
2016-06-07 00:45:30 +08:00
return 1;
2016-06-07 21:58:51 +08:00
}
return 0;
};
const sortUsersByEmoji = (a, b) => {
if ((EMOJI_STATUSES.indexOf(a.emoji.status) > -1)
&& (EMOJI_STATUSES.indexOf(b.emoji.status) > -1)) {
if (a.emoji.changedAt < b.emoji.changedAt) {
return -1;
} else if (a.emoji.changedAt > b.emoji.changedAt) {
return 1;
}
2016-06-07 21:58:51 +08:00
return sortUsersByName(a, b);
} else if (EMOJI_STATUSES.indexOf(a.emoji.status) > -1) {
2016-06-07 00:45:30 +08:00
return -1;
} else if (EMOJI_STATUSES.indexOf(b.emoji.status) > -1) {
2016-06-07 00:45:30 +08:00
return 1;
}
2016-06-07 21:58:51 +08:00
return 0;
};
const sortUsersByModerator = (a, b) => {
if (a.isModerator && b.isModerator) {
return sortUsersByName(a, b);
} else if (a.isModerator) {
2016-06-07 00:45:30 +08:00
return -1;
2016-06-07 21:58:51 +08:00
} else if (b.isModerator) {
2016-06-07 00:45:30 +08:00
return 1;
2016-06-07 21:58:51 +08:00
}
return 0;
};
const sortUsersByPhoneUser = (a, b) => {
if (!a.isPhoneUser && !b.isPhoneUser) {
2016-06-07 21:58:51 +08:00
return sortUsersByName(a, b);
} else if (!a.isPhoneUser) {
2016-06-07 00:45:30 +08:00
return -1;
} else if (!b.isPhoneUser) {
2016-06-07 00:45:30 +08:00
return 1;
}
return 0;
};
2016-06-07 21:58:51 +08:00
const sortUsers = (a, b) => {
let sort = sortUsersByEmoji(a, b);
if (sort === 0) {
sort = sortUsersByModerator(a, b);
}
if (sort === 0) {
sort = sortUsersByPhoneUser(a, b);
}
if (sort === 0) {
sort = sortUsersByName(a, b);
}
return sort;
};
2016-06-28 21:10:20 +08:00
const sortChatsByName = (a, b) => {
if (a.name.toLowerCase() < b.name.toLowerCase()) {
return -1;
} else if (a.name.toLowerCase() > b.name.toLowerCase()) {
return 1;
} else if (a.id.toLowerCase() > b.id.toLowerCase()) {
return -1;
} else if (a.id.toLowerCase() < b.id.toLowerCase()) {
return 1;
}
return 0;
};
const sortChatsByIcon = (a, b) => {
if (a.icon && b.icon) {
return sortChatsByName(a, b);
} else if (a.icon) {
return -1;
} else if (b.icon) {
return 1;
}
return 0;
};
const sortChats = (a, b) => {
let sort = sortChatsByIcon(a, b);
if (sort === 0) {
sort = sortChatsByName(a, b);
}
return sort;
};
const userFindSorting = {
'user.set_emoji_time': 1,
'user.role': 1,
'user.phone_user': 1,
'user._sort_name': 1,
'user.userid': 1,
};
2016-06-07 00:45:30 +08:00
const getUsers = () => {
let users = Users
2016-06-28 21:10:20 +08:00
.find({}, userFindSorting)
2016-06-07 00:45:30 +08:00
.fetch();
return users
.map(u => u.user)
.map(mapUser)
.sort(sortUsers);
2016-05-31 19:29:38 +08:00
};
2016-05-20 02:22:56 +08:00
2016-06-28 21:10:20 +08:00
const getOpenChats = openChat => {
let openChats = Chat
.find({ 'message.chat_type': PRIVATE_CHAT_TYPE })
.fetch()
.map(mapOpenChats);
let currentUserId = auth.getUser();
if (openChat && openChat.chatID) {
openChats.push(openChat.chatID);
}
openChats = _.uniq(openChats);
openChats = Users
.find({ 'user.userid': { $in: openChats } })
.map(u => u.user)
.map(mapUser);
openChats.push({
id: 'public',
name: 'Public Chat',
icon: 'group-chat',
});
return openChats
.sort(sortChats);
};
getCurrentUser = () => {
let currentUserId = auth.getUser();
window.Users = Users;
window.currentUserId = currentUserId;
return mapUser(
Users
.findOne({ 'user.userid': currentUserId })
.user
);
};
const userActions = {
setPresenter: (user) => callServer('setUserPresenter', user.userid, user.name),
kickUser: (user) => callServer('kickUser', user.userid),
openChat: (context, user) => context.props.router.push(`/users/chat/${user.id}`),
};
export default {
2016-06-07 00:45:30 +08:00
getUsers,
2016-06-28 21:10:20 +08:00
getOpenChats,
getCurrentUser,
userActions,
};