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

62 lines
1.5 KiB
JavaScript
Raw Normal View History

2017-06-29 04:37:45 +08:00
import RedisPubSub from '/imports/startup/server/redis2x';
import { check } from 'meteor/check';
import Polls from '/imports/api/2.0/polls';
import Logger from '/imports/startup/server/logger';
2017-07-25 20:26:45 +08:00
export default function publishVote(credentials, id, pollAnswerId) { // TODO discuss location
2017-06-29 04:37:45 +08:00
const REDIS_CONFIG = Meteor.settings.redis;
2017-06-30 22:26:19 +08:00
const CHANNEL = REDIS_CONFIG.channels.toAkkaApps;
const EVENT_NAME = 'RespondToPollReqMsg';
2017-06-29 04:37:45 +08:00
const { meetingId, requesterUserId } = credentials;
const currentPoll = Polls.findOne({
users: requesterUserId,
meetingId,
2017-07-25 20:26:45 +08:00
'answers.id': pollAnswerId,
id,
2017-06-29 04:37:45 +08:00
});
check(meetingId, String);
check(requesterUserId, String);
check(pollAnswerId, Number);
check(currentPoll.meetingId, String);
const payload = {
2017-06-30 22:26:19 +08:00
requesterId: requesterUserId,
2017-07-25 20:26:45 +08:00
pollId: currentPoll.id,
2017-06-30 22:26:19 +08:00
questionId: 0,
answerId: pollAnswerId,
};
const header = {
meetingId,
name: EVENT_NAME,
userId: requesterUserId,
2017-06-29 04:37:45 +08:00
};
const selector = {
users: requesterUserId,
meetingId,
2017-07-25 20:26:45 +08:00
'answers.id': pollAnswerId,
2017-06-29 04:37:45 +08:00
};
const modifier = {
$pull: {
users: requesterUserId,
},
};
const cb = (err) => {
if (err) {
2017-06-30 22:26:19 +08:00
return Logger.error(`Updating Polls2x collection: ${err}`);
2017-06-29 04:37:45 +08:00
}
2017-06-30 22:26:19 +08:00
return Logger.info(`Updating Polls2x collection (meetingId: ${meetingId},
2017-07-25 20:26:45 +08:00
pollId: ${currentPoll.id}!)`);
2017-06-29 04:37:45 +08:00
};
Polls.update(selector, modifier, cb);
2017-06-30 22:26:19 +08:00
return RedisPubSub.publish(CHANNEL, EVENT_NAME, meetingId, payload, header);
2017-06-29 04:37:45 +08:00
}