const { Types } = require('mongoose'); const puppeteer = require('puppeteer'); 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, NODE_ENV, HEADFUL } = process.env; // Always run headless in production, but allow configuring for development const headless = NODE_ENV === 'production' || !HEADFUL; const handleEventAsUser = async (event, browser, user) => { console.log(`Running event as ${user.username}`); const browserContext = await browser.createIncognitoBrowserContext(); const { conferenceId } = event; const conferenceUrl = `${EDUFPMI_URL}/mod/bigbluebuttonbn/view.php?id=${conferenceId}`; const page = await launchUserSession(user, browserContext); await page.goto(conferenceUrl); // Launch the conference in a new tab const conferencePagePromise = new Promise(resolve => browserContext.on( 'targetcreated', target => resolve(target.page()) )); await clickElementBySelector(page, 'input#join_button_input'); const conferencePage = await conferencePagePromise; await page.close(); await attendConference(conferencePage, () => console.log(`Joined the conference at ${conferenceUrl}`)); await browserContext.close(); }; const handleEvent = async event => { const participants = await UserModel.find({ username: { $in: event.participants } }); console.log('Participants: ', participants.map(participant => participant.username)); const browser = await puppeteer.launch({ headless, args: ['--no-sandbox', '--incognito'] }); try { await Bluebird.map(participants, participant => handleEventAsUser(event, browser, participant)); } finally { await browser.close(); } }; module.exports = handleEvent;