bigbluebutton-Github/bigbluebutton-html5/imports/api/polls/server/methods/publishVote.js

65 lines
1.8 KiB
JavaScript
Raw Normal View History

import RedisPubSub from '/imports/startup/server/redis';
import { check } from 'meteor/check';
import Polls from '/imports/api/polls';
import Logger from '/imports/startup/server/logger';
import { extractCredentials } from '/imports/api/common/server/helpers';
2016-10-21 21:21:09 +08:00
2021-02-05 00:54:50 +08:00
export default function publishVote(pollId, pollAnswerIds) {
const REDIS_CONFIG = Meteor.settings.private.redis;
2017-10-12 08:33:57 +08:00
const CHANNEL = REDIS_CONFIG.channels.toAkkaApps;
const EVENT_NAME = 'RespondToPollReqMsg';
const { meetingId, requesterUserId } = extractCredentials(this.userId);
2021-02-05 00:54:50 +08:00
check(pollAnswerIds, Array);
check(meetingId, String);
check(requesterUserId, String);
check(pollId, String);
2020-11-05 22:19:36 +08:00
const allowedToVote = Polls.findOne({ id: pollId, users: { $in: [requesterUserId] } }, {
fields: {
users: 1,
},
});
2020-11-05 22:19:36 +08:00
if (!allowedToVote) {
Logger.info(`Poll User={${requesterUserId}} has already voted in PollId={${pollId}}`);
return null;
2020-11-05 22:19:36 +08:00
}
const selector = {
users: requesterUserId,
2017-06-03 03:25:02 +08:00
meetingId,
2021-02-05 00:54:50 +08:00
'answers.id': { $all: pollAnswerIds },
};
2017-06-03 03:25:02 +08:00
const payload = {
2017-10-12 08:33:57 +08:00
requesterId: requesterUserId,
pollId,
2017-10-12 08:33:57 +08:00
questionId: 0,
2021-02-05 00:54:50 +08:00
answerIds: pollAnswerIds,
};
/*
We keep an array of people who were in the meeting at the time the poll
was started. The poll is published to them only.
Once they vote - their ID is removed and they cannot see the poll anymore
*/
const modifier = {
$pull: {
2016-10-21 21:21:09 +08:00
users: requesterUserId,
},
};
try {
const numberAffected = Polls.update(selector, modifier);
if (numberAffected) {
Logger.info(`Removed responded user=${requesterUserId} from poll (meetingId: ${meetingId}, pollId: ${pollId}!)`);
RedisPubSub.publishUserMessage(CHANNEL, EVENT_NAME, meetingId, requesterUserId, payload);
}
} catch (err) {
Logger.error(`Removing responded user from Polls collection: ${err}`);
}
2016-10-21 21:21:09 +08:00
}