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

61 lines
1.7 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 function publishVote(id, pollAnswerId) { // TODO discuss location
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';
2016-10-21 21:21:09 +08:00
const { meetingId, requesterUserId } = extractCredentials(this.userId);
/*
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 currentPoll = Polls.findOne({
users: requesterUserId,
2017-06-03 03:25:02 +08:00
meetingId,
2017-10-12 08:33:57 +08:00
'answers.id': pollAnswerId,
id,
});
check(pollAnswerId, Number);
2019-07-07 06:48:33 +08:00
check(currentPoll, Object);
check(currentPoll.meetingId, String);
2017-06-03 03:25:02 +08:00
const payload = {
2017-10-12 08:33:57 +08:00
requesterId: requesterUserId,
pollId: currentPoll.id,
questionId: 0,
answerId: pollAnswerId,
};
const selector = {
users: requesterUserId,
2017-06-03 03:25:02 +08:00
meetingId,
2017-10-12 08:33:57 +08:00
'answers.id': pollAnswerId,
};
const modifier = {
$pull: {
2016-10-21 21:21:09 +08:00
users: requesterUserId,
},
};
2017-06-06 03:12:06 +08:00
const cb = (err) => {
if (err) {
return Logger.error(`Updating Polls collection: ${err}`);
2016-10-21 21:21:09 +08:00
}
2019-03-09 05:12:00 +08:00
return Logger.info(`Updating Polls collection (meetingId: ${meetingId}, `
+ `pollId: ${currentPoll.id}!)`);
};
Polls.update(selector, modifier, cb);
2017-10-12 08:33:57 +08:00
return RedisPubSub.publishUserMessage(CHANNEL, EVENT_NAME, meetingId, requesterUserId, payload);
2016-10-21 21:21:09 +08:00
}