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

72 lines
2.1 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
export default async function publishVote(pollId, pollAnswerIds) {
2021-05-05 22:12:36 +08:00
try {
const REDIS_CONFIG = Meteor.settings.private.redis;
const CHANNEL = REDIS_CONFIG.channels.toAkkaApps;
const EVENT_NAME = 'RespondToPollReqMsg';
2021-05-05 22:12:36 +08:00
const { meetingId, requesterUserId } = extractCredentials(this.userId);
2021-05-05 22:12:36 +08:00
check(meetingId, String);
check(requesterUserId, String);
check(pollAnswerIds, Array);
2021-05-05 22:12:36 +08:00
check(pollId, String);
const allowedToVote = await Polls.findOneAsync({
id: pollId,
users: { $in: [requesterUserId] },
meetingId,
}, {
2021-05-05 22:12:36 +08:00
fields: {
users: 1,
},
});
2021-05-05 22:12:36 +08:00
if (!allowedToVote) {
Logger.info(`Poll User={${requesterUserId}} has already voted in PollId={${pollId}}`);
return null;
}
2021-05-05 22:12:36 +08:00
const selector = {
2016-10-21 21:21:09 +08:00
users: requesterUserId,
2021-05-05 22:12:36 +08:00
meetingId,
'answers.id': { $all: pollAnswerIds },
2021-05-05 22:12:36 +08:00
};
2021-05-05 22:12:36 +08:00
const payload = {
requesterId: requesterUserId,
pollId,
questionId: 0,
answerIds: pollAnswerIds,
2021-05-05 22:12:36 +08:00
};
2021-05-05 22:12:36 +08:00
/*
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: {
users: requesterUserId,
},
};
const numberAffected = await Polls.updateAsync(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) {
2021-05-05 22:12:36 +08:00
Logger.error(`Exception while invoking method publishVote ${err.stack}`);
}
//In this case we return true because
//lint asks for async functions to return some value.
return true;
2016-10-21 21:21:09 +08:00
}