const { Types } = require('mongoose'); const Bluebird = require('bluebird'); const UserModel = require('../services/users/user.model.js'); const launchUserSession = require('./launchUserSession.js'); const attendConference = require('./attendConference.js'); const { clickElementBySelector } = require('./utils.js'); const { EDUFPMI_URL } = process.env; const handleJobAsUser = async (job, user) => { console.log(`Running job as ${user.username}`); const { conferenceId } = job.attrs.data; const conferenceUrl = `${EDUFPMI_URL}/mod/bigbluebuttonbn/view.php?id=${conferenceId}`; const browser = await launchUserSession(user, false); const page = await browser.newPage(); await page.goto(conferenceUrl); // Launch the conference in a new tab const conferencePagePromise = new Promise(resolve => browser.on( 'targetcreated', target => resolve(target.page()) )); await clickElementBySelector(page, 'input#join_button_input'); const conferencePage = await conferencePagePromise; await attendConference(conferencePage, () => console.log(`Joined the conference at ${conferenceUrl}`)); await browser.close(); }; const handleJob = async job => { console.log('Running attend class job'); const { data } = job.attrs; const participants = await UserModel.find({ _id: { $in: data.participantIds.map(Types.ObjectId) } }); console.log('Participants: ', participants.map(participant => participant.username)); return Bluebird.map(participants, participant => handleJobAsUser(job, participant)); }; module.exports = handleJob;