2016-05-20 02:22:56 +08:00
|
|
|
import Users from '/imports/api/users';
|
2016-06-07 00:45:30 +08:00
|
|
|
import ath from '/imports/ui/services/auth';
|
|
|
|
|
|
|
|
const ROLE_MODERATOR = 'MODERATOR';
|
|
|
|
const EMOJI_STATUSES = ['raiseHand', 'happy', 'smile', 'neutral', 'sad', 'confused', 'away'];
|
|
|
|
|
|
|
|
/* TODO: Same map is done in the chat/service we should share this someway */
|
|
|
|
|
|
|
|
const mapUser = (user) => ({
|
|
|
|
id: user.userid,
|
|
|
|
name: user.name,
|
2016-06-08 00:46:24 +08:00
|
|
|
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,
|
|
|
|
isCurrent: user.userid === ath.getUser(),
|
|
|
|
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-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) => {
|
2016-06-08 00:46:24 +08:00
|
|
|
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);
|
2016-06-08 00:46:24 +08:00
|
|
|
} else if (EMOJI_STATUSES.indexOf(a.emoji.status) > -1) {
|
2016-06-07 00:45:30 +08:00
|
|
|
return -1;
|
2016-06-08 00:46:24 +08:00
|
|
|
} 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) => {
|
2016-06-08 00:46:24 +08:00
|
|
|
if (!a.isPhoneUser && !b.isPhoneUser) {
|
2016-06-07 21:58:51 +08:00
|
|
|
return sortUsersByName(a, b);
|
2016-06-08 00:46:24 +08:00
|
|
|
} else if (!a.isPhoneUser) {
|
2016-06-07 00:45:30 +08:00
|
|
|
return -1;
|
2016-06-08 00:46:24 +08:00
|
|
|
} 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-07 00:45:30 +08:00
|
|
|
const getUsers = () => {
|
|
|
|
let users = Users
|
|
|
|
.find({}, {
|
|
|
|
'user.set_emoji_time': 1,
|
|
|
|
'user.role': 1,
|
|
|
|
'user.phone_user': 1,
|
|
|
|
'user._sort_name': 1,
|
|
|
|
'user.userid': 1,
|
|
|
|
})
|
|
|
|
.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-01 04:25:42 +08:00
|
|
|
export default {
|
2016-06-07 00:45:30 +08:00
|
|
|
getUsers,
|
2016-06-01 04:25:42 +08:00
|
|
|
};
|